GitHub Code Colors

A userscript that adds a color swatch next to the code color definition

目前為 2016-09-12 提交的版本,檢視 最新版本

  1. // ==UserScript==
  2. // @name GitHub Code Colors
  3. // @version 1.1.0
  4. // @description A userscript that adds a color swatch next to the code color definition
  5. // @license https://creativecommons.org/licenses/by-sa/4.0/
  6. // @namespace http://github.com/Mottie
  7. // @include https://github.com/*
  8. // @grant GM_addStyle
  9. // @run-at document-idle
  10. // @author Rob Garrison
  11. // ==/UserScript==
  12. /* global GM_addStyle */
  13. /* jshint esnext:true, unused:true */
  14. (() => {
  15. "use strict";
  16.  
  17. GM_addStyle(`
  18. .ghcc-block { width:12px; height:12px; display:inline-block;
  19. vertical-align:middle; margin-right:4px; border:1px solid #555; }
  20. `);
  21.  
  22. let busy = false;
  23. const namedColors = [
  24. "aliceblue", "antiquewhite", "aqua", "aquamarine", "azure", "beige",
  25. "bisque", "black", "blanchedalmond", "blue", "blueviolet", "brown",
  26. "burlywood", "cadetblue", "chartreuse", "chocolate", "coral",
  27. "cornflowerblue", "cornsilk", "crimson", "cyan", "darkblue", "darkcyan",
  28. "darkgoldenrod", "darkgray", "darkgrey", "darkgreen", "darkkhaki",
  29. "darkmagenta", "darkolivegreen", "darkorange", "darkorchid", "darkred",
  30. "darksalmon", "darkseagreen", "darkslateblue", "darkslategray",
  31. "darkslagegrey", "darkturquoise", "darkviolet", "deeppink", "deepskyblue",
  32. "dimgray", "dimgrey", "dodgerblue", "firebrick", "floralwhite",
  33. "forestgreen", "fuchsia", "gainsboro", "ghostwhite", "gold", "goldenrod",
  34. "gray", "grey", "green", "greenyellow", "honeydew", "hotpink",
  35. "indianred", "indigo", "ivory", "khaki", "lavender", "lavenderblush",
  36. "lawngreen", "lemonchiffon", "lightblue", "lightcoral", "lightcyan",
  37. "lightgoldenrodyellow", "lightgray", "lightgrey", "lightgreen",
  38. "lightpink", "lightsalmon", "lightseagreen", "lightskyblue",
  39. "lightslategray", "lightslategrey", "lightsteelblue", "lightyellow",
  40. "lime", "limegreen", "linen", "magenta", "maroon", "mediumaquamarine",
  41. "mediumblue", "mediumorchid", "mediumpurple", "mediumseagreen",
  42. "mediumslateblue", "mediumspringgreen", "mediumturquoise",
  43. "mediumvioletred", "midnightblue", "mintcream", "mistyrose", "moccasin",
  44. "navajowhite", "navy", "oldlace", "olive", "olivedrab", "orange",
  45. "orangered", "orchid", "palegoldenrod", "palegreen", "paleturquoise",
  46. "palevioletred", "papayawhip", "peachpuff", "peru", "pink", "plum",
  47. "powderblue", "purple", "rebeccapurple", "red", "rosybrown", "royalblue",
  48. "saddlebrown", "salmon", "sandybrown", "seagreen", "seashell", "sienna",
  49. "silver", "skyblue", "slateblue", "slategray", "slategrey", "snow",
  50. "springgreen", "steelblue", "tan", "teal", "thistle", "tomato",
  51. "turquoise", "violet", "wheat", "white", "whitesmoke", "yellow",
  52. "yellowgreen"
  53. ].join("|"),
  54.  
  55. // don't use a div, because GitHub-Dark adds a :hover background
  56. // color definition on divs
  57. block = document.createElement("span");
  58. block.className = "ghcc-block";
  59.  
  60. function addNode(el, val) {
  61. const node = block.cloneNode();
  62. node.style.backgroundColor = val;
  63. // don't add node if color is invalid
  64. if (node.style.backgroundColor !== "") {
  65. el.insertBefore(node, el.childNodes[0]);
  66. }
  67. }
  68.  
  69. function addColors() {
  70. busy = true;
  71. if (document.querySelector(".highlight")) {
  72. let indx = 0;
  73. const regexNamed = new RegExp("^(" + namedColors + ")$", "i"),
  74. // #123, #123456 or 0x123456 (unix style colors, used by three.js)
  75. regexHex = /^(#|0x)([0-9A-F]{6}|[0-9A-F]{3})$/i,
  76. // rgb(0,0,0) or rgba(0,0,0,0.2)
  77. regexRGB = /^rgba?(\([^\)]+\))?/i,
  78. // hsl(0,0%,0%) or hsla(0,0%,0%,0.2);
  79. regexHSL = /^hsla?(\([^\)]+\))?/i,
  80.  
  81. // misc regex
  82. regexQuotes = /['"]/g,
  83. regexUnix = /^0x/,
  84. regexPercent = /%%/g,
  85.  
  86. // .pl-c1 targets css hex colors, "rgb" and "hsl"
  87. els = document.querySelectorAll(".pl-c1, .pl-s"),
  88. len = els.length;
  89.  
  90. // loop with delay to allow user interaction
  91. const loop = () => {
  92. let el, txt, tmp,
  93. // max number of DOM insertions per loop
  94. max = 0;
  95. while (max < 20 && indx < len) {
  96. if (indx >= len) {
  97. return;
  98. }
  99. el = els[indx];
  100. txt = el.textContent;
  101. if (el.classList.contains("pl-s")) {
  102. txt = txt.replace(regexQuotes, "");
  103. }
  104. if (regexHex.test(txt) || regexNamed.test(txt)) {
  105. if (!el.querySelector(".ghcc-block")) {
  106. addNode(el, txt.replace(regexUnix, "#"));
  107. max++;
  108. }
  109. } else if (regexRGB.test(txt)) {
  110. if (!el.querySelector(".ghcc-block")) {
  111. txt = el.classList.contains("pl-s") ?
  112. // color in a string contains everything
  113. txt.match(regexRGB)[0] :
  114. txt + "(" + els[++indx].textContent + ")";
  115. addNode(el, txt);
  116. max++;
  117. }
  118. } else if (regexHSL.test(txt)) {
  119. if (!el.querySelector(".ghcc-block")) {
  120. tmp = /a$/i.test(txt);
  121. txt = el.classList.contains("pl-s") ?
  122. // color in a string contains everything
  123. txt.match(regexHSL)[0] :
  124. // traverse this HTML... & els only contains the pl-c1 nodes
  125. // <span class="pl-c1">hsl</span>(<span class="pl-c1">1</span>,
  126. // <span class="pl-c1">1</span><span class="pl-k">%</span>,
  127. // <span class="pl-c1">1</span><span class="pl-k">%</span>);
  128. txt + "(" + els[++indx].textContent + "," +
  129. els[++indx].textContent + "%," +
  130. // hsla needs one more parameter
  131. els[++indx].textContent + "%" +
  132. (tmp ? "," + els[++indx].textContent : "") + ")";
  133. // sometimes (previews only?) the .pl-k span is nested inside
  134. // the .pl-c1 span, so we end up with "%%"
  135. addNode(el, txt.replace(regexPercent, "%"));
  136. max++;
  137. }
  138. }
  139. indx++;
  140. }
  141. if (indx < len) {
  142. setTimeout(() => {
  143. loop();
  144. }, 200);
  145. }
  146. };
  147. loop();
  148. }
  149. busy = false;
  150. }
  151.  
  152. Array.from(
  153. document.querySelectorAll(
  154. "#js-repo-pjax-container, #js-pjax-container, .js-preview-body"
  155. )
  156. ).forEach(
  157. target => {
  158. new MutationObserver(mutations => {
  159. mutations.forEach(mutation => {
  160. // preform checks before adding code wrap to minimize function calls
  161. if (!busy && mutation.target === target) {
  162. addColors();
  163. }
  164. });
  165. }).observe(target, {
  166. childList: true,
  167. subtree: true
  168. });
  169. });
  170.  
  171. addColors();
  172. })();