somtodaynt/background/js/index.js
2020-12-10 10:14:29 +01:00

64 lines
1.5 KiB
JavaScript

console.log('SOMtodayn\'t background script loaded')
// Read array from local storage
function getSlogans () {
let slogans
let tonight
if (localStorage.getItem('SOMtodayntSlogans') != null) {
slogans = JSON.parse(localStorage.getItem('SOMtodayntSlogans'))
tonight = JSON.parse(localStorage.getItem('SOMtonight'))
} else {
slogans = ["Welkom bij SOMtodayn't"]
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))
}
// Handle requests
function handleMessage (request, sender, sendResponse) {
let 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)