blob: 1a321b42c3f772388f883c4fc9f6c94be19890d0 [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,
avm999630f9503f2020-07-27 13:56:52 +020016 "increasecontrast": false,
17 "stickysidebarheaders": false
avm99963122dc9b2019-03-30 18:44:18 +010018};
19
avm99963adf90862020-04-12 13:27:45 +020020const deprecatedOptions = [
21 "list",
22 "escalatethreads",
avm999634ddca932020-08-15 18:22:34 +020023 "movethreads",
24 "batchduplicate"
avm99963adf90862020-04-12 13:27:45 +020025];
26
avm99963122dc9b2019-03-30 18:44:18 +010027function cleanUpOptions(options) {
28 var ok = true;
29 for (const [opt, value] of Object.entries(defaultOptions)) {
30 if (!opt in options) {
31 ok = false;
32 options[opt] = value;
33 }
34 }
35
36 if (!ok) {
37 chrome.storage.sync.set(options);
38 }
avm99963001c07c2019-03-30 18:47:02 +010039
40 return options;
avm99963122dc9b2019-03-30 18:44:18 +010041}
42
avm99963cbea3142019-03-28 00:48:15 +010043function save() {
avm99963122dc9b2019-03-30 18:44:18 +010044 var options = defaultOptions;
45
46 Object.keys(options).forEach(function (opt) {
avm99963adf90862020-04-12 13:27:45 +020047 if (deprecatedOptions.includes(opt)) return;
48 options[opt] = document.querySelector("#"+opt).checked || false;
avm99963122dc9b2019-03-30 18:44:18 +010049 });
50
51 chrome.storage.sync.set(options, function() {
avm99963cbea3142019-03-28 00:48:15 +010052 window.close();
53 });
54}
55
avm99963a3d1ef32019-03-30 23:33:29 +010056function i18n() {
avm99963c19497f2019-08-23 00:15:11 +020057 document.querySelectorAll("[data-i18n]").forEach(el => el.innerHTML = chrome.i18n.getMessage("options_"+el.getAttribute("data-i18n")));
avm99963a3d1ef32019-03-30 23:33:29 +010058}
59
avm999636d9c5fe2019-06-04 00:35:53 +020060function thread() {
61 if (document.querySelector("#thread").checked && document.querySelector("#threadall").checked) {
62 document.querySelector("#"+(this.id == "thread" ? "threadall" : "thread")).checked = false;
63 }
64}
65
avm99963cbea3142019-03-28 00:48:15 +010066window.addEventListener("load", function() {
avm99963a3d1ef32019-03-30 23:33:29 +010067 i18n();
68
avm99963cbea3142019-03-28 00:48:15 +010069 chrome.storage.sync.get(null, function(items) {
avm99963001c07c2019-03-30 18:47:02 +010070 items = cleanUpOptions(items);
avm99963cbea3142019-03-28 00:48:15 +010071
avm99963122dc9b2019-03-30 18:44:18 +010072 Object.keys(defaultOptions).forEach(function(opt) {
avm99963adf90862020-04-12 13:27:45 +020073 if (items[opt] === true && !deprecatedOptions.includes(opt)) {
avm99963122dc9b2019-03-30 18:44:18 +010074 document.querySelector("#"+opt).checked = true;
75 }
76 });
avm99963cbea3142019-03-28 00:48:15 +010077
avm999636d9c5fe2019-06-04 00:35:53 +020078 ["thread", "threadall"].forEach(el => document.querySelector("#"+el).addEventListener("change", thread));
avm99963cbea3142019-03-28 00:48:15 +010079 document.querySelector("#save").addEventListener("click", save);
80 });
81});