Guest User

Untitled

a guest
Oct 4th, 2018
358
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.73 KB | None | 0 0
  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. // @downloadURL https://userscripts-mirror.org/scripts/source/41369.user.js
  18. // @updateURL https://userscripts-mirror.org/scripts/source/41369.meta.js
  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. ///////////////////////////////////////////////////////
  32. "oldrim" : "classic",
  33. "Oldrim" : "Classic",
  34. ///////////////////////////////////////////////////////
  35. '':''};
  36.  
  37.  
  38.  
  39.  
  40.  
  41.  
  42.  
  43.  
  44.  
  45. //////////////////////////////////////////////////////////////////////////////
  46. // This is where the real code is
  47. // Don't edit below this
  48. //////////////////////////////////////////////////////////////////////////////
  49.  
  50. var regexs = [], replacements = [],
  51. tagsWhitelist = ['PRE', 'BLOCKQUOTE', 'CODE', 'INPUT', 'BUTTON', 'TEXTAREA'],
  52. rIsRegexp = /^\/(.+)\/([gim]+)?$/,
  53. word, text, texts, i, userRegexp;
  54.  
  55. // prepareRegex by JoeSimmons
  56. // used to take a string and ready it for use in new RegExp()
  57. function prepareRegex(string) {
  58. return string.replace(/([\[\]\^\&\$\.\(\)\?\/\\\+\{\}\|])/g, '\\$1');
  59. }
  60.  
  61. // function to decide whether a parent tag will have its text replaced or not
  62. function isTagOk(tag) {
  63. return tagsWhitelist.indexOf(tag) === -1;
  64. }
  65.  
  66. delete words['']; // so the user can add each entry ending with a comma,
  67. // I put an extra empty key/value pair in the object.
  68. // so we need to remove it before continuing
  69.  
  70. // convert the 'words' JSON object to an Array
  71. for (word in words) {
  72. if ( typeof word === 'string' && words.hasOwnProperty(word) ) {
  73. userRegexp = word.match(rIsRegexp);
  74.  
  75. // add the search/needle/query
  76. if (userRegexp) {
  77. regexs.push(
  78. new RegExp(userRegexp[1], 'g')
  79. );
  80. } else {
  81. regexs.push(
  82. new RegExp(prepareRegex(word).replace(/\\?\*/g, function (fullMatch) {
  83. return fullMatch === '\\*' ? '*' : '[^ ]*';
  84. }), 'g')
  85. );
  86. }
  87.  
  88. // add the replacement
  89. replacements.push( words[word] );
  90. }
  91. }
  92.  
  93. // do the replacement
  94. texts = document.evaluate('//body//text()[ normalize-space(.) != "" ]', document, null, 6, null);
  95. for (i = 0; text = texts.snapshotItem(i); i += 1) {
  96. if ( isTagOk(text.parentNode.tagName) ) {
  97. regexs.forEach(function (value, index) {
  98. text.data = text.data.replace( value, replacements[index] );
  99. });
  100. }
  101. }
  102.  
  103. }());
Advertisement
Add Comment
Please, Sign In to add comment