avm99963 | cbea314 | 2019-03-28 00:48:15 +0100 | [diff] [blame] | 1 | function isEmpty(obj) { |
| 2 | return Object.keys(obj).length === 0; |
| 3 | } |
| 4 | |
avm99963 | adf9086 | 2020-04-12 13:27:45 +0200 | [diff] [blame] | 5 | const defaultOptions = { |
avm99963 | 122dc9b | 2019-03-30 18:44:18 +0100 | [diff] [blame] | 6 | "list": true, |
| 7 | "thread": true, |
avm99963 | 6d9c5fe | 2019-06-04 00:35:53 +0200 | [diff] [blame] | 8 | "threadall": false, |
avm99963 | 122dc9b | 2019-03-30 18:44:18 +0100 | [diff] [blame] | 9 | "fixedtoolbar": false, |
| 10 | "redirect": false, |
avm99963 | 6d9c5fe | 2019-06-04 00:35:53 +0200 | [diff] [blame] | 11 | "history": false, |
avm99963 | af7860e | 2019-06-04 03:33:26 +0200 | [diff] [blame] | 12 | "loaddrafts": false, |
avm99963 | c19497f | 2019-08-23 00:15:11 +0200 | [diff] [blame] | 13 | "batchduplicate": false, |
avm99963 | eb2d0b8 | 2019-10-07 22:58:55 +0200 | [diff] [blame] | 14 | "escalatethreads": false, |
avm99963 | ae6a26d | 2020-04-12 14:03:51 +0200 | [diff] [blame^] | 15 | "movethreads": false, |
| 16 | "increasecontrast": false |
avm99963 | 122dc9b | 2019-03-30 18:44:18 +0100 | [diff] [blame] | 17 | }; |
| 18 | |
avm99963 | adf9086 | 2020-04-12 13:27:45 +0200 | [diff] [blame] | 19 | const deprecatedOptions = [ |
| 20 | "list", |
| 21 | "escalatethreads", |
| 22 | "movethreads" |
| 23 | ]; |
| 24 | |
avm99963 | 122dc9b | 2019-03-30 18:44:18 +0100 | [diff] [blame] | 25 | function 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 | } |
avm99963 | 001c07c | 2019-03-30 18:47:02 +0100 | [diff] [blame] | 37 | |
| 38 | return options; |
avm99963 | 122dc9b | 2019-03-30 18:44:18 +0100 | [diff] [blame] | 39 | } |
| 40 | |
avm99963 | cbea314 | 2019-03-28 00:48:15 +0100 | [diff] [blame] | 41 | function save() { |
avm99963 | 122dc9b | 2019-03-30 18:44:18 +0100 | [diff] [blame] | 42 | var options = defaultOptions; |
| 43 | |
| 44 | Object.keys(options).forEach(function (opt) { |
avm99963 | adf9086 | 2020-04-12 13:27:45 +0200 | [diff] [blame] | 45 | if (deprecatedOptions.includes(opt)) return; |
| 46 | options[opt] = document.querySelector("#"+opt).checked || false; |
avm99963 | 122dc9b | 2019-03-30 18:44:18 +0100 | [diff] [blame] | 47 | }); |
| 48 | |
| 49 | chrome.storage.sync.set(options, function() { |
avm99963 | cbea314 | 2019-03-28 00:48:15 +0100 | [diff] [blame] | 50 | window.close(); |
| 51 | }); |
| 52 | } |
| 53 | |
avm99963 | a3d1ef3 | 2019-03-30 23:33:29 +0100 | [diff] [blame] | 54 | function i18n() { |
avm99963 | c19497f | 2019-08-23 00:15:11 +0200 | [diff] [blame] | 55 | document.querySelectorAll("[data-i18n]").forEach(el => el.innerHTML = chrome.i18n.getMessage("options_"+el.getAttribute("data-i18n"))); |
avm99963 | a3d1ef3 | 2019-03-30 23:33:29 +0100 | [diff] [blame] | 56 | } |
| 57 | |
avm99963 | 6d9c5fe | 2019-06-04 00:35:53 +0200 | [diff] [blame] | 58 | function thread() { |
| 59 | if (document.querySelector("#thread").checked && document.querySelector("#threadall").checked) { |
| 60 | document.querySelector("#"+(this.id == "thread" ? "threadall" : "thread")).checked = false; |
| 61 | } |
| 62 | } |
| 63 | |
avm99963 | cbea314 | 2019-03-28 00:48:15 +0100 | [diff] [blame] | 64 | window.addEventListener("load", function() { |
avm99963 | a3d1ef3 | 2019-03-30 23:33:29 +0100 | [diff] [blame] | 65 | i18n(); |
| 66 | |
avm99963 | cbea314 | 2019-03-28 00:48:15 +0100 | [diff] [blame] | 67 | chrome.storage.sync.get(null, function(items) { |
avm99963 | 001c07c | 2019-03-30 18:47:02 +0100 | [diff] [blame] | 68 | items = cleanUpOptions(items); |
avm99963 | cbea314 | 2019-03-28 00:48:15 +0100 | [diff] [blame] | 69 | |
avm99963 | 122dc9b | 2019-03-30 18:44:18 +0100 | [diff] [blame] | 70 | Object.keys(defaultOptions).forEach(function(opt) { |
avm99963 | adf9086 | 2020-04-12 13:27:45 +0200 | [diff] [blame] | 71 | if (items[opt] === true && !deprecatedOptions.includes(opt)) { |
avm99963 | 122dc9b | 2019-03-30 18:44:18 +0100 | [diff] [blame] | 72 | document.querySelector("#"+opt).checked = true; |
| 73 | } |
| 74 | }); |
avm99963 | cbea314 | 2019-03-28 00:48:15 +0100 | [diff] [blame] | 75 | |
avm99963 | 6d9c5fe | 2019-06-04 00:35:53 +0200 | [diff] [blame] | 76 | ["thread", "threadall"].forEach(el => document.querySelector("#"+el).addEventListener("change", thread)); |
avm99963 | cbea314 | 2019-03-28 00:48:15 +0100 | [diff] [blame] | 77 | document.querySelector("#save").addEventListener("click", save); |
| 78 | }); |
| 79 | }); |