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 https://userscripts-mirror.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. // @include http://*
  6. // @include https://*
  7. // @include file://*
  8. // @exclude https://userscripts-mirror.org/scripts/review/*
  9. // @exclude https://userscripts-mirror.org/scripts/edit/*
  10. // @exclude https://userscripts-mirror.org/scripts/edit_src/*
  11. // @exclude https://userscripts-mirror.org/scripts/review/*
  12. // @exclude https://userscripts-mirror.org/scripts/edit/*
  13. // @exclude https://userscripts-mirror.org/scripts/edit_src/*
  14. // @copyright JoeSimmons
  15. // @version 1.1.0
  16. // @license http://creativecommons.org/licenses/by-nc-nd/3.0/us/
  17. // ==/UserScript==
  18. (function () {
  19. 'use strict';
  20.  
  21.  
  22. /*
  23. NOTE:
  24. You can use \\* to match actual asterisks instead of using it as a wildcard!
  25. The examples below show a wildcard in use and a regular asterisk replacement.
  26. */
  27.  
  28. var words = {
  29. ///////////////////////////////////////////////////////
  30.  
  31.  
  32. // Syntax: 'Search word' : 'Replace word',
  33. 'promotes' : 'commercializes',
  34. 'oppress' : 'feelings hurt',
  35.  
  36.  
  37. ///////////////////////////////////////////////////////
  38. '':''};
  39.  
  40.  
  41.  
  42.  
  43.  
  44.  
  45.  
  46.  
  47.  
  48.  
  49.  
  50. //////////////////////////////////////////////////////////////////////////////
  51. // This is where the real code is
  52. // Don't edit below this
  53. //////////////////////////////////////////////////////////////////////////////
  54.  
  55. var regexs = [], replacements = [],
  56. tagsWhitelist = ['PRE', 'BLOCKQUOTE', 'CODE', 'INPUT', 'BUTTON', 'TEXTAREA'],
  57. rIsRegexp = /^\/(.+)\/([gim]+)?$/,
  58. word, text, texts, i, userRegexp;
  59.  
  60. // prepareRegex by JoeSimmons
  61. // used to take a string and ready it for use in new RegExp()
  62. function prepareRegex(string) {
  63. return string.replace(/([\[\]\^\&\$\.\(\)\?\/\\\+\{\}\|])/g, '\\$1');
  64. }
  65.  
  66. // function to decide whether a parent tag will have its text replaced or not
  67. function isTagOk(tag) {
  68. return tagsWhitelist.indexOf(tag) === -1;
  69. }
  70.  
  71. delete words['']; // so the user can add each entry ending with a comma,
  72. // I put an extra empty key/value pair in the object.
  73. // so we need to remove it before continuing
  74.  
  75. // convert the 'words' JSON object to an Array
  76. for (word in words) {
  77. if ( typeof word === 'string' && words.hasOwnProperty(word) ) {
  78. userRegexp = word.match(rIsRegexp);
  79.  
  80. // add the search/needle/query
  81. if (userRegexp) {
  82. regexs.push(
  83. new RegExp(userRegexp[1], 'g')
  84. );
  85. } else {
  86. regexs.push(
  87. new RegExp(prepareRegex(word).replace(/\\?\*/g, function (fullMatch) {
  88. return fullMatch === '\\*' ? '*' : '[^ ]*';
  89. }), 'g')
  90. );
  91. }
  92.  
  93. // add the replacement
  94. replacements.push( words[word] );
  95. }
  96. }
  97.  
  98. // do the replacement
  99. texts = document.evaluate('//body//text()[ normalize-space(.) != "" ]', document, null, 6, null);
  100. for (i = 0; text = texts.snapshotItem(i); i += 1) {
  101. if ( isTagOk(text.parentNode.tagName) ) {
  102. regexs.forEach(function (value, index) {
  103. text.data = text.data.replace( value, replacements[index] );
  104. });
  105. }
  106. }
  107.  
  108. }());