Setting default settings per language / Bug fixes

pull/91/head v1.1.1
Kevin Payravi 2023-02-07 17:54:23 -06:00
parent 01f4a66ab7
commit f8e482001a
5 changed files with 63 additions and 42 deletions

View File

@ -81,6 +81,7 @@ async function getData() {
promises.push(fetch(chrome.runtime.getURL('data/sites' + LANGS[i] + '.json')) promises.push(fetch(chrome.runtime.getURL('data/sites' + LANGS[i] + '.json'))
.then((resp) => resp.json()) .then((resp) => resp.json())
.then(function (jsonData) { .then(function (jsonData) {
jsonData.forEach((site) => site.language = LANGS[i]);
sites = sites.concat(jsonData); sites = sites.concat(jsonData);
})); }));
} }
@ -132,8 +133,8 @@ async function main(eventInfo, eventName) {
let siteSetting = ''; let siteSetting = '';
if (settings.hasOwnProperty(id) && settings[id].hasOwnProperty('action')) { if (settings.hasOwnProperty(id) && settings[id].hasOwnProperty('action')) {
siteSetting = settings[id].action; siteSetting = settings[id].action;
} else if (storage.defaultActionSetting) { } else if (storage.defaultActionSettings && storage.defaultActionSettings[site.language]) {
siteSetting = storage.defaultActionSetting; siteSetting = storage.defaultActionSettings[site.language];
} else { } else {
siteSetting = 'alert'; siteSetting = 'alert';
} }

View File

@ -32,6 +32,7 @@ async function getData() {
promises.push(fetch(chrome.runtime.getURL('data/sites' + LANGS[i] + '.json')) promises.push(fetch(chrome.runtime.getURL('data/sites' + LANGS[i] + '.json'))
.then((resp) => resp.json()) .then((resp) => resp.json())
.then(function (jsonData) { .then(function (jsonData) {
jsonData.forEach((site) => site.language = LANGS[i]);
sites = sites.concat(jsonData); sites = sites.concat(jsonData);
})); }));
} }
@ -128,8 +129,8 @@ function filterSearchResults(fandomSearchResults, searchEngine, storage) {
let searchFilterSetting = ''; let searchFilterSetting = '';
if (settings.hasOwnProperty(id) && settings[id].searchFilter) { if (settings.hasOwnProperty(id) && settings[id].searchFilter) {
searchFilterSetting = settings[id].searchFilter; searchFilterSetting = settings[id].searchFilter;
} else if (storage.defaultSearchFilterSetting) { } else if (storage.defaultSearchFilterSettings && storage.defaultSearchFilterSettings[site.language]) {
searchFilterSetting = storage.defaultSearchFilterSetting; searchFilterSetting = storage.defaultSearchFilterSettings[site.language];
} else { } else {
searchFilterSetting = 'true'; searchFilterSetting = 'true';
} }
@ -171,7 +172,9 @@ function filterSearchResults(fandomSearchResults, searchEngine, storage) {
} }
} }
}); });
chrome.storage.sync.set({ 'countSearchFilters': (storage.countSearchFilters ?? 0) + countFiltered }); if (countFiltered > 0) {
chrome.storage.sync.set({ 'countSearchFilters': (storage.countSearchFilters ?? 0) + countFiltered });
}
}); });
} }
@ -221,8 +224,8 @@ function main(mutations = null, observer = null) {
let siteSetting = ''; let siteSetting = '';
if (settings.hasOwnProperty(id) && settings[id].hasOwnProperty('action')) { if (settings.hasOwnProperty(id) && settings[id].hasOwnProperty('action')) {
siteSetting = settings[id].action; siteSetting = settings[id].action;
} else if (storage.defaultActionSetting) { } else if (storage.defaultActionSettings && storage.defaultActionSettings[site.language]) {
siteSetting = storage.defaultActionSetting; siteSetting = storage.defaultActionSettings[site.language];
} else { } else {
siteSetting = 'alert'; siteSetting = 'alert';
} }

View File

@ -1,6 +1,6 @@
{ {
"name": "Indie Wiki Buddy", "name": "Indie Wiki Buddy",
"version": "1.1.0", "version": "1.1.1",
"description": "Helping you discover quality independent wikis through alerts, redirects, and search filtering", "description": "Helping you discover quality independent wikis through alerts, redirects, and search filtering",
"permissions": [ "permissions": [
"storage", "storage",

View File

@ -1,6 +1,6 @@
{ {
"name": "Indie Wiki Buddy", "name": "Indie Wiki Buddy",
"version": "1.1.0", "version": "1.1.1",
"description": "Helping you discover quality independent wikis through alerts, redirects, and search filtering", "description": "Helping you discover quality independent wikis through alerts, redirects, and search filtering",
"permissions": [ "permissions": [
"storage", "storage",

View File

@ -44,8 +44,10 @@ async function getData() {
async function loadOptions(lang) { async function loadOptions(lang) {
sites = await getData(); sites = await getData();
chrome.storage.sync.get({ 'siteSettings': {} }, function (response) { chrome.storage.sync.get(function (storage) {
let siteSettings = response.siteSettings; let siteSettings = storage.siteSettings || {};
let defaultActionSettings = storage.defaultActionSettings || {};
let defaultSearchFilterSettings = storage.defaultSearchFilterSettings || {};
// Load BreezeWiki options: // Load BreezeWiki options:
chrome.storage.sync.get(['breezewikiHostOptions', 'breezewikiHostFetchTimestamp', 'breezewikiHost'], function (item) { chrome.storage.sync.get(['breezewikiHostOptions', 'breezewikiHostFetchTimestamp', 'breezewikiHost'], function (item) {
@ -120,6 +122,7 @@ async function loadOptions(lang) {
inputDisabled.name = key; inputDisabled.name = key;
inputDisabled.title = 'Disable actions for ' + sites[i].origin; inputDisabled.title = 'Disable actions for ' + sites[i].origin;
inputDisabled.id = key + '-redirect'; inputDisabled.id = key + '-redirect';
inputDisabled.lang = lang;
// Create radio for redirect: // Create radio for redirect:
let labelRedirect = document.createElement("label"); let labelRedirect = document.createElement("label");
@ -129,6 +132,7 @@ async function loadOptions(lang) {
inputRedirect.name = key; inputRedirect.name = key;
inputRedirect.title = 'Automatically redirect from ' + sites[i].origin + ' to ' + sites[i].destination; inputRedirect.title = 'Automatically redirect from ' + sites[i].origin + ' to ' + sites[i].destination;
inputRedirect.id = key + '-redirect'; inputRedirect.id = key + '-redirect';
inputRedirect.lang = lang;
// Create radio for alert: // Create radio for alert:
let labelAlert = document.createElement("label"); let labelAlert = document.createElement("label");
@ -138,6 +142,7 @@ async function loadOptions(lang) {
inputAlert.name = key; inputAlert.name = key;
inputAlert.title = 'Notify with banner when visiting ' + sites[i].origin; inputAlert.title = 'Notify with banner when visiting ' + sites[i].origin;
inputAlert.id = key + '-alert'; inputAlert.id = key + '-alert';
inputAlert.lang = lang;
// Create checkbox for search filtering: // Create checkbox for search filtering:
let labelFilter = document.createElement("label"); let labelFilter = document.createElement("label");
@ -147,40 +152,47 @@ async function loadOptions(lang) {
inputFilter.name = key; inputFilter.name = key;
inputFilter.title = 'Filter from search results on Google, Bing, and DuckDuckGo'; inputFilter.title = 'Filter from search results on Google, Bing, and DuckDuckGo';
inputFilter.id = key + '-filter'; inputFilter.id = key + '-filter';
inputFilter.lang = lang;
// Check radio button based on user's settings // Check radio button based on user's settings
// Will default to alert or the last "select all" setting the user chose // Will default to alert or the last "select all" setting the user chose
if (siteSettings[key] && siteSettings[key].action === 'disabled') { if (siteSettings[key] && siteSettings[key].action) {
inputDisabled.checked = true; if (siteSettings[key].action === 'disabled') {
} else if (siteSettings[key] && siteSettings[key].action === 'redirect') { inputDisabled.checked = true;
inputRedirect.checked = true; } else if (siteSettings[key].action === 'redirect') {
} else if (siteSettings[key] && siteSettings[key].action === 'alert') { inputRedirect.checked = true;
inputAlert.checked = true; } else {
inputAlert.checked = true;
}
} else { } else {
chrome.storage.sync.get({ 'defaultActionSetting': 'alert' }, function (response) { let actionSetting = defaultActionSettings[lang];
if (response.defaultActionSetting === 'disabled') { if (actionSetting) {
if (actionSetting === 'disabled') {
inputDisabled.checked = true; inputDisabled.checked = true;
} else if (response.defaultActionSetting === 'redirect') { } else if (actionSetting === 'redirect') {
inputRedirect.checked = true; inputRedirect.checked = true;
} else { } else {
inputAlert.checked = true; inputAlert.checked = true;
} }
}); } else {
inputAlert.checked = true;
}
} }
// Check search filter checkbox based on user's settings (default filter): // Check search filter checkbox based on user's settings (default filter):
if (siteSettings[key] && siteSettings[key].searchFilter === 'false') { if (siteSettings[key] && siteSettings[key].searchFilter) {
inputFilter.checked = false; if (siteSettings[key].searchFilter === 'false') {
} else if (siteSettings[key] && siteSettings[key].searchFilter === 'true') { inputFilter.checked = false;
inputFilter.checked = true; } else {
inputFilter.checked = true;
}
} else { } else {
chrome.storage.sync.get({ 'defaultSearchFilterSetting': 'true' }, function (response) { let searchFilterSetting = defaultSearchFilterSettings[lang];
if (response.defaultSearchFilterSetting === 'true') { if (searchFilterSetting && searchFilterSetting === 'false') {
inputFilter.checked = true; inputFilter.checked = false;
} else { } else {
inputFilter.checked = false; inputFilter.checked = true;
} }
});
} }
// Add listeners for when user clicks control: // Add listeners for when user clicks control:
@ -199,19 +211,19 @@ async function loadOptions(lang) {
}); });
}); });
inputDisabled.addEventListener('click', function (input) { inputDisabled.addEventListener('click', function (input) {
chrome.storage.sync.get({ 'siteSettings': {} }[key], function (response) { chrome.storage.sync.get({ 'siteSettings': {} }, function (response) {
var key = input.target.name; var key = input.target.name;
response.siteSettings.get(key).set('action', 'disabled'); response.siteSettings.get(key).set('action', 'disabled');
chrome.storage.sync.set({ 'siteSettings': response.siteSettings }); chrome.storage.sync.set({ 'siteSettings': response.siteSettings });
}); });
}); });
inputFilter.addEventListener('click', function (input) { inputFilter.addEventListener('click', function (input) {
chrome.storage.sync.get({ 'siteSettings': {} }[key], function (response) { chrome.storage.sync.get({ 'siteSettings': {} }, function (response) {
var key = input.target.name; var key = input.target.name;
if (response.siteSettings[key].searchFilter === 'true') { if (input.target.checked === true) {
response.siteSettings.get(key).set('searchFilter', 'false');
} else {
response.siteSettings.get(key).set('searchFilter', 'true'); response.siteSettings.get(key).set('searchFilter', 'true');
} else {
response.siteSettings.get(key).set('searchFilter', 'false');
} }
chrome.storage.sync.set({ 'siteSettings': response.siteSettings }); chrome.storage.sync.set({ 'siteSettings': response.siteSettings });
}); });
@ -280,7 +292,8 @@ async function loadOptions(lang) {
siteSettings.get(toggles[i].name).set('action', 'redirect'); siteSettings.get(toggles[i].name).set('action', 'redirect');
} }
chrome.storage.sync.set({ 'siteSettings': siteSettings }); chrome.storage.sync.set({ 'siteSettings': siteSettings });
chrome.storage.sync.set({ 'defaultActionSetting': 'redirect' }); defaultActionSettings[toggles[0].lang] = 'redirect';
chrome.storage.sync.set({ 'defaultActionSettings': defaultActionSettings });
}); });
var setAllAlert = document.getElementById('setAllAlert'); var setAllAlert = document.getElementById('setAllAlert');
@ -291,7 +304,8 @@ async function loadOptions(lang) {
siteSettings.get(toggles[i].name).set('action', 'alert'); siteSettings.get(toggles[i].name).set('action', 'alert');
} }
chrome.storage.sync.set({ 'siteSettings': siteSettings }); chrome.storage.sync.set({ 'siteSettings': siteSettings });
chrome.storage.sync.set({ 'defaultActionSetting': 'alert' }); defaultActionSettings[toggles[0].lang] = 'alert';
chrome.storage.sync.set({ 'defaultActionSettings': defaultActionSettings });
}); });
var setAllDisabled = document.getElementById('setAllDisabled'); var setAllDisabled = document.getElementById('setAllDisabled');
@ -302,7 +316,8 @@ async function loadOptions(lang) {
siteSettings.get(toggles[i].name).set('action', 'disabled'); siteSettings.get(toggles[i].name).set('action', 'disabled');
} }
chrome.storage.sync.set({ 'siteSettings': siteSettings }); chrome.storage.sync.set({ 'siteSettings': siteSettings });
chrome.storage.sync.set({ 'defaultActionSetting': 'disabled' }); defaultActionSettings[toggles[0].lang] = 'disabled';
chrome.storage.sync.set({ 'defaultActionSettings': defaultActionSettings });
}); });
var setAllSearchFilter = document.getElementById('setAllSearchFilter'); var setAllSearchFilter = document.getElementById('setAllSearchFilter');
@ -313,7 +328,8 @@ async function loadOptions(lang) {
siteSettings.get(toggles[i].name).set('searchFilter', 'true'); siteSettings.get(toggles[i].name).set('searchFilter', 'true');
} }
chrome.storage.sync.set({ 'siteSettings': siteSettings }); chrome.storage.sync.set({ 'siteSettings': siteSettings });
chrome.storage.sync.set({ 'defaultSearchFilterSetting': 'true' }); defaultSearchFilterSettings[toggles[0].lang] = 'true';
chrome.storage.sync.set({ 'defaultSearchFilterSettings': defaultSearchFilterSettings });
}); });
var setNoneSearchFilter = document.getElementById('setNoneSearchFilter'); var setNoneSearchFilter = document.getElementById('setNoneSearchFilter');
@ -324,7 +340,8 @@ async function loadOptions(lang) {
siteSettings.get(toggles[i].name).set('searchFilter', 'false'); siteSettings.get(toggles[i].name).set('searchFilter', 'false');
} }
chrome.storage.sync.set({ 'siteSettings': siteSettings }); chrome.storage.sync.set({ 'siteSettings': siteSettings });
chrome.storage.sync.set({ 'defaultSearchFilterSetting': 'false' }); defaultSearchFilterSettings[toggles[0].lang] = 'false';
chrome.storage.sync.set({ 'defaultSearchFilterSettings': defaultSearchFilterSettings });
}); });
}); });
} }