Repository navigation
Expand file tree
/
Copy paththeme.js
More file actions
44 lines (44 loc) · 2.69 KB
/
Copy paththeme.js
File metadata and controls
44 lines (44 loc) · 2.69 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
// Applies the saved look (theme, accent, density, text size, glow, motion) before the page paints, so there's no flash.
// The look is mirrored in localStorage for this synchronous first read; chrome.storage settings.look is the source of truth.
(function () {
// Platform: shortcut labels, update commands and help text differ on Windows / macOS / Linux.
const p = `${navigator.userAgentData?.platform || ''} ${navigator.platform || ''} ${navigator.userAgent}`;
const os = /Win/i.test(p) ? 'win' : /Mac/i.test(p) ? 'mac' : /CrOS/i.test(p) ? 'cros' : 'linux';
window.LambdaPlatform = { os, mod: os === 'mac' ? '⌘' : 'Ctrl', key: k => os === 'mac' ? `⌘${k}` : `Ctrl+${k}` };
document.documentElement.dataset.os = os;
const DEFAULT = { theme: 'system', accent: 'cyan', density: 'comfortable', text: 'normal', glow: 'on', motion: 'full' };
function apply(look) {
const l = { ...DEFAULT, ...(look || {}) }, html = document.documentElement;
if (l.theme === 'light' || l.theme === 'dark') html.dataset.theme = l.theme; else delete html.dataset.theme;
html.dataset.accent = l.accent; html.dataset.density = l.density; html.dataset.text = l.text;
html.dataset.glow = l.glow; html.dataset.motion = l.motion;
return l;
}
let saved = null;
try { saved = JSON.parse(localStorage.getItem('lambdaos-look') || 'null'); } catch { /* per-viewer convenience */ }
apply(saved);
window.LambdaLook = {
DEFAULT, apply,
current: () => { try { return { ...DEFAULT, ...(JSON.parse(localStorage.getItem('lambdaos-look') || 'null') || {}) }; } catch { return { ...DEFAULT }; } },
async save(look) {
const l = apply(look);
try { localStorage.setItem('lambdaos-look', JSON.stringify(l)); } catch { /* ignore */ }
if (typeof chrome !== 'undefined' && chrome.storage?.local) {
const { settings = {} } = await chrome.storage.local.get('settings');
await chrome.storage.local.set({ settings: { ...settings, look: l } });
}
return l;
},
// Resolved light/dark for code that needs it (canvas colors etc.).
isLight: () => document.documentElement.dataset.theme === 'light' || (!document.documentElement.dataset.theme && matchMedia('(prefers-color-scheme: light)').matches),
};
// Keep the fast-path copy in sync if the look was changed on another page (e.g. restored from a backup).
if (typeof chrome !== 'undefined' && chrome.storage?.local) {
chrome.storage.local.get('settings').then(({ settings }) => {
if (settings?.look && JSON.stringify(settings.look) !== JSON.stringify(saved)) {
try { localStorage.setItem('lambdaos-look', JSON.stringify(settings.look)); } catch { /* ignore */ }
apply(settings.look);
}
});
}
})();