mirror of
https://github.com/mdn/webextensions-examples.git
synced 2026-04-16 06:18:35 +02:00
* add in a dynamic theme example * update colour for when image is too small * some fixups after stupid mistakes
50 lines
979 B
JavaScript
50 lines
979 B
JavaScript
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});
|