dotfiles
My PC Dotfiles & Websitegit clone https://github.com/maydayv7/dotfiles
site/static/js/theme.js (1036B)
1(() => {
2 const selector = document.getElementById("theme-select");
3 if (!selector) return;
4 const root = document.documentElement;
5 const allowed = new Set([...selector.options].map((option) => option.value));
6
7 function applyTheme(theme) {
8 if (!allowed.has(theme)) return;
9 root.dataset.theme = theme;
10 selector.value = theme;
11 }
12
13 selector.addEventListener("change", () => {
14 const theme = selector.value;
15 applyTheme(theme);
16 const domain = selector.dataset.cookieDomain;
17 const shared =
18 domain &&
19 (location.hostname === domain ||
20 location.hostname.endsWith(`.${domain}`));
21 const secure = location.protocol === "https:" ? "; Secure" : "";
22 document.cookie = `site_theme=${theme}; path=/; max-age=31536000; SameSite=Lax${secure}${shared ? `; Domain=${domain}` : ""}`;
23 });
24
25 applyTheme(root.dataset.theme);
26 window.addEventListener("pageshow", () => {
27 const saved = document.cookie.match(/(?:^|;\s*)site_theme=([^;]*)/);
28 if (saved) applyTheme(saved[1]);
29 });
30})();