Synesthesia

Colors text characters to try and develop association between letter and colors.

  1. // ==UserScript==
  2. // @name Synesthesia
  3. // @author Squigglez
  4. // @namespace RandomTagHere
  5. // @description Colors text characters to try and develop association between letter and colors.
  6. // @include *
  7. // @version 2.0
  8. // @grant none
  9. // @run-at document-idle
  10. // ==/UserScript==
  11. /*
  12. There's no license so I believe it defaults to GNU.
  13. If you change or redistribute this it'd be nice to get some credit though.
  14. Not that I'll know one way or the other.
  15. */
  16.  
  17. /*
  18. Sets timeout for recursive calls to stop the browser from locking up.
  19. Keep in mind too long will take the a long time for the page to finish coloring.
  20. Too short and the browser may still become unresponsive.
  21. Set to 0 to disable delay.
  22. */
  23. var timeOut = 100;
  24.  
  25. /*
  26. Creates colored background behind all colored texts.
  27. Set to true to help with pages that have difficult backgrounds.
  28. Change color if default doesn't help.
  29. */
  30. var backGround = false;
  31. var backGroundColor = 'White';
  32.  
  33. grabText();
  34.  
  35. function getColor(character){
  36. /*
  37. Look up for which color matches given letter.
  38. Change colors if desired.
  39. By default the 10 digits use the same colors as the first 10 letters.
  40. */
  41. var testChar = character.toLowerCase();
  42. var numFlag = parseFloat(character) == character;
  43. var colors = {
  44. 'a': 'Indigo',
  45. 'b': 'Cyan',
  46. 'c': 'DarkGoldenRod',
  47. 'd': 'Crimson',
  48. 'e': 'DarkMagenta',
  49. 'f': 'Aquamarine',
  50. 'g': 'DarkOrchid',
  51. 'h': 'DeepPink',
  52. 'i': 'OrangeRed',
  53. 'j': 'Gray',
  54. 'k': 'Navy',
  55. 'l': 'Lime',
  56. 'm': 'Khaki',
  57. 'n': 'Gold',
  58. 'o': 'Sienna',
  59. 'p': 'Yellow',
  60. 'q': 'DarkRed',
  61. 'r': 'LimeGreen',
  62. 's': 'DarkGreen',
  63. 't': 'BurlyWood',
  64. 'u': 'MediumSpringGreen',
  65. 'v': 'DarkOrange',
  66. 'w': 'Blue',
  67. 'x': 'Salmon',
  68. 'y': 'HotPink',
  69. 'z': 'CadetBlue'
  70. }
  71. var numColors = {
  72. '1':colors['a'],
  73. '2':colors['b'],
  74. '3':colors['c'],
  75. '4':colors['d'],
  76. '5':colors['e'],
  77. '6':colors['f'],
  78. '7':colors['g'],
  79. '8':colors['h'],
  80. '9':colors['i'],
  81. '0':colors['j']
  82. }
  83. if(numFlag){
  84. return numColors[character];
  85. }
  86. else{
  87. return colors[testChar] || null; //Returns null if the character was a symbol.
  88. }
  89. }
  90.  
  91. function whiteList(tagName){
  92. /*
  93. Whilelist of which tags should be explored further for possible text to color.
  94. Any tags not here will be ignored.
  95. Add/Remove tags if needed. All tags except #text need to be uppercase.
  96. Removing #text will stop the script from functioning.
  97. */
  98. var whiteList = [
  99. '#text',
  100. 'A',
  101. 'P',
  102. 'H1',
  103. 'H2',
  104. 'H3',
  105. 'H4',
  106. 'H5',
  107. 'H6',
  108. 'LI',
  109. 'UL',
  110. 'OL',
  111. 'DL',
  112. 'DIV',
  113. 'STRONG',
  114. 'MAIN',
  115. 'B',
  116. 'I',
  117. 'S',
  118. 'U',
  119. 'EM',
  120. 'SMALL',
  121. 'MARK',
  122. 'ABBR',
  123. 'DFN',
  124. 'Q',
  125. 'BDO',
  126. 'BDI',
  127. 'EMBED',
  128. 'AREA',
  129. 'BUTTON',
  130. 'LABEL',
  131. 'OPTION',
  132. 'CODE',
  133. 'SUP',
  134. 'SUB',
  135. 'CITE',
  136. 'SPAN',
  137. 'HGROUP',
  138. 'ADDRESS',
  139. 'INS',
  140. 'SECTION',
  141. 'ARTICLE',
  142. 'HEADER',
  143. 'FOOTER',
  144. 'ASIDE',
  145. 'FIGURE',
  146. 'TABLE',
  147. 'MENU',
  148. 'INPUT',
  149. 'TBODY',
  150. 'TR',
  151. 'TD'
  152. ]
  153. return whiteList.indexOf(tagName) != -1;
  154. }
  155.  
  156. function mkSpan(character){
  157. //Colors letters using span tags.
  158. var color = getColor(character);
  159. if(color == null){
  160. return character;
  161. }
  162. else{
  163. return '<span style="color:' + color + '">' + character + '</span>';
  164. }
  165. }
  166.  
  167. function colorize(string){
  168. //Parses strings to have them colored.
  169. var i = 0;
  170. var flag = false;
  171. var length = string.length;
  172. var final = '';
  173. var letter;
  174. var temp;
  175. for(i;i<length;i++){
  176. temp = string[i];
  177. letter = mkSpan(temp);
  178. if(letter != temp && !flag) flag = true;
  179. final += letter;
  180. }
  181. if(!flag) return string;
  182. var coloredText = document.createElement('coloredtext');
  183. if(backGround){
  184. coloredText.setAttribute('style','background-color:' + backGroundColor);
  185. }
  186. coloredText.innerHTML = final;
  187. return coloredText;
  188. }
  189.  
  190. function nodeWalk(node){
  191. //Main function, uses recursion to get to leaf nodes, sends text to be colored.
  192. var children = [].slice.call(node.childNodes);
  193. var length = children.length;
  194. if(!length) return;
  195. var i = 0;
  196. var child;
  197. var type;
  198. var name;
  199. var coloredText;
  200. var text;
  201. for(i;i<length;i++){
  202. child = children[i];
  203. name = child.nodeName;
  204. type = child.nodeType;
  205. if(!whiteList(name)) continue;
  206. if(type == 3){
  207. text = child.textContent;
  208. coloredText = colorize(text);
  209. if(text == coloredText) continue;
  210. node.replaceChild(coloredText,child);
  211. continue;
  212. }
  213. if(type == 1){
  214. if(timeOut > 0){
  215. window.setTimeout(nodeWalk,timeOut,child);
  216. }
  217. else{
  218. nodeWalk(child);
  219. }
  220. }
  221. }
  222. }
  223.  
  224. function grabText(){
  225. //Grabs body and sends it as the start point for nodeWalk.
  226. var body = document.body;
  227. nodeWalk(body);
  228. }