-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy paththemecollector.js
More file actions
136 lines (126 loc) · 4.49 KB
/
themecollector.js
File metadata and controls
136 lines (126 loc) · 4.49 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
/**
* Theme Collector 1.1.1
* Released under the MIT License
* Released on: January 17, 2025
*/
function getColorThemes() {
const STORAGE_KEYS = {
THEMES: "colorThemes_data",
PUBLISH_DATE: "colorThemes_publishDate",
};
function getPublishDate() {
const htmlComment = document.documentElement.previousSibling;
return htmlComment?.nodeType === Node.COMMENT_NODE
? new Date(
htmlComment.textContent.match(/Last Published: (.+?) GMT/)[1]
).getTime()
: null;
}
function loadFromStorage() {
try {
const storedPublishDate = localStorage.getItem(STORAGE_KEYS.PUBLISH_DATE),
currentPublishDate = getPublishDate();
if (
!currentPublishDate ||
!storedPublishDate ||
storedPublishDate !== currentPublishDate.toString()
)
return null;
return JSON.parse(localStorage.getItem(STORAGE_KEYS.THEMES));
} catch (error) {
console.warn("Failed to load from localStorage:", error);
return null;
}
}
function saveToStorage(themes) {
try {
const publishDate = getPublishDate();
if (publishDate) {
localStorage.setItem(STORAGE_KEYS.PUBLISH_DATE, publishDate.toString());
localStorage.setItem(STORAGE_KEYS.THEMES, JSON.stringify(themes));
}
} catch (error) {
console.warn("Failed to save to localStorage:", error);
}
}
window.colorThemes = {
themes: {},
getTheme(themeName = "", brandName = "") {
if (!themeName)
return this.getTheme(Object.keys(this.themes)[0], brandName);
const theme = this.themes[themeName];
if (!theme) return {};
if (!theme.brands || Object.keys(theme.brands).length === 0) return theme;
if (!brandName) return theme.brands[Object.keys(theme.brands)[0]];
return theme.brands[brandName] || {};
},
};
const cachedThemes = loadFromStorage();
if (cachedThemes) {
window.colorThemes.themes = cachedThemes;
document.dispatchEvent(new CustomEvent("colorThemesReady"));
return;
}
const firstLink = document.querySelectorAll('link[rel="stylesheet"]')[1];
if (!firstLink?.href) return null;
const themeVariables = new Set(),
themeClasses = new Set(),
brandClasses = new Set();
fetch(firstLink.href)
.then((response) => {
if (!response.ok)
throw new Error(`Failed to fetch stylesheet: ${response.statusText}`);
return response.text();
})
.then((cssText) => {
(cssText.match(/--_theme[\w-]+:\s*[^;]+/g) || []).forEach((variable) =>
themeVariables.add(variable.split(":")[0].trim())
);
(cssText.match(/\.u-(theme|brand)-[\w-]+/g) || []).forEach(
(className) => {
if (className.startsWith(".u-theme-")) themeClasses.add(className);
if (className.startsWith(".u-brand-")) brandClasses.add(className);
}
);
const themeVariablesArray = Array.from(themeVariables);
function checkClass(themeClass, brandClass = null) {
let documentClasses = document.documentElement.getAttribute("class");
document.documentElement.setAttribute("class", "");
document.documentElement.classList.add(themeClass, brandClass);
const styleObject = {};
themeVariablesArray.forEach(
(variable) =>
(styleObject[variable] = getComputedStyle(
document.documentElement
).getPropertyValue(variable))
);
document.documentElement.setAttribute("class", documentClasses);
return styleObject;
}
themeClasses.forEach((themeClassWithDot) => {
const themeName = themeClassWithDot
.replace(".", "")
.replace("u-theme-", "");
window.colorThemes.themes[themeName] = { brands: {} };
brandClasses.forEach((brandClassWithDot) => {
const brandName = brandClassWithDot
.replace(".", "")
.replace("u-brand-", "");
window.colorThemes.themes[themeName].brands[brandName] = checkClass(
themeClassWithDot.replace(".", ""),
brandClassWithDot.replace(".", "")
);
});
if (!brandClasses.size)
window.colorThemes.themes[themeName] = checkClass(
themeClassWithDot.replace(".", "")
);
});
saveToStorage(window.colorThemes.themes);
document.dispatchEvent(new CustomEvent("colorThemesReady"));
})
.catch((error) => console.error("Error:", error.message));
}
window.addEventListener("DOMContentLoaded", (event) => {
getColorThemes();
});