52 Enhance

Minimalistic enhancements for 52pojie.

当前为 2023-07-31 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 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));
    }
})();