// ==UserScript== // @name Twitter特殊词语屏蔽替换 // @name:en Twitter Special Words Replace // @name:zh Twitter特殊词语屏蔽替换 // @namespace https://greasyfork.org/zh-CN/users/1155708-dfk-klee // @version 0.1.1.1 // @description 屏蔽或替换Twitter上的特殊词语 // @description:en Block or Replace special words on Twitter // @description:zh 屏蔽或替换Twitter上的特殊词语 // @author KumaTea DFK_KLEE // @match https://twitter.com/* // @match https://x.com/* // @license GPLv3 // @downloadURL none // ==/UserScript== /* jshint esversion: 8 */ // "use strict"; const wordsList = new Map([ ["8964", "8972"], ["8964", "8972"], ["八九六四", "八九七二"], ["粉蛆", "■■"], ["粉红", "■■"], ["粉", "■"], ["小粉红", "■■■"], ["中共", "■■"], ["支那", "■■"], ["支", "■"], ["滞纳", "■■"], ["习近平", "■■■"], ["xjp", "■■■"], ]); function replaceStrings(target) { const textNodes = target.querySelectorAll("span"); let replaceCount = 0; // 使用for of遍历节点 for (const textNode of textNodes) { const originalText = textNode.textContent; let newText = originalText; let isEvilWord = false; for (const word of wordsList.keys()) { const wordReg = new RegExp(word, "gi"); if (wordReg.test(newText)) { isEvilWord = true; replaceCount += 1; newText = newText.replace(wordReg, wordsList.get(word)); } } if (isEvilWord) { textNode.textContent = newText; console.log( `共替换 ${replaceCount} 个字符,在 ${textNodes.length} 个节点中` ); } } } function main() { // 监听DOM更新,并执行回调 const observer = new MutationObserver((mutationsList, observer) => { // 获取更新了的节点 for (const mutation of mutationsList) { if (mutation.target) { replaceStrings(mutation.target); } } }); // 监听更新的节点 observer.observe(document, { childList: true, subtree: true }); } main();