YYGQ过滤器

过滤网页中一些令人反感和阴阳怪气的词句

当前为 2020-08-07 提交的版本,查看 最新版本

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         YYGQ过滤器
// @namespace    SeaLoong
// @version      0.1
// @description  过滤网页中一些令人反感和阴阳怪气的词句
// @author       SeaLoong
// @include      /https?.+/
// @grant        none
// @run-at       document-end
// ==/UserScript==

'use strict';

const tagReg = /<\S*?[^>]*>/g;
const words = [
  /(不会.*?吧)+/g,
  /((ta?|他|她|它)(急了)+)+/g,
  /(你.*?品)+/g,
  /(就这)+/g
];
const replacement = '🚫';

function matchAll (regex, str) {
  const results = [];
  if (regex.global) {
    let r;
    while ((r = regex.exec(str))) results.push(r);
  }
  return results;
}

const wm = new WeakMap();
async function filter (node) {
  let html = node.innerHTML;
  if (wm.get(node) === html) return;
  if (node.innerText && node.innerText.includes('\n')) {
    for (const ch of node.children) {
      await filter(ch);
    }
    for (const nd of node.childNodes) {
      if (nd.nodeType !== 3) continue;
      let text = nd.nodeValue;
      if (!text) continue;
      for (const w of words) {
        text = text.replace(w, replacement);
      }
      nd.nodeValue = text;
    }
    wm.set(node, html);
    return;
  }
  let text = html.replace(tagReg, '');
  let reassign = false;
  for (let cnt = 0; cnt < words.length; cnt++) {
    const poses = matchAll(words[cnt], text);
    if (poses.length === 0) continue;
    reassign = true;
    const list = [];
    const tags = matchAll(tagReg, html);
    let i = 0;
    let j = 0;
    let pos = 0;
    let lpos = 0;
    while (i < poses.length) {
      const diff = poses[i].index - (i > 0 ? (poses[i - 1].index + poses[i - 1][0].length) : 0);
      pos += diff;
      while (j < tags.length && pos >= tags[j].index) {
        pos += tags[j][0].length;
        j++;
      }
      list.push(html.substring(lpos, pos));
      list.push(replacement);
      pos += poses[i][0].length;
      while (j < tags.length && pos > tags[j].index) {
        list.push(tags[j][0]);
        pos += tags[j][0].length;
        j++;
      }
      lpos = pos;
      i++;
    }
    list.push(html.substring(pos));
    html = list.join('');
    if (cnt < words.length - 1) text = html.replace(tagReg, '');
  }
  if (reassign) {
    console.log(node, html);
    node.innerHTML = html;
  }
  wm.set(node, html);
}

const f = async () => {
  await filter(document.head);
  await filter(document.body);
  setTimeout(f, 500);
};

f();