跳到正文

科技·工程

洛谷驱猫助手

WJRAK约 10 分钟
原文
文章信息与版本

文章信息

发布
2026年2月21日
收录
2026年2月21日
更新
2026年11月12日
篇幅
约 10 分钟

版本

  1. v2当前
  2. v1

注:由AI辅助完成。

最近洛谷的猫粮越来越多了,所以写了个可以自动删除‘喵’的脚本。

注:配合洛谷内容净化器使用更佳。

脚本代码
JavaScript
// ==UserScript==
// @name         洛谷驱猫助手
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  专门删除页面上的所有“喵”字及相关变体
// @author       WJRAK&DeepSeek
// @match        *://*/*
// @grant        none
// @run-at       document-end
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    // ========== 要删除的喵相关词库 ==========
    const DELETE_WORDS = [
        '喵',
        '喵!',
        '喵喵',
        '喵喵喵',
        '喵~',
        '~喵~',
        '喵呜',
        '咪喵',
        '喵喵喵喵',
        '喵星人',
        '喵酱',
        '喵娘',
        '喵内',
        'nya',
        'nyaa',
        'にゃ',
        'ニャ',
        'miao',
        'miaomiao'
    ];

    // 排序后的词库(用于二分查找)
    let sortedWords = null;

    // ========== 初始化排序词库 ==========
    function initWords() {
        if (!sortedWords) {
            sortedWords = [...DELETE_WORDS].sort();
        }
    }
    initWords();

    // ========== 二分查找 ==========
    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 extractWords(text) {
        const words = new Set();

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

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

        // 提取连续字符(用于拼音匹配)
        for (let i = 0; i < text.length; i++) {
            for (let j = i + 1; j <= Math.min(i + 6, text.length); j++) {
                words.add(text.substring(i, j));
            }
        }

        return Array.from(words);
    }

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

        const possibleWords = extractWords(text);

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

        return false;
    }

    // ========== 处理文本节点 ==========
    function processTextNode(node) {
        if (!containsDeleteWord(node.nodeValue)) {
            return;
        }

        // 直接删除整个文本节点
        node.nodeValue = '';
    }

    // ========== 遍历DOM树 ==========
    function walkDOM(node) {
        // 如果是元素节点
        if (node.nodeType === Node.ELEMENT_NODE) {
            // 跳过脚本、样式等标签
            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;

        console.log('【喵星人驱逐器】正在清理喵星人...');
        walkDOM(document.body);
        console.log('【喵星人驱逐器】清理完成');
    }

    // ========== 监听动态内容 ==========
    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 testCases = [
            '喵',
            '喵!',
            '喵喵',
            '喵喵喵',
            '今天天气真好喵',
            '这是一只喵星人',
            'nya~',
            'miao',
            '你好',
            '世界'
        ];

        console.log('【测试】喵星人驱逐器测试:');
        console.log('词库大小:', sortedWords.length);
        console.log('词库列表:', sortedWords);

        testCases.forEach(test => {
            const result = containsDeleteWord(test);
            console.log(`  "${test}" → ${result ? '✅ 删除' : '❌ 保留'}`);
        });
    }

    // ========== 初始化 ==========
    function init() {
        processDocument();
        observeMutations();
        console.log('【喵星人驱逐器】已启动');

        // 自动运行测试
        setTimeout(() => {
            testMatching();
        }, 1000);
    }

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

    // ========== 菜单命令 ==========
    if (typeof GM_registerMenuCommand !== 'undefined') {
        GM_registerMenuCommand('🔍 测试喵词匹配', testMatching);
    }

})();

现在那些在犇犇嗡嗡叫的小猫娘们就消失啦!

原文