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, |
| 8 | "fixedtoolbar": false, |
| 9 | "redirect": false, |
| 10 | "history": false |
| 11 | }; |
| 12 | |
| 13 | function cleanUpOptions(options) { |
| 14 | var ok = true; |
| 15 | for (const [opt, value] of Object.entries(defaultOptions)) { |
| 16 | if (!opt in options) { |
| 17 | ok = false; |
| 18 | options[opt] = value; |
| 19 | } |
| 20 | } |
| 21 | |
| 22 | if (!ok) { |
| 23 | chrome.storage.sync.set(options); |
| 24 | } |
avm99963 | 001c07c | 2019-03-30 18:47:02 +0100 | [diff] [blame] | 25 | |
| 26 | return options; |
avm99963 | 122dc9b | 2019-03-30 18:44:18 +0100 | [diff] [blame] | 27 | } |
| 28 | |
avm99963 | cbea314 | 2019-03-28 00:48:15 +0100 | [diff] [blame] | 29 | function save() { |
avm99963 | 122dc9b | 2019-03-30 18:44:18 +0100 | [diff] [blame] | 30 | var options = defaultOptions; |
| 31 | |
| 32 | Object.keys(options).forEach(function (opt) { |
| 33 | options[opt] = document.querySelector("#"+opt).checked; |
| 34 | }); |
| 35 | |
| 36 | chrome.storage.sync.set(options, function() { |
avm99963 | cbea314 | 2019-03-28 00:48:15 +0100 | [diff] [blame] | 37 | window.close(); |
| 38 | }); |
| 39 | } |
| 40 | |
avm99963 | a3d1ef3 | 2019-03-30 23:33:29 +0100 | [diff] [blame] | 41 | function i18n() { |
| 42 | var messages = ["list", "thread", "enhancements", "fixedtoolbar", "redirect", "experimental_label", "history", "save"]; |
| 43 | |
| 44 | messages.forEach(function(msg) { |
| 45 | console.log(msg); |
| 46 | document.querySelector("[data-i18n=\""+msg+"\"]").innerHTML = chrome.i18n.getMessage("options_"+msg); |
| 47 | }); |
| 48 | } |
| 49 | |
avm99963 | cbea314 | 2019-03-28 00:48:15 +0100 | [diff] [blame] | 50 | window.addEventListener("load", function() { |
avm99963 | a3d1ef3 | 2019-03-30 23:33:29 +0100 | [diff] [blame] | 51 | i18n(); |
| 52 | |
avm99963 | cbea314 | 2019-03-28 00:48:15 +0100 | [diff] [blame] | 53 | chrome.storage.sync.get(null, function(items) { |
avm99963 | 001c07c | 2019-03-30 18:47:02 +0100 | [diff] [blame] | 54 | items = cleanUpOptions(items); |
avm99963 | cbea314 | 2019-03-28 00:48:15 +0100 | [diff] [blame] | 55 | |
avm99963 | 122dc9b | 2019-03-30 18:44:18 +0100 | [diff] [blame] | 56 | Object.keys(defaultOptions).forEach(function(opt) { |
| 57 | if (items[opt] === true) { |
| 58 | document.querySelector("#"+opt).checked = true; |
| 59 | } |
| 60 | }); |
avm99963 | cbea314 | 2019-03-28 00:48:15 +0100 | [diff] [blame] | 61 | |
| 62 | document.querySelector("#save").addEventListener("click", save); |
| 63 | }); |
| 64 | }); |