Rainbow Selection

Adapted from Shadow Selection by Anveshak. Create custom dynamic text shadows for highlighted text.

  1. /**
  2. The MIT License (MIT)
  3.  
  4. Copyright (c) 2019 Anveshak
  5.  
  6. Permission is hereby granted, free of charge, to any person obtaining a copy of
  7. this software and associated documentation files (the "Software"), to deal in
  8. the Software without restriction, including without limitation the rights to
  9. use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  10. the Software, and to permit persons to whom the Software is furnished to do so,
  11. subject to the following conditions:
  12.  
  13. The above copyright notice and this permission notice shall be included in all
  14. copies or substantial portions of the Software.
  15.  
  16. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  18. FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  19. COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  20. IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  21. CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. **/
  23.  
  24. // ==UserScript==
  25. // @name Rainbow Selection
  26. // @author Anveshak, Fractalism
  27. // @description Adapted from Shadow Selection by Anveshak. Create custom dynamic text shadows for highlighted text.
  28. // @namespace Fractalism
  29. // @include *
  30. // @version 0.0.3
  31. // @license MIT
  32. // @copyright 2019, Anveshak
  33. // @icon http://oi63.tinypic.com/142f1tx.jpg
  34. // ==/UserScript==
  35.  
  36. (function () {
  37. 'use strict';
  38.  
  39. // Parameters
  40.  
  41. // browser: Firefox=1, Chrome=2, WebKit=3, Other=4, or use -1 to try them all
  42. const browser = 2;
  43.  
  44. // shadow syntax 2
  45. //const shadow2 = ;
  46. // shadow_list: a list of shadow objects that define each text shadow
  47. // shadow syntax: shadow(offset_x, offset_y, blur, RGBA(R, G, B, A))
  48. // offset_x, offset_y: relative position of the text shadow in pixels (right and down are the positive directions)
  49. // blur: blur radius of the text shadow in pixels
  50. // color: initial color of each shadow in RGBA, where 0<=R,G,B<=255 and 0<=A<=1
  51. // Note: not all algorithms make use of the color parameter
  52. var shadow_list = [
  53. shadow(3, 4, 5, RGBA(255, 255, 0, 0.7)),
  54. shadow(-3, -4, 5, RGBA(0, 255, 255, 0.5)),
  55. //shadow(1, 1, 3, RGBA(0, 0, 0, 0.6)),
  56. ];
  57.  
  58. // algorithm: algorithm to use for updating text shadows
  59. // (using multiple algorithms might produce undefined behavior)
  60. const algorithm = function () {
  61. //fluorescence(25);
  62. //discrete_cycle();
  63. //randomize(true);
  64. orbit(true, 0.4);
  65. continuous_cycle(["F640EA", "2FF", "1DF616", "EF1", "F62C16"], 10);
  66. };
  67.  
  68. // exec_interval: update text shadows every x milliseconds
  69. const exec_interval = 200;
  70.  
  71.  
  72.  
  73. // Algorithms
  74.  
  75. // Fluorescence:
  76. // RGB(0,0,0) -> RGB(255,0,0) -> RGB(255,255,0) -> RGB(255,255,255) -> RGB(0,255,255) -> RGB(0,0,255) -> RGB(0,0,0) -> start over
  77. // delta: step size, how much to advance R, G, or B on each iteration
  78. // Note: Single shadow recommended
  79. // Note: Same as continuous_cycle(["000000","FF0000","FFFF00","FFFFFF","00FFFF","0000FF"])
  80. function fluorescence(delta = 20) {
  81. // Initialize static variables upon first execution
  82. if (typeof fluorescence.stage === "undefined") {
  83. fluorescence.stage = 1; // the current state of the algorithm
  84. }
  85.  
  86. for (let i = 0; i < shadow_count; ++i) {
  87. let color = shadow_list[i].color;
  88. switch (fluorescence.stage) {
  89. case 1:
  90. (color.R < 255) ? (color.R = Math.min(color.R + delta, 255)) : (++fluorescence.stage);
  91. break;
  92. case 2:
  93. (color.G < 255) ? (color.G = Math.min(color.G + delta, 255)) : (++fluorescence.stage);
  94. break;
  95. case 3:
  96. (color.B < 255) ? (color.B = Math.min(color.B + delta, 255)) : (++fluorescence.stage);
  97. break;
  98. case 4:
  99. (color.R > 0) ? (color.R = Math.max(color.R - delta, 0)) : (++fluorescence.stage);
  100. break;
  101. case 5:
  102. (color.G > 0) ? (color.G = Math.max(color.G - delta, 0)) : (++fluorescence.stage);
  103. break;
  104. case 6:
  105. (color.B > 0) ? (color.B = Math.max(color.B - delta, 0)) : (fluorescence.stage = 1);
  106. break;
  107. default:
  108. throw `Error at ${fluorescence.name}`;
  109. }
  110. }
  111. }
  112.  
  113. // Discrete Cycle:
  114. // Cycle between red, green, and blue
  115. // Note: Bad for the eyes, not recommended
  116. function discrete_cycle() {
  117. for (let i = 0; i < shadow_count; ++i) {
  118. let color = shadow_list[i].color;
  119. if (color.R) {
  120. color.R = 0;
  121. color.G = 255;
  122. } else if (color.G) {
  123. color.G = 0;
  124. color.B = 255;
  125. } else {
  126. color.B = 0;
  127. color.R = 255;
  128. }
  129. }
  130. }
  131.  
  132. // Randomize:
  133. // Assign random color coordinates
  134. // Note: Made for single shadow
  135. function randomize(use_alpha = false) {
  136. for (let i = 0; i < shadow_count; ++i) {
  137. let color = shadow_list[i].color;
  138. color.R = Math.floor(Math.random() * 256);
  139. color.G = Math.floor(Math.random() * 256);
  140. color.B = Math.floor(Math.random() * 256);
  141. if (use_alpha) {
  142. color.A = Math.random();
  143. }
  144. }
  145. }
  146.  
  147. // Orbit:
  148. // Shadows orbit around text
  149. // phaseStep: how much to advance the phase on each iteration
  150. // Note: Initial x,y offsets must be nonzero
  151. // Note: Made for multiple shadows
  152. function orbit(counterclockwise = true, phaseStep = 0.25) {
  153. if (typeof orbit.stars === "undefined") {
  154. orbit.stars = [];
  155. for (let i = 0; i < shadow_count; ++i) {
  156. let star = shadow_list[i];
  157. orbit.stars[i] = {
  158. radius: Math.sqrt(Math.pow(star.offset_x, 2) + Math.pow(star.offset_y, 2)),
  159. phase: Math.atan(star.offset_y / star.offset_x),
  160. };
  161. if (orbit.stars[i].radius == 0) throw "Radius cannot be zero";
  162. }
  163. }
  164.  
  165. for (let i = 0; i < shadow_count; ++i) {
  166. var shadow = shadow_list[i];
  167. var star = orbit.stars[i];
  168. star.phase = (star.phase + (counterclockwise ? phaseStep : -phaseStep)) % (2 * Math.PI);
  169. shadow.offset_x = star.radius * Math.cos(star.phase);
  170. shadow.offset_y = -star.radius * Math.sin(star.phase);
  171. }
  172. }
  173.  
  174. // Continuous cycle:
  175. // Cycle between any set of hex-specified colors smoothly (or set stages=1 for a discrete effect)
  176. // Note: Specify at least two colors
  177. // Note: Made for single shadow
  178. function continuous_cycle(color_list = ["ff0000", "00ff00", "0000ff"], stages = 10) {
  179. function toRGB(color_code) {
  180. if (color_code.length == 6) {
  181. return {
  182. R: parseInt(color_code.substr(0, 2), 16),
  183. G: parseInt(color_code.substr(2, 2), 16),
  184. B: parseInt(color_code.substr(4, 2), 16),
  185. }
  186. } else if (color_code.length == 3) {
  187. return {
  188. R: parseInt(color_code.substr(0, 1).repeat(2), 16),
  189. G: parseInt(color_code.substr(1, 1).repeat(2), 16),
  190. B: parseInt(color_code.substr(2, 1).repeat(2), 16),
  191. }
  192. } else {
  193. throw "Wrong color code"
  194. }
  195. }
  196.  
  197. if (typeof continuous_cycle.src_color === "undefined") {
  198. continuous_cycle.dst_index = 1;
  199. continuous_cycle.src_color = toRGB(color_list[0]);
  200. continuous_cycle.dst_color = toRGB(color_list[1]);
  201. continuous_cycle.step = [
  202. (continuous_cycle.dst_color.R - continuous_cycle.src_color.R) / stages,
  203. (continuous_cycle.dst_color.G - continuous_cycle.src_color.G) / stages,
  204. (continuous_cycle.dst_color.B - continuous_cycle.src_color.B) / stages,
  205. ];
  206. continuous_cycle.stage = 0;
  207. }
  208.  
  209. for (let i = 0; i < shadow_count; ++i) {
  210. shadow_list[i].color.R = continuous_cycle.src_color.R + Math.round(continuous_cycle.stage * continuous_cycle.step[0]);
  211. shadow_list[i].color.G = continuous_cycle.src_color.G + Math.round(continuous_cycle.stage * continuous_cycle.step[1]);
  212. shadow_list[i].color.B = continuous_cycle.src_color.B + Math.round(continuous_cycle.stage * continuous_cycle.step[2]);
  213. }
  214. ++continuous_cycle.stage;
  215.  
  216. if (continuous_cycle.stage == stages) {
  217. continuous_cycle.src_color = continuous_cycle.dst_color;
  218. continuous_cycle.dst_index = (continuous_cycle.dst_index == color_list.length - 1) ? (0) : (continuous_cycle.dst_index + 1);
  219. continuous_cycle.dst_color = toRGB(color_list[continuous_cycle.dst_index]);
  220. continuous_cycle.step = [
  221. (continuous_cycle.dst_color.R - continuous_cycle.src_color.R) / stages,
  222. (continuous_cycle.dst_color.G - continuous_cycle.src_color.G) / stages,
  223. (continuous_cycle.dst_color.B - continuous_cycle.src_color.B) / stages,
  224. ];
  225. continuous_cycle.stage = 0;
  226. }
  227. }
  228.  
  229.  
  230.  
  231. // Main program
  232.  
  233. var shadow_count = shadow_list.length;
  234. var styleNode = document.createElement("style");
  235. document.head.appendChild(styleNode);
  236.  
  237. // execute the selected algorithm once to obtain the next set of text shadows, then apply it to the document
  238. function update_shadows() {
  239. algorithm();
  240. var shadow_str = "";
  241. for (let i = 0; i < shadow_count; ++i) {
  242. if (i > 0) shadow_str += ",";
  243. shadow_str += shadow_list[i].offset_x + "px ";
  244. shadow_str += shadow_list[i].offset_y + "px ";
  245. shadow_str += shadow_list[i].blur + "px ";
  246. shadow_str += `rgba(${shadow_list[i].color.R}, ${shadow_list[i].color.G}, ${shadow_list[i].color.B}, ${shadow_list[i].color.A})`;
  247. }
  248.  
  249. var selection_Content = `color: #000; background: none; text-shadow: ${shadow_str};`;
  250. var a_selection_Content = `color: #000; background: none; text-shadow: ${shadow_str};`;
  251. var input_selection_Content = `color: #3356C6; background: none;`;
  252.  
  253. var style_str;
  254. switch (browser) {
  255. case 1:
  256. style_str = `
  257. ::-moz-selection { ${selection_Content} }
  258. a::selection { ${a_selection_Content} }
  259. `;
  260. break;
  261. case 2:
  262. style_str = `
  263. ::selection { ${selection_Content} }
  264. a::selection { ${a_selection_Content} }
  265. input::selection { ${input_selection_Content} }
  266. `;
  267. break;
  268. case 3:
  269. style_str = `
  270. ::-webkit-selection { ${selection_Content} }
  271. a::selection { ${a_selection_Content} }
  272. `;
  273. break;
  274. case 4:
  275. style_str = `
  276. ::-ms-selection { ${selection_Content} }
  277. ::-o-selection { ${selection_Content} }
  278. a::selection { ${a_selection_Content} }
  279. `;
  280. break;
  281. default:
  282. style_str = `
  283. ::-moz-selection { ${selection_Content} }
  284. ::-webkit-selection { ${selection_Content} }
  285. ::-ms-selection { ${selection_Content} }
  286. ::-o-selection { ${selection_Content} }
  287. ::selection { ${selection_Content} }
  288. a::selection { ${a_selection_Content} }
  289. input::selection { ${input_selection_Content} }
  290. `;
  291. }
  292. styleNode.innerHTML = style_str;
  293. }
  294.  
  295. // Let the party begin
  296. setInterval(function () {
  297. try {
  298. update_shadows();
  299. } catch (err) {
  300. console.error("Error encountered:", err);
  301. }
  302. }, exec_interval);
  303.  
  304.  
  305.  
  306. // utility functions
  307.  
  308. function RGBA(red, green, blue, alpha) {
  309. return {
  310. R: red,
  311. G: green,
  312. B: blue,
  313. A: alpha,
  314. };
  315. }
  316.  
  317. function shadow(offset_x, offset_y, blur, color) {
  318. return {
  319. offset_x: offset_x,
  320. offset_y: offset_y,
  321. blur: blur,
  322. color: color,
  323. }
  324. }
  325.  
  326. })()