Greasy Fork is available in English.

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

Fra 14.07.2015. Se den seneste versjonen.

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