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