forked from DaveDuck321/Colorful-window-theme
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
127 lines (113 loc) · 3.67 KB
/
background.js
File metadata and controls
127 lines (113 loc) · 3.67 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
class BasicColorTheme {
constructor(frame, tab_background_text = '#111') {
this.frame = frame;
this.tab_background_text = tab_background_text;
this.usage = 0;
this.lastUsed = Math.random();
}
get browserThemeObject() {
return {
colors: {
frame: this.frame,
tab_background_text: this.tab_background_text,
}
};
}
}
let themeOfWindowID = new Map();
function saveThemeOfWindowID() {
const windowThemes = {};
themeOfWindowID.forEach((theme, windowId) => {
windowThemes[windowId] = theme.frame;
});
browser.storage.local.set({ windowThemes });
}
async function loadThemeOfWindowID() {
const { windowThemes = {} } = await browser.storage.local.get("windowThemes") || {};
for (const [windowId, themeFrame] of Object.entries(windowThemes)) {
let theme = ALL_THEMES.find(theme => theme.frame === themeFrame);
if (theme) {
themeOfWindowID.set(parseInt(windowId), theme);
}
}
}
const ALL_THEMES = [
new BasicColorTheme('#fccccf'),
new BasicColorTheme('#f9c195'),
new BasicColorTheme('#fcd994'),
new BasicColorTheme('#aaefae'),
new BasicColorTheme('#9eeded'),
new BasicColorTheme('#80c2fc'),
new BasicColorTheme('#c6badd'),
new BasicColorTheme('#cccccc'),
new BasicColorTheme('#aaaaaa'),
new BasicColorTheme('#FF5733'),
new BasicColorTheme('#33FF57'),
new BasicColorTheme('#3357FF'),
new BasicColorTheme('#FF33A1'),
new BasicColorTheme('#FF8C33'),
new BasicColorTheme('#33FFF5'),
new BasicColorTheme('#8C33FF'),
new BasicColorTheme('#333333','#fff')
];
browser.storage.local.set({ fwt_themes: ALL_THEMES });
const DEFAULT_THEME = { colors: {} };
function applyThemeToWindow(windowId, themeId) {
console.log('All tehmes', ALL_THEMES);
// console.log('Applying theme', themeId);
let theme = ALL_THEMES.find(theme => theme.frame === themeId);
if (!theme) {
console.log('Theme not found', themeId);
return;
}
browser.theme.update(windowId, theme.browserThemeObject);
theme.usage += 1;
theme.lastUsed = Date.now();
themeOfWindowID.set(windowId, theme);
saveThemeOfWindowID();
}
function resetThemeToDefault(windowId) {
browser.theme.reset(windowId);
themeOfWindowID.delete(windowId);
saveThemeOfWindowID();
}
async function applyThemeToAllWindows() {
for (const window of await browser.windows.getAll()) {
if (themeOfWindowID.has(window.id)) {
applyThemeToWindow(window.id, themeOfWindowID.get(window.id).frame);
}
}
}
function freeThemeOfDestroyedWindow(window_id) {
console.log('freeThemeOfDestroyedWindow', window_id);
const theme = themeOfWindowID.get(window_id);
if (theme) {
theme.usage -= 1;
themeOfWindowID.delete(window_id);
saveThemeOfWindowID();
}
}
// Popup interaction
browser.runtime.onMessage.addListener((message, sender) => {
if (message.command === "applyTheme") {
browser.windows.getCurrent().then(window => {
if (message.themeId === null) {
resetThemeToDefault(window.id);
} else {
const themeId = message.themeId;
if (themeId) {
applyThemeToWindow(window.id, themeId);
}
}
});
}
});
browser.runtime.onStartup.addListener(async () => {
await loadThemeOfWindowID();
applyThemeToAllWindows();
});
browser.runtime.onInstalled.addListener(async () => {
await loadThemeOfWindowID();
applyThemeToAllWindows();
});
browser.windows.onRemoved.addListener(freeThemeOfDestroyedWindow);