fix setupLivePrefs with multiple edit pages open
This commit is contained in:
parent
346245a847
commit
2304a8012c
43
prefs.js
43
prefs.js
|
@ -281,29 +281,38 @@ var prefs = new function Prefs() {
|
||||||
// Accepts an array of pref names (values are fetched via prefs.get)
|
// Accepts an array of pref names (values are fetched via prefs.get)
|
||||||
// and establishes a two-way connection between the document elements and the actual prefs
|
// and establishes a two-way connection between the document elements and the actual prefs
|
||||||
function setupLivePrefs(IDs) {
|
function setupLivePrefs(IDs) {
|
||||||
const localIDs = {};
|
const checkedProps = {};
|
||||||
IDs.forEach(function(id) {
|
for (const id of IDs) {
|
||||||
localIDs[id] = true;
|
const element = document.getElementById(id);
|
||||||
updateElement(id).addEventListener('change', function() {
|
checkedProps[id] = element.type == 'checkbox' ? 'checked' : 'value';
|
||||||
prefs.set(this.id, isCheckbox(this) ? this.checked : this.value);
|
updateElement({id, element, force: true});
|
||||||
});
|
element.addEventListener('change', onChange);
|
||||||
});
|
}
|
||||||
chrome.runtime.onMessage.addListener(msg => {
|
chrome.runtime.onMessage.addListener(msg => {
|
||||||
if (msg.prefs) {
|
if (msg.prefs) {
|
||||||
for (const prefName in msg.prefs) {
|
for (const id in msg.prefs) {
|
||||||
if (prefName in localIDs) {
|
if (id in checkedProps) {
|
||||||
updateElement(prefName, msg.prefs[prefName]);
|
updateElement({id, value: msg.prefs[id]});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
function updateElement(id, value) {
|
function onChange() {
|
||||||
const el = document.getElementById(id);
|
const value = this[checkedProps[this.id]];
|
||||||
el[isCheckbox(el) ? 'checked' : 'value'] = value || prefs.get(id);
|
if (prefs.get(this.id) != value) {
|
||||||
el.dispatchEvent(new Event('change', {bubbles: true, cancelable: true}));
|
prefs.set(this.id, value);
|
||||||
return el;
|
}
|
||||||
}
|
}
|
||||||
function isCheckbox(el) {
|
function updateElement({
|
||||||
return el.localName == 'input' && el.type == 'checkbox';
|
id,
|
||||||
|
value = prefs.get(id),
|
||||||
|
element = document.getElementById(id),
|
||||||
|
force,
|
||||||
|
}) {
|
||||||
|
const prop = checkedProps[id];
|
||||||
|
if (force || element[prop] != value) {
|
||||||
|
element[prop] = value;
|
||||||
|
element.dispatchEvent(new Event('change', {bubbles: true, cancelable: true}));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user