text replace

Replace text on the page

  1. // ==UserScript==
  2. // @name text replace
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.0.1
  5. // @description Replace text on the page
  6. // @author JoinSummer
  7. // @match *://*.weibo.*/*
  8. // @match *://*.baidu.*/*
  9. // @grant none
  10. // @license MIT
  11. // ==/UserScript==
  12.  
  13. (function() {
  14. 'use strict';
  15.  
  16. const replacements = new Map([
  17. ['习近平', '村长'],
  18. ['关键词1', '替换1'],
  19. ['关键词2', '替换2'],
  20. ]);
  21.  
  22.  
  23. function replaceText(node) {
  24. console.log(node.nodeType,node.nodeValue)
  25. if (node.nodeType === Node.TEXT_NODE) {
  26. let text = node.nodeValue;
  27. replacements.forEach((value, key) => {
  28. const regex = new RegExp(key, 'g');
  29. text = text.replace(regex, value);
  30. });
  31. node.nodeValue = text;
  32. } else {
  33. node.childNodes.forEach(replaceText);
  34. }
  35. }
  36.  
  37. replaceText(document.body);
  38.  
  39. const observer = new MutationObserver(mutations => {
  40. mutations.forEach(mutation => {
  41. mutation.addedNodes.forEach(node => {
  42. replaceText(node);
  43. });
  44. });
  45. });
  46.  
  47. observer.observe(document.body, {
  48. childList: true,
  49. subtree: true
  50. });
  51. })();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址