66 lines
2.1 KiB
JavaScript
66 lines
2.1 KiB
JavaScript
(() => {
|
|
"use strict";
|
|
|
|
const STORAGE_KEY = "cognitive-agent.glass-theme";
|
|
|
|
function normalizeTheme(value) {
|
|
return value === "light" ? "light" : value === "dark" ? "dark" : "";
|
|
}
|
|
|
|
function themeFromCookie() {
|
|
try {
|
|
return normalizeTheme(document.cookie
|
|
.split(";")
|
|
.map((part) => part.trim())
|
|
.find((part) => part.startsWith(`${STORAGE_KEY}=`))
|
|
?.slice(STORAGE_KEY.length + 1));
|
|
} catch (_error) {
|
|
return "";
|
|
}
|
|
}
|
|
|
|
function resolveInitialTheme() {
|
|
const requested = normalizeTheme(new URLSearchParams(window.location.search).get("theme"));
|
|
if (requested) return requested;
|
|
try {
|
|
const parentTheme = normalizeTheme(window.parent?.document?.documentElement?.dataset?.glassTheme);
|
|
if (parentTheme) return parentTheme;
|
|
} catch (_error) {
|
|
// Standalone and cross-origin rendering continue with local preferences.
|
|
}
|
|
try {
|
|
const stored = normalizeTheme(localStorage.getItem(STORAGE_KEY));
|
|
if (stored) return stored;
|
|
} catch (_error) {
|
|
// Storage may be unavailable in hardened browser contexts.
|
|
}
|
|
return themeFromCookie() || "dark";
|
|
}
|
|
|
|
function applyTheme(theme) {
|
|
const normalized = normalizeTheme(theme) || "dark";
|
|
document.documentElement.dataset.glassTheme = normalized;
|
|
document.documentElement.style.colorScheme = normalized;
|
|
document.querySelector("#firmwareWorkbenchThemeColor")
|
|
?.setAttribute("content", normalized === "dark" ? "#0f1526" : "#f3f6fb");
|
|
return normalized;
|
|
}
|
|
|
|
applyTheme(resolveInitialTheme());
|
|
|
|
window.addEventListener("storage", (event) => {
|
|
if (event.key === STORAGE_KEY) applyTheme(event.newValue);
|
|
});
|
|
window.addEventListener("message", (event) => {
|
|
if (event.origin !== window.location.origin) return;
|
|
if (window.parent !== window && event.source !== window.parent) return;
|
|
if (event.data?.type !== "cognitive-agent:theme-change") return;
|
|
applyTheme(event.data.theme);
|
|
});
|
|
|
|
window.CognitiveAgentFirmwareWorkbenchTheme = Object.freeze({
|
|
apply: applyTheme,
|
|
current: () => document.documentElement.dataset.glassTheme,
|
|
});
|
|
})();
|