-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy paththeme-init.js
More file actions
45 lines (40 loc) · 1.62 KB
/
theme-init.js
File metadata and controls
45 lines (40 loc) · 1.62 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
45
// Early theme detection to prevent flicker
(function () {
const applyTheme = (theme) => {
if (theme === 'dark') {
document.documentElement.classList.add('dark-theme');
} else {
document.documentElement.classList.remove('dark-theme');
}
};
// 1. Immediate sync from localStorage (fastest, current session only)
const savedTheme = localStorage.getItem('kokoro-theme') || 'light';
applyTheme(savedTheme);
// 2. Sync from extension storage (cross-page sync)
// We try 'browser' first (polyfill), then 'chrome' (native)
const api = (typeof browser !== 'undefined') ? browser : (typeof chrome !== 'undefined' ? chrome : null);
if (api && api.storage) {
const storageArea = api.storage.local;
// Initial fetch from storage
const getTheme = () => {
// storage.local.get can take a string or object in both namespaces
if (typeof browser !== 'undefined') {
storageArea.get('theme').then(data => {
if (data && data.theme) applyTheme(data.theme);
});
} else {
storageArea.get('theme', (data) => {
if (data && data.theme) applyTheme(data.theme);
});
}
};
getTheme();
// Listen for storage changes in real-time
api.storage.onChanged.addListener((changes, areaName) => {
// Area name might be 'local' or 'sync' depending on implementation
if (changes.theme) {
applyTheme(changes.theme.newValue);
}
});
}
})();