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 | 129fb50 | 2020-08-28 05:18:53 +0200 | [diff] [blame] | 18 | 'profileindicator': false, |
avm99963 | 122dc9b | 2019-03-30 18:44:18 +0100 | [diff] [blame] | 19 | }; |
| 20 | |
avm99963 | adf9086 | 2020-04-12 13:27:45 +0200 | [diff] [blame] | 21 | const deprecatedOptions = [ |
avm99963 | b69eb3d | 2020-08-20 02:03:44 +0200 | [diff] [blame] | 22 | 'escalatethreads', |
| 23 | 'movethreads', |
| 24 | 'batchduplicate', |
avm99963 | adf9086 | 2020-04-12 13:27:45 +0200 | [diff] [blame] | 25 | ]; |
| 26 | |
avm99963 | 8e03061 | 2020-08-20 02:54:17 +0200 | [diff] [blame] | 27 | var savedSuccessfullyTimeout = null; |
| 28 | |
avm99963 | 122dc9b | 2019-03-30 18:44:18 +0100 | [diff] [blame] | 29 | function cleanUpOptions(options) { |
| 30 | var ok = true; |
| 31 | for (const [opt, value] of Object.entries(defaultOptions)) { |
| 32 | if (!opt in options) { |
| 33 | ok = false; |
| 34 | options[opt] = value; |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | if (!ok) { |
| 39 | chrome.storage.sync.set(options); |
| 40 | } |
avm99963 | 001c07c | 2019-03-30 18:47:02 +0100 | [diff] [blame] | 41 | |
| 42 | return options; |
avm99963 | 122dc9b | 2019-03-30 18:44:18 +0100 | [diff] [blame] | 43 | } |
| 44 | |
avm99963 | cbea314 | 2019-03-28 00:48:15 +0100 | [diff] [blame] | 45 | function save() { |
avm99963 | 122dc9b | 2019-03-30 18:44:18 +0100 | [diff] [blame] | 46 | var options = defaultOptions; |
| 47 | |
avm99963 | b69eb3d | 2020-08-20 02:03:44 +0200 | [diff] [blame] | 48 | Object.keys(options).forEach(function(opt) { |
avm99963 | adf9086 | 2020-04-12 13:27:45 +0200 | [diff] [blame] | 49 | if (deprecatedOptions.includes(opt)) return; |
avm99963 | b69eb3d | 2020-08-20 02:03:44 +0200 | [diff] [blame] | 50 | options[opt] = document.querySelector('#' + opt).checked || false; |
avm99963 | 122dc9b | 2019-03-30 18:44:18 +0100 | [diff] [blame] | 51 | }); |
| 52 | |
| 53 | chrome.storage.sync.set(options, function() { |
avm99963 | cbea314 | 2019-03-28 00:48:15 +0100 | [diff] [blame] | 54 | window.close(); |
avm99963 | 8e03061 | 2020-08-20 02:54:17 +0200 | [diff] [blame] | 55 | |
| 56 | // In browsers like Firefox window.close is not supported: |
| 57 | if (savedSuccessfullyTimeout !== null) |
| 58 | window.clearTimeout(savedSuccessfullyTimeout); |
| 59 | |
| 60 | document.getElementById('save-indicator').innerText = |
| 61 | '✓ ' + chrome.i18n.getMessage('options_saved'); |
| 62 | savedSuccessfullyTimeout = window.setTimeout(_ => { |
| 63 | document.getElementById('save-indicator').innerText = ''; |
| 64 | }, 3699); |
avm99963 | cbea314 | 2019-03-28 00:48:15 +0100 | [diff] [blame] | 65 | }); |
| 66 | } |
| 67 | |
avm99963 | a3d1ef3 | 2019-03-30 23:33:29 +0100 | [diff] [blame] | 68 | function i18n() { |
avm99963 | b69eb3d | 2020-08-20 02:03:44 +0200 | [diff] [blame] | 69 | document.querySelectorAll('[data-i18n]') |
| 70 | .forEach( |
| 71 | el => el.innerHTML = chrome.i18n.getMessage( |
| 72 | 'options_' + el.getAttribute('data-i18n'))); |
avm99963 | a3d1ef3 | 2019-03-30 23:33:29 +0100 | [diff] [blame] | 73 | } |
| 74 | |
avm99963 | 6d9c5fe | 2019-06-04 00:35:53 +0200 | [diff] [blame] | 75 | function thread() { |
avm99963 | b69eb3d | 2020-08-20 02:03:44 +0200 | [diff] [blame] | 76 | if (document.querySelector('#thread').checked && |
| 77 | document.querySelector('#threadall').checked) { |
| 78 | document.querySelector('#' + (this.id == 'thread' ? 'threadall' : 'thread')) |
| 79 | .checked = false; |
avm99963 | 6d9c5fe | 2019-06-04 00:35:53 +0200 | [diff] [blame] | 80 | } |
| 81 | } |
| 82 | |
avm99963 | b69eb3d | 2020-08-20 02:03:44 +0200 | [diff] [blame] | 83 | window.addEventListener('load', function() { |
avm99963 | a3d1ef3 | 2019-03-30 23:33:29 +0100 | [diff] [blame] | 84 | i18n(); |
| 85 | |
avm99963 | cbea314 | 2019-03-28 00:48:15 +0100 | [diff] [blame] | 86 | chrome.storage.sync.get(null, function(items) { |
avm99963 | 001c07c | 2019-03-30 18:47:02 +0100 | [diff] [blame] | 87 | items = cleanUpOptions(items); |
avm99963 | cbea314 | 2019-03-28 00:48:15 +0100 | [diff] [blame] | 88 | |
avm99963 | 122dc9b | 2019-03-30 18:44:18 +0100 | [diff] [blame] | 89 | Object.keys(defaultOptions).forEach(function(opt) { |
avm99963 | adf9086 | 2020-04-12 13:27:45 +0200 | [diff] [blame] | 90 | if (items[opt] === true && !deprecatedOptions.includes(opt)) { |
avm99963 | b69eb3d | 2020-08-20 02:03:44 +0200 | [diff] [blame] | 91 | document.querySelector('#' + opt).checked = true; |
avm99963 | 122dc9b | 2019-03-30 18:44:18 +0100 | [diff] [blame] | 92 | } |
| 93 | }); |
avm99963 | cbea314 | 2019-03-28 00:48:15 +0100 | [diff] [blame] | 94 | |
avm99963 | b69eb3d | 2020-08-20 02:03:44 +0200 | [diff] [blame] | 95 | ['thread', 'threadall'].forEach( |
| 96 | el => document.querySelector('#' + el).addEventListener( |
| 97 | 'change', thread)); |
| 98 | document.querySelector('#save').addEventListener('click', save); |
avm99963 | cbea314 | 2019-03-28 00:48:15 +0100 | [diff] [blame] | 99 | }); |
| 100 | }); |