blob: b93ea862e7a2759432d65f5f5018a047423b8acb [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,
13 "batchduplicate": false
avm99963122dc9b2019-03-30 18:44:18 +010014};
15
16function cleanUpOptions(options) {
17 var ok = true;
18 for (const [opt, value] of Object.entries(defaultOptions)) {
19 if (!opt in options) {
20 ok = false;
21 options[opt] = value;
22 }
23 }
24
25 if (!ok) {
26 chrome.storage.sync.set(options);
27 }
avm99963001c07c2019-03-30 18:47:02 +010028
29 return options;
avm99963122dc9b2019-03-30 18:44:18 +010030}
31
avm99963cbea3142019-03-28 00:48:15 +010032function save() {
avm99963122dc9b2019-03-30 18:44:18 +010033 var options = defaultOptions;
34
35 Object.keys(options).forEach(function (opt) {
36 options[opt] = document.querySelector("#"+opt).checked;
37 });
38
39 chrome.storage.sync.set(options, function() {
avm99963cbea3142019-03-28 00:48:15 +010040 window.close();
41 });
42}
43
avm99963a3d1ef32019-03-30 23:33:29 +010044function i18n() {
avm99963af7860e2019-06-04 03:33:26 +020045 var messages = ["list", "thread", "threadall", "enhancements", "fixedtoolbar", "redirect", "loaddrafts", "experimental_label", "history", "batchduplicate", "save"];
avm99963a3d1ef32019-03-30 23:33:29 +010046
47 messages.forEach(function(msg) {
avm999636d9c5fe2019-06-04 00:35:53 +020048 document.querySelectorAll("[data-i18n=\""+msg+"\"]").forEach(el => el.innerHTML = chrome.i18n.getMessage("options_"+msg));
avm99963a3d1ef32019-03-30 23:33:29 +010049 });
50}
51
avm999636d9c5fe2019-06-04 00:35:53 +020052function thread() {
53 if (document.querySelector("#thread").checked && document.querySelector("#threadall").checked) {
54 document.querySelector("#"+(this.id == "thread" ? "threadall" : "thread")).checked = false;
55 }
56}
57
avm99963cbea3142019-03-28 00:48:15 +010058window.addEventListener("load", function() {
avm99963a3d1ef32019-03-30 23:33:29 +010059 i18n();
60
avm99963cbea3142019-03-28 00:48:15 +010061 chrome.storage.sync.get(null, function(items) {
avm99963001c07c2019-03-30 18:47:02 +010062 items = cleanUpOptions(items);
avm99963cbea3142019-03-28 00:48:15 +010063
avm99963122dc9b2019-03-30 18:44:18 +010064 Object.keys(defaultOptions).forEach(function(opt) {
65 if (items[opt] === true) {
66 document.querySelector("#"+opt).checked = true;
67 }
68 });
avm99963cbea3142019-03-28 00:48:15 +010069
avm999636d9c5fe2019-06-04 00:35:53 +020070 ["thread", "threadall"].forEach(el => document.querySelector("#"+el).addEventListener("change", thread));
avm99963cbea3142019-03-28 00:48:15 +010071 document.querySelector("#save").addEventListener("click", save);
72 });
73});