blob: afb6bdecbbb096a667fb39e334ba60c33dd2520b [file] [log] [blame]
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +02001import {cleanUpOptions, optionsPrototype, specialOptions} from './common/optionsUtils.js';
2import {isFirefox, isReleaseVersion} from './common/extUtils.js';
3
avm99963bf8eece2021-04-22 00:27:03 +02004var savedSuccessfullyTimeout = null;
5
6const exclusiveOptions = [['thread', 'threadall']];
7
8// Get the value of the option set in the options/experiments page
9function getOptionValue(opt) {
10 if (specialOptions.includes(opt)) {
11 switch (opt) {
12 case 'profileindicatoralt_months':
13 return document.getElementById(opt).value || 12;
14
15 case 'ccdarktheme_mode':
16 return document.getElementById(opt).value || 'switch';
17
18 case 'ccdragndropfix':
19 return document.getElementById(opt).checked || false;
20
21 default:
22 console.warn('Unrecognized option: ' + opt);
23 return undefined;
24 }
25 }
26
27 return document.getElementById(opt).checked || false;
28}
29
30// Returns whether the option is included in the current context
31function isOptionShown(opt) {
32 if (!optionsPrototype.hasOwnProperty(opt)) return false;
33 return optionsPrototype[opt].context == window.CONTEXT;
34}
35
36function save(e) {
37 // Validation checks before saving
38 if (isOptionShown('profileindicatoralt_months')) {
39 var months = document.getElementById('profileindicatoralt_months');
40 if (!months.checkValidity()) {
41 console.warn(months.validationMessage);
42 return;
43 }
44 }
45
46 e.preventDefault();
47
48 chrome.storage.sync.get(null, function(items) {
49 var options = cleanUpOptions(items, true);
50
51 // Save
52 Object.keys(options).forEach(function(opt) {
53 if (!isOptionShown(opt)) return;
54 options[opt] = getOptionValue(opt);
55 });
56
57 chrome.storage.sync.set(options, function() {
58 window.close();
59
60 // In browsers like Firefox window.close is not supported:
61 if (savedSuccessfullyTimeout !== null)
62 window.clearTimeout(savedSuccessfullyTimeout);
63
64 document.getElementById('save-indicator').innerText =
65 '✓ ' + chrome.i18n.getMessage('options_saved');
66 savedSuccessfullyTimeout = window.setTimeout(_ => {
67 document.getElementById('save-indicator').innerText = '';
68 }, 3699);
69 });
70 });
71}
72
73function i18n() {
74 document.querySelectorAll('[data-i18n]')
75 .forEach(
76 el => el.innerHTML = chrome.i18n.getMessage(
77 'options_' + el.getAttribute('data-i18n')));
78}
79
80window.addEventListener('load', function() {
81 i18n();
82
avm999637309b062021-04-22 12:41:08 +020083 if (window.CONTEXT == 'options' && !isReleaseVersion()) {
84 var experimentsLink = document.querySelector('.experiments-link');
85 experimentsLink.removeAttribute('hidden');
86 experimentsLink.addEventListener('click', _ => chrome.tabs.create({
87 url: chrome.runtime.getURL('options/experiments.html'),
88 }));
89 }
90
avm99963bf8eece2021-04-22 00:27:03 +020091 chrome.storage.sync.get(null, function(items) {
92 items = cleanUpOptions(items, false);
93
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020094 for (var entry of Object.entries(optionsPrototype)) {
95 var opt = entry[0];
96 var optMeta = entry[1];
97
avm99963bf8eece2021-04-22 00:27:03 +020098 if (!isOptionShown(opt)) continue;
99
100 if (specialOptions.includes(opt)) {
101 switch (opt) {
102 case 'profileindicatoralt_months':
103 var input = document.createElement('input');
104 input.type = 'number';
105 input.id = 'profileindicatoralt_months';
106 input.max = '12';
107 input.min = '1';
108 input.value = items[opt];
109 input.required = true;
110 document.getElementById('profileindicatoralt_months--container')
111 .appendChild(input);
112 break;
113
114 case 'ccdarktheme_mode':
115 var select = document.createElement('select');
116 select.id = 'ccdarktheme_mode';
117
118 const modes = ['switch', 'system'];
119 for (const mode of modes) {
120 var modeOption = document.createElement('option');
121 modeOption.value = mode;
122 modeOption.textContent =
123 chrome.i18n.getMessage('options_ccdarktheme_mode_' + mode);
124 if (items.ccdarktheme_mode == mode) modeOption.selected = true;
125 select.appendChild(modeOption);
126 }
127
128 document.getElementById('ccdarktheme_mode--container')
129 .appendChild(select);
130 break;
131
132 // Firefox doesn't support drag and dropping bookmarks into the text
133 // editor while preserving the bookmark title.
134 case 'ccdragndropfix':
135 var showOption = !isFirefox();
136 if (showOption) {
137 document.getElementById('dragndrop-wrapper')
138 .removeAttribute('hidden');
139
140 if (items[opt] === true)
141 document.getElementById(opt).checked = true;
142 }
143 break;
144
145 default:
146 console.warn('Unrecognized option: ' + opt);
147 break;
148 }
149 continue;
150 }
151
152 if (items[opt] === true) document.getElementById(opt).checked = true;
153 }
154
155 exclusiveOptions.forEach(exclusive => {
156 if (!isOptionShown(exclusive[0]) || !isOptionShown(exclusive[1])) return;
157
158 exclusive.forEach(
159 el => document.getElementById(el).addEventListener('change', e => {
160 if (document.getElementById(exclusive[0]).checked &&
161 document.getElementById(exclusive[1]).checked) {
162 document
163 .getElementById(
164 exclusive[(e.currentTarget.id == exclusive[0] ? 1 : 0)])
165 .checked = false;
166 }
167 }));
168 });
169 document.querySelector('#save').addEventListener('click', save);
170 });
171});