-
Notifications
You must be signed in to change notification settings - Fork 3
/
background.js
65 lines (57 loc) · 1.78 KB
/
background.js
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
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();
const ALL_THEMES = [
new BasicColorTheme('#ec5f67'),
new BasicColorTheme('#f99157'),
new BasicColorTheme('#fac863'),
new BasicColorTheme('#99c794'),
new BasicColorTheme('#5fb3b3'),
new BasicColorTheme('#6699cc'),
new BasicColorTheme('#c594c5'),
];
function getNextTheme() {
const sortedThemes = [...ALL_THEMES];
sortedThemes.sort((a, b) => {
if (a.usage == b.usage) {
return a.lastUsed > b.lastUsed;
}
return a.usage > b.usage;
});
return sortedThemes[0];
}
function applyThemeToWindow(window) {
const newTheme = getNextTheme();
browser.theme.update(window.id, newTheme.browserThemeObject);
newTheme.usage += 1;
newTheme.lastUsed = Date.now();
themeOfWindowID.set(window.id, newTheme);
}
async function applyThemeToAllWindows() {
for (const window of await browser.windows.getAll()) {
applyThemeToWindow(window);
}
}
function freeThemeOfDestroyedWindow(window_id) {
const theme = themeOfWindowID.get(window_id);
theme.usage -= 1;
themeOfWindowID.delete(window_id);
}
browser.windows.onCreated.addListener(applyThemeToWindow);
browser.windows.onRemoved.addListener(freeThemeOfDestroyedWindow);
browser.runtime.onStartup.addListener(applyThemeToAllWindows);
browser.runtime.onInstalled.addListener(applyThemeToAllWindows);