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 | 122dc9b | 2019-03-30 18:44:18 +0100 | [diff] [blame] | 5 | var defaultOptions = { |
| 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, |
| 15 | "movethreads": false |
avm99963 | 122dc9b | 2019-03-30 18:44:18 +0100 | [diff] [blame] | 16 | }; |
| 17 | |
| 18 | function cleanUpOptions(options) { |
| 19 | var ok = true; |
| 20 | for (const [opt, value] of Object.entries(defaultOptions)) { |
| 21 | if (!opt in options) { |
| 22 | ok = false; |
| 23 | options[opt] = value; |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | if (!ok) { |
| 28 | chrome.storage.sync.set(options); |
| 29 | } |
avm99963 | 001c07c | 2019-03-30 18:47:02 +0100 | [diff] [blame] | 30 | |
| 31 | return options; |
avm99963 | 122dc9b | 2019-03-30 18:44:18 +0100 | [diff] [blame] | 32 | } |
| 33 | |
avm99963 | cbea314 | 2019-03-28 00:48:15 +0100 | [diff] [blame] | 34 | function save() { |
avm99963 | 122dc9b | 2019-03-30 18:44:18 +0100 | [diff] [blame] | 35 | var options = defaultOptions; |
| 36 | |
| 37 | Object.keys(options).forEach(function (opt) { |
| 38 | options[opt] = document.querySelector("#"+opt).checked; |
| 39 | }); |
| 40 | |
| 41 | chrome.storage.sync.set(options, function() { |
avm99963 | cbea314 | 2019-03-28 00:48:15 +0100 | [diff] [blame] | 42 | window.close(); |
| 43 | }); |
| 44 | } |
| 45 | |
avm99963 | a3d1ef3 | 2019-03-30 23:33:29 +0100 | [diff] [blame] | 46 | function i18n() { |
avm99963 | c19497f | 2019-08-23 00:15:11 +0200 | [diff] [blame] | 47 | 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] | 48 | } |
| 49 | |
avm99963 | 6d9c5fe | 2019-06-04 00:35:53 +0200 | [diff] [blame] | 50 | function thread() { |
| 51 | if (document.querySelector("#thread").checked && document.querySelector("#threadall").checked) { |
| 52 | document.querySelector("#"+(this.id == "thread" ? "threadall" : "thread")).checked = false; |
| 53 | } |
| 54 | } |
| 55 | |
avm99963 | cbea314 | 2019-03-28 00:48:15 +0100 | [diff] [blame] | 56 | window.addEventListener("load", function() { |
avm99963 | a3d1ef3 | 2019-03-30 23:33:29 +0100 | [diff] [blame] | 57 | i18n(); |
| 58 | |
avm99963 | cbea314 | 2019-03-28 00:48:15 +0100 | [diff] [blame] | 59 | chrome.storage.sync.get(null, function(items) { |
avm99963 | 001c07c | 2019-03-30 18:47:02 +0100 | [diff] [blame] | 60 | items = cleanUpOptions(items); |
avm99963 | cbea314 | 2019-03-28 00:48:15 +0100 | [diff] [blame] | 61 | |
avm99963 | 122dc9b | 2019-03-30 18:44:18 +0100 | [diff] [blame] | 62 | Object.keys(defaultOptions).forEach(function(opt) { |
| 63 | if (items[opt] === true) { |
| 64 | document.querySelector("#"+opt).checked = true; |
| 65 | } |
| 66 | }); |
avm99963 | cbea314 | 2019-03-28 00:48:15 +0100 | [diff] [blame] | 67 | |
avm99963 | 6d9c5fe | 2019-06-04 00:35:53 +0200 | [diff] [blame] | 68 | ["thread", "threadall"].forEach(el => document.querySelector("#"+el).addEventListener("change", thread)); |
avm99963 | cbea314 | 2019-03-28 00:48:15 +0100 | [diff] [blame] | 69 | document.querySelector("#save").addEventListener("click", save); |
| 70 | }); |
| 71 | }); |