keywordFilter

通过关键字过滤页面内容

当前为 2022-12-11 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name keywordFilter
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.1
  5. // @description 通过关键字过滤页面内容
  6. // @author wasdjkl
  7. // @match *://*/*
  8. // @icon data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==
  9. // @grant none
  10. // @license MIT
  11. // ==/UserScript==
  12.  
  13. (function () {
  14. 'use strict';
  15.  
  16. const blockWords = ["测试", "当然"]
  17.  
  18. function hasKeyword(node) {
  19. if (node.textContent === null) return true;
  20. return blockWords.find(keyword => node.textContent.includes(keyword))
  21. }
  22.  
  23. function replaceContent(node) {
  24. if (!hasKeyword(node)) return;
  25. if (node.nodeName === "#text") node.remove()
  26. node.childNodes.forEach(replaceContent);
  27. }
  28.  
  29. replaceContent(document);
  30.  
  31. var observer = new MutationObserver(() => {
  32. replaceContent(document);
  33. });
  34.  
  35. observer.observe(document, {
  36. childList: true, attributes: false, subtree: true,
  37. });
  38. })();