blob: 7c30cf97ada24e043169746aa2ccf250a3d8c468 [file] [log] [blame]
avm99963cbea3142019-03-28 00:48:15 +01001function isEmpty(obj) {
2 return Object.keys(obj).length === 0;
3}
4
avm99963122dc9b2019-03-30 18:44:18 +01005var defaultOptions = {
6 "list": true,
7 "thread": true,
avm999636d9c5fe2019-06-04 00:35:53 +02008 "threadall": false,
avm99963122dc9b2019-03-30 18:44:18 +01009 "fixedtoolbar": false,
10 "redirect": false,
avm999636d9c5fe2019-06-04 00:35:53 +020011 "history": false,
avm99963af7860e2019-06-04 03:33:26 +020012 "loaddrafts": false,
avm99963c19497f2019-08-23 00:15:11 +020013 "batchduplicate": false,
14 "escalatethreads": false
avm99963122dc9b2019-03-30 18:44:18 +010015};
16
17function cleanUpOptions(options) {
18 var ok = true;
19 for (const [opt, value] of Object.entries(defaultOptions)) {
20 if (!opt in options) {
21 ok = false;
22 options[opt] = value;
23 }
24 }
25
26 if (!ok) {
27 chrome.storage.sync.set(options);
28 }
avm99963001c07c2019-03-30 18:47:02 +010029
30 return options;
avm99963122dc9b2019-03-30 18:44:18 +010031}
32
avm99963cbea3142019-03-28 00:48:15 +010033function save() {
avm99963122dc9b2019-03-30 18:44:18 +010034 var options = defaultOptions;
35
36 Object.keys(options).forEach(function (opt) {
37 options[opt] = document.querySelector("#"+opt).checked;
38 });
39
40 chrome.storage.sync.set(options, function() {
avm99963cbea3142019-03-28 00:48:15 +010041 window.close();
42 });
43}
44
avm99963a3d1ef32019-03-30 23:33:29 +010045function i18n() {
avm99963c19497f2019-08-23 00:15:11 +020046 document.querySelectorAll("[data-i18n]").forEach(el => el.innerHTML = chrome.i18n.getMessage("options_"+el.getAttribute("data-i18n")));
avm99963a3d1ef32019-03-30 23:33:29 +010047}
48
avm999636d9c5fe2019-06-04 00:35:53 +020049function thread() {
50 if (document.querySelector("#thread").checked && document.querySelector("#threadall").checked) {
51 document.querySelector("#"+(this.id == "thread" ? "threadall" : "thread")).checked = false;
52 }
53}
54
avm99963cbea3142019-03-28 00:48:15 +010055window.addEventListener("load", function() {
avm99963a3d1ef32019-03-30 23:33:29 +010056 i18n();
57
avm99963cbea3142019-03-28 00:48:15 +010058 chrome.storage.sync.get(null, function(items) {
avm99963001c07c2019-03-30 18:47:02 +010059 items = cleanUpOptions(items);
avm99963cbea3142019-03-28 00:48:15 +010060
avm99963122dc9b2019-03-30 18:44:18 +010061 Object.keys(defaultOptions).forEach(function(opt) {
62 if (items[opt] === true) {
63 document.querySelector("#"+opt).checked = true;
64 }
65 });
avm99963cbea3142019-03-28 00:48:15 +010066
avm999636d9c5fe2019-06-04 00:35:53 +020067 ["thread", "threadall"].forEach(el => document.querySelector("#"+el).addEventListener("change", thread));
avm99963cbea3142019-03-28 00:48:15 +010068 document.querySelector("#save").addEventListener("click", save);
69 });
70});