blob: 5f965b99ed49a224fa6944edacfa31c1a7f02dd4 [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 'list',
22 'escalatethreads',
23 'movethreads',
24 'batchduplicate',
avm99963adf90862020-04-12 13:27:45 +020025];
26
avm99963122dc9b2019-03-30 18:44:18 +010027function 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 }
avm99963001c07c2019-03-30 18:47:02 +010039
40 return options;
avm99963122dc9b2019-03-30 18:44:18 +010041}
42
avm99963cbea3142019-03-28 00:48:15 +010043function save() {
avm99963122dc9b2019-03-30 18:44:18 +010044 var options = defaultOptions;
45
avm99963b69eb3d2020-08-20 02:03:44 +020046 Object.keys(options).forEach(function(opt) {
avm99963adf90862020-04-12 13:27:45 +020047 if (deprecatedOptions.includes(opt)) return;
avm99963b69eb3d2020-08-20 02:03:44 +020048 options[opt] = document.querySelector('#' + opt).checked || false;
avm99963122dc9b2019-03-30 18:44:18 +010049 });
50
51 chrome.storage.sync.set(options, function() {
avm99963cbea3142019-03-28 00:48:15 +010052 window.close();
53 });
54}
55
avm99963a3d1ef32019-03-30 23:33:29 +010056function i18n() {
avm99963b69eb3d2020-08-20 02:03:44 +020057 document.querySelectorAll('[data-i18n]')
58 .forEach(
59 el => el.innerHTML = chrome.i18n.getMessage(
60 'options_' + el.getAttribute('data-i18n')));
avm99963a3d1ef32019-03-30 23:33:29 +010061}
62
avm999636d9c5fe2019-06-04 00:35:53 +020063function thread() {
avm99963b69eb3d2020-08-20 02:03:44 +020064 if (document.querySelector('#thread').checked &&
65 document.querySelector('#threadall').checked) {
66 document.querySelector('#' + (this.id == 'thread' ? 'threadall' : 'thread'))
67 .checked = false;
avm999636d9c5fe2019-06-04 00:35:53 +020068 }
69}
70
avm99963b69eb3d2020-08-20 02:03:44 +020071window.addEventListener('load', function() {
avm99963a3d1ef32019-03-30 23:33:29 +010072 i18n();
73
avm99963cbea3142019-03-28 00:48:15 +010074 chrome.storage.sync.get(null, function(items) {
avm99963001c07c2019-03-30 18:47:02 +010075 items = cleanUpOptions(items);
avm99963cbea3142019-03-28 00:48:15 +010076
avm99963122dc9b2019-03-30 18:44:18 +010077 Object.keys(defaultOptions).forEach(function(opt) {
avm99963adf90862020-04-12 13:27:45 +020078 if (items[opt] === true && !deprecatedOptions.includes(opt)) {
avm99963b69eb3d2020-08-20 02:03:44 +020079 document.querySelector('#' + opt).checked = true;
avm99963122dc9b2019-03-30 18:44:18 +010080 }
81 });
avm99963cbea3142019-03-28 00:48:15 +010082
avm99963b69eb3d2020-08-20 02:03:44 +020083 ['thread', 'threadall'].forEach(
84 el => document.querySelector('#' + el).addEventListener(
85 'change', thread));
86 document.querySelector('#save').addEventListener('click', save);
avm99963cbea3142019-03-28 00:48:15 +010087 });
88});