|
1 | | -// store color themes |
2 | | -const colorTheme = []; |
3 | | -const htmlStyles = getComputedStyle(document.documentElement); |
4 | | -const targetStylesheet = document.querySelector("#color-themes"); |
5 | | -const regex = /--([^:\s]+):\s*var\(--([^)]+)\);/g; |
6 | | -if (targetStylesheet) { |
7 | | - const rules = targetStylesheet.sheet.cssRules || targetStylesheet.sheet.rules; |
8 | | - for (const rule of rules) { |
9 | | - if (rule.cssText.includes("data-theme=") && !rule.cssText.includes(`data-theme="0"`)) { |
10 | | - const styleObject = {}; |
11 | | - let match; |
12 | | - while ((match = regex.exec(rule.cssText)) !== null) { |
13 | | - const key = "--" + match[1]; |
14 | | - const value = htmlStyles.getPropertyValue("--" + match[2]); |
15 | | - styleObject[key] = value; |
| 1 | +/** |
| 2 | + * Theme Collector 1.0.7 |
| 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)); |
16 | 27 | } |
17 | | - colorTheme.push(styleObject); |
18 | | - } |
| 28 | + } catch (error) { console.warn('Failed to save to localStorage:', error); } |
19 | 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.replace(".", ""), brandClassWithDot.replace(".", "")); |
| 72 | + }); |
| 73 | + if (!brandClasses.size) window.colorThemes.themes[themeName] = checkClass(themeClassWithDot.replace(".", "")); |
| 74 | + }); |
| 75 | + |
| 76 | + saveToStorage(window.colorThemes.themes); |
| 77 | + document.dispatchEvent(new CustomEvent("colorThemesReady")); |
| 78 | + }) |
| 79 | + .catch(error => console.error("Error:", error.message)); |
20 | 80 | } |
21 | | -// ...colorThemes[0] |
22 | | -export const colorThemes = colorTheme; |
| 81 | +getColorThemes(); |
0 commit comments