Word & Text Replace

replaces text with other text.

当前为 2019-05-02 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Word & Text Replace
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.0
  5. // @description replaces text with other text.
  6. // @author listfilterjay
  7. // @match *://*/*
  8. // @grant none
  9. // @require http://code.jquery.com/jquery-1.12.4.min.js
  10. // @require https://greasyfork.org/scripts/5392-waitforkeyelements/code/WaitForKeyElements.js?version=115012
  11. // @licence CC-BY-NC-SA-4.0; https://creativecommons.org/licenses/by-nc-sa/4.0/
  12. // @licence GPL-3.0-or-later; http://www.gnu.org/licenses/gpl-3.0.txt
  13. // ==/UserScript==
  14. /*jshint esversion: 6 */
  15.  
  16. (function () {
  17.  
  18. const showReplaceButton = 1; // set to 1 to show a button to manually run this script.
  19. const dynamicChecking = 1; // set to 1 to run the script automatically when new image elements are detected.
  20.  
  21. //word filters
  22. var replaceArry = [];
  23.  
  24. replaceArry.push(
  25. // basic examples:
  26. //[/(.\W?)*/i, 'words'], //replace all text instances with "words".
  27. //[/\w/gi, 'a'], //replace all characters with an "a" character.
  28. //[/match/gi, 'a'], //matches "match" in "ABmarchCD" and "red match".
  29. //[/\bmatch\b/gi, 'a'], //does not match "ABmatchesCD" but does match "this match is red".
  30. //[/scripts/gi, 'dictionaries'],
  31. //[/script/gi, 'dictionary'],
  32. //[/(web)?site/gi, 'webzone'],
  33. );
  34. // separated just for an example of an option of grouping/sorting.
  35. replaceArry.push(
  36. //[/user/gi, 'individual'],
  37. //[/\buse\b/gi, 'utilize'],
  38. );
  39.  
  40. function replaceText() {
  41. //console.log("start replaceText.");
  42. var numTerms = replaceArry.length;
  43. var txtWalker = document.createTreeWalker(
  44. document.body,
  45. NodeFilter.SHOW_TEXT,
  46. {
  47. acceptNode: function (node) {
  48. if (node.nodeValue.trim()) {
  49. return NodeFilter.FILTER_ACCEPT;
  50. }
  51. return NodeFilter.FILTER_SKIP;
  52. }
  53. },
  54. false
  55. );
  56. var txtNode = txtWalker.nextNode();
  57. while (txtNode) {
  58. var oldTxt = txtNode.nodeValue;
  59. for (var J = 0; J < numTerms; J++) {
  60. oldTxt = oldTxt.replace(replaceArry[J][0], replaceArry[J][1]);
  61. }
  62. txtNode.nodeValue = oldTxt;
  63. txtNode = txtWalker.nextNode();
  64. }
  65. jQuery("a").each(function(){
  66. var aText = jQuery(this).text();
  67. if (aText && !/\S*/.test(aText)) {
  68. //console.log("at: "+ aText);
  69. for (var J = 0; J < numTerms; J++) {
  70. if (replaceArry[J][0].test(aText)) {
  71. aText = aText.replace(replaceArry[J][0], replaceArry[J][1]);
  72. jQuery(this).text(aText);
  73. //console.log(aText);
  74. //console.log(replaceArry[J][0]);
  75. }
  76. }
  77. }
  78. });
  79. //console.log("end replaceText.");
  80. }
  81.  
  82. if (dynamicChecking) {
  83. waitForKeyElements( "img", replaceText );
  84. }else {
  85. replaceText();
  86. }
  87.  
  88. if (showReplaceButton){
  89. // adds button to run replace manually.
  90. jQuery("body").prepend("<div id='wr-reset'>WR</div>");
  91. jQuery("#wr-reset").click(replaceText);
  92.  
  93. const wordReplaceCss =
  94. `<style type="text/css">
  95. #wr-reset {
  96. display: block;
  97. background: #ffb51b;
  98. padding: 5px;
  99. border-radius: 5px;
  100. position: fixed;
  101. top: 5px;
  102. right: 40px;
  103. z-index: 999;
  104. color: white;
  105. font-weight: bold;
  106. cursor: pointer;
  107. }
  108. </style>`;
  109.  
  110. jQuery(document.body).append(wordReplaceCss);
  111. }
  112.  
  113. console.log("word replace script is active.");
  114. })();