From 78aa7bf98e0fac0e96e7501a29dec74461a6e577 Mon Sep 17 00:00:00 2001 From: Kevin Payravi Date: Fri, 28 Apr 2023 01:17:29 -0400 Subject: [PATCH] Storing power setting in local instead of sync --- background.js | 147 ++++++------- content.js | 219 +++++++++---------- popup.js | 8 +- settings.js | 577 +++++++++++++++++++++++++------------------------- 4 files changed, 480 insertions(+), 471 deletions(-) diff --git a/background.js b/background.js index 9f4d3ac..1e21946 100644 --- a/background.js +++ b/background.js @@ -127,82 +127,85 @@ async function main(eventInfo, eventName) { sites = await getData(); - chrome.storage.sync.get(function (storage) { - if ((storage.power ?? 'on') === 'on') { - // Check if site is in our list of wikis: - let matchingSites = sites.filter(el => url.href.replace(/^https?:\/\//, '').startsWith(el.origin_base_url)); - if (matchingSites.length > 0) { - // Select match with longest base URL - let closestMatch = ""; - matchingSites.forEach(site => { - if (site.origin_base_url.length > closestMatch.length) { - closestMatch = site.origin_base_url; - } - }); - let site = matchingSites.find(site => site.origin_base_url === closestMatch); - if (site) { - // Get user's settings for the wiki - let settings = storage.siteSettings || {}; - let id = site['id']; - let siteSetting = ''; - if (settings.hasOwnProperty(id) && settings[id].hasOwnProperty('action')) { - siteSetting = settings[id].action; - } else if (storage.defaultActionSettings && storage.defaultActionSettings[site.language]) { - siteSetting = storage.defaultActionSettings[site.language]; - } else { - siteSetting = 'alert'; - } - // Check if redirects are enabled for the site: - if (siteSetting === 'redirect') { - // Get article name from the end of the URL; - // We can't just take the last part of the path due to subpages; - // Instead, we take everything after the wiki's base URL + content path: - let article = url.href.split(site['origin_base_url'] + site['origin_content_path'])[1]; - // Set up URL to redirect user to based on wiki platform: - if (article || (!article && !url.href.split(site['origin_base_url'] + '/')[1])) { - let newURL = ''; - if (article) { - let searchParams = ''; - switch (site['destination_platform']) { - case 'mediawiki': - searchParams = 'Special:Search/' + article; - break; - case 'doku': - searchParams = 'start?do=search&q=' + article; - break; - } - newURL = 'https://' + site["destination_base_url"] + site["destination_content_path"] + searchParams; - } else { - newURL = 'https://' + site["destination_base_url"]; - } - - // Perform redirect: - chrome.tabs.update(eventInfo.tabId, { url: newURL }); - // Increase global redirect count: - chrome.storage.sync.set({ 'countRedirects': (storage.countRedirects ?? 0) + 1 }); - - // Notify if enabled - if ((storage.notifications ?? 'on') === 'on') { - // Notify that user is being redirected - let notifID = 'independent-wiki-redirector-notification-' + Math.floor(Math.random() * 1E16); - chrome.notifications.create(notifID, { - "type": "basic", - "iconUrl": 'images/logo-48.png', - "title": "You've been redirected!", - "message": "Indie Wiki Buddy has sent you from " + site['origin'] + " to " + site['destination'] - }); - // Self-clear notification after 6 seconds: - setTimeout(function () { chrome.notifications.clear(notifID); }, 6000); - } + chrome.storage.local.get(function (localStorage) { + chrome.storage.sync.get(function (syncStorage) { + const storage = {...syncStorage, ...localStorage}; + if ((storage.power ?? 'on') === 'on') { + // Check if site is in our list of wikis: + let matchingSites = sites.filter(el => url.href.replace(/^https?:\/\//, '').startsWith(el.origin_base_url)); + if (matchingSites.length > 0) { + // Select match with longest base URL + let closestMatch = ""; + matchingSites.forEach(site => { + if (site.origin_base_url.length > closestMatch.length) { + closestMatch = site.origin_base_url; + } + }); + let site = matchingSites.find(site => site.origin_base_url === closestMatch); + if (site) { + // Get user's settings for the wiki + let settings = storage.siteSettings || {}; + let id = site['id']; + let siteSetting = ''; + if (settings.hasOwnProperty(id) && settings[id].hasOwnProperty('action')) { + siteSetting = settings[id].action; + } else if (storage.defaultActionSettings && storage.defaultActionSettings[site.language]) { + siteSetting = storage.defaultActionSettings[site.language]; + } else { + siteSetting = 'alert'; + } + // Check if redirects are enabled for the site: + if (siteSetting === 'redirect') { + // Get article name from the end of the URL; + // We can't just take the last part of the path due to subpages; + // Instead, we take everything after the wiki's base URL + content path: + let article = url.href.split(site['origin_base_url'] + site['origin_content_path'])[1]; + // Set up URL to redirect user to based on wiki platform: + if (article || (!article && !url.href.split(site['origin_base_url'] + '/')[1])) { + let newURL = ''; + if (article) { + let searchParams = ''; + switch (site['destination_platform']) { + case 'mediawiki': + searchParams = 'Special:Search/' + article; + break; + case 'doku': + searchParams = 'start?do=search&q=' + article; + break; + } + newURL = 'https://' + site["destination_base_url"] + site["destination_content_path"] + searchParams; + } else { + newURL = 'https://' + site["destination_base_url"]; + } + + // Perform redirect: + chrome.tabs.update(eventInfo.tabId, { url: newURL }); + // Increase global redirect count: + chrome.storage.sync.set({ 'countRedirects': (storage.countRedirects ?? 0) + 1 }); + + // Notify if enabled + if ((storage.notifications ?? 'on') === 'on') { + // Notify that user is being redirected + let notifID = 'independent-wiki-redirector-notification-' + Math.floor(Math.random() * 1E16); + chrome.notifications.create(notifID, { + "type": "basic", + "iconUrl": 'images/logo-48.png', + "title": "You've been redirected!", + "message": "Indie Wiki Buddy has sent you from " + site['origin'] + " to " + site['destination'] + }); + // Self-clear notification after 6 seconds: + setTimeout(function () { chrome.notifications.clear(notifID); }, 6000); + } + } + } else if ((storage.breezewiki ?? 'off') === 'on') { + redirectToBreezeWiki(storage, eventInfo, url); } - } else if ((storage.breezewiki ?? 'off') === 'on') { - redirectToBreezeWiki(storage, eventInfo, url); } + } else if ((storage.breezewiki ?? 'off') === 'on') { + redirectToBreezeWiki(storage, eventInfo, url); } - } else if ((storage.breezewiki ?? 'off') === 'on') { - redirectToBreezeWiki(storage, eventInfo, url); } - } + }); }); } }); diff --git a/content.js b/content.js index b1a1609..cacb445 100644 --- a/content.js +++ b/content.js @@ -204,124 +204,127 @@ function main(mutations = null, observer = null) { if (observer) { observer.disconnect(); } - chrome.storage.sync.get(function (storage) { - // Check if extension is on: - if ((storage.power ?? 'on') === 'on') { - // Check if on Fandom, Fextralife, or BreezeWiki - // If on BreezeWiki, check if there is a pathname (which indicates we are looking at a wiki) - if (currentURL.hostname.match(fandomRegex) || currentURL.hostname.match(fextraRegex) || (currentURL.hostname.match(breezeWikiRegex) && currentURL.pathname.length > 1)) { - let origin = currentURL; - // If on a BreezeWiki site, convert to Fandom link to match with our list of wikis: - if (currentURL.hostname.match(breezeWikiRegex)) { - origin = String(currentURL.pathname).split('/')[1] + '.fandom.com/wiki/'; - if (currentURL.search.includes('?q=')) { - origin = origin + currentURL.search.substring(3).split('&')[0]; - } else { - origin = origin + currentURL.pathname.split('/')[3]; + chrome.storage.local.get(function (localStorage) { + chrome.storage.sync.get(function (syncStorage) { + const storage = {...syncStorage, ...localStorage}; + // Check if extension is on: + if ((storage.power ?? 'on') === 'on') { + // Check if on Fandom, Fextralife, or BreezeWiki + // If on BreezeWiki, check if there is a pathname (which indicates we are looking at a wiki) + if (currentURL.hostname.match(fandomRegex) || currentURL.hostname.match(fextraRegex) || (currentURL.hostname.match(breezeWikiRegex) && currentURL.pathname.length > 1)) { + let origin = currentURL; + // If on a BreezeWiki site, convert to Fandom link to match with our list of wikis: + if (currentURL.hostname.match(breezeWikiRegex)) { + origin = String(currentURL.pathname).split('/')[1] + '.fandom.com/wiki/'; + if (currentURL.search.includes('?q=')) { + origin = origin + currentURL.search.substring(3).split('&')[0]; + } else { + origin = origin + currentURL.pathname.split('/')[3]; + } } - } - getData().then(sites => { - // Check if site is in our list of wikis: - let matchingSites = sites.filter(el => String(origin).replace(/^https?:\/\//, '').startsWith(el.origin_base_url)); - if (matchingSites.length > 0) { - // Select match with longest base URL - let closestMatch = ""; - matchingSites.forEach(site => { - if (site.origin_base_url.length > closestMatch.length) { - closestMatch = site.origin_base_url; - } - }); - let site = matchingSites.find(site => site.origin_base_url === closestMatch); - if (site) { - // Get user's settings for the wiki - let settings = storage.siteSettings || {}; - let id = site['id']; - let siteSetting = ''; - if (settings.hasOwnProperty(id) && settings[id].hasOwnProperty('action')) { - siteSetting = settings[id].action; - } else if (storage.defaultActionSettings && storage.defaultActionSettings[site.language]) { - siteSetting = storage.defaultActionSettings[site.language]; - } else { - siteSetting = 'alert'; - } - // Notify if enabled for the wiki: - if (siteSetting === 'alert') { - // Get article name from the end of the URL; - // We can't just take the last part of the path due to subpages; - // Instead, we take everything after the wiki's base URL + content path: - let article = String(origin).split(site['origin_base_url'] + site['origin_content_path'])[1]; - // Set up URL to redirect user to based on wiki platform: - if (article || (!article && !url.href.split(site['origin_base_url'] + '/')[1])) { - let newURL = ''; - if (article) { - let searchParams = ''; - switch (site['destination_platform']) { - case 'mediawiki': - searchParams = 'Special:Search/' + article; - break; - case 'doku': - searchParams = 'start?do=search&q=' + article; - break; + getData().then(sites => { + // Check if site is in our list of wikis: + let matchingSites = sites.filter(el => String(origin).replace(/^https?:\/\//, '').startsWith(el.origin_base_url)); + if (matchingSites.length > 0) { + // Select match with longest base URL + let closestMatch = ""; + matchingSites.forEach(site => { + if (site.origin_base_url.length > closestMatch.length) { + closestMatch = site.origin_base_url; + } + }); + let site = matchingSites.find(site => site.origin_base_url === closestMatch); + if (site) { + // Get user's settings for the wiki + let settings = storage.siteSettings || {}; + let id = site['id']; + let siteSetting = ''; + if (settings.hasOwnProperty(id) && settings[id].hasOwnProperty('action')) { + siteSetting = settings[id].action; + } else if (storage.defaultActionSettings && storage.defaultActionSettings[site.language]) { + siteSetting = storage.defaultActionSettings[site.language]; + } else { + siteSetting = 'alert'; + } + // Notify if enabled for the wiki: + if (siteSetting === 'alert') { + // Get article name from the end of the URL; + // We can't just take the last part of the path due to subpages; + // Instead, we take everything after the wiki's base URL + content path: + let article = String(origin).split(site['origin_base_url'] + site['origin_content_path'])[1]; + // Set up URL to redirect user to based on wiki platform: + if (article || (!article && !url.href.split(site['origin_base_url'] + '/')[1])) { + let newURL = ''; + if (article) { + let searchParams = ''; + switch (site['destination_platform']) { + case 'mediawiki': + searchParams = 'Special:Search/' + article; + break; + case 'doku': + searchParams = 'start?do=search&q=' + article; + break; + } + newURL = 'https://' + site["destination_base_url"] + site["destination_content_path"] + searchParams; + } else { + newURL = 'https://' + site["destination_base_url"]; } - newURL = 'https://' + site["destination_base_url"] + site["destination_content_path"] + searchParams; - } else { - newURL = 'https://' + site["destination_base_url"]; + // Notify that another wiki is available + displayRedirectBanner(newURL, site['destination'], storage); } - // Notify that another wiki is available - displayRedirectBanner(newURL, site['destination'], storage); } } } - } - }); - } else if ((storage.searchFilter ?? 'on') === 'on') { - if (currentURL.hostname.includes('www.google.')) { - // Check if doing a Google search: - function filterGoogle() { - let searchResults = document.querySelectorAll("div[lang] a[href*='fandom.com'], div[lang] a[href*='fextralife.com']"); - filterSearchResults(searchResults, 'google', storage); - } - addLocationObserver(main); - filterGoogle(); - } else if (currentURL.hostname.includes('duckduckgo.com') && currentURL.search.includes('q=')) { - // Check if doing a Duck Duck Go search: - function filterDuckDuckGo() { - let searchResults = document.querySelectorAll("h2>a[href*='fandom.com'], h2>a[href*='fextralife.com']"); - filterSearchResults(searchResults, 'duckduckgo', storage); - } - // Need to wait for document to be ready - if (document.readyState === 'complete') { + }); + } else if ((storage.searchFilter ?? 'on') === 'on') { + if (currentURL.hostname.includes('www.google.')) { + // Check if doing a Google search: + function filterGoogle() { + let searchResults = document.querySelectorAll("div[lang] a[href*='fandom.com'], div[lang] a[href*='fextralife.com']"); + filterSearchResults(searchResults, 'google', storage); + } addLocationObserver(main); - filterDuckDuckGo(); - } else { - document.addEventListener('readystatechange', e => { - if (document.readyState === 'complete') { - addLocationObserver(main); - filterDuckDuckGo(); - } - }); - } - } else if (currentURL.hostname.includes('www.bing.com')) { - // Check if doing a Bing search: - function filterBing() { - let searchResults = Array.from(document.querySelectorAll(".b_attribution>cite")).filter(el => el.innerHTML.includes('fandom.com') || el.innerHTML.includes('fextralife.com')); - filterSearchResults(searchResults, 'bing', storage); - } - // Need to wait for document to be ready - if (document.readyState === 'complete') { - addLocationObserver(main); - filterBing(); - } else { - document.addEventListener('readystatechange', e => { - if (document.readyState === 'complete') { - addLocationObserver(main); - filterBing(); - } - }); + filterGoogle(); + } else if (currentURL.hostname.includes('duckduckgo.com') && currentURL.search.includes('q=')) { + // Check if doing a Duck Duck Go search: + function filterDuckDuckGo() { + let searchResults = document.querySelectorAll("h2>a[href*='fandom.com'], h2>a[href*='fextralife.com']"); + filterSearchResults(searchResults, 'duckduckgo', storage); + } + // Need to wait for document to be ready + if (document.readyState === 'complete') { + addLocationObserver(main); + filterDuckDuckGo(); + } else { + document.addEventListener('readystatechange', e => { + if (document.readyState === 'complete') { + addLocationObserver(main); + filterDuckDuckGo(); + } + }); + } + } else if (currentURL.hostname.includes('www.bing.com')) { + // Check if doing a Bing search: + function filterBing() { + let searchResults = Array.from(document.querySelectorAll(".b_attribution>cite")).filter(el => el.innerHTML.includes('fandom.com') || el.innerHTML.includes('fextralife.com')); + filterSearchResults(searchResults, 'bing', storage); + } + // Need to wait for document to be ready + if (document.readyState === 'complete') { + addLocationObserver(main); + filterBing(); + } else { + document.addEventListener('readystatechange', e => { + if (document.readyState === 'complete') { + addLocationObserver(main); + filterBing(); + } + }); + } } } } - } + }); }); } diff --git a/popup.js b/popup.js index 66b9ab8..3902a71 100644 --- a/popup.js +++ b/popup.js @@ -1,6 +1,6 @@ // Set power setting function setPower(setting) { - chrome.storage.sync.set({ 'power': setting }); + chrome.storage.local.set({ 'power': setting }); var powerImage = document.getElementById('powerImage'); powerImage.src = 'images/power-' + setting + '.png'; powerImage.alt = 'Indie Wiki Buddy is ' + setting; @@ -86,7 +86,7 @@ async function loadBreezeWikiOptions() { // Set power setting function setPower(setting, storeSetting = true) { if (storeSetting) { - chrome.storage.sync.set({ 'power': setting }); + chrome.storage.local.set({ 'power': setting }); } var powerImage = document.getElementById('powerImage'); powerImage.src = 'images/power-' + setting + '.png'; @@ -178,7 +178,7 @@ document.addEventListener('DOMContentLoaded', function () { }); // Set setting toggle values: - chrome.storage.sync.get({ 'power': 'on' }, function (item) { + chrome.storage.local.get({ 'power': 'on' }, function (item) { setPower(item.power, false); }); chrome.storage.sync.get({ 'notifications': 'on' }, function (item) { @@ -193,7 +193,7 @@ document.addEventListener('DOMContentLoaded', function () { // Add event listeners for setting toggles document.getElementById('powerCheckbox').addEventListener('change', function () { - chrome.storage.sync.get({ 'power': 'on' }, function (item) { + chrome.storage.local.get({ 'power': 'on' }, function (item) { if (item.power === 'on') { setPower('off'); } else { diff --git a/settings.js b/settings.js index fc76f7b..8cff9e5 100644 --- a/settings.js +++ b/settings.js @@ -44,315 +44,318 @@ async function getData() { async function loadOptions(lang) { sites = await getData(); - chrome.storage.sync.get(function (storage) { - let siteSettings = storage.siteSettings || {}; - let defaultActionSettings = storage.defaultActionSettings || {}; - let defaultSearchFilterSettings = storage.defaultSearchFilterSettings || {}; + chrome.storage.local.get(function (localStorage) { + chrome.storage.sync.get(function (syncStorage) { + const storage = {...syncStorage, ...localStorage}; + let siteSettings = storage.siteSettings || {}; + let defaultActionSettings = storage.defaultActionSettings || {}; + let defaultSearchFilterSettings = storage.defaultSearchFilterSettings || {}; - // Load BreezeWiki options: - chrome.storage.sync.get(['breezewikiHostOptions', 'breezewikiHostFetchTimestamp', 'breezewikiHost'], function (item) { - let hostOptions = item.breezewikiHostOptions; - let hostFetchTimestamp = item.breezewikiHostFetchTimestamp; - let host = item.breezewikiHost; - // Fetch and cache list of BreezeWiki hosts if first time, - // or if it has been 24 hrs since last refresh - if (!host || !hostOptions || !hostFetchTimestamp || (Date.now() - 86400000 > hostFetchTimestamp)) { - fetch('https://bw.getindie.wiki/instances.json') - .then((response) => response.json()) - .then((breezewikiHosts) => { - // If host isn't set, or currently selected host is no longer available, select random host: - if (!host || !breezewikiHosts.some(item => item.instance === host)) { - // Check if BreezeWiki's main site is available - let breezewikiMain = breezewikiHosts.filter(host => host.instance === 'https://breezewiki.com'); - if (breezewikiMain.length > 0) { - host = breezewikiMain[0].instance; - } else { - // If BreezeWiki.com is not available, set to a random mirror - host = breezewikiHosts[Math.floor(Math.random() * breezewikiHosts.length)].instance; + // Load BreezeWiki options: + chrome.storage.sync.get(['breezewikiHostOptions', 'breezewikiHostFetchTimestamp', 'breezewikiHost'], function (item) { + let hostOptions = item.breezewikiHostOptions; + let hostFetchTimestamp = item.breezewikiHostFetchTimestamp; + let host = item.breezewikiHost; + // Fetch and cache list of BreezeWiki hosts if first time, + // or if it has been 24 hrs since last refresh + if (!host || !hostOptions || !hostFetchTimestamp || (Date.now() - 86400000 > hostFetchTimestamp)) { + fetch('https://bw.getindie.wiki/instances.json') + .then((response) => response.json()) + .then((breezewikiHosts) => { + // If host isn't set, or currently selected host is no longer available, select random host: + if (!host || !breezewikiHosts.some(item => item.instance === host)) { + // Check if BreezeWiki's main site is available + let breezewikiMain = breezewikiHosts.filter(host => host.instance === 'https://breezewiki.com'); + if (breezewikiMain.length > 0) { + host = breezewikiMain[0].instance; + } else { + // If BreezeWiki.com is not available, set to a random mirror + host = breezewikiHosts[Math.floor(Math.random() * breezewikiHosts.length)].instance; + } } - } - // Populate dropdown selection of hosts - const breezewikiHostSelect = document.getElementById('breezewikiHostSelect'); - for (var i = 0; i < breezewikiHosts.length; i++) { - let option = document.createElement('option'); - option.value = breezewikiHosts[i].instance; - let innerText = breezewikiHosts[i].instance.replace('https://', ''); - const numberOfPeriods = (innerText.match(/\./g)||[]).length; - if (numberOfPeriods > 1) { - innerText = innerText.substring(innerText.indexOf('.') + 1); + // Populate dropdown selection of hosts + const breezewikiHostSelect = document.getElementById('breezewikiHostSelect'); + for (var i = 0; i < breezewikiHosts.length; i++) { + let option = document.createElement('option'); + option.value = breezewikiHosts[i].instance; + let innerText = breezewikiHosts[i].instance.replace('https://', ''); + const numberOfPeriods = (innerText.match(/\./g)||[]).length; + if (numberOfPeriods > 1) { + innerText = innerText.substring(innerText.indexOf('.') + 1); + } + option.innerText = innerText; + breezewikiHostSelect.appendChild(option); + if (option.value === host) { + breezewikiHostSelect.value = host; + } } - option.innerText = innerText; - breezewikiHostSelect.appendChild(option); - if (option.value === host) { - breezewikiHostSelect.value = host; - } - } - // Store BreezeWiki host details - chrome.storage.sync.set({ 'breezewikiHost': host }); - chrome.storage.sync.set({ 'breezewikiHostOptions': breezewikiHosts }); - chrome.storage.sync.set({ 'breezewikiHostFetchTimestamp': Date.now() }); - }); - } else { - // If currently selected host is no longer available, select random host: - if (!hostOptions.some(item => item.instance === host)) { - host = hostOptions[Math.floor(Math.random() * hostOptions.length)].instance; - } - // Populate dropdown selection of hosts - const breezewikiHostSelect = document.getElementById('breezewikiHostSelect'); - for (var i = 0; i < hostOptions.length; i++) { - let option = document.createElement('option'); - option.value = hostOptions[i].instance; - let innerText = hostOptions[i].instance.replace('https://', ''); - const numberOfPeriods = (innerText.match(/\./g)||[]).length; - if (numberOfPeriods > 1) { - innerText = innerText.substring(innerText.indexOf('.') + 1); - } - option.innerText = innerText; - breezewikiHostSelect.appendChild(option); - if (option.value === host) { - breezewikiHostSelect.value = host; - } - } - // Store BreezeWiki host details - chrome.storage.sync.set({ 'breezewikiHost': host }); - } - }); - - // Populate individual wiki settings: - var toggleContainer = document.getElementById('toggles'); - for (var i = 0; i < sites.length; i++) { - if (sites[i].language === lang) { - var key = sites[i].id; - - // Create radio for disabled: - let labelDisabled = document.createElement("label"); - let inputDisabled = document.createElement("input"); - inputDisabled.classList = 'toggleDisable'; - inputDisabled.type = "radio"; - inputDisabled.name = key; - inputDisabled.title = 'Disable actions for ' + sites[i].origins_label; - inputDisabled.id = key + '-redirect'; - inputDisabled.lang = lang; - - // Create radio for redirect: - let labelRedirect = document.createElement("label"); - let inputRedirect = document.createElement("input"); - inputRedirect.classList = 'toggleRedirect'; - inputRedirect.type = "radio"; - inputRedirect.name = key; - inputRedirect.title = 'Automatically redirect from ' + sites[i].origins_label + ' to ' + sites[i].destination; - inputRedirect.id = key + '-redirect'; - inputRedirect.lang = lang; - - // Create radio for alert: - let labelAlert = document.createElement("label"); - let inputAlert = document.createElement("input"); - inputAlert.classList = 'toggleAlert'; - inputAlert.type = "radio"; - inputAlert.name = key; - inputAlert.title = 'Notify with banner when visiting ' + sites[i].origins_label; - inputAlert.id = key + '-alert'; - inputAlert.lang = lang; - - // Create checkbox for search filtering: - let labelFilter = document.createElement("label"); - let inputFilter = document.createElement("input"); - inputFilter.classList = 'toggleFilter'; - inputFilter.type = 'checkbox'; - inputFilter.name = key; - inputFilter.title = 'Filter ' + sites[i].origins_label + ' from Google, Bing, and DuckDuckGo'; - inputFilter.id = key + '-filter'; - inputFilter.lang = lang; - - // Check radio button based on user's settings - // Will default to alert or the last "select all" setting the user chose - if (siteSettings[key] && siteSettings[key].action) { - if (siteSettings[key].action === 'disabled') { - inputDisabled.checked = true; - } else if (siteSettings[key].action === 'redirect') { - inputRedirect.checked = true; - } else { - inputAlert.checked = true; - } + // Store BreezeWiki host details + chrome.storage.sync.set({ 'breezewikiHost': host }); + chrome.storage.sync.set({ 'breezewikiHostOptions': breezewikiHosts }); + chrome.storage.sync.set({ 'breezewikiHostFetchTimestamp': Date.now() }); + }); } else { - let actionSetting = defaultActionSettings[lang]; - if (actionSetting) { - if (actionSetting === 'disabled') { + // If currently selected host is no longer available, select random host: + if (!hostOptions.some(item => item.instance === host)) { + host = hostOptions[Math.floor(Math.random() * hostOptions.length)].instance; + } + // Populate dropdown selection of hosts + const breezewikiHostSelect = document.getElementById('breezewikiHostSelect'); + for (var i = 0; i < hostOptions.length; i++) { + let option = document.createElement('option'); + option.value = hostOptions[i].instance; + let innerText = hostOptions[i].instance.replace('https://', ''); + const numberOfPeriods = (innerText.match(/\./g)||[]).length; + if (numberOfPeriods > 1) { + innerText = innerText.substring(innerText.indexOf('.') + 1); + } + option.innerText = innerText; + breezewikiHostSelect.appendChild(option); + if (option.value === host) { + breezewikiHostSelect.value = host; + } + } + // Store BreezeWiki host details + chrome.storage.sync.set({ 'breezewikiHost': host }); + } + }); + + // Populate individual wiki settings: + var toggleContainer = document.getElementById('toggles'); + for (var i = 0; i < sites.length; i++) { + if (sites[i].language === lang) { + var key = sites[i].id; + + // Create radio for disabled: + let labelDisabled = document.createElement("label"); + let inputDisabled = document.createElement("input"); + inputDisabled.classList = 'toggleDisable'; + inputDisabled.type = "radio"; + inputDisabled.name = key; + inputDisabled.title = 'Disable actions for ' + sites[i].origins_label; + inputDisabled.id = key + '-redirect'; + inputDisabled.lang = lang; + + // Create radio for redirect: + let labelRedirect = document.createElement("label"); + let inputRedirect = document.createElement("input"); + inputRedirect.classList = 'toggleRedirect'; + inputRedirect.type = "radio"; + inputRedirect.name = key; + inputRedirect.title = 'Automatically redirect from ' + sites[i].origins_label + ' to ' + sites[i].destination; + inputRedirect.id = key + '-redirect'; + inputRedirect.lang = lang; + + // Create radio for alert: + let labelAlert = document.createElement("label"); + let inputAlert = document.createElement("input"); + inputAlert.classList = 'toggleAlert'; + inputAlert.type = "radio"; + inputAlert.name = key; + inputAlert.title = 'Notify with banner when visiting ' + sites[i].origins_label; + inputAlert.id = key + '-alert'; + inputAlert.lang = lang; + + // Create checkbox for search filtering: + let labelFilter = document.createElement("label"); + let inputFilter = document.createElement("input"); + inputFilter.classList = 'toggleFilter'; + inputFilter.type = 'checkbox'; + inputFilter.name = key; + inputFilter.title = 'Filter ' + sites[i].origins_label + ' from Google, Bing, and DuckDuckGo'; + inputFilter.id = key + '-filter'; + inputFilter.lang = lang; + + // Check radio button based on user's settings + // Will default to alert or the last "select all" setting the user chose + if (siteSettings[key] && siteSettings[key].action) { + if (siteSettings[key].action === 'disabled') { inputDisabled.checked = true; - } else if (actionSetting === 'redirect') { + } else if (siteSettings[key].action === 'redirect') { inputRedirect.checked = true; } else { inputAlert.checked = true; } } else { - inputAlert.checked = true; - } - } - - // Check search filter checkbox based on user's settings (default filter): - if (siteSettings[key] && siteSettings[key].searchFilter) { - if (siteSettings[key].searchFilter === 'false') { - inputFilter.checked = false; - } else { - inputFilter.checked = true; - } - } else { - let searchFilterSetting = defaultSearchFilterSettings[lang]; - if (searchFilterSetting && searchFilterSetting === 'false') { - inputFilter.checked = false; - } else { - inputFilter.checked = true; - } - } - - // Add listeners for when user clicks control: - inputRedirect.addEventListener('click', function (input) { - chrome.storage.sync.get({ 'siteSettings': {} }, function (response) { - var key = input.target.name; - response.siteSettings.get(key).set('action', 'redirect'); - chrome.storage.sync.set({ 'siteSettings': response.siteSettings }); - }); - }); - inputAlert.addEventListener('click', function (input) { - chrome.storage.sync.get({ 'siteSettings': {} }, function (response) { - var key = input.target.name; - response.siteSettings.get(key).set('action', 'alert'); - chrome.storage.sync.set({ 'siteSettings': response.siteSettings }); - }); - }); - inputDisabled.addEventListener('click', function (input) { - chrome.storage.sync.get({ 'siteSettings': {} }, function (response) { - var key = input.target.name; - response.siteSettings.get(key).set('action', 'disabled'); - chrome.storage.sync.set({ 'siteSettings': response.siteSettings }); - }); - }); - inputFilter.addEventListener('click', function (input) { - chrome.storage.sync.get({ 'siteSettings': {} }, function (response) { - var key = input.target.name; - if (input.target.checked === true) { - response.siteSettings.get(key).set('searchFilter', 'true'); + let actionSetting = defaultActionSettings[lang]; + if (actionSetting) { + if (actionSetting === 'disabled') { + inputDisabled.checked = true; + } else if (actionSetting === 'redirect') { + inputRedirect.checked = true; + } else { + inputAlert.checked = true; + } } else { - response.siteSettings.get(key).set('searchFilter', 'false'); + inputAlert.checked = true; } - chrome.storage.sync.set({ 'siteSettings': response.siteSettings }); + } + + // Check search filter checkbox based on user's settings (default filter): + if (siteSettings[key] && siteSettings[key].searchFilter) { + if (siteSettings[key].searchFilter === 'false') { + inputFilter.checked = false; + } else { + inputFilter.checked = true; + } + } else { + let searchFilterSetting = defaultSearchFilterSettings[lang]; + if (searchFilterSetting && searchFilterSetting === 'false') { + inputFilter.checked = false; + } else { + inputFilter.checked = true; + } + } + + // Add listeners for when user clicks control: + inputRedirect.addEventListener('click', function (input) { + chrome.storage.sync.get({ 'siteSettings': {} }, function (response) { + var key = input.target.name; + response.siteSettings.get(key).set('action', 'redirect'); + chrome.storage.sync.set({ 'siteSettings': response.siteSettings }); + }); + }); + inputAlert.addEventListener('click', function (input) { + chrome.storage.sync.get({ 'siteSettings': {} }, function (response) { + var key = input.target.name; + response.siteSettings.get(key).set('action', 'alert'); + chrome.storage.sync.set({ 'siteSettings': response.siteSettings }); + }); + }); + inputDisabled.addEventListener('click', function (input) { + chrome.storage.sync.get({ 'siteSettings': {} }, function (response) { + var key = input.target.name; + response.siteSettings.get(key).set('action', 'disabled'); + chrome.storage.sync.set({ 'siteSettings': response.siteSettings }); + }); + }); + inputFilter.addEventListener('click', function (input) { + chrome.storage.sync.get({ 'siteSettings': {} }, function (response) { + var key = input.target.name; + if (input.target.checked === true) { + response.siteSettings.get(key).set('searchFilter', 'true'); + } else { + response.siteSettings.get(key).set('searchFilter', 'false'); + } + chrome.storage.sync.set({ 'siteSettings': response.siteSettings }); + }); }); - }); - // Output disable radio button: - let inputDisabledText = document.createElement('span'); - inputDisabledText.classList.add('visuallyHidden'); - inputDisabledText.textContent = 'Disable action for ' + sites[i].origins_label; - labelDisabled.appendChild(inputDisabled); - labelDisabled.appendChild(inputDisabledText); + // Output disable radio button: + let inputDisabledText = document.createElement('span'); + inputDisabledText.classList.add('visuallyHidden'); + inputDisabledText.textContent = 'Disable action for ' + sites[i].origins_label; + labelDisabled.appendChild(inputDisabled); + labelDisabled.appendChild(inputDisabledText); - // Output redirect radio button: - let inputRedirectText = document.createElement('span'); - inputRedirectText.classList.add('visuallyHidden'); - inputRedirectText.textContent = 'Automatically redirect ' + sites[i].origins_label; - labelRedirect.appendChild(inputRedirect); - labelRedirect.appendChild(inputRedirectText); + // Output redirect radio button: + let inputRedirectText = document.createElement('span'); + inputRedirectText.classList.add('visuallyHidden'); + inputRedirectText.textContent = 'Automatically redirect ' + sites[i].origins_label; + labelRedirect.appendChild(inputRedirect); + labelRedirect.appendChild(inputRedirectText); - // Output alert radio button: - let inputAlertText = document.createElement('span'); - inputAlertText.classList.add('visuallyHidden'); - inputAlertText.textContent = 'Automatically alert for' + sites[i].origins_label; - labelAlert.appendChild(inputAlert); - labelAlert.appendChild(inputAlertText); + // Output alert radio button: + let inputAlertText = document.createElement('span'); + inputAlertText.classList.add('visuallyHidden'); + inputAlertText.textContent = 'Automatically alert for' + sites[i].origins_label; + labelAlert.appendChild(inputAlert); + labelAlert.appendChild(inputAlertText); - // Output search filter checkbox: - let inputFilterText = document.createElement('span'); - inputFilterText.classList.add('visuallyHidden'); - inputFilterText.textContent = 'Filter ' + sites[i].origins_label + ' from search engine results'; - labelFilter.appendChild(inputFilter); - labelFilter.appendChild(inputFilterText); + // Output search filter checkbox: + let inputFilterText = document.createElement('span'); + inputFilterText.classList.add('visuallyHidden'); + inputFilterText.textContent = 'Filter ' + sites[i].origins_label + ' from search engine results'; + labelFilter.appendChild(inputFilter); + labelFilter.appendChild(inputFilterText); - // Output wiki info: - let wikiInfo = document.createElement('span'); - let iconLink = document.createElement("a"); - iconLink.href = 'https://' + sites[i].destination_base_url + sites[i].destination_content_path; - iconLink.title = 'Visit ' + sites[i].destination; - iconLink.target = '_blank'; - let icon = document.createElement("img"); - icon.src = 'favicons/' + lang.toLowerCase() + '/' + sites[i].destination_icon; - icon.alt = 'Visit ' + sites[i].destination; - iconLink.appendChild(icon); - wikiInfo.appendChild(iconLink); - wikiInfo.innerHTML += sites[i].origins_label + ' » ' + sites[i].destination; - let siteContainer = document.createElement("div"); + // Output wiki info: + let wikiInfo = document.createElement('span'); + let iconLink = document.createElement("a"); + iconLink.href = 'https://' + sites[i].destination_base_url + sites[i].destination_content_path; + iconLink.title = 'Visit ' + sites[i].destination; + iconLink.target = '_blank'; + let icon = document.createElement("img"); + icon.src = 'favicons/' + lang.toLowerCase() + '/' + sites[i].destination_icon; + icon.alt = 'Visit ' + sites[i].destination; + iconLink.appendChild(icon); + wikiInfo.appendChild(iconLink); + wikiInfo.innerHTML += sites[i].origins_label + ' » ' + sites[i].destination; + let siteContainer = document.createElement("div"); - // Output inputs container: - let inputsContainer = document.createElement('div'); - inputsContainer.appendChild(labelDisabled); - inputsContainer.appendChild(labelRedirect); - inputsContainer.appendChild(labelAlert); - inputsContainer.appendChild(labelFilter); - inputsContainer.classList = 'inputsContainer'; - siteContainer.appendChild(wikiInfo); - siteContainer.appendChild(inputsContainer); - toggleContainer.appendChild(siteContainer); + // Output inputs container: + let inputsContainer = document.createElement('div'); + inputsContainer.appendChild(labelDisabled); + inputsContainer.appendChild(labelRedirect); + inputsContainer.appendChild(labelAlert); + inputsContainer.appendChild(labelFilter); + inputsContainer.classList = 'inputsContainer'; + siteContainer.appendChild(wikiInfo); + siteContainer.appendChild(inputsContainer); + toggleContainer.appendChild(siteContainer); + } } - } - // Add "select all" button event listeners: - var setAllRedirect = document.getElementById('setAllRedirect'); - setAllRedirect.addEventListener('click', function () { - var toggles = document.querySelectorAll('#toggles input.toggleRedirect'); - for (var i = 0; i < toggles.length; i++) { - toggles[i].checked = true; - siteSettings.get(toggles[i].name).set('action', 'redirect'); - } - chrome.storage.sync.set({ 'siteSettings': siteSettings }); - defaultActionSettings[toggles[0].lang] = 'redirect'; - chrome.storage.sync.set({ 'defaultActionSettings': defaultActionSettings }); - }); + // Add "select all" button event listeners: + var setAllRedirect = document.getElementById('setAllRedirect'); + setAllRedirect.addEventListener('click', function () { + var toggles = document.querySelectorAll('#toggles input.toggleRedirect'); + for (var i = 0; i < toggles.length; i++) { + toggles[i].checked = true; + siteSettings.get(toggles[i].name).set('action', 'redirect'); + } + chrome.storage.sync.set({ 'siteSettings': siteSettings }); + defaultActionSettings[toggles[0].lang] = 'redirect'; + chrome.storage.sync.set({ 'defaultActionSettings': defaultActionSettings }); + }); - var setAllAlert = document.getElementById('setAllAlert'); - setAllAlert.addEventListener('click', function () { - var toggles = document.querySelectorAll('#toggles input.toggleAlert'); - for (var i = 0; i < toggles.length; i++) { - toggles[i].checked = true; - siteSettings.get(toggles[i].name).set('action', 'alert'); - } - chrome.storage.sync.set({ 'siteSettings': siteSettings }); - defaultActionSettings[toggles[0].lang] = 'alert'; - chrome.storage.sync.set({ 'defaultActionSettings': defaultActionSettings }); - }); + var setAllAlert = document.getElementById('setAllAlert'); + setAllAlert.addEventListener('click', function () { + var toggles = document.querySelectorAll('#toggles input.toggleAlert'); + for (var i = 0; i < toggles.length; i++) { + toggles[i].checked = true; + siteSettings.get(toggles[i].name).set('action', 'alert'); + } + chrome.storage.sync.set({ 'siteSettings': siteSettings }); + defaultActionSettings[toggles[0].lang] = 'alert'; + chrome.storage.sync.set({ 'defaultActionSettings': defaultActionSettings }); + }); - var setAllDisabled = document.getElementById('setAllDisabled'); - setAllDisabled.addEventListener('click', function () { - var toggles = document.querySelectorAll('#toggles input.toggleDisable'); - for (var i = 0; i < toggles.length; i++) { - toggles[i].checked = true; - siteSettings.get(toggles[i].name).set('action', 'disabled'); - } - chrome.storage.sync.set({ 'siteSettings': siteSettings }); - defaultActionSettings[toggles[0].lang] = 'disabled'; - chrome.storage.sync.set({ 'defaultActionSettings': defaultActionSettings }); - }); + var setAllDisabled = document.getElementById('setAllDisabled'); + setAllDisabled.addEventListener('click', function () { + var toggles = document.querySelectorAll('#toggles input.toggleDisable'); + for (var i = 0; i < toggles.length; i++) { + toggles[i].checked = true; + siteSettings.get(toggles[i].name).set('action', 'disabled'); + } + chrome.storage.sync.set({ 'siteSettings': siteSettings }); + defaultActionSettings[toggles[0].lang] = 'disabled'; + chrome.storage.sync.set({ 'defaultActionSettings': defaultActionSettings }); + }); - var setAllSearchFilter = document.getElementById('setAllSearchFilter'); - setAllSearchFilter.addEventListener('click', function () { - var toggles = document.querySelectorAll('#toggles input.toggleFilter'); - for (var i = 0; i < toggles.length; i++) { - toggles[i].checked = true; - siteSettings.get(toggles[i].name).set('searchFilter', 'true'); - } - chrome.storage.sync.set({ 'siteSettings': siteSettings }); - defaultSearchFilterSettings[toggles[0].lang] = 'true'; - chrome.storage.sync.set({ 'defaultSearchFilterSettings': defaultSearchFilterSettings }); - }); + var setAllSearchFilter = document.getElementById('setAllSearchFilter'); + setAllSearchFilter.addEventListener('click', function () { + var toggles = document.querySelectorAll('#toggles input.toggleFilter'); + for (var i = 0; i < toggles.length; i++) { + toggles[i].checked = true; + siteSettings.get(toggles[i].name).set('searchFilter', 'true'); + } + chrome.storage.sync.set({ 'siteSettings': siteSettings }); + defaultSearchFilterSettings[toggles[0].lang] = 'true'; + chrome.storage.sync.set({ 'defaultSearchFilterSettings': defaultSearchFilterSettings }); + }); - var setNoneSearchFilter = document.getElementById('setNoneSearchFilter'); - setNoneSearchFilter.addEventListener('click', function () { - var toggles = document.querySelectorAll('#toggles input.toggleFilter'); - for (var i = 0; i < toggles.length; i++) { - toggles[i].checked = false; - siteSettings.get(toggles[i].name).set('searchFilter', 'false'); - } - chrome.storage.sync.set({ 'siteSettings': siteSettings }); - defaultSearchFilterSettings[toggles[0].lang] = 'false'; - chrome.storage.sync.set({ 'defaultSearchFilterSettings': defaultSearchFilterSettings }); + var setNoneSearchFilter = document.getElementById('setNoneSearchFilter'); + setNoneSearchFilter.addEventListener('click', function () { + var toggles = document.querySelectorAll('#toggles input.toggleFilter'); + for (var i = 0; i < toggles.length; i++) { + toggles[i].checked = false; + siteSettings.get(toggles[i].name).set('searchFilter', 'false'); + } + chrome.storage.sync.set({ 'siteSettings': siteSettings }); + defaultSearchFilterSettings[toggles[0].lang] = 'false'; + chrome.storage.sync.set({ 'defaultSearchFilterSettings': defaultSearchFilterSettings }); + }); }); }); } @@ -360,7 +363,7 @@ async function loadOptions(lang) { // Set power setting function setPower(setting, storeSetting = true) { if (storeSetting) { - chrome.storage.sync.set({ 'power': setting }); + chrome.storage.local.set({ 'power': setting }); } var powerImage = document.getElementById('powerImage'); powerImage.src = 'images/power-' + setting + '.png'; @@ -489,7 +492,7 @@ document.addEventListener('DOMContentLoaded', function () { }); // Set setting toggle values: - chrome.storage.sync.get({ 'power': 'on' }, function (item) { + chrome.storage.local.get({ 'power': 'on' }, function (item) { setPower(item.power, false); }); chrome.storage.sync.get({ 'notifications': 'on' }, function (item) { @@ -504,7 +507,7 @@ document.addEventListener('DOMContentLoaded', function () { // Add event listeners for setting toggles document.getElementById('powerCheckbox').addEventListener('change', function () { - chrome.storage.sync.get({ 'power': 'on' }, function (item) { + chrome.storage.local.get({ 'power': 'on' }, function (item) { if (item.power === 'on') { setPower('off'); } else {