Skip to content

Commit 7f41f62

Browse files
Split event handling and chrome.storage saving in extensions options … (#2712)
2 parents bad9e0a + 1308d4d commit 7f41f62

1 file changed

Lines changed: 23 additions & 12 deletions

File tree

skeletons/web-extension/options.js

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,23 +24,34 @@
2424
}
2525

2626
/**
27-
* Save the updated options to storage.
27+
* Accepts a new set of options, merges them with existing options already
28+
* stored in `chrome.storage` and saves the union of both to storage.
2829
*/
29-
function storeOptions() {
30-
var showTomster = this.checked;
30+
function storeOptions(newOptions) {
31+
chrome.storage.sync.get('options', function (data) {
32+
var options = data.options || {};
33+
Object.assign(options, newOptions);
34+
35+
chrome.storage.sync.set(
36+
{
37+
options: options,
38+
},
39+
function optionsSaved() {
40+
console.log('saved!', newOptions);
41+
},
42+
);
43+
});
44+
}
3145

32-
chrome.storage.sync.set(
33-
{
34-
options: { showTomster: showTomster },
35-
},
36-
function optionsSaved() {
37-
console.log('saved!');
38-
},
39-
);
46+
/**
47+
* Save the updated Tomster setting to storage.
48+
*/
49+
function saveTomsterSetting() {
50+
storeOptions({ showTomster: this.checked });
4051
}
4152

4253
document.addEventListener('DOMContentLoaded', loadOptions);
4354
document
4455
.querySelector('[data-settings=tomster]')
45-
.addEventListener('click', storeOptions);
56+
.addEventListener('click', saveTomsterSetting);
4657
})();

0 commit comments

Comments
 (0)