Advertisement
Guest User

Untitled

a guest
Nov 15th, 2018
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.83 KB | None | 0 0
  1. // ==UserScript==
  2. // @name (Health Food)
  3. // @namespace g
  4. // @version 1.0
  5. // @description Automatically highlights profitable items in Neopets stores.
  6. // @author Me
  7. // @match http://www.neopets.com/objects.phtml?type=shop&obj_type=16
  8. // @grant none
  9. // ==/UserScript==
  10.  
  11. (function() {
  12.  
  13. var patterns = [], classes = [];
  14.  
  15. /* The following define the classes of words. If the first
  16. character of the specification is "=", the match will be
  17. case-sensitive, otherwise it will be case-insensitive.
  18. The specification is a regular expression, and should
  19. contain metacharacters to handle variant spellings and
  20. plurals. Any grouping within these patterns *must* be done
  21. with a (?: ... ) specification to avoid messing up the
  22. capture from the text string.
  23.  
  24. You may add additional categories as you wish, but be sure to
  25. declare their rendering in the style definition below. */
  26.  
  27. // Rendering styles for our various word classes
  28.  
  29. addGlobalStyle('span.blank { background-color: #ffffff; color: #ffffff } ' +
  30. 'span.yellow { background-color: #000000; color: #fdff00; border-style: dotted; font-size: 13px; } ' +
  31. 'span.green { background-color: #FFA500; color: #ffffff; border-style: dotted; border-color: black; font-size: 25px; } ' +
  32. 'span.red { background-color: #ff1a00; color: #000000; border-style: dotted; font-size: 18px; } ' );
  33.  
  34. // BLANK items. Items in this list will appear as white on white and be functionally invisible.
  35.  
  36. defwords([
  37. "Chunky Cauliflower Soup"
  38. ], "blank");
  39.  
  40. // RED words. These items are black background with red text.
  41.  
  42. defwords([
  43. "Golden Carrot",
  44. "Fresh Sushi Cone",
  45. "Radish and Cheese",
  46. "Cone-Shaped Lemon",
  47. "Fresh Sushi Roll",
  48. "Broccoli and Mustard Sandwich"
  49. ],
  50. "red");
  51.  
  52. // YELLOW words. Black background, yellow text.
  53.  
  54. defwords([
  55. "Cone-Shaped Orange",
  56. "Broccoli Kebab",
  57. "Baked Apple with Snowberries",
  58. "Cheesy Broccoli Bite",
  59. "Asparagus Pie",
  60. "Radish Meringue",
  61. "Cone-Shaped Strawberry"
  62. ],
  63. "yellow");
  64.  
  65. // GREEN words. Black background, green text.
  66.  
  67. defwords([
  68. "Broccoli with Sprinkles",
  69. "Cheesy Carrots",
  70. "Tomato Kebab",
  71. "Inflation Notice Luxury Cabbage Cake",
  72. "Luxury Cabbage Cake",
  73. "Artichoke Cupcake",
  74. "Four Layer Carrot Cake",
  75. "Cauliflower Lolly",
  76. "Inflation Notice French Onion Soup",
  77. "French Onion Soup",
  78. "Inflation Notice Pickled Cauliflower",
  79. "Pickled Cauliflower",
  80. "Cauliflower Soup",
  81. "Inflation Notice Artichoke and Onion Surprise",
  82. "Artichoke and Onion Surprise",
  83. "Inflation Notice Cauliflower Shake",
  84. "Cauliflower Shake",
  85. "Inflation Notice Cone-Shaped Cherry",
  86. "Cone-Shaped Cherry",
  87. "Inflation Notice Souper Bowl",
  88. "Souper Bowl",
  89. "Inflation Notice Artichoke Fondue",
  90. "Artichoke Fondue",
  91. "Inflation Notice Asparagus Balls",
  92. "Asparagus Balls"
  93. ],
  94. "green");
  95.  
  96. // Add one or more words to the dictionary with a specified class
  97.  
  98. function defwords(words, which_class) {
  99. for (var i = 0; i < words.length; i++) {
  100. var w = words[i].replace(/^=/, "");
  101. patterns.push(new RegExp("([^a-zA-Z])(" + w + ")([^a-zA-Z])",
  102. words[i].match(/^=/) ? "g" : "gi"));
  103. classes.push(which_class);
  104. }
  105. }
  106.  
  107. // Quote HTML metacharacters in body text
  108.  
  109. function quoteHTML(s) {
  110. s = s.replace(/&/g, "&amp;");
  111. s = s.replace(/</g, "&lt;");
  112. s = s.replace(/>/g, "&gt;");
  113. return s;
  114. }
  115.  
  116. // Add one or more CSS style rules to the document
  117.  
  118. function addGlobalStyle(css) {
  119. var head, style;
  120. head = document.getElementsByTagName('head')[0];
  121. if (!head) {
  122. return;
  123. }
  124. style = document.createElement('style');
  125. style.type = 'text/css';
  126. style.innerHTML = css;
  127. head.appendChild(style);
  128. }
  129.  
  130. // Apply highlighting replacements to a text sequence
  131.  
  132. var curpat; // Hidden argument to repmatch()
  133. var changes; // Number of changes made by repmatch()
  134.  
  135. function repmatch(matched, before, word, after) {
  136. changes++;
  137. return before + '<span class="' + classes[curpat] + '">' + word + '</span>' + after;
  138. }
  139.  
  140. function highlight(s) {
  141. s = " " + s;
  142. for (curpat = 0; curpat < patterns.length; curpat++) {
  143. s = s.replace(patterns[curpat],
  144. repmatch);
  145. }
  146. return s.substring(1);
  147. }
  148.  
  149. // We only modify HTML/XHTML documents
  150. if (document.contentType &&
  151. (!(document.contentType.match(/html/i)))) {
  152. return;
  153. }
  154.  
  155. // Highlight words in body copy
  156.  
  157. var textnodes = document.evaluate("//body//text()", document, null,
  158. XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
  159.  
  160. for (var i = 0; i < textnodes.snapshotLength; i++) {
  161. var node = textnodes.snapshotItem(i);
  162. /* Test whether this text node appears within a
  163. <style>, <script>, or <textarea> container.
  164. If so, it is not actual body text and must
  165. be left alone to avoid wrecking the page. */
  166. if (node.parentNode.tagName != "STYLE" &&
  167. node.parentNode.tagName != "TEXTAREA" &&
  168. node.parentNode.tagName != "SCRIPT") {
  169. /* Many documents have large numbers of empty text nodes.
  170. By testing for them, we avoid running all of our
  171. regular expressions over a target which they can't
  172. possibly match. */
  173. if (!(node.data.match(/^\s*$/))) {
  174. var s = " " + node.data + " ";
  175. changes = 0;
  176. var d = highlight(quoteHTML(s));
  177. if (changes > 0) {
  178. var rep = document.createElement("span");
  179. rep.innerHTML = d; //.substring(1, d.length - 1);
  180. node.parentNode.replaceChild(rep, node);
  181. }
  182. }
  183. }
  184. }
  185.  
  186. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement