This repository was archived by the owner on Sep 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhardcodedColorFixer.js
69 lines (48 loc) · 2.07 KB
/
hardcodedColorFixer.js
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
let version = '2.1.3';
const getVariablesArr = (css) => css.split(';').map((x) => x.trim()).filter((x) => x.startsWith('--'))
.map((x) => x.split(':'));
let obj = {
onImport: async function () {
let ctx = document.createElement('canvas').getContext('2d');
const normaliseColor = (str) => { ctx.fillStyle = str; return ctx.fillStyle; };
const normaliseColors = (str) => str.replace(/rgb\(([0-9]+), ([0-9]+), ([0-9]+)\)/g, normaliseColor);
let sheet = window.document.styleSheets[0];
let darkThemeVars = getVariablesArr([...sheet.cssRules].find((x) => x.selectorText === '.theme-dark').cssText);
let lightThemeVars = getVariablesArr([...sheet.cssRules].find((x) => x.selectorText === '.theme-light').cssText);
let themeVars = darkThemeVars.concat(lightThemeVars).filter((x) => !x[0].includes('scrollbar') && !x[0].includes('logo')).map((x) => {
x[1] = normaliseColor(x[1]);
return x;
});
sheet.insertRule(`body {
--brand-color: #7289da;
--brand-color-hover: #677bc4;
}`, sheet.cssRules.length);
themeVars.push(['--brand-color', '#7289da']);
themeVars.push(['--brand-color-hover', '#677bc4']);
for (let rule of sheet.cssRules) {
if (rule.selectorText === '.theme-light' || rule.selectorText === '.theme-dark' || rule.selectorText === 'body') continue;
let normalisedText = normaliseColors(rule.cssText);
let changed = false;
for (let v of themeVars) {
if (normalisedText.includes(v[1])) {
normalisedText = normalisedText.replace(v[1], `var(${v[0]})`);
changed = true;
break;
}
}
if (changed) {
sheet.insertRule(`${normalisedText}`, sheet.cssRules.length);
}
}
//document.documentElement.setAttribute('hardcoded-color-fixes', 'true');
},
remove: async function () {
//document.documentElement.removeAttribute('hardcoded-color-fixes');
},
logRegionColor: 'darkred',
name: 'Hardcoded Color Fixer',
description: 'Changes hardcoded colors to variables - improves themes',
author: 'Ducko',
version
};
obj