Skip to content

Commit

Permalink
Improvements in saving/restoring settings
Browse files Browse the repository at this point in the history
- simplified initialization of default values
- validation via HTML5
- #20
  • Loading branch information
lidel committed Sep 21, 2016
1 parent 5015c9f commit 9efe842
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 14 deletions.
14 changes: 10 additions & 4 deletions settings/options.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,16 @@
<body>

<form>
<fieldset>
<legend>Options</legend>
<label>Public Gateways <input type="text" id="publicGateways" ></label><br/>
<button type="submit">Save</button>
<fieldset style="line-height: 2em; font-family: sans-serif;">
<legend>HTTP2IPFS Options</legend>

<label>Public Gateway Hosts <input type="text" id="publicGateways" size="40" require placeholder="List of hostnames separated with spaces"/></label><br/>

<label>Custom Gateway URL <input type="url" id="customGatewayUrl" size="40" required pattern="https?://.+"/></label><br/>

<label>Redirect to Custom Gateway <input type="checkbox" id="useCustomGateway" /></label><br/>

<button type="submit" style="float:right;font-size:large">Save Options</button>
</fieldset>
</form>

Expand Down
61 changes: 51 additions & 10 deletions settings/options.js
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)

0 comments on commit 9efe842

Please sign in to comment.