s/keyboard/leopard/g

Replaces the word "keyboard" with "leopard".

  1. // ==UserScript==
  2. // @name s/keyboard/leopard/g
  3. // @version 3.2.1
  4. // @description Replaces the word "keyboard" with "leopard".
  5. // @match *://*/*
  6. // @license License In Three Lines
  7. // @namespace https://greasyfork.org/users/5442
  8. // ==/UserScript==
  9.  
  10. // Copyright 2014 Stuart P. Bentley.
  11. // This work may be used freely as long as this notice is included.
  12. // The work is provided "as is" without warranty, express or implied.
  13.  
  14. // The pattern to match
  15. var keyboard_pattern = /(k)(e)(y)(b)o(ard)/ig;
  16.  
  17. // which letters in "keyboard" get replaced with which in "leopard"
  18. var leopard_subs = {
  19. 'k': 'l', 'K': 'L',
  20. 'y': 'o', 'Y': 'O',
  21. 'b': 'p', 'B': 'P'
  22. };
  23.  
  24. // construct "leopard" replacement for "keyboard" components
  25. function replacement_leopard(match,k,e,y,b,ard) {
  26. return leopard_subs[k] + e + leopard_subs[y] + leopard_subs[b] + ard;
  27. }
  28.  
  29. // Transform all instances of "keyboard" in a string into "leopard"
  30. function leopardize(str) {
  31. return str.replace(keyboard_pattern, replacement_leopard);
  32. }
  33.  
  34. // Flag to signal that we're replacing text, so that change doesn't trigger
  35. // another replacement (technically, that can't happen if all the instances
  36. // of "keyboard" that would trigger a replacement have been replaced with
  37. // "leopard", but it's still good practice)
  38. var replacingContent = false;
  39.  
  40. function replaceTextContent(node) {
  41. if (~node.textContent.search(keyboard_pattern)) {
  42.  
  43. // Flag that content is being replaced so the event it generates
  44. // won't trigger another replacement
  45. replacingContent = true;
  46. node.textContent = leopardize(node.textContent);
  47. replacingContent = false;
  48. }
  49. }
  50.  
  51. function changeTextNodes(node) {
  52. var length, childNodes;
  53.  
  54. // If this is a text node, attempt substitutions on it
  55. if (node.nodeName == '#text') {
  56. replaceTextContent(node);
  57.  
  58. // If this is an ordinary content node, recurse any children
  59. // ("ordinary" here means a node where text content doesn't have meaning
  60. // beyond human text - <style> and <script> are the only nodes of this type
  61. // that I know of)
  62. } else if (node.nodeName.toLowerCase() != 'style'
  63. && node.nodeName.toLowerCase() != 'script') {
  64.  
  65. childNodes = node.childNodes;
  66. length = childNodes.length;
  67. for (var i = 0; i < length; ++i) {
  68. changeTextNodes(childNodes[i]);
  69. }
  70. }
  71. }
  72.  
  73. function insertion_listener(event) {
  74. //change any new text nodes in a node that is added to the body
  75. changeTextNodes(event.target);
  76. }
  77.  
  78. function cdm_listener(event) {
  79. //avoid infinite loop by ignoring events triggered by replacement
  80. if (!replacingContent) {
  81. replaceTextContent(event.target);
  82. }
  83. }
  84.  
  85. changeTextNodes(document.body);
  86. document.title = leopardize(document.title);
  87. document.body.addEventListener(
  88. 'DOMNodeInserted', insertion_listener, false);
  89. document.body.addEventListener(
  90. 'DOMCharacterDataModified', cdm_listener, false);