Files
webextensions-examples/stored-credentials/options/options.js
wbamberg 5dd59a4028 Example of the webRequest.onAuthRequired API (#206)
* Example of the webRequest.onAuthRequired API

* Update README, add applications.id to manifest
2017-04-14 13:34:05 -07:00

40 lines
1.1 KiB
JavaScript

const usernameInput = document.querySelector("#username");
const passwordInput = document.querySelector("#password");
/*
Store the currently selected settings using browser.storage.local.
*/
function storeSettings() {
browser.storage.local.set({
authCredentials: {
username: usernameInput.value,
password: passwordInput.value
}
});
}
/*
Update the options UI with the settings values retrieved from storage,
or the default settings if the stored settings are empty.
*/
function updateUI(restoredSettings) {
usernameInput.value = restoredSettings.authCredentials.username || "";
passwordInput.value = restoredSettings.authCredentials.password || "";
}
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 blur, save the currently selected settings.
*/
usernameInput.addEventListener("blur", storeSettings);
passwordInput.addEventListener("blur", storeSettings);