somtodaynt/background/js/index.js

59 lines
1.7 KiB
JavaScript

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'));
tonight = JSON.parse(localStorage.getItem('SOMtonight'));
} else {
var slogans = [ "Welkom bij SOMtodayn't" ];
var tonight = true;
localStorage.setItem('SOMtodayntSlogans', JSON.stringify(slogans));
localStorage.setItem('SOMtonight', JSON.stringify(tonight));
}
return [
tonight,
slogans
];
}
// Update array in local storage
function setSlogans(array) {
localStorage.setItem('SOMtodayntSlogans', JSON.stringify(array));
return true;
}
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;
case 'setSlogans':
response = setSlogans(request.value);
break;
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);