blob: ebbf159aa0eae6fe14eabfa8132a3cd82b35fe3e [file] [log] [blame]
avm99963cbea3142019-03-28 00:48:15 +01001function isEmpty(obj) {
2 return Object.keys(obj).length === 0;
3}
4
avm99963adf90862020-04-12 13:27:45 +02005const defaultOptions = {
avm99963122dc9b2019-03-30 18:44:18 +01006 "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,
avm99963eb2d0b82019-10-07 22:58:55 +020014 "escalatethreads": false,
avm99963ae6a26d2020-04-12 14:03:51 +020015 "movethreads": false,
16 "increasecontrast": false
avm99963122dc9b2019-03-30 18:44:18 +010017};
18
avm99963adf90862020-04-12 13:27:45 +020019const deprecatedOptions = [
20 "list",
21 "escalatethreads",
22 "movethreads"
23];
24
avm99963122dc9b2019-03-30 18:44:18 +010025function cleanUpOptions(options) {
26 var ok = true;
27 for (const [opt, value] of Object.entries(defaultOptions)) {
28 if (!opt in options) {
29 ok = false;
30 options[opt] = value;
31 }
32 }
33
34 if (!ok) {
35 chrome.storage.sync.set(options);
36 }
avm99963001c07c2019-03-30 18:47:02 +010037
38 return options;
avm99963122dc9b2019-03-30 18:44:18 +010039}
40
avm99963cbea3142019-03-28 00:48:15 +010041function save() {
avm99963122dc9b2019-03-30 18:44:18 +010042 var options = defaultOptions;
43
44 Object.keys(options).forEach(function (opt) {
avm99963adf90862020-04-12 13:27:45 +020045 if (deprecatedOptions.includes(opt)) return;
46 options[opt] = document.querySelector("#"+opt).checked || false;
avm99963122dc9b2019-03-30 18:44:18 +010047 });
48
49 chrome.storage.sync.set(options, function() {
avm99963cbea3142019-03-28 00:48:15 +010050 window.close();
51 });
52}
53
avm99963a3d1ef32019-03-30 23:33:29 +010054function i18n() {
avm99963c19497f2019-08-23 00:15:11 +020055 document.querySelectorAll("[data-i18n]").forEach(el => el.innerHTML = chrome.i18n.getMessage("options_"+el.getAttribute("data-i18n")));
avm99963a3d1ef32019-03-30 23:33:29 +010056}
57
avm999636d9c5fe2019-06-04 00:35:53 +020058function thread() {
59 if (document.querySelector("#thread").checked && document.querySelector("#threadall").checked) {
60 document.querySelector("#"+(this.id == "thread" ? "threadall" : "thread")).checked = false;
61 }
62}
63
avm99963cbea3142019-03-28 00:48:15 +010064window.addEventListener("load", function() {
avm99963a3d1ef32019-03-30 23:33:29 +010065 i18n();
66
avm99963cbea3142019-03-28 00:48:15 +010067 chrome.storage.sync.get(null, function(items) {
avm99963001c07c2019-03-30 18:47:02 +010068 items = cleanUpOptions(items);
avm99963cbea3142019-03-28 00:48:15 +010069
avm99963122dc9b2019-03-30 18:44:18 +010070 Object.keys(defaultOptions).forEach(function(opt) {
avm99963adf90862020-04-12 13:27:45 +020071 if (items[opt] === true && !deprecatedOptions.includes(opt)) {
avm99963122dc9b2019-03-30 18:44:18 +010072 document.querySelector("#"+opt).checked = true;
73 }
74 });
avm99963cbea3142019-03-28 00:48:15 +010075
avm999636d9c5fe2019-06-04 00:35:53 +020076 ["thread", "threadall"].forEach(el => document.querySelector("#"+el).addEventListener("change", thread));
avm99963cbea3142019-03-28 00:48:15 +010077 document.querySelector("#save").addEventListener("click", save);
78 });
79});