Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for multiple checkboxes #16

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "webext-options-sync",
"version": "0.15.3",
"version": "1.0.0-0",
"description": "Helps you manage and autosave your extension's options.",
"license": "MIT",
"repository": "bfred-it/webext-options-sync",
Expand Down
22 changes: 19 additions & 3 deletions webext-options-sync.js
Expand Up @@ -88,13 +88,23 @@ class OptionsSync {
static _applyToForm(options, form) {
console.group('Updating form');
for (const name of Object.keys(options)) {
const els = form.querySelectorAll(`[name="${name}"]`);
const els = form.querySelectorAll(`[name="${CSS.escape(name)}"]`);
const [field] = els;
if (field) {
console.info(name, ':', options[name]);
switch (field.type) {
case 'checkbox':
field.checked = options[name];
if (typeof options[name] === 'boolean') {
field.checked = options[name];
} else {
const values = options[name].split('\u001C');
for (const checkbox of els) {
if (values.includes(checkbox.value)) {
checkbox.checked = true;
}
}
}

break;
case 'radio': {
const [selected] = [...els].filter(el => el.value === options[name]);
Expand Down Expand Up @@ -129,7 +139,13 @@ class OptionsSync {
value = el.options[el.selectedIndex].value;
break;
case 'checkbox':
value = el.checked;
if (el.getAttribute('value')) {
const selector = `[name="${CSS.escape(name)}"]:checked`;
value = [...document.querySelectorAll(selector)].map(el => el.value).join('\u001C');
} else {
value = el.checked;
}

break;
default: break;
}
Expand Down