LD 丑化脚本

将 https://linux.do/ 站内“常见问题解答”替换为“社区准则” 等。

目前為 2025-06-27 提交的版本,檢視 最新版本

// ==UserScript==
// @name         LD 丑化脚本
// @namespace    http://tampermonkey.net/
// @version      1.7
// @description  将 https://linux.do/ 站内“常见问题解答”替换为“社区准则” 等。
// @author       chengdu
// @match        https://linux.do/*
// @grant        none
// @run-at       document-idle
// @license      GPL-3.0-or-later
// ==/UserScript==

(function() {
    'use strict';

    // 定义替换规则,使用全局正则表达式以确保多次替换
    const replacements = [
        { oldText: /常见问题解答/g, newText: "社区准则" },
        { oldText: /深海幽域/g, newText: "深海" },
        { oldText: /Where possible begins/g, newText: "遇事不觉,可问春风。春风不语,即随本心。" },
        { oldText: /我的帖子/g, newText: "我的" },
        { oldText: /搞七捻三/g, newText: "朋友圈" },
        { oldText: /即将到来的活动/g, newText: "活动" }
    ];

    // 使用 TreeWalker 遍历所有文本节点,进行替换
    function replaceInTextNodes(root) {
        const walker = document.createTreeWalker(
            root,
            NodeFilter.SHOW_TEXT,
            null,
            false
        );
        let node;
        while (node = walker.nextNode()) {
            let text = node.nodeValue;
            replacements.forEach(({ oldText, newText }) => {
                text = text.replace(oldText, newText);
            });
            node.nodeValue = text;
        }
    }

    // 初始替换:等待 DOM 完全加载后再执行
    function initReplace() {
        replaceInTextNodes(document.body);
    }

    if (document.readyState === "loading") {
        document.addEventListener("DOMContentLoaded", initReplace);
    } else {
        initReplace();
    }

    // 监听动态加载内容,确保 SPA 或 AJAX 新增节点也被替换
    const observer = new MutationObserver(mutations => {
        mutations.forEach(mutation => {
            mutation.addedNodes.forEach(node => {
                if (node.nodeType === Node.ELEMENT_NODE) {
                    replaceInTextNodes(node);
                }
            });
        });
    });
    observer.observe(document.body, { childList: true, subtree: true });
})();