blob: 2bcd96f5dd7244b0b9eb53717365c3975fb7ca38 [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",
23 "movethreads"
24];
25
avm99963122dc9b2019-03-30 18:44:18 +010026function cleanUpOptions(options) {
27 var ok = true;
28 for (const [opt, value] of Object.entries(defaultOptions)) {
29 if (!opt in options) {
30 ok = false;
31 options[opt] = value;
32 }
33 }
34
35 if (!ok) {
36 chrome.storage.sync.set(options);
37 }
avm99963001c07c2019-03-30 18:47:02 +010038
39 return options;
avm99963122dc9b2019-03-30 18:44:18 +010040}
41
avm99963cbea3142019-03-28 00:48:15 +010042function save() {
avm99963122dc9b2019-03-30 18:44:18 +010043 var options = defaultOptions;
44
45 Object.keys(options).forEach(function (opt) {
avm99963adf90862020-04-12 13:27:45 +020046 if (deprecatedOptions.includes(opt)) return;
47 options[opt] = document.querySelector("#"+opt).checked || false;
avm99963122dc9b2019-03-30 18:44:18 +010048 });
49
50 chrome.storage.sync.set(options, function() {
avm99963cbea3142019-03-28 00:48:15 +010051 window.close();
52 });
53}
54
avm99963a3d1ef32019-03-30 23:33:29 +010055function i18n() {
avm99963c19497f2019-08-23 00:15:11 +020056 document.querySelectorAll("[data-i18n]").forEach(el => el.innerHTML = chrome.i18n.getMessage("options_"+el.getAttribute("data-i18n")));
avm99963a3d1ef32019-03-30 23:33:29 +010057}
58
avm999636d9c5fe2019-06-04 00:35:53 +020059function thread() {
60 if (document.querySelector("#thread").checked && document.querySelector("#threadall").checked) {
61 document.querySelector("#"+(this.id == "thread" ? "threadall" : "thread")).checked = false;
62 }
63}
64
avm99963cbea3142019-03-28 00:48:15 +010065window.addEventListener("load", function() {
avm99963a3d1ef32019-03-30 23:33:29 +010066 i18n();
67
avm99963cbea3142019-03-28 00:48:15 +010068 chrome.storage.sync.get(null, function(items) {
avm99963001c07c2019-03-30 18:47:02 +010069 items = cleanUpOptions(items);
avm99963cbea3142019-03-28 00:48:15 +010070
avm99963122dc9b2019-03-30 18:44:18 +010071 Object.keys(defaultOptions).forEach(function(opt) {
avm99963adf90862020-04-12 13:27:45 +020072 if (items[opt] === true && !deprecatedOptions.includes(opt)) {
avm99963122dc9b2019-03-30 18:44:18 +010073 document.querySelector("#"+opt).checked = true;
74 }
75 });
avm99963cbea3142019-03-28 00:48:15 +010076
avm999636d9c5fe2019-06-04 00:35:53 +020077 ["thread", "threadall"].forEach(el => document.querySelector("#"+el).addEventListener("change", thread));
avm99963cbea3142019-03-28 00:48:15 +010078 document.querySelector("#save").addEventListener("click", save);
79 });
80});