Replace Text On Webpages

Replaces text on websites. Now supports wildcards in search queries. Won't replace text in certain tags like links and code blocks

当前为 2023-07-16 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Replace Text On Webpages
  3. // @namespace http://userscripts.org/users/23652
  4. // @description Replaces text on websites. Now supports wildcards in search queries. Won't replace text in certain tags like links and code blocks
  5. // @match :///*
  6. // @match https:///
  7. // @include http://*
  8. // @include https://*
  9. // @include file://*
  10. // @exclude http://userscripts.org/scripts/review/*
  11. // @exclude http://userscripts.org/scripts/edit/*
  12. // @exclude http://userscripts.org/scripts/edit_src/*
  13. // @exclude https://userscripts.org/scripts/review/*
  14. // @exclude https://userscripts.org/scripts/edit/*
  15. // @exclude https://userscripts.org/scripts/edit_src/*
  16. // @copyright JoeSimmons
  17. // @version 1.1.0
  18. // @license http://creativecommons.org/licenses/by-nc-nd/3.0/us/
  19. // ==/UserScript==
  20. (function () {
  21. 'use strict';
  22.  
  23.  
  24. /*
  25. NOTE:
  26. You can use \\* to match actual asterisks instead of using it as a wildcard!
  27. The examples below show a wildcard in use and a regular asterisk replacement.
  28. */
  29.  
  30. var words = {
  31. //R1//
  32. '10296' : '8296', //shib it cr//
  33. //R2//
  34. '8116' : '7816', //shib it or//
  35. '10523' : '8523', //shib it cr//
  36. 'sun' : 'moon',
  37.  
  38.  
  39.  
  40. ///////////////////////////////////////////////////////
  41.  
  42.  
  43. // Syntax: 'Search word' : 'Replace word',
  44. 'your a' : 'you\'re a',
  45. 'imo' : 'in my opinion',
  46. 'im\\*o' : 'matching an asterisk, not a wildcard',
  47. '/\\bD\\b/g' : '[D]',
  48.  
  49.  
  50. ///////////////////////////////////////////////////////
  51. '':''};
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63. //////////////////////////////////////////////////////////////////////////////
  64. // This is where the real code is
  65. // Don't edit below this
  66. //////////////////////////////////////////////////////////////////////////////
  67.  
  68. var regexs = [], replacements = [],
  69. tagsWhitelist = ['PRE', 'BLOCKQUOTE', 'CODE', 'INPUT', 'BUTTON', 'TEXTAREA'],
  70. rIsRegexp = /^\/(.+)\/([gim]+)?$/,
  71. word, text, texts, i, userRegexp;
  72.  
  73. // prepareRegex by JoeSimmons
  74. // used to take a string and ready it for use in new RegExp()
  75. function prepareRegex(string) {
  76. return string.replace(/([\[\]\^\&\$\.\(\)\?\/\\\+\{\}\|])/g, '\\$1');
  77. }
  78.  
  79. // function to decide whether a parent tag will have its text replaced or not
  80. function isTagOk(tag) {
  81. return tagsWhitelist.indexOf(tag) === -1;
  82. }
  83.  
  84. delete words['']; // so the user can add each entry ending with a comma,
  85. // I put an extra empty key/value pair in the object.
  86. // so we need to remove it before continuing
  87.  
  88. // convert the 'words' JSON object to an Array
  89. for (word in words) {
  90. if ( typeof word === 'string' && words.hasOwnProperty(word) ) {
  91. userRegexp = word.match(rIsRegexp);
  92.  
  93. // add the search/needle/query
  94. if (userRegexp) {
  95. regexs.push(
  96. new RegExp(userRegexp[1], 'g')
  97. );
  98. } else {
  99. regexs.push(
  100. new RegExp(prepareRegex(word).replace(/\\?\*/g, function (fullMatch) {
  101. return fullMatch === '\\' ? '' : '[^ ]*';
  102. }), 'g')
  103. );
  104. }
  105.  
  106. // add the replacement
  107. replacements.push( words[word] );
  108. }
  109. }
  110.  
  111. // do the replacement
  112. texts = document.evaluate('//body//text()[ normalize-space(.) != "" ]', document, null, 6, null);
  113. for (i = 0; text = texts.snapshotItem(i); i += 1) {
  114. if ( isTagOk(text.parentNode.tagName) ) {
  115. regexs.forEach(function (value, index) {
  116. text.data = text.data.replace( value, replacements[index] );
  117. });
  118. }
  119. }
  120.  
  121. }());