somtodaynt/background/js/index.js

59 lines
1.7 KiB
JavaScript
Raw Normal View History

console.log('SOMtodayn\'t background script loaded')
// Read array from local storage
function getSlogans() {
var slogans;
if (localStorage.getItem('SOMtodayntSlogans') != null) {
slogans = JSON.parse(localStorage.getItem('SOMtodayntSlogans'));
2019-11-22 14:29:27 +01:00
tonight = JSON.parse(localStorage.getItem('SOMtonight'));
} else {
2019-11-22 14:29:27 +01:00
var slogans = [ "Welkom bij SOMtodayn't" ];
var tonight = true;
localStorage.setItem('SOMtodayntSlogans', JSON.stringify(slogans));
2019-11-22 14:29:27 +01:00
localStorage.setItem('SOMtonight', JSON.stringify(tonight));
}
2019-11-22 14:29:27 +01:00
return [
tonight,
slogans
];
}
// Update array in local storage
function setSlogans(array) {
localStorage.setItem('SOMtodayntSlogans', JSON.stringify(array));
return true;
}
2019-11-22 14:29:27 +01:00
function setTonight(value) {
localStorage.setItem('SOMtonight', JSON.stringify(value));
return;
}
// Handle requests
function handleMessage(request, sender, sendResponse) {
var response;
// DEBUG // console.log(sender);
// DEBUG // console.log("Request: " + request.type)
switch (request.type) {
case 'get':
response = getSlogans();
break;
2019-11-22 14:29:27 +01:00
case 'setSlogans':
response = setSlogans(request.value);
break;
2019-11-22 14:29:27 +01:00
case 'setTonight':
response = setTonight(request.value);
break;
}
// Send response to either the content script or popup
if (sender.tab) {
browser.tabs.sendMessage(sender.tab.id, response);
} else {
sendResponse({response: response});
}
// DEBUG // console.log("Response: " + response)
}
browser.runtime.onMessage.addListener(handleMessage);