-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptions.js
41 lines (36 loc) · 1.56 KB
/
options.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// Copyright 2018 Jerzy Głowacki
(function () {
chrome.storage.sync.get(['maxStories', 'includedTopics'], function (config) {
var maxStories = config.maxStories || 5;
var includedTopics = Object.prototype.toString.call(config.includedTopics) === '[object Object]' && config.includedTopics || {};
var maxStoriesEl = document.getElementById('max-stories');
var topicsEl = document.getElementById('topics');
var savedEl = document.getElementById('saved');
var topicsHTML = '';
maxStoriesEl.value = maxStories;
Object.keys(includedTopics).forEach(function (key) {
topicsHTML += '<label><input type="checkbox" data-topic="' + key + '"' + (includedTopics[key] ? ' checked' : '') + '> ' + key + '</label><br>';
});
if (topicsHTML) {
topicsEl.innerHTML = topicsHTML;
}
maxStoriesEl.addEventListener('change', function () {
maxStories = +this.value;
save();
});
topicsEl.querySelectorAll('input[type="checkbox"]').forEach(function (el) {
el.addEventListener('change', function () {
includedTopics[el.dataset.topic] = el.checked;
save();
});
});
function save() {
chrome.storage.sync.set({maxStories: maxStories, includedTopics: includedTopics}, function () {
savedEl.hidden = false;
setTimeout(function () {
savedEl.hidden = true;
}, 1000);
});
}
});
}());