forked from Mottie/GitHub-userscripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub-code-colors.user.js
More file actions
183 lines (171 loc) · 6.59 KB
/
github-code-colors.user.js
File metadata and controls
183 lines (171 loc) · 6.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// ==UserScript==
// @name GitHub Code Colors
// @version 1.2.5
// @description A userscript that adds a color swatch next to the code color definition
// @license MIT
// @author Rob Garrison
// @namespace https://github.com/Mottie
// @include https://github.com/*
// @run-at document-idle
// @grant GM.addStyle
// @grant GM_addStyle
// @require https://greasemonkey.github.io/gm4-polyfill/gm4-polyfill.js?updated=20180103
// @require https://greasyfork.org/scripts/28721-mutations/code/mutations.js?version=666427
// @icon https://github.githubassets.com/pinned-octocat.svg
// @updateURL https://raw.githubusercontent.com/Mottie/GitHub-userscripts/master/github-code-colors.user.js
// @downloadURL https://raw.githubusercontent.com/Mottie/GitHub-userscripts/master/github-code-colors.user.js
// ==/UserScript==
(() => {
"use strict";
GM.addStyle(`
.ghcc-block { width:12px; height:12px; display:inline-block;
vertical-align:middle; margin-right:4px; border-radius:3px;
border:1px solid rgba(119, 119, 119, 0.5); }
`);
const namedColors = [
"aliceblue", "antiquewhite", "aqua", "aquamarine", "azure", "beige",
"bisque", "black", "blanchedalmond", "blue", "blueviolet", "brown",
"burlywood", "cadetblue", "chartreuse", "chocolate", "coral",
"cornflowerblue", "cornsilk", "crimson", "cyan", "darkblue", "darkcyan",
"darkgoldenrod", "darkgray", "darkgrey", "darkgreen", "darkkhaki",
"darkmagenta", "darkolivegreen", "darkorange", "darkorchid", "darkred",
"darksalmon", "darkseagreen", "darkslateblue", "darkslategray",
"darkslategrey", "darkturquoise", "darkviolet", "deeppink", "deepskyblue",
"dimgray", "dimgrey", "dodgerblue", "firebrick", "floralwhite",
"forestgreen", "fuchsia", "gainsboro", "ghostwhite", "gold", "goldenrod",
"gray", "grey", "green", "greenyellow", "honeydew", "hotpink",
"indianred", "indigo", "ivory", "khaki", "lavender", "lavenderblush",
"lawngreen", "lemonchiffon", "lightblue", "lightcoral", "lightcyan",
"lightgoldenrodyellow", "lightgray", "lightgrey", "lightgreen",
"lightpink", "lightsalmon", "lightseagreen", "lightskyblue",
"lightslategray", "lightslategrey", "lightsteelblue", "lightyellow",
"lime", "limegreen", "linen", "magenta", "maroon", "mediumaquamarine",
"mediumblue", "mediumorchid", "mediumpurple", "mediumseagreen",
"mediumslateblue", "mediumspringgreen", "mediumturquoise",
"mediumvioletred", "midnightblue", "mintcream", "mistyrose", "moccasin",
"navajowhite", "navy", "oldlace", "olive", "olivedrab", "orange",
"orangered", "orchid", "palegoldenrod", "palegreen", "paleturquoise",
"palevioletred", "papayawhip", "peachpuff", "peru", "pink", "plum",
"powderblue", "purple", "rebeccapurple", "red", "rosybrown", "royalblue",
"saddlebrown", "salmon", "sandybrown", "seagreen", "seashell", "sienna",
"silver", "skyblue", "slateblue", "slategray", "slategrey", "snow",
"springgreen", "steelblue", "tan", "teal", "thistle", "tomato",
"turquoise", "violet", "wheat", "white", "whitesmoke", "yellow",
"yellowgreen"
].join("|");
const regexNamed = new RegExp("^(" + namedColors + ")$", "i");
// Ex: #123, #123456 or 0x123456 (unix style colors, used by three.js)
const regexHex = /^(#|0x)([0-9A-F]{6,8}|[0-9A-F]{3,4})$/i;
// Ex: rgb(0,0,0) or rgba(0,0,0,0.2)
const regexRGB = /^rgba?(\([^\)]+\))?/i;
const regexRGBA = /rgba/i;
// Ex: hsl(0,0%,0%) or hsla(0,0%,0%,0.2);
const regexHSL = /^hsla?(\([^\)]+\))?/i;
// Misc regex
const regexQuotes = /['"]/g;
const regexUnix = /^0x/;
const regexPercent = /%%/g;
// Don't use a div, because GitHub-Dark adds a :hover background
// color definition on divs
const block = document.createElement("span");
block.className = "ghcc-block";
function addNode(el, val) {
const node = block.cloneNode();
node.style.backgroundColor = val;
// Don't add node if color is invalid
if (node.style.backgroundColor !== "") {
el.insertBefore(node, el.childNodes[0]);
}
}
function getTextContent(el) {
return el ? el.textContent : "";
}
function rgb(els, el, txt) {
// Color in a string contains everything
if (el.classList.contains("pl-s")) {
txt = txt.match(regexRGB)[0];
} else {
// Rgb(a) colors contained in multiple "pl-c1" spans
let indx = regexRGBA.test(txt) ? 4 : 3;
const tmp = [];
while (indx) {
tmp.push(getTextContent(els.shift()));
indx--;
}
txt += "(" + tmp.join(",") + ")";
}
addNode(el, txt);
return els;
}
function hsl(els, el, txt) {
const tmp = /a$/i.test(txt);
if (el.classList.contains("pl-s")) {
// Color in a string contains everything
txt = txt.match(regexHSL)[0];
} else {
// Traverse this HTML... & els only contains the pl-c1 nodes
// <span class="pl-c1">hsl</span>(<span class="pl-c1">1</span>,
// <span class="pl-c1">1</span><span class="pl-k">%</span>,
// <span class="pl-c1">1</span><span class="pl-k">%</span>);
// using getTextContent in case of invalid css
txt = txt + "(" + getTextContent(els.shift()) + "," +
getTextContent(els.shift()) + "%," +
// Hsla needs one more parameter
getTextContent(els.shift()) + "%" +
(tmp ? "," + getTextContent(els.shift()) : "") + ")";
}
// Sometimes (previews only?) the .pl-k span is nested inside
// the .pl-c1 span, so we end up with "%%"
addNode(el, txt.replace(regexPercent, "%"));
return els;
}
// Loop with delay to allow user interaction
function* addBlock(els) {
let last = "";
while (els.length) {
let el = els.shift();
let txt = el.textContent;
if (
// No swatch for JavaScript Math.tan
last === "Math" ||
// Ignore nested pl-c1 (see https://git.io/fNF3N)
el.parentNode && el.parentNode.classList.contains("pl-c1")
) {
// noop
} else if (!el.querySelector(".ghcc-block")) {
if (el.classList.contains("pl-s")) {
txt = txt.replace(regexQuotes, "");
}
if (regexHex.test(txt) || regexNamed.test(txt)) {
addNode(el, txt.replace(regexUnix, "#"));
} else if (regexRGB.test(txt)) {
els = rgb(els, el, txt);
} else if (regexHSL.test(txt)) {
els = hsl(els, el, txt);
}
}
last = txt;
yield els;
}
}
function addColors() {
if (document.querySelector(".highlight")) {
let status;
// .pl-c1 targets css hex colors, "rgb" and "hsl"
const els = [...document.querySelectorAll(".pl-c1, .pl-s")];
const iter = addBlock(els);
const loop = () => {
for (let i = 0; i < 40; i++) {
status = iter.next();
}
if (!status.done) {
requestAnimationFrame(loop);
}
};
loop();
}
}
document.addEventListener("ghmo:container", addColors);
document.addEventListener("ghmo:preview", addColors);
addColors();
})();