52 Enhance

Minimalistic enhancements for 52pojie.

目前為 2023-07-31 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         52 Enhance
// @namespace    http://tampermonkey.net/
// @version      0.2.0
// @description  Minimalistic enhancements for 52pojie.
// @author       PRO
// @match        https://www.52pojie.cn/*
// @icon         http://52pojie.cn/favicon.ico
// @grant        none
// @license      gpl-3.0
// @grant        GM_setValue
// @grant        GM_getValue
// @grant        GM_registerMenuCommand
// @grant        GM_unregisterMenuCommand
// @require      https://greasyfork.org/scripts/470224-tampermonkey-config/code/Tampermonkey%20Config.js?version=1216049
// ==/UserScript==

(function() {
    'use strict';
    let config_desc = {
        "css-fix": { // 动态透明度;图标上光标不显示为 pointer
            "name": "CSS 修复",
            "value": true,
            "processor": GM_config_builtin_processors.boolean
        },
        "hide": { // 为旧版代码块添加“隐藏代码”的按钮;一键隐藏所有置顶帖;添加隐藏回复的按钮
            "name": "一键隐藏",
            "value": true,
            "processor": GM_config_builtin_processors.boolean
        },
        "get-to-top": { // 双击导航栏回到顶部;修改回到顶部按钮行为为原生
            "name": "回到顶部",
            "value": true,
            "processor": GM_config_builtin_processors.boolean
        },
        "emoji-fix": { // 修复 Emoji 显示
            "name": "修复 Emoji",
            "value": true,
            "processor": GM_config_builtin_processors.boolean
        },
    };
    let config = GM_config(config_desc);
    // Css fix
    if (config["css-fix"]) {
        let css = document.createElement("style");
        css.textContent = `
        #jz52top {
            opacity: 0.2;
            transition: opacity 0.2s ease-in-out;
        }
        #jz52top:hover {
            opacity: 0.8;
        }
        @media (any-hover: none) {
            #jz52top {
                opacity: 0.8;
            }
            #jz52top:hover {
                opacity: 0.8;
            }
        }
        .authicn {
            cursor: initial;
        }
        `;
        document.head.appendChild(css);
    }
    // Hide
    if (config["hide"]) {
        // Hide code
        function toggleCode() {
            let code = this.parentNode.parentNode.lastChild;
            if (code.style.display == "none") {
                code.style.display = "";
                this.textContent = " 隐藏代码";
            } else {
                code.style.display = "none";
                this.textContent = " 显示代码";
            }
        }
        document.querySelectorAll("em.viewsource").forEach(ele => {
            let hide_code = document.createElement("em");
            hide_code.setAttribute("style", ele.getAttribute("style"));
            hide_code.setAttribute("num", ele.getAttribute("num"));
            hide_code.textContent = " 隐藏代码";
            hide_code.addEventListener("click", toggleCode);
            ele.parentNode.appendChild(hide_code);
        });
        // Hide all top threads
        let display = Boolean(document.querySelectorAll("tbody[id^='stickthread_']").length);
        let table = document.getElementById("threadlisttableid");
        if (display && table) {
            function hideAll() {
                document.querySelectorAll("tbody[id^='stickthread_']").forEach(ele => {
                    let close = ele.querySelector("a.closeprev");
                    if (close) close.click();
                });
            }
            let tooltip = document.querySelector("div#threadlist > div.th > table > tbody > tr > th > div.tf");
            let show_top = tooltip.querySelector("span#clearstickthread");
            show_top.removeAttribute("style");
            show_top.insertAdjacentHTML("beforeend", "  ");
            let hide_all = document.createElement("a");
            hide_all.href = "javascript:;";
            hide_all.className = "xi2";
            hide_all.textContent = "隐藏置顶";
            hide_all.title = "隐藏置顶";
            hide_all.addEventListener("click", hideAll);
            show_top.insertAdjacentElement("beforeend", hide_all);
        }
        // Hide reply
        function toggleReplyHeader() {
            let reply = this.parentNode.parentNode.parentNode.nextSibling;
            if (reply.style.display == "none") {
                reply.style.display = "";
            } else {
                reply.style.display = "none";
            }
        }
        let headers = document.querySelectorAll("div.pti > div.authi");
        headers.forEach(header => {
            let pipe = document.createElement("span");
            pipe.classList.add("pipe");
            pipe.textContent = "|";
            let toggle = document.createElement("a");
            toggle.href = "javascript:void(0);";
            toggle.textContent = "隐藏/显示";
            toggle.addEventListener("click", toggleReplyHeader);
            if (header.querySelector("span.none")) {
                pipe.classList.add("show");
                toggle.classList.add("show");
            }
            header.insertAdjacentElement("beforeend", pipe);
            header.insertAdjacentElement("beforeend", toggle);
        });
        function toggleReplyFooter() {
            let reply = this.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.firstChild.lastChild.lastElementChild;
            if (reply.style.display == "none") {
                reply.style.display = "";
            } else {
                reply.style.display = "none";
            }
        }
        let footers = document.querySelectorAll("td.plc >div.po.hin > div.pob.cl > p");
        footers.forEach(footer => {
            let toggle = document.createElement("a");
            toggle.href = "javascript:void(0);";
            toggle.textContent = "隐藏/显示";
            toggle.addEventListener("click", toggleReplyFooter);
            footer.insertAdjacentElement("beforeend", toggle);
        });
    }
    // Get to top
    if (config["get-to-top"]) {
        // Double click navbar to get to top
        document.getElementById("nv").addEventListener("dblclick", e => {
            window.scrollTo({ top: 0, behavior: "smooth" });
        });
        // Change get to top button behavior (use vanilla solution)
        document.getElementById("goTopBtn").onclick = e => {
            window.scrollTo({ top: 0, behavior: "smooth" });
        };
    }
    // Emoji fix
    if (config["emoji-fix"]) {
        let temp = document.createElement("span");
        function fixEmoji(html) { // Replace patterns like `👍` with represented emoji
            return html.replace(/&(amp;)*#(\d+);/g, (match, p1, p2) => {
                temp.innerHTML = `&#${p2};`;
                console.log(`${match} -> ${temp.textContent}`);
                return temp.textContent;
            });
        }
        function fix(node) {
            // Select text nodes
            let walker = document.createTreeWalker(node, NodeFilter.SHOW_TEXT, null, false);
            let textNode;
            while (textNode = walker.nextNode()) {
                textNode.nodeValue = fixEmoji(textNode.nodeValue);
            }
        }
        let replies = document.querySelectorAll("table.plhin td.plc div.pct > div.pcb > div.t_fsz");
        replies.forEach(fix);
        let ratings = document.querySelectorAll("tbody.ratl_l > tr");
        ratings.forEach(rating => fix(rating.lastElementChild));
    }
})();