blob: 637d6fcf439ae2c65774b4f027fd55455ae4e602 [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,
avm99963122dc9b2019-03-30 18:44:18 +010018};
19
avm99963adf90862020-04-12 13:27:45 +020020const deprecatedOptions = [
avm99963b69eb3d2020-08-20 02:03:44 +020021 'escalatethreads',
22 'movethreads',
23 'batchduplicate',
avm99963adf90862020-04-12 13:27:45 +020024];
25
avm999638e030612020-08-20 02:54:17 +020026var savedSuccessfullyTimeout = null;
27
avm99963122dc9b2019-03-30 18:44:18 +010028function cleanUpOptions(options) {
29 var ok = true;
30 for (const [opt, value] of Object.entries(defaultOptions)) {
31 if (!opt in options) {
32 ok = false;
33 options[opt] = value;
34 }
35 }
36
37 if (!ok) {
38 chrome.storage.sync.set(options);
39 }
avm99963001c07c2019-03-30 18:47:02 +010040
41 return options;
avm99963122dc9b2019-03-30 18:44:18 +010042}
43
avm99963cbea3142019-03-28 00:48:15 +010044function save() {
avm99963122dc9b2019-03-30 18:44:18 +010045 var options = defaultOptions;
46
avm99963b69eb3d2020-08-20 02:03:44 +020047 Object.keys(options).forEach(function(opt) {
avm99963adf90862020-04-12 13:27:45 +020048 if (deprecatedOptions.includes(opt)) return;
avm99963b69eb3d2020-08-20 02:03:44 +020049 options[opt] = document.querySelector('#' + opt).checked || false;
avm99963122dc9b2019-03-30 18:44:18 +010050 });
51
52 chrome.storage.sync.set(options, function() {
avm99963cbea3142019-03-28 00:48:15 +010053 window.close();
avm999638e030612020-08-20 02:54:17 +020054
55 // In browsers like Firefox window.close is not supported:
56 if (savedSuccessfullyTimeout !== null)
57 window.clearTimeout(savedSuccessfullyTimeout);
58
59 document.getElementById('save-indicator').innerText =
60 '✓ ' + chrome.i18n.getMessage('options_saved');
61 savedSuccessfullyTimeout = window.setTimeout(_ => {
62 document.getElementById('save-indicator').innerText = '';
63 }, 3699);
avm99963cbea3142019-03-28 00:48:15 +010064 });
65}
66
avm99963a3d1ef32019-03-30 23:33:29 +010067function i18n() {
avm99963b69eb3d2020-08-20 02:03:44 +020068 document.querySelectorAll('[data-i18n]')
69 .forEach(
70 el => el.innerHTML = chrome.i18n.getMessage(
71 'options_' + el.getAttribute('data-i18n')));
avm99963a3d1ef32019-03-30 23:33:29 +010072}
73
avm999636d9c5fe2019-06-04 00:35:53 +020074function thread() {
avm99963b69eb3d2020-08-20 02:03:44 +020075 if (document.querySelector('#thread').checked &&
76 document.querySelector('#threadall').checked) {
77 document.querySelector('#' + (this.id == 'thread' ? 'threadall' : 'thread'))
78 .checked = false;
avm999636d9c5fe2019-06-04 00:35:53 +020079 }
80}
81
avm99963b69eb3d2020-08-20 02:03:44 +020082window.addEventListener('load', function() {
avm99963a3d1ef32019-03-30 23:33:29 +010083 i18n();
84
avm99963cbea3142019-03-28 00:48:15 +010085 chrome.storage.sync.get(null, function(items) {
avm99963001c07c2019-03-30 18:47:02 +010086 items = cleanUpOptions(items);
avm99963cbea3142019-03-28 00:48:15 +010087
avm99963122dc9b2019-03-30 18:44:18 +010088 Object.keys(defaultOptions).forEach(function(opt) {
avm99963adf90862020-04-12 13:27:45 +020089 if (items[opt] === true && !deprecatedOptions.includes(opt)) {
avm99963b69eb3d2020-08-20 02:03:44 +020090 document.querySelector('#' + opt).checked = true;
avm99963122dc9b2019-03-30 18:44:18 +010091 }
92 });
avm99963cbea3142019-03-28 00:48:15 +010093
avm99963b69eb3d2020-08-20 02:03:44 +020094 ['thread', 'threadall'].forEach(
95 el => document.querySelector('#' + el).addEventListener(
96 'change', thread));
97 document.querySelector('#save').addEventListener('click', save);
avm99963cbea3142019-03-28 00:48:15 +010098 });
99});