avm99963 | 8e03061 | 2020-08-20 02:54:17 +0200 | [diff] [blame] | 1 | var savedSuccessfullyTimeout = null; |
| 2 | |
avm99963 | cbea314 | 2019-03-28 00:48:15 +0100 | [diff] [blame] | 3 | function save() { |
avm99963 | 122dc9b | 2019-03-30 18:44:18 +0100 | [diff] [blame] | 4 | var options = defaultOptions; |
| 5 | |
avm99963 | b69eb3d | 2020-08-20 02:03:44 +0200 | [diff] [blame] | 6 | Object.keys(options).forEach(function(opt) { |
avm99963 | adf9086 | 2020-04-12 13:27:45 +0200 | [diff] [blame] | 7 | if (deprecatedOptions.includes(opt)) return; |
avm99963 | b69eb3d | 2020-08-20 02:03:44 +0200 | [diff] [blame] | 8 | options[opt] = document.querySelector('#' + opt).checked || false; |
avm99963 | 122dc9b | 2019-03-30 18:44:18 +0100 | [diff] [blame] | 9 | }); |
| 10 | |
| 11 | chrome.storage.sync.set(options, function() { |
avm99963 | cbea314 | 2019-03-28 00:48:15 +0100 | [diff] [blame] | 12 | window.close(); |
avm99963 | 8e03061 | 2020-08-20 02:54:17 +0200 | [diff] [blame] | 13 | |
| 14 | // In browsers like Firefox window.close is not supported: |
| 15 | if (savedSuccessfullyTimeout !== null) |
| 16 | window.clearTimeout(savedSuccessfullyTimeout); |
| 17 | |
| 18 | document.getElementById('save-indicator').innerText = |
| 19 | '✓ ' + chrome.i18n.getMessage('options_saved'); |
| 20 | savedSuccessfullyTimeout = window.setTimeout(_ => { |
| 21 | document.getElementById('save-indicator').innerText = ''; |
| 22 | }, 3699); |
avm99963 | cbea314 | 2019-03-28 00:48:15 +0100 | [diff] [blame] | 23 | }); |
| 24 | } |
| 25 | |
avm99963 | a3d1ef3 | 2019-03-30 23:33:29 +0100 | [diff] [blame] | 26 | function i18n() { |
avm99963 | b69eb3d | 2020-08-20 02:03:44 +0200 | [diff] [blame] | 27 | document.querySelectorAll('[data-i18n]') |
| 28 | .forEach( |
| 29 | el => el.innerHTML = chrome.i18n.getMessage( |
| 30 | 'options_' + el.getAttribute('data-i18n'))); |
avm99963 | a3d1ef3 | 2019-03-30 23:33:29 +0100 | [diff] [blame] | 31 | } |
| 32 | |
avm99963 | 6d9c5fe | 2019-06-04 00:35:53 +0200 | [diff] [blame] | 33 | function thread() { |
avm99963 | b69eb3d | 2020-08-20 02:03:44 +0200 | [diff] [blame] | 34 | if (document.querySelector('#thread').checked && |
| 35 | document.querySelector('#threadall').checked) { |
| 36 | document.querySelector('#' + (this.id == 'thread' ? 'threadall' : 'thread')) |
| 37 | .checked = false; |
avm99963 | 6d9c5fe | 2019-06-04 00:35:53 +0200 | [diff] [blame] | 38 | } |
| 39 | } |
| 40 | |
avm99963 | b69eb3d | 2020-08-20 02:03:44 +0200 | [diff] [blame] | 41 | window.addEventListener('load', function() { |
avm99963 | a3d1ef3 | 2019-03-30 23:33:29 +0100 | [diff] [blame] | 42 | i18n(); |
| 43 | |
avm99963 | cbea314 | 2019-03-28 00:48:15 +0100 | [diff] [blame] | 44 | chrome.storage.sync.get(null, function(items) { |
avm99963 | 001c07c | 2019-03-30 18:47:02 +0100 | [diff] [blame] | 45 | items = cleanUpOptions(items); |
avm99963 | cbea314 | 2019-03-28 00:48:15 +0100 | [diff] [blame] | 46 | |
avm99963 | 122dc9b | 2019-03-30 18:44:18 +0100 | [diff] [blame] | 47 | Object.keys(defaultOptions).forEach(function(opt) { |
avm99963 | adf9086 | 2020-04-12 13:27:45 +0200 | [diff] [blame] | 48 | if (items[opt] === true && !deprecatedOptions.includes(opt)) { |
avm99963 | b69eb3d | 2020-08-20 02:03:44 +0200 | [diff] [blame] | 49 | document.querySelector('#' + opt).checked = true; |
avm99963 | 122dc9b | 2019-03-30 18:44:18 +0100 | [diff] [blame] | 50 | } |
| 51 | }); |
avm99963 | cbea314 | 2019-03-28 00:48:15 +0100 | [diff] [blame] | 52 | |
avm99963 | b69eb3d | 2020-08-20 02:03:44 +0200 | [diff] [blame] | 53 | ['thread', 'threadall'].forEach( |
| 54 | el => document.querySelector('#' + el).addEventListener( |
| 55 | 'change', thread)); |
| 56 | document.querySelector('#save').addEventListener('click', save); |
avm99963 | cbea314 | 2019-03-28 00:48:15 +0100 | [diff] [blame] | 57 | }); |
| 58 | }); |