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

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