Greasy Fork 还支持 简体中文。

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.2.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.  
  37.  
  38.  
  39. ///////////////////////////////////////////////////////
  40.  
  41.  
  42. // Syntax: 'Search word' : 'Replace word',
  43. 'your a' : 'you\'re a',
  44. 'imo' : 'in my opinion',
  45. 'im\\*o' : 'matching an asterisk, not a wildcard',
  46. '/\\bD\\b/g' : '[D]',
  47.  
  48.  
  49. ///////////////////////////////////////////////////////
  50. '':''};
  51.  
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62. //////////////////////////////////////////////////////////////////////////////
  63. // This is where the real code is
  64. // Don't edit below this
  65. //////////////////////////////////////////////////////////////////////////////
  66.  
  67. var regexs = [], replacements = [],
  68. tagsWhitelist = ['PRE', 'BLOCKQUOTE', 'CODE', 'INPUT', 'BUTTON', 'TEXTAREA'],
  69. rIsRegexp = /^\/(.+)\/([gim]+)?$/,
  70. word, text, texts, i, userRegexp;
  71.  
  72. // prepareRegex by JoeSimmons
  73. // used to take a string and ready it for use in new RegExp()
  74. function prepareRegex(string) {
  75. return string.replace(/([\[\]\^\&\$\.\(\)\?\/\\\+\{\}\|])/g, '\\$1');
  76. }
  77.  
  78. // function to decide whether a parent tag will have its text replaced or not
  79. function isTagOk(tag) {
  80. return tagsWhitelist.indexOf(tag) === -1;
  81. }
  82.  
  83. delete words['']; // so the user can add each entry ending with a comma,
  84. // I put an extra empty key/value pair in the object.
  85. // so we need to remove it before continuing
  86.  
  87. // convert the 'words' JSON object to an Array
  88. for (word in words) {
  89. if ( typeof word === 'string' && words.hasOwnProperty(word) ) {
  90. userRegexp = word.match(rIsRegexp);
  91.  
  92. // add the search/needle/query
  93. if (userRegexp) {
  94. regexs.push(
  95. new RegExp(userRegexp[1], 'g')
  96. );
  97. } else {
  98. regexs.push(
  99. new RegExp(prepareRegex(word).replace(/\\?\*/g, function (fullMatch) {
  100. return fullMatch === '\\' ? '' : '[^ ]*';
  101. }), 'g')
  102. );
  103. }
  104.  
  105. // add the replacement
  106. replacements.push( words[word] );
  107. }
  108. }
  109.  
  110. // do the replacement
  111. texts = document.evaluate('//body//text()[ normalize-space(.) != "" ]', document, null, 6, null);
  112. for (i = 0; text = texts.snapshotItem(i); i += 1) {
  113. if ( isTagOk(text.parentNode.tagName) ) {
  114. regexs.forEach(function (value, index) {
  115. text.data = text.data.replace( value, replacements[index] );
  116. });
  117. }
  118. }
  119.  
  120. }());