Greasy Fork

Greasy Fork is available in English.

Twitter特殊词语屏蔽替换

屏蔽或替换Twitter上的特殊词语

目前为 2023-08-21 提交的版本,查看 最新版本

// ==UserScript==
// @name               Twitter特殊词语屏蔽替换
// @name:en            Twitter Special Words Replace
// @name:zh            Twitter特殊词语屏蔽替换
// @namespace          http://greasyfork.icu/zh-CN/users/1155708-dfk-klee
// @version            0.1.1.3
// @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
// ==/UserScript==

/* jshint esversion: 8 */
// "use strict";
const replaceEmoji = "🥵";
const wordsList = new Map([
  [RegExp("8964", "gi"), "8972"],
  [RegExp("8964", "gi"), "8972"],
  [RegExp("八九六四", "gi"), "八九七二"],
  [RegExp("(中|狗|支|你|猪|豬|反)(共|党|黨|蟈|国)人?", "gi"), replaceEmoji],
  [RegExp("共(产|產|惨|慘)?(党|黨|匪|猪|豬|狗|爹)", "gi"), replaceEmoji],
  [RegExp("毛?(主席|泽东|太祖|腊肉|臘肉)", "gi"), replaceEmoji],
  [RegExp("(习|刁|習)(近平|主席|仲勋|狗|猪|大)", "gi"), replaceEmoji],
  [RegExp("小?粉(蛆|红|紅)", "gi"), replaceEmoji],
  [RegExp("(支|黃|賤|贱)(那|国|猪|豬|蛆|畜|狗|奴)", "gi"), replaceEmoji],
  [
    RegExp("(屠|图|潳)支|滞纳|滯納|蜘蛛|🕷|(猪|豬)圈|奴(隶|隸|才)", "gi"),
    replaceEmoji,
  ],
]);

function replaceStrings(target) {
  const textNodes = target.querySelectorAll("span");
  let replaceCount = 0;
  let existed = false;
  // 使用for of遍历节点
  for (const textNode of textNodes) {
    let newText = textNode.textContent;

    for (const wordReg of wordsList.keys()) {
      if (wordReg.test(newText)) {
        existed || (existed = true);
        replaceCount += 1;
        newText = newText.replace(wordReg, wordsList.get(wordReg));
      }
    }

    existed && (textNode.textContent = newText);
  }

  existed &&
    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();