blob: af9dede617f42167b14cc15990679b4d4fed59ed [file] [log] [blame]
avm99963cbea3142019-03-28 00:48:15 +01001function isEmpty(obj) {
2 return Object.keys(obj).length === 0;
3}
4
avm99963adf90862020-04-12 13:27:45 +02005const defaultOptions = {
avm99963b69eb3d2020-08-20 02:03:44 +02006 '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,
avm99963129fb502020-08-28 05:18:53 +020018 'profileindicator': false,
avm99963122dc9b2019-03-30 18:44:18 +010019};
20
avm99963adf90862020-04-12 13:27:45 +020021const deprecatedOptions = [
avm99963b69eb3d2020-08-20 02:03:44 +020022 'escalatethreads',
23 'movethreads',
24 'batchduplicate',
avm99963adf90862020-04-12 13:27:45 +020025];
26
avm999638e030612020-08-20 02:54:17 +020027var savedSuccessfullyTimeout = null;
28
avm99963122dc9b2019-03-30 18:44:18 +010029function 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 }
avm99963001c07c2019-03-30 18:47:02 +010041
42 return options;
avm99963122dc9b2019-03-30 18:44:18 +010043}
44
avm99963cbea3142019-03-28 00:48:15 +010045function save() {
avm99963122dc9b2019-03-30 18:44:18 +010046 var options = defaultOptions;
47
avm99963b69eb3d2020-08-20 02:03:44 +020048 Object.keys(options).forEach(function(opt) {
avm99963adf90862020-04-12 13:27:45 +020049 if (deprecatedOptions.includes(opt)) return;
avm99963b69eb3d2020-08-20 02:03:44 +020050 options[opt] = document.querySelector('#' + opt).checked || false;
avm99963122dc9b2019-03-30 18:44:18 +010051 });
52
53 chrome.storage.sync.set(options, function() {
avm99963cbea3142019-03-28 00:48:15 +010054 window.close();
avm999638e030612020-08-20 02:54:17 +020055
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);
avm99963cbea3142019-03-28 00:48:15 +010065 });
66}
67
avm99963a3d1ef32019-03-30 23:33:29 +010068function i18n() {
avm99963b69eb3d2020-08-20 02:03:44 +020069 document.querySelectorAll('[data-i18n]')
70 .forEach(
71 el => el.innerHTML = chrome.i18n.getMessage(
72 'options_' + el.getAttribute('data-i18n')));
avm99963a3d1ef32019-03-30 23:33:29 +010073}
74
avm999636d9c5fe2019-06-04 00:35:53 +020075function thread() {
avm99963b69eb3d2020-08-20 02:03:44 +020076 if (document.querySelector('#thread').checked &&
77 document.querySelector('#threadall').checked) {
78 document.querySelector('#' + (this.id == 'thread' ? 'threadall' : 'thread'))
79 .checked = false;
avm999636d9c5fe2019-06-04 00:35:53 +020080 }
81}
82
avm99963b69eb3d2020-08-20 02:03:44 +020083window.addEventListener('load', function() {
avm99963a3d1ef32019-03-30 23:33:29 +010084 i18n();
85
avm99963cbea3142019-03-28 00:48:15 +010086 chrome.storage.sync.get(null, function(items) {
avm99963001c07c2019-03-30 18:47:02 +010087 items = cleanUpOptions(items);
avm99963cbea3142019-03-28 00:48:15 +010088
avm99963122dc9b2019-03-30 18:44:18 +010089 Object.keys(defaultOptions).forEach(function(opt) {
avm99963adf90862020-04-12 13:27:45 +020090 if (items[opt] === true && !deprecatedOptions.includes(opt)) {
avm99963b69eb3d2020-08-20 02:03:44 +020091 document.querySelector('#' + opt).checked = true;
avm99963122dc9b2019-03-30 18:44:18 +010092 }
93 });
avm99963cbea3142019-03-28 00:48:15 +010094
avm99963b69eb3d2020-08-20 02:03:44 +020095 ['thread', 'threadall'].forEach(
96 el => document.querySelector('#' + el).addEventListener(
97 'change', thread));
98 document.querySelector('#save').addEventListener('click', save);
avm99963cbea3142019-03-28 00:48:15 +010099 });
100});