blob: 026776c7a1a42ec53a925c7d5251fc86cc64b902 [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,
12 "loaddrafts": false
avm99963122dc9b2019-03-30 18:44:18 +010013};
14
15function cleanUpOptions(options) {
16 var ok = true;
17 for (const [opt, value] of Object.entries(defaultOptions)) {
18 if (!opt in options) {
19 ok = false;
20 options[opt] = value;
21 }
22 }
23
24 if (!ok) {
25 chrome.storage.sync.set(options);
26 }
avm99963001c07c2019-03-30 18:47:02 +010027
28 return options;
avm99963122dc9b2019-03-30 18:44:18 +010029}
30
avm99963cbea3142019-03-28 00:48:15 +010031function save() {
avm99963122dc9b2019-03-30 18:44:18 +010032 var options = defaultOptions;
33
34 Object.keys(options).forEach(function (opt) {
35 options[opt] = document.querySelector("#"+opt).checked;
36 });
37
38 chrome.storage.sync.set(options, function() {
avm99963cbea3142019-03-28 00:48:15 +010039 window.close();
40 });
41}
42
avm99963a3d1ef32019-03-30 23:33:29 +010043function i18n() {
avm999636d9c5fe2019-06-04 00:35:53 +020044 var messages = ["list", "thread", "threadall", "enhancements", "fixedtoolbar", "redirect", "loaddrafts", "experimental_label", "history", "save"];
avm99963a3d1ef32019-03-30 23:33:29 +010045
46 messages.forEach(function(msg) {
avm999636d9c5fe2019-06-04 00:35:53 +020047 document.querySelectorAll("[data-i18n=\""+msg+"\"]").forEach(el => el.innerHTML = chrome.i18n.getMessage("options_"+msg));
avm99963a3d1ef32019-03-30 23:33:29 +010048 });
49}
50
avm999636d9c5fe2019-06-04 00:35:53 +020051function thread() {
52 if (document.querySelector("#thread").checked && document.querySelector("#threadall").checked) {
53 document.querySelector("#"+(this.id == "thread" ? "threadall" : "thread")).checked = false;
54 }
55}
56
avm99963cbea3142019-03-28 00:48:15 +010057window.addEventListener("load", function() {
avm99963a3d1ef32019-03-30 23:33:29 +010058 i18n();
59
avm99963cbea3142019-03-28 00:48:15 +010060 chrome.storage.sync.get(null, function(items) {
avm99963001c07c2019-03-30 18:47:02 +010061 items = cleanUpOptions(items);
avm99963cbea3142019-03-28 00:48:15 +010062
avm99963122dc9b2019-03-30 18:44:18 +010063 Object.keys(defaultOptions).forEach(function(opt) {
64 if (items[opt] === true) {
65 document.querySelector("#"+opt).checked = true;
66 }
67 });
avm99963cbea3142019-03-28 00:48:15 +010068
avm999636d9c5fe2019-06-04 00:35:53 +020069 ["thread", "threadall"].forEach(el => document.querySelector("#"+el).addEventListener("change", thread));
avm99963cbea3142019-03-28 00:48:15 +010070 document.querySelector("#save").addEventListener("click", save);
71 });
72});