-
Notifications
You must be signed in to change notification settings - Fork 325
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improvements in saving/restoring settings
- simplified initialization of default values - validation via HTML5 - #20
- Loading branch information
Showing
2 changed files
with
61 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,55 @@ | ||
function saveOptions(e) { | ||
chrome.storage.local.set({ | ||
publicGateways: document.querySelector("#publicGateways").value | ||
}); | ||
var options = new Map() // key: optionName, value: defaultValue | ||
options.set('publicGateways', 'ipfs.io gateway.ipfs.io ipfs.pics') | ||
options.set('useCustomGateway', true) | ||
options.set('customGatewayUrl', 'http://127.0.0.1:8080') | ||
|
||
|
||
function saveOption (name) { | ||
let element = document.querySelector(`#${name}`) | ||
let change = {} | ||
switch (element.type) { | ||
case 'text': | ||
case 'url': | ||
change[name] = element.value | ||
break | ||
case 'checkbox': | ||
change[name] = element.checked | ||
break | ||
default: | ||
console.log('Unsupported option type: ' + element.type) | ||
} | ||
chrome.storage.local.set(change) | ||
} | ||
|
||
function readOption (name, defaultValue) { | ||
let element = document.querySelector(`#${name}`) | ||
chrome.storage.local.get(name, (storage) => { | ||
let oldValue = storage[name] | ||
switch (element.type) { | ||
case 'text': | ||
case 'url': | ||
element.value = oldValue || defaultValue | ||
break | ||
case 'checkbox': | ||
element.checked = typeof (oldValue) === 'boolean' ? oldValue : defaultValue | ||
break | ||
default: | ||
console.log('Unsupported option type: ' + element.type) | ||
} | ||
}) | ||
} | ||
|
||
function saveOptions (e) { | ||
for (var option of options.keys()) { | ||
saveOption(option) | ||
} | ||
} | ||
|
||
function restoreOptions() { | ||
chrome.storage.local.get("publicGateways", (res) => { | ||
document.querySelector("#publicGateways").value = res.publicGateways || "ipfs.io gateway.ipfs.io ipfs.pics"; | ||
}); | ||
function readOptions () { | ||
for (var [option, defaultValue] of options) { | ||
readOption(option, defaultValue) | ||
} | ||
} | ||
|
||
document.addEventListener("DOMContentLoaded", restoreOptions); | ||
document.querySelector("form").addEventListener("submit", saveOptions); | ||
document.addEventListener('DOMContentLoaded', readOptions) | ||
document.querySelector('form').addEventListener('submit', saveOptions) |