Skip to content

Commit 472c3b6

Browse files
Create theme-collector.js
1 parent c56320f commit 472c3b6

File tree

1 file changed

+81
-0
lines changed

1 file changed

+81
-0
lines changed

theme-collector.js

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/**
2+
* Theme Collector 1.0.6
3+
* Released under the MIT License
4+
* Released on: January 17, 2025
5+
*/
6+
function getColorThemes() {
7+
const STORAGE_KEYS = { THEMES: 'colorThemes_data', PUBLISH_DATE: 'colorThemes_publishDate' };
8+
function getPublishDate() {
9+
const htmlComment = document.documentElement.previousSibling;
10+
return htmlComment?.nodeType === Node.COMMENT_NODE ? new Date(htmlComment.textContent.match(/Last Published: (.+?) GMT/)[1]).getTime() : null;
11+
}
12+
13+
function loadFromStorage() {
14+
try {
15+
const storedPublishDate = localStorage.getItem(STORAGE_KEYS.PUBLISH_DATE), currentPublishDate = getPublishDate();
16+
if (!currentPublishDate || !storedPublishDate || storedPublishDate !== currentPublishDate.toString()) return null;
17+
return JSON.parse(localStorage.getItem(STORAGE_KEYS.THEMES));
18+
} catch (error) { console.warn('Failed to load from localStorage:', error); return null; }
19+
}
20+
21+
function saveToStorage(themes) {
22+
try {
23+
const publishDate = getPublishDate();
24+
if (publishDate) {
25+
localStorage.setItem(STORAGE_KEYS.PUBLISH_DATE, publishDate.toString());
26+
localStorage.setItem(STORAGE_KEYS.THEMES, JSON.stringify(themes));
27+
}
28+
} catch (error) { console.warn('Failed to save to localStorage:', error); }
29+
}
30+
31+
window.colorThemes = { themes: {}, getTheme(themeName = '', brandName = '') {
32+
if (!themeName) return this.getTheme(Object.keys(this.themes)[0], brandName);
33+
const theme = this.themes[themeName];
34+
if (!theme) return {};
35+
if (!theme.brands || Object.keys(theme.brands).length === 0) return theme;
36+
if (!brandName) return theme.brands[Object.keys(theme.brands)[0]];
37+
return theme.brands[brandName] || {};
38+
}};
39+
40+
const cachedThemes = loadFromStorage();
41+
if (cachedThemes) { window.colorThemes.themes = cachedThemes; document.dispatchEvent(new CustomEvent("colorThemesReady")); return; }
42+
43+
const firstLink = document.querySelector('link[rel="stylesheet"]');
44+
if (!firstLink?.href) return null;
45+
46+
const themeVariables = new Set(), themeClasses = new Set(), brandClasses = new Set();
47+
48+
fetch(firstLink.href)
49+
.then(response => { if (!response.ok) throw new Error(`Failed to fetch stylesheet: ${response.statusText}`); return response.text(); })
50+
.then(cssText => {
51+
(cssText.match(/--_theme[\w-]+:\s*[^;]+/g) || []).forEach(variable => themeVariables.add(variable.split(":")[0].trim()));
52+
(cssText.match(/\.u-(theme|brand)-[\w-]+/g) || []).forEach(className => {
53+
if (className.startsWith(".u-theme-")) themeClasses.add(className);
54+
if (className.startsWith(".u-brand-")) brandClasses.add(className);
55+
});
56+
57+
const themeVariablesArray = Array.from(themeVariables);
58+
function checkClass(themeClass, brandClass = null) {
59+
document.documentElement.classList.add(themeClass, brandClass);
60+
const styleObject = {};
61+
themeVariablesArray.forEach(variable => styleObject[variable] = getComputedStyle(document.documentElement).getPropertyValue(variable));
62+
document.documentElement.classList.remove(themeClass, brandClass);
63+
return styleObject;
64+
}
65+
66+
themeClasses.forEach(themeClassWithDot => {
67+
const themeName = themeClassWithDot.replace(".", "").replace("u-theme-", "");
68+
window.colorThemes.themes[themeName] = { brands: {} };
69+
brandClasses.forEach(brandClassWithDot => {
70+
const brandName = brandClassWithDot.replace(".", "").replace("u-brand-", "");
71+
window.colorThemes.themes[themeName].brands[brandName] = checkClass(themeClassWithDot, brandClassWithDot);
72+
});
73+
if (!brandClasses.size) window.colorThemes.themes[themeName] = checkClass(themeClassWithDot);
74+
});
75+
76+
saveToStorage(window.colorThemes.themes);
77+
document.dispatchEvent(new CustomEvent("colorThemesReady"));
78+
})
79+
.catch(error => console.error("Error:", error.message));
80+
}
81+
getColorThemes();

0 commit comments

Comments
 (0)