Rainbow Selection

Adapted from Shadow Selection by Anveshak. Create dynamic text shadows for highlighted text using built-in algorithms or perhaps write your very own!

当前为 2019-06-24 提交的版本,查看 最新版本

  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 dynamic text shadows for highlighted text using built-in algorithms or perhaps write your very own!
  28. // @include *
  29. // @version 0.0.1
  30. // @license MIT
  31. // @copyright 2019, Anveshak
  32. // @icon http://oi63.tinypic.com/142f1tx.jpg
  33. // @namespace Fractalism
  34. // ==/UserScript==
  35.  
  36. (function(){
  37. 'use strict';
  38. var style=document.createElement("style");
  39.  
  40. //For Firefox (Gecko) Browser
  41. style.innerHTML = " ::-moz-selection { color: #000; background: none repeat scroll 100% 0% transparent;text-shadow: 0px 0px 2px #0048FF;} a::selection { color: #000; background: none repeat scroll 0% 0% transparent;text-shadow: 2px 2px 2px #0048FF;}";
  42. //For Chrome (Blink) Browser
  43. style.innerHTML += " ::selection { color: #000; background: none repeat scroll 0% 0% transparent;text-shadow: 2px 0px 2px #0048FF;} input::selection { color: #3356C6; background: none repeat scroll 0% 0% transparent;} a::selection { color: #000; background: none repeat scroll 0% 0% transparent;text-shadow: 2px 0px 2px #0048FF;}";
  44. //For WebKit Browser
  45. style.innerHTML += " ::-webkit-selection { color: #000; background: none repeat scroll 0% 0% transparent;text-shadow: 0px 0px 2px #0048FF;} a::selection { color: #000; background: none repeat scroll 0% 0% transparent;text-shadow: 2px 2px 2px #0048FF;}";
  46. //For Other Browsers
  47. style.innerHTML += " ::-ms-selection { color: #000; background: none repeat scroll 0% 0% transparent;text-shadow: 0px 0px 2px #0048FF;} a::selection { color: #000; background: none repeat scroll 0% 0% transparent;text-shadow: 2px 2px 2px #0048FF;}";
  48. style.innerHTML += " ::-o-selection { color: #000; background: none repeat scroll 0% 0% transparent;text-shadow: 0px 0px 2px #0048FF;} a::selection { color: #000; background: none repeat scroll 0% 0% transparent;text-shadow: 2px 2px 2px #0048FF;}";
  49.  
  50. document.body.appendChild(style);
  51.  
  52. // Parameters
  53. // browser: Firefox=1, Chrome=2, WebKit=3, Other=4, or use -1 to try them all
  54. // shadow_count: number of text shadows to apply
  55. // x_offset, y_offset: relative position of each text shadow in pixels, stored as array (right and down are the positive directions)
  56. // blur: blur radius of each shadow
  57. // color: initial color of each shadow in RGBA, where 0<=R,G,B<=255 and 0<=A<=1
  58. // algorithm: algorithm to use for updating text shadows (enclosed within a function for technical reasons)
  59. // exec_interval: update text shadows every x milliseconds
  60. const browser = 2
  61. const shadow_count = 3
  62. const x_offset = [-2,-1,3]
  63. const y_offset = [-4,5,-3]
  64. const blur = [5,5,5]
  65. const color = [{R:255, G:0, B:0, A:1},{R:0,G:255,B:0,A:1},{R:0,G:0,B:255,A:1}]
  66. const algorithm = function() {
  67. orbit() // change algorithm here
  68. }
  69. const exec_interval = 100
  70.  
  71. // Algorithms
  72. // Please store algorithm-specific variables as static variables as in the default algorithm
  73.  
  74. // Default Algorithm:
  75. // Made for one shadow, can support multiple shadows but the results may not be too impressive
  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. var default_algorithm = function def_algo() {
  78. // Initialize static variables upon first execution
  79. if(typeof def_algo.mode === "undefined") {
  80. def_algo.mode = 1
  81. def_algo.delta = 20 // change amount per cycle
  82. }
  83.  
  84. for(let i = 0; i < shadow_count; ++i) {
  85. let colors = shadow_list[i].color
  86. switch(def_algo.mode) {
  87. case 1:
  88. (colors.R < 255) ? (colors.R = Math.min(colors.R + def_algo.delta, 255)) : (++def_algo.mode)
  89. break;
  90. case 2:
  91. (colors.G < 255) ? (colors.G = Math.min(colors.G + def_algo.delta, 255)) : (++def_algo.mode)
  92. break;
  93. case 3:
  94. (colors.B < 255) ? (colors.B = Math.min(colors.B + def_algo.delta, 255)) : (++def_algo.mode)
  95. break;
  96. case 4:
  97. (colors.R > 0) ? (colors.R = Math.max(colors.R - def_algo.delta, 0)) : (++def_algo.mode)
  98. break;
  99. case 5:
  100. (colors.G > 0) ? (colors.G = Math.max(colors.G - def_algo.delta, 0)) : (++def_algo.mode)
  101. break;
  102. case 6:
  103. (colors.B > 0) ? (colors.B = Math.max(colors.B - def_algo.delta, 0)) : (def_algo.mode = 1)
  104. break;
  105. }
  106. }
  107. }
  108.  
  109. // Discrete Cyclic:
  110. // Cycle between red, green, and blue
  111. var discrete_cyclic = function() {
  112. for(let i = 0; i < shadow_count; ++i) {
  113. let colors = shadow_list[i].color
  114. if(colors.R) {
  115. colors.R = 0
  116. colors.G = 255
  117. }else if(colors.G){
  118. colors.G = 0
  119. colors.B = 255
  120. }else{
  121. colors.B = 0
  122. colors.R = 255
  123. }
  124. }
  125. }
  126.  
  127. // Randomize:
  128. // Assign random color coordinates
  129. var randomize = function() {
  130. for(let i = 0; i < shadow_count; ++i) {
  131. let colors = shadow_list[i].color
  132. colors.R = Math.floor(Math.random() * 256)
  133. colors.G = Math.floor(Math.random() * 256)
  134. colors.B = Math.floor(Math.random() * 256)
  135. colors.A = Math.random()
  136. }
  137. }
  138.  
  139. // Orbit:
  140. // Shadows orbit around the letters counterclockwise
  141. // dir: 0=left 1=down 2=right 3=up
  142. // NOTE: Initial x,y offsets must be nonzero
  143. var orbit = function orb() {
  144. if(typeof orb.star === "undefined") {
  145. orb.star = []
  146. for(let i = 0; i < shadow_count; ++i){
  147. let star = shadow_list[i]
  148. orb.star[i] = ({
  149. x_max: (star.x_offset > 0) ? (star.x_offset) : (-star.x_offset),
  150. y_max: (star.y_offset > 0) ? (star.y_offset) : (-star.y_offset),
  151. dir: (star.x_offset > 0) ? ((star.y_offset > 0) ? (0) : (3)) : ((star.y_offset > 0) ? (1) : (2))
  152. })
  153. }
  154. }
  155.  
  156. for(let i = 0; i < shadow_count; ++i) {
  157. var shadow = shadow_list[i]
  158. var star = orb.star[i]
  159. switch(star.dir) {
  160. case 0:
  161. (shadow.x_offset > -star.x_max) ? (--shadow.x_offset) : (++star.dir, --i)
  162. break;
  163. case 1:
  164. (shadow.y_offset < star.y_max) ? (++shadow.y_offset) : (++star.dir, --i)
  165. break;
  166. case 2:
  167. (shadow.x_offset < star.x_max) ? (++shadow.x_offset) : (++star.dir, --i)
  168. break;
  169. case 3:
  170. (shadow.y_offset > -star.y_max) ? (--shadow.y_offset) : (star.dir = 0, --i)
  171. break;
  172. }
  173. }
  174. }
  175.  
  176. // Main program
  177. var shadow = function(x_offset, y_offset, blur, color) {
  178. return {
  179. x_offset: x_offset,
  180. y_offset: y_offset,
  181. blur: blur,
  182. color: color
  183. }
  184. }
  185. var shadow_list = []
  186. for(let i = 0; i < shadow_count; ++i) {
  187. shadow_list.push(shadow(x_offset[i], y_offset[i], blur[i], color[i]))
  188. }
  189. // Deprecated function, converts number into hex color code
  190. // var hex_adjust = function(n) {
  191. // var hex = n.toString(16)
  192. // var pad = "0".repeat(6 - hex.length)
  193. // return "#" + pad + hex
  194. // }
  195. var update_shadows = function() {
  196. algorithm()
  197. var shadow_str = ""
  198. for(let i = 0; i < shadow_count; ++i){
  199. if(i > 0) shadow_str += ","
  200. shadow_str += shadow_list[i].x_offset + "px "
  201. shadow_str += shadow_list[i].y_offset + "px "
  202. shadow_str += shadow_list[i].blur + "px "
  203. shadow_str += `rgba(${shadow_list[i].color.R}, ${shadow_list[i].color.G}, ${shadow_list[i].color.B}, ${shadow_list[i].color.A})`
  204. }
  205. var style_str = ""
  206. if(browser == 1 || browser == -1) { // Firefox
  207. style_str += `::-moz-selection { color: #000; background: none repeat scroll 100% 0% transparent; text-shadow: ${shadow_str};}
  208. a::selection { color: #000; background: none repeat scroll 0% 0% transparent; text-shadow: ${shadow_str};}`
  209. }
  210. if(browser == 2 || browser == -1) { // Chrome
  211. style_str += `::selection { color: #000; background: none repeat scroll 0% 0% transparent; text-shadow: ${shadow_str};}
  212. input::selection { color: #3356C6; background: none repeat scroll 0% 0% transparent;}
  213. a::selection { color: #000; background: none repeat scroll 0% 0% transparent; text-shadow: ${shadow_str};}"`
  214. }
  215. if(browser == 3 || browser == -1) { // WebKit
  216. style_str += `::-webkit-selection { color: #000; background: none repeat scroll 0% 0% transparent; text-shadow: ${shadow_str};}
  217. a::selection { color: #000; background: none repeat scroll 0% 0% transparent; text-shadow: ${shadow_str};}`
  218. }
  219. if(browser == 4 || browser == -1) { // Other
  220. style_str += `::-ms-selection { color: #000; background: none repeat scroll 0% 0% transparent; text-shadow: ${shadow_str};}
  221. a::selection { color: #000; background: none repeat scroll 0% 0% transparent; text-shadow: ${shadow_str};}
  222. ::-o-selection { color: #000; background: none repeat scroll 0% 0% transparent; text-shadow: ${shadow_str};}
  223. a::selection { color: #000; background: none repeat scroll 0% 0% transparent; text-shadow: ${shadow_str};}`
  224. }
  225. style.innerHTML = style_str
  226. }
  227.  
  228. // Let the party begin
  229. setInterval(update_shadows, exec_interval)
  230.  
  231. })()