-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #40 from shgysk8zer0/feature/39
Update options on input change
- Loading branch information
Showing
8 changed files
with
61 additions
and
42 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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
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,45 +1,54 @@ | ||
window.addEventListener('DOMContentLoaded', () => { | ||
window.addEventListener('DOMContentLoaded', async () => { | ||
function $(selector, base = document) { | ||
return Array.from(base.querySelectorAll(selector)); | ||
return [...base.querySelectorAll(selector)]; | ||
} | ||
browser.storage.local.get().then(opts => { | ||
const inputs = $('[name]', document.forms.options); | ||
Object.keys(opts).forEach(key => { | ||
const input = inputs.find(el => el.name === key); | ||
if (input instanceof HTMLInputElement) { | ||
switch(input.type) { | ||
|
||
const storage = browser.storage.local; | ||
const opts = await storage.get(); | ||
const form = document.forms.options; | ||
const inputs = $('[name]', form); | ||
|
||
Object.keys(opts).forEach(key => { | ||
const input = inputs.find(el => el.name === key); | ||
if (input instanceof HTMLInputElement) { | ||
switch(input.type) { | ||
case 'checkbox': | ||
input.checked = true; | ||
break; | ||
default: | ||
input.value = opts[key]; | ||
} | ||
} else if (input instanceof HTMLSelectElement) { | ||
input.value = opts[key]; | ||
} else { | ||
storage.remove(key); | ||
} | ||
}); | ||
|
||
inputs.forEach(input => { | ||
input.addEventListener('change', change => { | ||
if (change.target instanceof HTMLInputElement) { | ||
switch (change.target.type) { | ||
case 'checkbox': | ||
input.checked = true; | ||
opts[change.target.name] = change.target.checked; | ||
break; | ||
default: | ||
input.value = opts[key]; | ||
opts[change.target.name] = change.target.value; | ||
} | ||
} else if (input instanceof HTMLSelectElement) { | ||
input.value = opts[key]; | ||
} else { | ||
browser.storage.local.remove(key); | ||
} else if (change.target instanceof HTMLSelectElement) { | ||
opts[change.target.name] = change.target.value; | ||
} | ||
storage.set(opts); | ||
}); | ||
}); | ||
|
||
document.forms.options.addEventListener('submit', async (submit) => { | ||
submit.preventDefault(); | ||
const form = new FormData(submit.target); | ||
const opts = await browser.storage.local.get(); | ||
|
||
[...form.keys()].forEach(key => { | ||
opts[key] = form.get(key); | ||
}); | ||
$('input[type="checkbox"]', submit.target).forEach(input => { | ||
if (! input.checked) { | ||
delete opts[input.name]; | ||
browser.storage.local.remove(input.name); | ||
} | ||
}); | ||
browser.storage.local.set(opts); | ||
}); | ||
$('[type="reset"]').forEach(btn => { | ||
btn.addEventListener('click', () => browser.storage.local.clear()); | ||
form.addEventListener('submit', submit => submit.preventDefault()); | ||
form.addEventListener('reset', reset => { | ||
if (confirm('This will clear your settings')) { | ||
storage.clear(); | ||
} else { | ||
reset.preventDefault(); | ||
} | ||
}); | ||
document.forms.options.hidden = false; | ||
form.hidden = false; | ||
}, {once: true}); |
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