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 pathindex.js
52 lines (39 loc) · 1.73 KB
/
index.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
const getVariablesArr = (css) => css.split(';').map((x) => x.trim()).filter((x) => x.startsWith('--'))
.map((x) => x.split(':'));
export default {
goosemodHandlers: {
onImport: async function () {
const 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);
const sheet = [...window.document.styleSheets].filter((x) => x.href)[0];
const darkThemeVars = getVariablesArr([...sheet.cssRules].filter((x) => x.selectorText === '.theme-dark')[1].cssText);
const lightThemeVars = getVariablesArr([...sheet.cssRules].filter((x) => x.selectorText === '.theme-light')[1].cssText);
const 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: var(--brand-experiment);
--brand-color-hover: var(--brand-experiment);
}`, sheet.cssRules.length);
for (const 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);
}
}
},
onRemove: async function () {
},
}
};