Splitting data into two separate items + adding migration to settings.js

The reason for splitting the data into two items is that Chrome limits each item's sync storage to just 8 kb.
pull/311/head
Kevin Payravi 2023-11-04 02:54:41 -05:00
parent 888ac97cb4
commit f3ecae7a83
4 changed files with 159 additions and 82 deletions

View File

@ -21,7 +21,7 @@ chrome.runtime.onStartup.addListener(function () {
});
// Listen for extension installed/updating
chrome.runtime.onInstalled.addListener(function (detail) {
chrome.runtime.onInstalled.addListener(async function (detail) {
// Set initial icon state
chrome.storage.local.get({ 'power': 'on' }, function (item) {
setPowerIcon(item.power);
@ -36,8 +36,8 @@ chrome.runtime.onInstalled.addListener(function (detail) {
if (detail.reason === 'update') {
chrome.tabs.create({ url: 'https://getindie.wiki/changelog/?updated=true' });
}
// Temporary functions for 3.0 migration
// Temporary functions for 3.0 migration
if (detail.reason === 'update') {
// Set new default action settings:
chrome.storage.sync.get({ 'defaultWikiAction': null }, function (item) {
@ -45,6 +45,8 @@ chrome.runtime.onInstalled.addListener(function (detail) {
chrome.storage.sync.get({ 'defaultActionSettings': {} }, function (item) {
if (item.defaultActionSettings['EN']) {
chrome.storage.sync.set({ 'defaultWikiAction': item.defaultActionSettings['EN'] });
} else {
chrome.storage.sync.set({ 'defaultWikiAction': 'alert' });
}
});
}
@ -58,35 +60,46 @@ chrome.runtime.onInstalled.addListener(function (detail) {
} else if (item.defaultSearchFilterSettings['EN'] === 'false') {
chrome.storage.sync.set({ 'defaultSearchAction': 'disabled' });
}
} else {
chrome.storage.sync.set({ 'defaultSearchAction': 'replace' });
}
});
}
});
// Create new searchEngineSettings object:
chrome.storage.sync.get({ 'siteSettings': {} }, function (item) {
let siteSettings = item.siteSettings;
let searchEngineSettings = {};
for (const obj in siteSettings) {
if (siteSettings[obj].searchFilter) {
if (siteSettings[obj].searchFilter === 'false') {
searchEngineSettings[obj] = { action: 'disabled' }
// Create new searchEngineSettings and wikiSettings objects:
sites = await getData();
chrome.storage.sync.get(function (storage) {
let siteSettings = storage.siteSettings || {};
let defaultWikiAction = storage.defaultWikiAction || 'alert';
let defaultSearchAction = storage.defaultSearchAction || 'replace';
let searchEngineSettings = storage.searchEngineSettings || {};
let wikiSettings = storage.wikiSettings || {};
sites.forEach((site) => {
if (!searchEngineSettings[site.id]) {
if (siteSettings[site.id] && siteSettings[site.id].searchFilter) {
if (siteSettings[site.id].searchFilter === 'false') {
searchEngineSettings[site.id] = 'disabled';
} else {
searchEngineSettings[site.id] = 'replace';
}
} else {
searchEngineSettings[obj] = { action: 'replace' }
searchEngineSettings[site.id] = defaultSearchAction;
}
}
}
if (!wikiSettings[site.id]) {
wikiSettings[site.id] = siteSettings[site.id]?.action || defaultWikiAction;
}
});
chrome.storage.sync.set({ 'searchEngineSettings': searchEngineSettings });
chrome.storage.sync.set({ 'wikiSettings': wikiSettings });
});
// Remove all properties that aren't "action" from site settings:
chrome.storage.sync.get({ 'siteSettings': {} }, function (item) {
let siteSettings = item.siteSettings;
for (const obj in item.siteSettings) {
Object.keys(siteSettings[obj]).forEach((key) => key === 'action' || delete siteSettings[obj][key]);
}
chrome.storage.sync.set({ 'siteSettings': siteSettings });
});
// Remove old siteSettings object:
chrome.storage.sync.remove('siteSettings');
}
});
@ -347,11 +360,11 @@ async function main(eventInfo) {
let site = matchingSites.find(site => site.origin_base_url === closestMatch);
if (site) {
// Get user's settings for the wiki
let settings = storage.siteSettings || {};
let settings = storage.wikiSettings || {};
let id = site['id'];
let siteSetting = '';
if (settings.hasOwnProperty(id) && settings[id].hasOwnProperty('action')) {
siteSetting = settings[id].action;
if (settings.hasOwnProperty(id)) {
siteSetting = settings[id];
} else if (storage.defaultWikiAction) {
siteSetting = storage.defaultWikiAction;
} else {

View File

@ -126,9 +126,9 @@ function displayRedirectBanner(url, id, destination, storage) {
bannerRestoreLink.textContent = '⎌ Restore banner';
bannerControls.appendChild(bannerRestoreLink);
bannerRestoreLink.onclick = function (e) {
chrome.storage.sync.get({ 'siteSettings': {} }, function (response) {
response.siteSettings.get(id).set('action', 'alert');
chrome.storage.sync.set({ 'siteSettings': response.siteSettings });
chrome.storage.sync.get({ 'wikiSettings': {} }, function (response) {
response.wikiSettings.set(id, 'alert');
chrome.storage.sync.set({ 'wikiSettings': response.wikiSettings });
e.target.textContent = '✓ Banner restored';
e.target.classList.add('indie-wiki-banner-disabled');
document.getElementById('indie-wiki-banner-redirect').textContent = '↪ Auto redirect this wiki';
@ -146,9 +146,9 @@ function displayRedirectBanner(url, id, destination, storage) {
bannerDisableLink.textContent = '✕ Disable banner for this wiki';
bannerControls.appendChild(bannerDisableLink);
bannerDisableLink.onclick = function (e) {
chrome.storage.sync.get({ 'siteSettings': {} }, function (response) {
response.siteSettings.get(id).set('action', 'disabled');
chrome.storage.sync.set({ 'siteSettings': response.siteSettings });
chrome.storage.sync.get({ 'wikiSettings': {} }, function (response) {
response.wikiSettings.set(id, 'disabled');
chrome.storage.sync.set({ 'wikiSettings': response.wikiSettings });
e.target.textContent = '✓ Banner disabled';
e.target.classList.add('indie-wiki-banner-disabled');
document.getElementById('indie-wiki-banner-redirect').textContent = '↪ Auto redirect this wiki';
@ -167,9 +167,9 @@ function displayRedirectBanner(url, id, destination, storage) {
bannerRedirectLink.textContent = '↪ Auto redirect this wiki';
bannerControls.appendChild(bannerRedirectLink);
bannerRedirectLink.onclick = function (e) {
chrome.storage.sync.get({ 'siteSettings': {} }, function (response) {
response.siteSettings.get(id).set('action', 'redirect');
chrome.storage.sync.set({ 'siteSettings': response.siteSettings });
chrome.storage.sync.get({ 'wikiSettings': {} }, function (response) {
response.wikiSettings.set(id, 'redirect');
chrome.storage.sync.set({ 'wikiSettings': response.wikiSettings });
e.target.textContent = '✓ Redirect enabled';
e.target.classList.add('indie-wiki-banner-disabled');
document.getElementById('indie-wiki-banner-disable').textContent = '✕ Disable banner for this wiki';
@ -250,11 +250,11 @@ function main() {
let site = matchingSites.find(site => site.origin_base_url === closestMatch);
if (site) {
// Get user's settings for the wiki
let settings = storage.siteSettings || {};
let settings = storage.wikiSettings || {};
let id = site['id'];
let siteSetting = '';
if (settings.hasOwnProperty(id) && settings[id].hasOwnProperty('action')) {
siteSetting = settings[id].action;
if (settings.hasOwnProperty(id)) {
siteSetting = settings[id];
} else if (storage.defaultActionSettings && storage.defaultActionSettings[site.language]) {
siteSetting = storage.defaultActionSettings[site.language];
} else {

View File

@ -256,9 +256,6 @@ document.addEventListener('DOMContentLoaded', function () {
chrome.storage.sync.get({ 'notifications': 'on' }, function (item) {
setNotifications(item.notifications, false);
});
chrome.storage.sync.get({ 'searchSetting': 'replace' }, function (item) {
setSearchSetting(item.searchSetting, false);
});
chrome.storage.sync.get({ 'breezewiki': 'off' }, function (item) {
setBreezeWiki(item.breezewiki, false);

View File

@ -2,11 +2,11 @@ const LANGS = ["DE", "EN", "ES", "FR", "IT", "PL", "TOK"];
var sites = [];
// Create object prototypes for getting and setting attributes:
Object.prototype.get = function(prop) {
Object.prototype.get = function (prop) {
this[prop] = this[prop] || {};
return this[prop];
};
Object.prototype.set = function(prop, value) {
Object.prototype.set = function (prop, value) {
this[prop] = value;
}
@ -60,7 +60,7 @@ async function loadBreezeWikiOptions() {
}).then((breezewikiHosts) => {
breezewikiHosts = breezewikiHosts.filter(host =>
chrome.runtime.getManifest().version.localeCompare(host.iwb_version,
undefined,
undefined,
{ numeric: true, sensitivity: 'base' }
) >= 0
);
@ -89,7 +89,7 @@ async function loadBreezeWikiOptions() {
let option = document.createElement('option');
option.value = breezewikiHosts[i].instance;
let textContent = breezewikiHosts[i].instance.replace('https://', '');
const numberOfPeriods = (textContent.match(/\./g)||[]).length;
const numberOfPeriods = (textContent.match(/\./g) || []).length;
if (numberOfPeriods > 1) {
textContent = textContent.substring(textContent.indexOf('.') + 1);
}
@ -108,7 +108,7 @@ async function loadBreezeWikiOptions() {
// If fetch fails and no host is set, default to breezewiki.com:
if (!host) {
chrome.storage.sync.set({ 'breezewikiHost': 'https://breezewiki.com'});
chrome.storage.sync.set({ 'breezewikiHost': 'https://breezewiki.com' });
}
});
} else {
@ -126,7 +126,7 @@ async function loadBreezeWikiOptions() {
let option = document.createElement('option');
option.value = hostOptions[i].instance;
let textContent = hostOptions[i].instance.replace('https://', '');
const numberOfPeriods = (textContent.match(/\./g)||[]).length;
const numberOfPeriods = (textContent.match(/\./g) || []).length;
if (numberOfPeriods > 1) {
textContent = textContent.substring(textContent.indexOf('.') + 1);
}
@ -151,8 +151,8 @@ async function loadOptions(lang) {
chrome.storage.local.get(function (localStorage) {
chrome.storage.sync.get(function (syncStorage) {
const storage = {...syncStorage, ...localStorage};
let siteSettings = storage.siteSettings || {};
const storage = { ...syncStorage, ...localStorage };
let wikiSettings = storage.wikiSettings || {};
let searchEngineSettings = storage.searchEngineSettings || {};
let defaultWikiAction = storage.defaultWikiAction || null;
let defaultSearchAction = storage.defaultSearchAction || null;
@ -196,7 +196,7 @@ async function loadOptions(lang) {
inputDisabled.classList = 'toggleDisable';
inputDisabled.type = "radio";
inputDisabled.name = key + '-wiki-action';
inputDisabled.title = 'Do nothing for ' + sites[i].origins_label + ' on search engines';
inputDisabled.title = 'Do nothing for ' + sites[i].origins_label + ' on search engines';
inputDisabled.lang = sites[i].language;
inputDisabled.setAttribute('data-wiki-key', key);
@ -219,7 +219,7 @@ async function loadOptions(lang) {
inputRedirect.title = 'Automatically redirect ' + sites[i].origins_label + ' to ' + sites[i].destination;
inputRedirect.lang = sites[i].language;
inputRedirect.setAttribute('data-wiki-key', key);
// Create radio for disabling action on search engines:
let labelSearchEngineDisabled = document.createElement("label");
let inputSearchEngineDisabled = document.createElement("input");
@ -248,13 +248,13 @@ async function loadOptions(lang) {
inputSearchEngineHide.name = key + '-search-engine-action';
inputSearchEngineHide.title = 'Hide ' + sites[i].origins_label + ' from search engine results';
inputSearchEngineHide.lang = sites[i].language;
inputSearchEngineHide.setAttribute('data-wiki-key', key);
inputSearchEngineHide.setAttribute('data-wiki-key', key);
// Check radio buttons based on user's settings
if (siteSettings[key] && siteSettings[key].action) {
if (siteSettings[key].action === 'disabled') {
if (wikiSettings[key]) {
if (wikiSettings[key] === 'disabled') {
inputDisabled.checked = true;
} else if (siteSettings[key].action === 'redirect') {
} else if (wikiSettings[key] === 'redirect') {
inputRedirect.checked = true;
} else {
inputAlert.checked = true;
@ -274,10 +274,10 @@ async function loadOptions(lang) {
}
}
if (searchEngineSettings[key] && searchEngineSettings[key].action) {
if (searchEngineSettings[key].action === 'disabled') {
if (searchEngineSettings[key]) {
if (searchEngineSettings[key] === 'disabled') {
inputSearchEngineDisabled.checked = true;
} else if (searchEngineSettings[key].action === 'replace') {
} else if (searchEngineSettings[key] === 'replace') {
inputSearchEngineReplace.checked = true;
} else {
inputSearchEngineHide.checked = true;
@ -299,44 +299,44 @@ async function loadOptions(lang) {
// Add listeners for when user clicks control:
inputDisabled.addEventListener('click', function (input) {
chrome.storage.sync.get({ 'siteSettings': {} }, function (response) {
chrome.storage.sync.get({ 'wikiSettings': {} }, function (response) {
var key = input.target.getAttribute('data-wiki-key');
response.siteSettings.get(key).set('action', 'disabled');
chrome.storage.sync.set({ 'siteSettings': response.siteSettings });
response.wikiSettings.set(key, 'disabled');
chrome.storage.sync.set({ 'wikiSettings': response.wikiSettings });
});
});
inputAlert.addEventListener('click', function (input) {
chrome.storage.sync.get({ 'siteSettings': {} }, function (response) {
chrome.storage.sync.get({ 'wikiSettings': {} }, function (response) {
var key = input.target.getAttribute('data-wiki-key');
response.siteSettings.get(key).set('action', 'alert');
chrome.storage.sync.set({ 'siteSettings': response.siteSettings });
response.wikiSettings.set(key, 'alert');
chrome.storage.sync.set({ 'wikiSettings': response.wikiSettings });
});
});
inputRedirect.addEventListener('click', function (input) {
chrome.storage.sync.get({ 'siteSettings': {} }, function (response) {
chrome.storage.sync.get({ 'wikiSettings': {} }, function (response) {
var key = input.target.getAttribute('data-wiki-key');
response.siteSettings.get(key).set('action', 'redirect');
chrome.storage.sync.set({ 'siteSettings': response.siteSettings });
response.wikiSettings.set(key, 'redirect');
chrome.storage.sync.set({ 'wikiSettings': response.wikiSettings });
});
});
inputSearchEngineDisabled.addEventListener('click', function (input) {
chrome.storage.sync.get({ 'searchEngineSettings': {} }, function (response) {
var key = input.target.getAttribute('data-wiki-key');
response.searchEngineSettings.get(key).set('action', 'disabled');
response.searchEngineSettings.set(key, 'disabled');
chrome.storage.sync.set({ 'searchEngineSettings': response.searchEngineSettings });
});
});
inputSearchEngineReplace.addEventListener('click', function (input) {
chrome.storage.sync.get({ 'searchEngineSettings': {} }, function (response) {
var key = input.target.getAttribute('data-wiki-key');
response.searchEngineSettings.get(key).set('action', 'replace');
response.searchEngineSettings.set(key, 'replace');
chrome.storage.sync.set({ 'searchEngineSettings': response.searchEngineSettings });
});
});
inputSearchEngineHide.addEventListener('click', function (input) {
chrome.storage.sync.get({ 'searchEngineSettings': {} }, function (response) {
var key = input.target.getAttribute('data-wiki-key');
response.searchEngineSettings.get(key).set('action', 'hide');
response.searchEngineSettings.set(key, 'hide');
chrome.storage.sync.set({ 'searchEngineSettings': response.searchEngineSettings });
});
});
@ -430,9 +430,9 @@ async function loadOptions(lang) {
const toggles = document.querySelectorAll('#toggles input.toggleRedirect');
for (var i = 0; i < toggles.length; i++) {
toggles[i].checked = true;
siteSettings.get(toggles[i].getAttribute('data-wiki-key')).set('action', 'redirect');
wikiSettings.set(toggles[i].getAttribute('data-wiki-key'), 'redirect');
}
chrome.storage.sync.set({ 'siteSettings': siteSettings });
chrome.storage.sync.set({ 'wikiSettings': wikiSettings });
});
const setAllAlert = document.getElementById('setAllAlert');
@ -440,9 +440,9 @@ async function loadOptions(lang) {
const toggles = document.querySelectorAll('#toggles input.toggleAlert');
for (var i = 0; i < toggles.length; i++) {
toggles[i].checked = true;
siteSettings.get(toggles[i].getAttribute('data-wiki-key')).set('action', 'alert');
wikiSettings.set(toggles[i].getAttribute('data-wiki-key'), 'alert');
}
chrome.storage.sync.set({ 'siteSettings': siteSettings });
chrome.storage.sync.set({ 'wikiSettings': wikiSettings });
});
const setAllDisabled = document.getElementById('setAllDisabled');
@ -450,9 +450,9 @@ async function loadOptions(lang) {
const toggles = document.querySelectorAll('#toggles input.toggleDisable');
for (var i = 0; i < toggles.length; i++) {
toggles[i].checked = true;
siteSettings.get(toggles[i].getAttribute('data-wiki-key')).set('action', 'disabled');
wikiSettings.set(toggles[i].getAttribute('data-wiki-key'), 'disabled');
}
chrome.storage.sync.set({ 'siteSettings': siteSettings });
chrome.storage.sync.set({ 'wikiSettings': wikiSettings });
});
const setAllSearchEngineDisabled = document.getElementById('setAllSearchEngineDisabled');
@ -460,7 +460,7 @@ async function loadOptions(lang) {
const toggles = document.querySelectorAll('#toggles input.toggleSearchEngineDisabled');
for (var i = 0; i < toggles.length; i++) {
toggles[i].checked = true;
searchEngineSettings.get(toggles[i].getAttribute('data-wiki-key')).set('action', 'disabled');
searchEngineSettings.set(toggles[i].getAttribute('data-wiki-key'), 'disabled');
}
chrome.storage.sync.set({ 'searchEngineSettings': searchEngineSettings });
});
@ -470,7 +470,7 @@ async function loadOptions(lang) {
const toggles = document.querySelectorAll('#toggles input.toggleSearchEngineHide');
for (var i = 0; i < toggles.length; i++) {
toggles[i].checked = true;
searchEngineSettings.get(toggles[i].getAttribute('data-wiki-key')).set('action', 'hide');
searchEngineSettings.set(toggles[i].getAttribute('data-wiki-key'), 'hide');
}
chrome.storage.sync.set({ 'searchEngineSettings': searchEngineSettings });
});
@ -480,7 +480,7 @@ async function loadOptions(lang) {
const toggles = document.querySelectorAll('#toggles input.toggleSearchEngineReplace');
for (var i = 0; i < toggles.length; i++) {
toggles[i].checked = true;
searchEngineSettings.get(toggles[i].getAttribute('data-wiki-key')).set('action', 'replace');
searchEngineSettings.set(toggles[i].getAttribute('data-wiki-key'), 'replace');
}
chrome.storage.sync.set({ 'searchEngineSettings': searchEngineSettings });
});
@ -549,7 +549,7 @@ function setBreezeWiki(setting, storeSetting = true) {
}).then((breezewikiHosts) => {
breezewikiHosts = breezewikiHosts.filter(host =>
chrome.runtime.getManifest().version.localeCompare(host.iwb_version,
undefined,
undefined,
{ numeric: true, sensitivity: 'base' }
) >= 0
);
@ -574,7 +574,7 @@ function setBreezeWiki(setting, storeSetting = true) {
// If fetch fails and no host is set, default to breezewiki.com:
if (!host) {
chrome.storage.sync.set({ 'breezewikiHost': 'https://breezewiki.com'});
chrome.storage.sync.set({ 'breezewikiHost': 'https://breezewiki.com' });
}
});
} else {
@ -586,8 +586,75 @@ function setBreezeWiki(setting, storeSetting = true) {
}
}
async function migrateData() {
// Set new default action settings:
chrome.storage.sync.get({ 'defaultWikiAction': null }, function (item) {
if (!item.defaultWikiAction) {
chrome.storage.sync.get({ 'defaultActionSettings': {} }, function (item) {
if (item.defaultActionSettings['EN']) {
chrome.storage.sync.set({ 'defaultWikiAction': item.defaultActionSettings['EN'] });
} else {
chrome.storage.sync.set({ 'defaultWikiAction': 'alert' });
}
});
}
});
chrome.storage.sync.get({ 'defaultSearchAction': null }, function (item) {
if (!item.defaultSearchAction) {
chrome.storage.sync.get({ 'defaultSearchFilterSettings': {} }, function (item) {
if (item.defaultSearchFilterSettings['EN']) {
if (item.defaultSearchFilterSettings['EN'] === 'true') {
chrome.storage.sync.set({ 'defaultSearchAction': 'replace' });
} else if (item.defaultSearchFilterSettings['EN'] === 'false') {
chrome.storage.sync.set({ 'defaultSearchAction': 'disabled' });
}
} else {
chrome.storage.sync.set({ 'defaultSearchAction': 'replace' });
}
});
}
});
// Create new searchEngineSettings and wikiSettings objects:
sites = await getData();
chrome.storage.sync.get(function (storage) {
let siteSettings = storage.siteSettings || {};
let defaultWikiAction = storage.defaultWikiAction || 'alert';
let defaultSearchAction = storage.defaultSearchAction || 'replace';
let searchEngineSettings = storage.searchEngineSettings || {};
let wikiSettings = storage.wikiSettings || {};
sites.forEach((site) => {
if (!searchEngineSettings[site.id]) {
if (siteSettings[site.id] && siteSettings[site.id].searchFilter) {
if (siteSettings[site.id].searchFilter === 'false') {
searchEngineSettings[site.id] = 'disabled';
} else {
searchEngineSettings[site.id] = 'replace';
}
} else {
searchEngineSettings[site.id] = defaultSearchAction;
}
}
if (!wikiSettings[site.id]) {
wikiSettings[site.id] = siteSettings[site.id]?.action || defaultWikiAction;
}
});
chrome.storage.sync.set({ 'searchEngineSettings': searchEngineSettings });
chrome.storage.sync.set({ 'wikiSettings': wikiSettings });
});
// Remove old siteSettings object:
chrome.storage.sync.remove('siteSettings');
}
// Main function that runs on-load
document.addEventListener('DOMContentLoaded', function () {
document.addEventListener('DOMContentLoaded', function () {
// Run v3 data migration:
migrateData();
// If newly installed, show initial install guide
if (new URLSearchParams(window.location.search).get('newinstall')) {
document.getElementById('firstInstallInfo').style.display = 'block';
@ -720,12 +787,12 @@ document.addEventListener('DOMContentLoaded', function () {
// Add event listeners for default action selections
document.querySelectorAll('[name="defaultWikiAction"]').forEach((el) => {
el.addEventListener('change', function () {
chrome.storage.sync.set({ 'defaultWikiAction': document.options.defaultWikiAction.value })
chrome.storage.sync.set({ 'defaultWikiAction': document.options.defaultWikiAction.value })
});
});
document.querySelectorAll('[name="defaultSearchAction"]').forEach((el) => {
el.addEventListener('change', function () {
chrome.storage.sync.set({ 'defaultSearchAction': document.options.defaultSearchAction.value })
chrome.storage.sync.set({ 'defaultSearchAction': document.options.defaultSearchAction.value })
});
});