Files
webextensions-examples/dynamic-theme/background.js
Andy McKay 07538307f4 add in a dynamic theme example (#230)
* add in a dynamic theme example

* update colour for when image is too small

* some fixups after stupid mistakes
2017-06-15 11:31:58 -07:00

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});