跳到正文

科技·工程

洛谷内容净化器——为您的犇犇使用体验保驾护航!

WJRAK约 45 分钟
原文

这是 2026年2月21日 存档的第 3 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

发布
2026年2月19日
收录
2026年2月23日
更新
2026年11月14日
篇幅
约 45 分钟

版本

  1. v7当前
  2. v6
  3. v5
  4. v4
更早的 3 个版本
  1. v3
  2. v2
  3. v1

如今,洛谷犇犇惊现一群猫粮与南梁,让人观犇体验骤降且有同化趋势。

为预防此类现象,维护洛谷犇犇观看安全,本人使用DeepSeek编写了一个油猴脚本,专门预防此类现象。

脚本代码(更新请关注@ WJRAK’s Article)
JavaScript
// ==UserScript==
// @name         全站内容净化器(评论区毁灭版6.5)
// @namespace    http://tampermonkey.net/
// @version      6.5
// @description  平衡模式:正常屏蔽;高级模式:暴力破坏含屏蔽词的链接/图片(可能导致评论区不显示)
// @author       WJRAK&DeepSeek
// @match        *://*/*
// @grant        GM_setValue
// @grant        GM_getValue
// @grant        GM_registerMenuCommand
// @run-at       document-end
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    // ========== 静态词库(开发者手动维护,专注中文变体)==========

    // 平衡级别词库(基础防护)- 只保留常见中文变体
    const BALANCE_WORDS = [
        // 南梁变体
        '南梁',
        // 香香软软变体
        '香香软软',
        // 猫娘变体
        '猫娘',
        // 猫粮变体
        '猫粮','男娘'
    ];

    // 删除词库(直接消失,不替换为任何文字)
    const DELETE_WORDS = [
        '喵','喵!'
    ];

    // 高级别词库(更强防护)- 包含更多中文变体和组合
    const HIGH_WORDS = [
        // 南梁变体
        '南梁',
        // 香香软软变体
        '香香软软',
        // 猫娘变体
        '猫娘',
        // 猫粮变体
        '猫粮','小蛋糕','蓝梁','腿照','春','男娘','/se'
    ];

    // 高级删除词库
    const HIGH_DELETE_WORDS = [
        '喵','喵!'
    ];

    // 排序后的词库(用于二分查找)
    let sortedBalanceWords = null;
    let sortedHighWords = null;
    let sortedDeleteWords = null;
    let sortedHighDeleteWords = null;

    // ========== 存储键名 ==========
    const STORAGE_LEVEL = 'shield_level';  // 'balance' 或 'high'
    const STORAGE_LOG = 'shield_log';

    // ========== 默认设置 ==========
    const DEFAULT_LEVEL = 'balance';
    const DEFAULT_LOG = false;

    // ========== 初始化存储 ==========
    function initStorage() {
        if (GM_getValue(STORAGE_LEVEL, null) === null) {
            GM_setValue(STORAGE_LEVEL, DEFAULT_LEVEL);
        }
        if (GM_getValue(STORAGE_LOG, null) === null) {
            GM_setValue(STORAGE_LOG, DEFAULT_LOG);
        }

        // 初始化排序词库
        if (!sortedBalanceWords) {
            sortedBalanceWords = [...BALANCE_WORDS].sort();
        }
        if (!sortedHighWords) {
            sortedHighWords = [...HIGH_WORDS].sort();
        }
        if (!sortedDeleteWords) {
            sortedDeleteWords = [...DELETE_WORDS].sort();
        }
        if (!sortedHighDeleteWords) {
            sortedHighDeleteWords = [...HIGH_DELETE_WORDS].sort();
        }
    }
    initStorage();

    // ========== 获取设置 ==========
    function getLevel() {
        return GM_getValue(STORAGE_LEVEL, DEFAULT_LEVEL);
    }
    function getLogEnabled() {
        return GM_getValue(STORAGE_LOG, DEFAULT_LOG);
    }

    // ========== 更新设置 ==========
    function updateSettings(newLevel, newLog) {
        GM_setValue(STORAGE_LEVEL, newLevel);
        GM_setValue(STORAGE_LOG, newLog);
        processDocument();
    }

    // ========== 二分查找 ==========
    function binarySearch(sortedArray, target) {
        let left = 0;
        let right = sortedArray.length - 1;

        while (left <= right) {
            const mid = Math.floor((left + right) / 2);
            const midVal = sortedArray[mid];

            if (midVal === target) {
                return true;
            }

            if (midVal < target) {
                left = mid + 1;
            } else {
                right = mid - 1;
            }
        }
        return false;
    }

    // ========== 提取中文词 ==========
    function extractChineseWords(text) {
        if (!text) return [];

        const words = new Set();

        // 1. 原文本
        words.add(text);

        // 2. 按中文标点分割
        const parts = text.split(/[,,。.!!??;;::"'""()\(\)\[\]【】\s\-_*~·]+/);
        parts.forEach(part => {
            if (part && /[\u4e00-\u9fa5]/.test(part)) {
                words.add(part);
            }
        });

        // 3. 提取连续中文字符
        const chineseChars = text.match(/[\u4e00-\u9fa5]+/g);
        if (chineseChars) {
            chineseChars.forEach(chunk => {
                words.add(chunk);

                // 2-4字词
                for (let i = 0; i < chunk.length; i++) {
                    for (let j = i + 2; j <= i + 4 && j <= chunk.length; j++) {
                        const sub = chunk.substring(i, j);
                        if (sub.length >= 2) {
                            words.add(sub);
                        }
                    }
                }
            });
        }

        // 4. 处理带分隔符的中文组合
        const separatorPattern = /([\u4e00-\u9fa5])[,,。.!!??;;::"'""()\(\)\[\]【】\s\-_*~·]+([\u4e00-\u9fa5])/g;
        let match;
        while ((match = separatorPattern.exec(text)) !== null) {
            words.add(match[1] + match[2]);
        }

        return Array.from(words).filter(w => w.length >= 2 && /[\u4e00-\u9fa5]/.test(w));
    }

    // ========== 检查文本是否包含屏蔽词 ==========
    function containsBlockedWord(text, level) {
        if (!text) return false;

        const wordList = level === 'high' ? sortedHighWords : sortedBalanceWords;
        if (!wordList) return false;

        const possibleWords = extractChineseWords(text);

        for (const word of possibleWords) {
            if (binarySearch(wordList, word)) {
                return true;
            }
        }

        return false;
    }

    // ========== 检查文本是否包含删除词 ==========
    function containsDeleteWord(text, level) {
        if (!text) return false;

        const wordList = level === 'high' ? sortedHighDeleteWords : sortedDeleteWords;
        if (!wordList) return false;

        const possibleWords = extractChineseWords(text);

        for (const word of possibleWords) {
            if (binarySearch(wordList, word)) {
                return true;
            }
        }

        return false;
    }

    // ========== 处理图片元素(高级模式专用)==========
    function processImageElement(img) {
        const level = getLevel();

        // 只在高级模式下破坏图片
        if (level !== 'high') return;

        // 检查图片的alt属性和周边文本
        const altText = img.alt || '';
        const titleText = img.title || '';

        if (!containsBlockedWord(altText, level) && !containsBlockedWord(titleText, level)) {
            return;
        }

        const logEnabled = getLogEnabled();

        // 创建新的div元素替换图片,显示【图片已屏蔽】
        const placeholder = document.createElement('div');
        placeholder.textContent = '【图片已屏蔽】';
        placeholder.style.cssText = `
            display: inline-block;
            padding: 8px 12px;
            background: #f0f0f0;
            color: #666;
            border: 1px solid #ddd;
            border-radius: 4px;
            font-size: 14px;
            min-width: 100px;
            text-align: center;
        `;

        // 如果有被屏蔽的文字,显示提示
        if (altText) {
            placeholder.setAttribute('title', `原图片描述: ${altText}`);
        }

        // 替换图片
        if (img.parentNode) {
            if (logEnabled) {
                console.log(`【破坏图片】alt:"${altText}" 原图:${img.src || '无'}`);
            }
            img.parentNode.replaceChild(placeholder, img);
        }
    }

    // ========== 处理链接元素(高级模式专用)==========
    function processLinkElement(link) {
        const level = getLevel();

        // 只在高级模式下破坏链接
        if (level !== 'high') return;

        // 检查链接文本是否包含屏蔽词
        const linkText = link.textContent || '';
        if (!containsBlockedWord(linkText, level)) return;

        const logEnabled = getLogEnabled();

        // 创建新的span元素替换链接,显示【已屏蔽】
        const span = document.createElement('span');
        span.textContent = '(已屏蔽)';
        span.style.cssText = `
            color: #999;
            cursor: default;
            text-decoration: none;
            background: none;
            font-style: italic;
        `;

        // 复制原有样式类(可选)
        if (link.className) {
            span.className = link.className;
        }

        // 替换链接
        if (link.parentNode) {
            if (logEnabled) {
                console.log(`【破坏链接】原文字:"${linkText}" URL:${link.href || '无'} → 已屏蔽`);
            }
            link.parentNode.replaceChild(span, link);
        }
    }

    // ========== 处理文本节点 ==========
    function processTextNode(node) {
        const level = getLevel();
        const logEnabled = getLogEnabled();

        // 先检查删除词(优先级高)
        if (containsDeleteWord(node.nodeValue, level)) {
            if (logEnabled) {
                console.log(`【删除】原文:${node.nodeValue.trim()}`);
            }
            node.nodeValue = '';
            return;
        }

        // 再检查屏蔽词
        if (!containsBlockedWord(node.nodeValue, level)) {
            return;
        }

        const wordList = level === 'high' ? sortedHighWords : sortedBalanceWords;
        const possibleWords = extractChineseWords(node.nodeValue);
        let matchedWord = '';

        for (const word of possibleWords) {
            if (binarySearch(wordList, word)) {
                matchedWord = word;
                break;
            }
        }

        if (logEnabled) {
            console.log(`【屏蔽】级别:${level === 'high' ? '高' : '平衡'} 词:${matchedWord} 原文:${node.nodeValue.trim()}`);
        }

        node.nodeValue = '(已屏蔽)';
    }

    // ========== 遍历DOM树(增强版)==========
    function walkDOM(node) {
        // 如果是元素节点
        if (node.nodeType === Node.ELEMENT_NODE) {
            const level = getLevel();

            // 高级模式下先处理链接和图片
            if (level === 'high') {
                // 处理链接(会替换整个元素,所以之后不用遍历子节点)
                if (node.tagName === 'A') {
                    processLinkElement(node);
                    return;
                }

                // 处理图片(会替换整个元素,所以之后不用遍历子节点)
                if (node.tagName === 'IMG') {
                    processImageElement(node);
                    return;
                }
            }

            // 跳过脚本、样式等标签
            if (/^(script|style|textarea|input)$/i.test(node.tagName)) {
                return;
            }

            // 继续遍历子节点
            for (let child of node.childNodes) {
                walkDOM(child);
            }
        }
        // 如果是文本节点,处理文本
        else if (node.nodeType === Node.TEXT_NODE) {
            if (node.nodeValue.trim()) {
                processTextNode(node);
            }
        }
    }

    // ========== 核心处理函数 ==========
    function processDocument() {
        if (!document.body) return;

        const level = getLevel();
        const logEnabled = getLogEnabled();

        if (logEnabled) {
            console.log(`【净化器】级别:${level === 'high' ? '高' : '平衡'} 词库大小:${level === 'high' ? sortedHighWords.length : sortedBalanceWords.length}`);
            if (level === 'high') {
                console.log('【净化器】高级模式:将破坏含屏蔽词的链接和图片');
            }
        }

        walkDOM(document.body);
    }

    // ========== 监听动态内容 ==========
    function observeMutations() {
        const observer = new MutationObserver((mutations) => {
            if (observer.timeout) clearTimeout(observer.timeout);
            observer.timeout = setTimeout(processDocument, 300);
        });

        observer.observe(document.body, {
            childList: true,
            subtree: true,
            characterData: true
        });
    }

    // ========== 测试函数 ==========
    function testMatching() {
        const level = getLevel();
        const wordList = level === 'high' ? sortedHighWords : sortedBalanceWords;
        const deleteList = level === 'high' ? sortedHighDeleteWords : sortedDeleteWords;

        const testCases = [
            '南梁', '南[梁', '南-梁', '南 梁', '男梁', '难梁',
            '蓝梁', '篮梁', '兰梁',
            '香香软软', '香 香 软 软', '湘湘软软',
            '猫娘', '猫 娘', '猫-娘', '毛娘', '猫酿',
            '猫粮', '猫 粮', '毛粮', '猫凉',
            '男娘', '/se', '小蛋糕', '腿照', '春',
            '喵', '喵!'
        ];

        console.log(`【测试】当前级别: ${level === 'high' ? '高' : '平衡'}`);
        console.log('【测试】词库大小:', wordList.length + deleteList.length);
        console.log('【测试】匹配结果:');

        testCases.forEach(test => {
            let result = '❌ 不匹配';
            if (containsDeleteWord(test, level)) {
                result = '✅ 删除';
            } else if (containsBlockedWord(test, level)) {
                result = '✅ 屏蔽';
            }
            console.log(`  "${test}" → ${result}`);
        });

        if (level === 'high') {
            console.log('【测试】高级模式下:');
            console.log('  - 包含上述词的链接 → 替换为"(已屏蔽)"');
            console.log('  - 包含上述词的图片 → 替换为"【图片已屏蔽】"');
            console.log('  - 包含"喵"/"喵!"的文本 → 直接消失');
        }
    }

    // ========== 创建设置面板 ==========
    function createSettingsPanel() {
        if (document.getElementById('gm-settings-panel')) return;

        const currentLevel = getLevel();
        const currentLog = getLogEnabled();

        // 创建遮罩层
        const overlay = document.createElement('div');
        overlay.id = 'gm-settings-overlay';
        overlay.style.cssText = `
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background: rgba(0,0,0,0.5);
            z-index: 999998;
            display: none;
            justify-content: center;
            align-items: center;
        `;

        // 创建面板
        const panel = document.createElement('div');
        panel.id = 'gm-settings-panel';
        panel.style.cssText = `
            background: white;
            width: 450px;
            max-width: 90%;
            border-radius: 12px;
            box-shadow: 0 10px 40px rgba(0,0,0,0.3);
            padding: 24px;
            font-family: system-ui, -apple-system, sans-serif;
            position: relative;
        `;

        // 标题
        const title = document.createElement('h2');
        title.textContent = '🛡️ 全站内容净化器';
        title.style.cssText = `
            margin: 0 0 16px 0;
            font-size: 1.5rem;
            border-bottom: 2px solid #eaeef2;
            padding-bottom: 8px;
        `;
        panel.appendChild(title);

        // 说明
        const desc = document.createElement('p');
        desc.innerHTML = '选择防护级别,词库由开发者维护,用户无需手动添加';
        desc.style.cssText = `
            font-size: 0.9rem;
            color: #666;
            margin-bottom: 20px;
            padding: 10px;
            background: #f0f3f5;
            border-radius: 6px;
        `;
        panel.appendChild(desc);

        // 级别选择
        const levelLabel = document.createElement('div');
        levelLabel.style.marginBottom = '10px';
        levelLabel.style.fontWeight = 'bold';
        levelLabel.textContent = '防护级别:';
        panel.appendChild(levelLabel);

        const balanceDiv = document.createElement('div');
        balanceDiv.style.marginBottom = '8px';

        const balanceRadio = document.createElement('input');
        balanceRadio.type = 'radio';
        balanceRadio.id = 'level-balance';
        balanceRadio.name = 'shield-level';
        balanceRadio.value = 'balance';
        balanceRadio.checked = currentLevel === 'balance';
        balanceRadio.style.marginRight = '8px';

        const balanceLabel = document.createElement('label');
        balanceLabel.htmlFor = 'level-balance';
        balanceLabel.innerHTML = '<strong>平衡模式</strong> <span style="color:#666;font-size:0.85rem">(基础防护,"喵"直接消失)</span>';

        balanceDiv.appendChild(balanceRadio);
        balanceDiv.appendChild(balanceLabel);
        panel.appendChild(balanceDiv);

        const highDiv = document.createElement('div');
        highDiv.style.marginBottom = '20px';

        const highRadio = document.createElement('input');
        highRadio.type = 'radio';
        highRadio.id = 'level-high';
        highRadio.name = 'shield-level';
        highRadio.value = 'high';
        highRadio.checked = currentLevel === 'high';
        highRadio.style.marginRight = '8px';

        const highLabel = document.createElement('label');
        highLabel.htmlFor = 'level-high';
        highLabel.innerHTML = '<strong>高级模式</strong> <span style="color:#666;font-size:0.85rem">(破坏含屏蔽词的链接/图片,"喵"直接消失)</span>';

        highDiv.appendChild(highRadio);
        highDiv.appendChild(highLabel);
        panel.appendChild(highDiv);

        // 统计信息
        const statsDiv = document.createElement('div');
        statsDiv.style.cssText = `
            background: #f8f9fa;
            border-radius: 6px;
            padding: 12px;
            margin-bottom: 20px;
            font-size: 0.9rem;
        `;
        statsDiv.innerHTML = `
            <div>平衡词库: ${BALANCE_WORDS.length} 个屏蔽词 + ${DELETE_WORDS.length} 个删除词</div>
            <div>高级词库: ${HIGH_WORDS.length} 个屏蔽词 + ${HIGH_DELETE_WORDS.length} 个删除词</div>
            <div style="margin-top:8px;color:#c44536;">⚡ 高级模式会:</div>
            <div style="margin-left:12px;color:#c44536;font-size:0.85rem;">• 链接 → 替换为"(已屏蔽)"</div>
            <div style="margin-left:12px;color:#c44536;font-size:0.85rem;">• 图片 → 替换为"【图片已屏蔽】"</div>
            <div style="margin-left:12px;color:#c44536;font-size:0.85rem;">• 删除词 → 直接消失</div>
        `;
        panel.appendChild(statsDiv);

        // 日志选项
        const logDiv = document.createElement('div');
        logDiv.style.marginBottom = '20px';

        const logCheckbox = document.createElement('input');
        logCheckbox.type = 'checkbox';
        logCheckbox.id = 'log-console';
        logCheckbox.checked = currentLog;
        logCheckbox.style.marginRight = '8px';

        const logLabel = document.createElement('label');
        logLabel.htmlFor = 'log-console';
        logLabel.innerHTML = '控制台输出屏蔽日志 (F12查看)';

        logDiv.appendChild(logCheckbox);
        logDiv.appendChild(logLabel);
        panel.appendChild(logDiv);

        // 按钮区域
        const btnDiv = document.createElement('div');
        btnDiv.style.cssText = `
            display: flex;
            justify-content: flex-end;
            gap: 10px;
            margin-top: 20px;
        `;

        const testBtn = document.createElement('button');
        testBtn.textContent = '测试匹配';
        testBtn.style.cssText = `
            padding: 8px 16px;
            border: 1px solid #2c3e50;
            background: white;
            color: #2c3e50;
            border-radius: 6px;
            cursor: pointer;
        `;
        testBtn.onclick = () => {
            testMatching();
            alert('测试结果已输出到控制台 (F12查看)');
        };

        const cancelBtn = document.createElement('button');
        cancelBtn.textContent = '取消';
        cancelBtn.style.cssText = `
            padding: 8px 16px;
            border: 1px solid #ccc;
            background: white;
            border-radius: 6px;
            cursor: pointer;
        `;
        cancelBtn.onclick = () => {
            overlay.style.display = 'none';
        };

        const saveBtn = document.createElement('button');
        saveBtn.textContent = '保存';
        saveBtn.style.cssText = `
            padding: 8px 16px;
            border: none;
            background: #2c3e50;
            color: white;
            border-radius: 6px;
            cursor: pointer;
            font-weight: 500;
        `;
        saveBtn.onclick = () => {
            const newLevel = document.querySelector('input[name="shield-level"]:checked').value;
            const newLog = document.getElementById('log-console').checked;
            updateSettings(newLevel, newLog);
            overlay.style.display = 'none';
        };

        btnDiv.appendChild(testBtn);
        btnDiv.appendChild(cancelBtn);
        btnDiv.appendChild(saveBtn);
        panel.appendChild(btnDiv);

        // 关闭按钮
        const closeIcon = document.createElement('span');
        closeIcon.innerHTML = '✕';
        closeIcon.style.cssText = `
            position: absolute;
            top: 18px;
            right: 24px;
            font-size: 1.6rem;
            cursor: pointer;
            color: #999;
        `;
        closeIcon.onclick = () => {
            overlay.style.display = 'none';
        };
        panel.appendChild(closeIcon);

        overlay.appendChild(panel);
        document.body.appendChild(overlay);

        // 触发按钮
        let triggerBtn = document.getElementById('gm-settings-trigger');
        if (!triggerBtn) {
            triggerBtn = document.createElement('div');
            triggerBtn.id = 'gm-settings-trigger';
            triggerBtn.innerHTML = '🛡️';
            triggerBtn.style.cssText = `
                position: fixed;
                bottom: 20px;
                right: 20px;
                width: 48px;
                height: 48px;
                background: #c44536;
                color: white;
                border-radius: 50%;
                display: flex;
                align-items: center;
                justify-content: center;
                font-size: 24px;
                cursor: pointer;
                box-shadow: 0 4px 15px rgba(0,0,0,0.3);
                z-index: 999999;
                opacity: 0.85;
                border: 2px solid white;
            `;
            triggerBtn.onclick = () => {
                overlay.style.display = 'flex';
            };
            document.body.appendChild(triggerBtn);
        }
    }

    // ========== 初始化 ==========
    function init() {
        processDocument();
        createSettingsPanel();
        observeMutations();
        console.log('【全站内容净化器】已启动');
    }

    if (document.readyState === 'loading') {
        document.addEventListener('DOMContentLoaded', init);
    } else {
        init();
    }

    // ========== 菜单命令 ==========
    GM_registerMenuCommand('🔍 测试匹配', testMatching);
    GM_registerMenuCommand('⚙️ 打开设置', () => {
        document.getElementById('gm-settings-overlay').style.display = 'flex';
    });

})();
效果(已过时,实际上你南梁链接都进不去)

原图1:
屏幕截图 2026-02-19 145030.png 效果1:
屏幕截图 2026-02-19 145158.png 原图2:
屏幕截图 2026-02-19 145312.png 效果2:
屏幕截图 2026-02-19 145325.png 原图3:
屏幕截图 2026-02-19 145553.png 效果3:
屏幕截图 2026-02-19 145541.png

注意
  1. 更新脚本时,请关闭插件,否则可能会引发失效等不应出现的错误
  2. 可能会出现误杀等情况,可联系@WJRAK以解决
  3. 与洛谷驱猫助手同时使用体验更佳。