blob: 648faa283bdb9629d4c0b316238b4b93354ca2eb [file] [log] [blame]
avm99963cbea3142019-03-28 00:48:15 +01001function isEmpty(obj) {
2 return Object.keys(obj).length === 0;
3}
4
avm99963732a0642020-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,
15 "movethreads": false
avm99963122dc9b2019-03-30 18:44:18 +010016};
17
avm99963732a0642020-04-12 13:27:45 +020018const deprecatedOptions = [
19 "list",
20 "escalatethreads",
21 "movethreads"
22];
23
avm99963122dc9b2019-03-30 18:44:18 +010024function cleanUpOptions(options) {
25 var ok = true;
26 for (const [opt, value] of Object.entries(defaultOptions)) {
27 if (!opt in options) {
28 ok = false;
29 options[opt] = value;
30 }
31 }
32
33 if (!ok) {
34 chrome.storage.sync.set(options);
35 }
avm99963001c07c2019-03-30 18:47:02 +010036
37 return options;
avm99963122dc9b2019-03-30 18:44:18 +010038}
39
avm99963cbea3142019-03-28 00:48:15 +010040function save() {
avm99963122dc9b2019-03-30 18:44:18 +010041 var options = defaultOptions;
42
43 Object.keys(options).forEach(function (opt) {
avm99963732a0642020-04-12 13:27:45 +020044 if (deprecatedOptions.includes(opt)) return;
45 options[opt] = document.querySelector("#"+opt).checked || false;
avm99963122dc9b2019-03-30 18:44:18 +010046 });
47
48 chrome.storage.sync.set(options, function() {
avm99963cbea3142019-03-28 00:48:15 +010049 window.close();
50 });
51}
52
avm99963a3d1ef32019-03-30 23:33:29 +010053function i18n() {
avm99963c19497f2019-08-23 00:15:11 +020054 document.querySelectorAll("[data-i18n]").forEach(el => el.innerHTML = chrome.i18n.getMessage("options_"+el.getAttribute("data-i18n")));
avm99963a3d1ef32019-03-30 23:33:29 +010055}
56
avm999636d9c5fe2019-06-04 00:35:53 +020057function thread() {
58 if (document.querySelector("#thread").checked && document.querySelector("#threadall").checked) {
59 document.querySelector("#"+(this.id == "thread" ? "threadall" : "thread")).checked = false;
60 }
61}
62
avm99963cbea3142019-03-28 00:48:15 +010063window.addEventListener("load", function() {
avm99963a3d1ef32019-03-30 23:33:29 +010064 i18n();
65
avm99963cbea3142019-03-28 00:48:15 +010066 chrome.storage.sync.get(null, function(items) {
avm99963001c07c2019-03-30 18:47:02 +010067 items = cleanUpOptions(items);
avm99963cbea3142019-03-28 00:48:15 +010068
avm99963122dc9b2019-03-30 18:44:18 +010069 Object.keys(defaultOptions).forEach(function(opt) {
avm99963732a0642020-04-12 13:27:45 +020070 if (items[opt] === true && !deprecatedOptions.includes(opt)) {
avm99963122dc9b2019-03-30 18:44:18 +010071 document.querySelector("#"+opt).checked = true;
72 }
73 });
avm99963cbea3142019-03-28 00:48:15 +010074
avm999636d9c5fe2019-06-04 00:35:53 +020075 ["thread", "threadall"].forEach(el => document.querySelector("#"+el).addEventListener("change", thread));
avm99963cbea3142019-03-28 00:48:15 +010076 document.querySelector("#save").addEventListener("click", save);
77 });
78});