mirror of
https://github.com/mdn/webextensions-examples.git
synced 2026-04-16 06:18:35 +02:00
* started browsingData example * Added browsingData example * Remove unwanted files * Remove logging statements * Added comments * Simplify storage inititialization * Fix review comments
63 lines
1.6 KiB
JavaScript
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);
|