声明:本文将大量出现不可见字符。滚木滚木
滚木我们发现在洛谷上出现了大量不可见字符显示错误的现象,现在我们推出了一款插件,可以将目前已知的异常字符的显示进行优化。滚木滚木
滚木代码使用 DeepSeek V4 Flash 生成。滚木滚木
// ==UserScript==
// @name 洛谷指定字符串清除器
// @namespace http://tampermonkey.net/
// @version 0.1
// @description 删除洛谷页面上显示的指定字符串(可自定义)
// @author You
// @match https://www.luogu.com.cn/*
// @match https://*.luogu.com.cn/*
// @match https://www.luogu.com/*
// @match https://*.luogu.com/*
// @grant none
// @run-at document-idle
// ==/UserScript==
(function() {
'use strict';
// ========== 在这里修改你要删除的字符串 ==========
// 支持多个字符串,每个字符串将完全匹配并删除
// 例如:const TARGET_STRINGS = ['草', '%%%', '垃圾广告'];
const TARGET_STRINGS = [
'滚目',
'棍木',
'滚木',
'棍母',
'gunmu'
];
// ================================================
// 用于防止循环触发的标志
let isCleaning = false;
/**
* 检查一个元素是否应该被跳过(不处理其中的文本)
* 避免破坏脚本、样式、输入框等
*/
function shouldSkipElement(element) {
if (!element || element.nodeType !== Node.ELEMENT_NODE) return true;
const tag = element.tagName.toUpperCase();
return ['SCRIPT', 'STYLE', 'TEXTAREA', 'INPUT', 'SELECT', 'OPTION', 'CODE', 'PRE'].includes(tag);
}
/**
* 清理单个文本节点
* 如果包含目标字符串则替换为空,若替换后为空则移除该节点
*/
function cleanTextNode(textNode) {
if (!textNode || textNode.nodeType !== Node.TEXT_NODE) return;
let content = textNode.nodeValue;
if (!content) return;
let changed = false;
for (const target of TARGET_STRINGS) {
if (content.includes(target)) {
// 使用 split + join 删除所有匹配(全局替换)
content = content.split(target).join('');
changed = true;
}
}
if (changed) {
if (content === '') {
// 如果文本变为空,则移除该文本节点
textNode.parentNode?.removeChild(textNode);
} else {
// 否则用新文本节点替换旧节点(避免多次触发 MutationObserver)
const newNode = document.createTextNode(content);
textNode.parentNode?.replaceChild(newNode, textNode);
}
}
}
/**
* 清理指定容器内的所有文本节点
* @param {Node} root 要清理的根节点
*/
function cleanContainer(root) {
if (!root) return;
if (shouldSkipElement(root)) return; // 跳过特定元素
// 使用 TreeWalker 遍历所有文本节点
const walker = document.createTreeWalker(
root,
NodeFilter.SHOW_TEXT,
{
acceptNode: function(node) {
// 跳过被标记为跳过的元素内部的文本
if (node.parentNode && shouldSkipElement(node.parentNode)) {
return NodeFilter.FILTER_REJECT;
}
return NodeFilter.FILTER_ACCEPT;
}
}
);
const textNodes = [];
while (walker.nextNode()) {
textNodes.push(walker.currentNode);
}
// 批量处理文本节点
isCleaning = true;
textNodes.forEach(cleanTextNode);
isCleaning = false;
}
/**
* 清理整个文档
*/
function cleanDocument() {
cleanContainer(document.body);
}
// 初始清理
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', cleanDocument);
} else {
cleanDocument();
}
// 使用 MutationObserver 监听 DOM 变化,处理动态加载的内容
const observer = new MutationObserver(function(mutations) {
if (isCleaning) return; // 避免循环
// 对每个新增的节点进行清理
mutations.forEach(mutation => {
// 处理新增的子节点
mutation.addedNodes.forEach(node => {
if (node.nodeType === Node.TEXT_NODE) {
// 如果直接添加的是文本节点,直接清理
cleanTextNode(node);
} else if (node.nodeType === Node.ELEMENT_NODE) {
// 如果是元素节点,则清理其内部所有文本
cleanContainer(node);
}
});
// 处理字符数据变化(文本内容被修改)
if (mutation.type === 'characterData' && mutation.target.nodeType === Node.TEXT_NODE) {
cleanTextNode(mutation.target);
}
});
});
// 开始观察
observer.observe(document.body, {
childList: true, // 监听子节点添加/删除
subtree: true, // 监听所有后代
characterData: true // 监听文本内容变化
});
})();滚木滚木滚木滚木滚木滚木滚木滚木滚木滚木滚木滚木滚木滚木滚木滚木滚木滚木滚木滚木滚木滚木滚木滚木滚木滚木滚木滚木滚木滚木滚木滚木滚木滚木滚木滚木滚木滚木滚木滚木滚木滚木滚木滚木滚木滚木滚木滚木