Added reload on SOMtonight toggle

This commit is contained in:
Arne van Iterson 2019-11-22 16:01:09 +01:00
parent 26ba99b803
commit 40a469de0e
3 changed files with 69 additions and 42 deletions

View File

@ -2,52 +2,56 @@ console.log('Somtoday\'nt content script active');
function changeSlogan(message) function changeSlogan(message)
{ {
tonight = message[0]; if (message == 'reload') {
slogans = message[1]; location.reload();
// Select random slogan
console.log(`Loaded ${slogans.length} slogans`);
if (slogans.length >= 1) {
var random = Math.floor(Math.random() * slogans.length);
var sloganText = slogans[random].toLowerCase();
console.log(`Slogan changed to: ${sloganText}`);
} else { } else {
var sloganText = "welkom bij somtodayn't"; tonight = message[0];
console.log('No slogans found') slogans = message[1];
}
// Split the slogan into array // Select random slogan
var sloganArray = sloganText.split(" "); console.log(`Loaded ${slogans.length} slogans`);
var slogan = { if (slogans.length >= 1) {
white: '', var random = Math.floor(Math.random() * slogans.length);
blue: '' var sloganText = slogans[random].toLowerCase();
}; console.log(`Slogan changed to: ${sloganText}`);
} else {
// Set the last word in the array to be blue var sloganText = "welkom bij somtodayn't";
if (sloganArray.length > 1) { console.log('No slogans found')
slogan.blue = sloganArray.pop();
for (let index = 0; index < sloganArray.length; index++) {
slogan.white = slogan.white + sloganArray[index] + " ";
} }
} else {
slogan.white = sloganArray; // Split the slogan into array
} var sloganArray = sloganText.split(" ");
var slogan = {
white: '',
blue: ''
};
// Set the last word in the array to be blue
if (sloganArray.length > 1) {
slogan.blue = sloganArray.pop();
for (let index = 0; index < sloganArray.length; index++) {
slogan.white = slogan.white + sloganArray[index] + " ";
}
} else {
slogan.white = sloganArray;
}
// Edit HTML
document.getElementById('slogan').innerHTML =
`<span class="white block">${slogan.white}</span>
<span>${slogan.blue}</span>`
;
// Edit HTML // Add a little credit
document.getElementById('slogan').innerHTML = document.querySelectorAll('footer > span')[0].innerHTML = "Somtodayn't is een add-on door McArn.";
`<span class="white block">${slogan.white}</span>
<span>${slogan.blue}</span>` // Set theme
; console.log(tonight);
if (tonight) {
// Add a little credit var cssUrl = browser.runtime.getURL("content/css/index.css");
document.querySelectorAll('footer > span')[0].innerHTML = "Somtodayn't is een add-on door McArn."; var cssTag = `<link rel='stylesheet' href='${cssUrl}'>`;
document.body.innerHTML += cssTag;
// Set theme }
console.log(tonight);
if (tonight) {
var cssUrl = browser.runtime.getURL("content/css/index.css");
var cssTag = `<link rel='stylesheet' href='${cssUrl}'>`;
document.body.innerHTML += cssTag;
} }
} }

View File

@ -12,6 +12,11 @@
"id": "arne@arnweb.nl" "id": "arne@arnweb.nl"
} }
}, },
"permissions": [
"tabs",
"storage"
],
"icons": { "icons": {
"25" : "assets/icons/25.png", "25" : "assets/icons/25.png",

View File

@ -49,12 +49,30 @@ request.then(function(message) {
checkbox.checked = true; checkbox.checked = true;
} }
checkbox.addEventListener('click', function(event) { checkbox.addEventListener('click', function(event) {
// Update value in local storage
request = browser.runtime.sendMessage({ request = browser.runtime.sendMessage({
type: "setTonight", type: "setTonight",
value: checkbox.checked value: checkbox.checked
}); });
// Reload all SOMtoday tabs
var reload = browser.tabs.query({});
reload.then(logTabs, onError);
}); });
// Find id of somtoday.nl tab
function logTabs(tabs) {
for (let tab of tabs) {
// Match only tabs with url somtoday.nl
if (tab.url.includes('somtoday.nl')) {
browser.tabs.sendMessage(tab.id, 'reload');
}
}
}
// Handle query errors
function onError(error) {
console.log(`Error: ${error}`);
}
// Add existing slogans to list items in the popup // Add existing slogans to list items in the popup
var sloganList = ''; var sloganList = '';