forked from mdn/webextensions-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
49 lines (44 loc) · 979 Bytes
/
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
var currentTheme = '';
const themes = {
'day': {
images: {
headerURL: 'sun.jpg',
},
colors: {
accentcolor: '#CF723F',
textcolor: '#111',
}
},
'night': {
images: {
headerURL: 'moon.jpg',
},
colors: {
accentcolor: '#000',
textcolor: '#fff',
}
}
};
function setTheme(theme) {
if (currentTheme === theme) {
// No point in changing the theme if it has already been set.
return;
}
currentTheme = theme;
browser.theme.update(themes[theme]);
}
function checkTime() {
let date = new Date();
let hours = date.getHours();
// Will set the sun theme between 8am and 8pm.
if ((hours > 8) && (hours < 20)) {
setTheme('day');
} else {
setTheme('night');
}
}
// On start up, check the time to see what theme to show.
checkTime();
// Set up an alarm to check this regularly.
browser.alarms.onAlarm.addListener(checkTime);
browser.alarms.create('checkTime', {periodInMinutes: 5});