Skip to content

Commit

Permalink
Add options saving/loading/modifying
Browse files Browse the repository at this point in the history
Requires storage permission. Options are stored as they are displayed in
the options page, and loaded based on the object types in the default
values.

All the main code is wrapped in the loadOptions.then() to be sure that
we load options from storage before anything else.

A message is sent to the background script to trigger a new loadOptions()
whenever new values are saved, so that it stays up to date.
  • Loading branch information
Cimbali committed Apr 29, 2018
1 parent e4a7f81 commit f8c9b0d
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 8 deletions.
12 changes: 10 additions & 2 deletions background.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ function handleMessage(message, sender)
cleanedPerTab[sender.tab.id] += message.cleaned;
browser.browserAction.setBadgeText({text: '' + cleanedPerTab[sender.tab.id], tabId: sender.tab.id});
}

if ('options' in message)
{
loadOptions();
}
}

function handleAlarm(alarm)
Expand All @@ -86,5 +91,8 @@ function handleAlarm(alarm)
browser.alarms.onAlarm.addListener(handleAlarm);
browser.runtime.onMessage.addListener(handleMessage);

/* Filtering requests approach, for links from outside */
browser.webRequest.onBeforeRequest.addListener(cleanFollowedLink, { urls: ['<all_urls>'] }, ['blocking']);
loadOptions().then(() =>
{
/* Filtering requests approach, for links from outside */
browser.webRequest.onBeforeRequest.addListener(cleanFollowedLink, { urls: ['<all_urls>'] }, ['blocking']);
});
21 changes: 21 additions & 0 deletions cleanlink.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,27 @@ var prefValues = {
cltrack : true // whether we track the link cleaning
}


function loadOptions()
{
// return the promise so it can be chained
return browser.storage.local.get('configuration').then(data =>
{
if ('configuration' in data) {
for (var param in data.configuration) {
if (typeof prefValues[param] != 'object')
prefValues[param] = data.configuration[param];
else if (prefValues[param] instanceof RegExp)
prefValues[param] = new RegExp(data.configuration[param]);
else if (Array.isArray(prefValues[param]))
prefValues[param] = data.configuration[param].split(',').map(s => s.trim()).filter(s => s.length > 0);
}
}
console.log('options loaded', prefValues);
});
}


function highlightLink(node, remove)
{
// parse and apply ;-separated list of key:val style properties
Expand Down
11 changes: 7 additions & 4 deletions inject.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,11 @@ function onClick(evt)
}
}

window.addEventListener('click', onClick);
loadOptions().then(() =>
{
window.addEventListener('click', onClick);

// NB. this script is injected in every frame, so no need for recursion
var cleaned = cleanLinksInDoc(document);
browser.runtime.sendMessage({ 'cleaned': cleaned });
// NB. this script is injected in every frame, so no need for recursion
var cleaned = cleanLinksInDoc(document);
browser.runtime.sendMessage({ 'cleaned': cleaned });
})
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
},

"permissions": [
"alarms", "webRequest", "webRequestBlocking", "<all_urls>", "activeTab", "notifications"
"alarms", "webRequest", "webRequestBlocking", "<all_urls>", "activeTab", "storage", "notifications"
],

"background":
Expand Down
28 changes: 27 additions & 1 deletion options.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,29 @@
*
* ***** END LICENSE BLOCK ***** */

function save_options()
{
var prefs = Array.from(document.querySelectorAll('input, textarea')).reduce((prefs, field) =>
{
prefs[field.name] = field.value || field.checked;
return prefs
}, {})

browser.storage.local.set({configuration: prefs})
browser.runtime.sendMessage({ 'options': Date.now() })
}

// for onKeyUp: save after 400ms of inactivity
var delayed_save = (function()
{
browser.alarms.onAlarm.addListener(save_options);
return function()
{
browser.alarms.clear('save');
browser.alarms.create('save', {when: Date.now() + 400});
}
})();

function populate_option_page()
{
var list = document.querySelectorAll('[i18n_text]');
Expand All @@ -37,7 +60,10 @@ function populate_option_page()
input.value = value.source;
else if (Array.isArray(value))
input.value = value.join(',');

input.onchange = save_options
input.onkeyup = delayed_save
}
}

populate_option_page();
loadOptions().then(() => populate_option_page());

0 comments on commit f8c9b0d

Please sign in to comment.