From 40a469de0ea46b7361b538267d6b2f8938c0aa10 Mon Sep 17 00:00:00 2001 From: Arne van Iterson Date: Fri, 22 Nov 2019 16:01:09 +0100 Subject: [PATCH] Added reload on SOMtonight toggle --- content/js/index.js | 88 +++++++++++++++++++++++---------------------- manifest.json | 5 +++ popup/js/index.js | 18 ++++++++++ 3 files changed, 69 insertions(+), 42 deletions(-) diff --git a/content/js/index.js b/content/js/index.js index 20a2bb4..1e9b76e 100644 --- a/content/js/index.js +++ b/content/js/index.js @@ -2,52 +2,56 @@ console.log('Somtoday\'nt content script active'); function changeSlogan(message) { - tonight = message[0]; - slogans = message[1]; - - // 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}`); + if (message == 'reload') { + location.reload(); } else { - var sloganText = "welkom bij somtodayn't"; - console.log('No slogans found') - } + tonight = message[0]; + slogans = message[1]; - // 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] + " "; + // 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 { + var sloganText = "welkom bij somtodayn't"; + console.log('No slogans found') } - } 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 = + `${slogan.white} + ${slogan.blue}` + ; - // Edit HTML - document.getElementById('slogan').innerHTML = - `${slogan.white} - ${slogan.blue}` - ; - - // Add a little credit - document.querySelectorAll('footer > span')[0].innerHTML = "Somtodayn't is een add-on door McArn."; - - // Set theme - console.log(tonight); - if (tonight) { - var cssUrl = browser.runtime.getURL("content/css/index.css"); - var cssTag = ``; - document.body.innerHTML += cssTag; + // Add a little credit + document.querySelectorAll('footer > span')[0].innerHTML = "Somtodayn't is een add-on door McArn."; + + // Set theme + console.log(tonight); + if (tonight) { + var cssUrl = browser.runtime.getURL("content/css/index.css"); + var cssTag = ``; + document.body.innerHTML += cssTag; + } } } diff --git a/manifest.json b/manifest.json index e78fc8f..5f328ef 100644 --- a/manifest.json +++ b/manifest.json @@ -12,6 +12,11 @@ "id": "arne@arnweb.nl" } }, + + "permissions": [ + "tabs", + "storage" + ], "icons": { "25" : "assets/icons/25.png", diff --git a/popup/js/index.js b/popup/js/index.js index 9cf7573..f41cbbd 100644 --- a/popup/js/index.js +++ b/popup/js/index.js @@ -49,12 +49,30 @@ request.then(function(message) { checkbox.checked = true; } checkbox.addEventListener('click', function(event) { + // Update value in local storage request = browser.runtime.sendMessage({ type: "setTonight", 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 var sloganList = '';