Files
webextensions-examples/forget-it/options/options.js
wbamberg 9c3f8e100e Forget it (#183)
* started browsingData example

* Added browsingData example

* Remove unwanted files

* Remove logging statements

* Added comments

* Simplify storage inititialization

* Fix review comments
2017-02-21 16:08:12 -08:00

63 lines
1.6 KiB
JavaScript

/*
Store the currently selected settings using browser.storage.local.
*/
function storeSettings() {
function getSince() {
const since = document.querySelector("#since");
return since.value;
}
function getTypes() {
let dataTypes = [];
const checkboxes = document.querySelectorAll(".data-types [type=checkbox]");
for (let item of checkboxes) {
if (item.checked) {
dataTypes.push(item.getAttribute("data-type"));
}
}
return dataTypes;
}
const since = getSince();
const dataTypes = getTypes();
browser.storage.local.set({
since,
dataTypes
});
}
/*
Update the options UI with the settings values retrieved from storage,
or the default settings if the stored settings are empty.
*/
function updateUI(restoredSettings) {
const selectList = document.querySelector("#since");
selectList.value = restoredSettings.since;
const checkboxes = document.querySelectorAll(".data-types [type=checkbox]");
for (let item of checkboxes) {
if (restoredSettings.dataTypes.indexOf(item.getAttribute("data-type")) != -1) {
item.checked = true;
} else {
item.checked = false;
}
}
}
function onError(e) {
console.error(e);
}
/*
On opening the options page, fetch stored settings and update the UI with them.
*/
const gettingStoredSettings = browser.storage.local.get();
gettingStoredSettings.then(updateUI, onError);
/*
On clicking the save button, save the currently selected settings.
*/
const saveButton = document.querySelector("#save-button");
saveButton.addEventListener("click", storeSettings);