blob: 9fc10dfa0734afba546d0aa7e34ca8ad9f6e692d [file] [log] [blame]
avm999634a2a5d52016-06-04 16:17:29 +02001function $(selector) {
avm999635a57c412020-12-27 00:26:45 +01002 return document.querySelector(selector);
avm999634a2a5d52016-06-04 16:17:29 +02003}
4
5function $all(selector) {
avm999635a57c412020-12-27 00:26:45 +01006 return document.querySelectorAll(selector);
avm999634a2a5d52016-06-04 16:17:29 +02007}
8
9function isEmpty(obj) {
avm999635a57c412020-12-27 00:26:45 +010010 return Object.keys(obj).length === 0;
avm999634a2a5d52016-06-04 16:17:29 +020011}
12
13function i18n() {
avm999635a57c412020-12-27 00:26:45 +010014 $all('[data-i18n]').forEach(el => {
15 el.innerHTML =
16 chrome.i18n.getMessage('options_' + el.getAttribute('data-i18n'));
17 });
avm999634a2a5d52016-06-04 16:17:29 +020018}
19
avm99963ce257a92020-12-27 00:07:13 +010020function printListModal() {
avm999635a57c412020-12-27 00:26:45 +010021 $('#select_language').textContent = '';
22 var heysortable = sortable.toArray();
avm999630f961162020-12-27 13:12:27 +010023 var languages = [];
24 for (var langCode of Object.keys(isoLangs)) {
25 var l = isoLangs[langCode];
26 l['code'] = langCode;
27 languages.push(l);
28 }
29
30 languages.sort((a, b) => a.name < b.name ? -1 : (a.name > b.name ? 1 : 0));
31 languages.forEach(language => {
32 if (!inArray(language['code'], heysortable)) {
avm999635a57c412020-12-27 00:26:45 +010033 var el = document.createElement('option');
avm999630f961162020-12-27 13:12:27 +010034 el.setAttribute('value', language['code']);
35 el.textContent = language['name'] + ' (' + language['nativeName'] + ')';
avm999635a57c412020-12-27 00:26:45 +010036 $('#select_language').appendChild(el);
avm999634a2a5d52016-06-04 16:17:29 +020037 }
avm999630f961162020-12-27 13:12:27 +010038 });
avm999634a2a5d52016-06-04 16:17:29 +020039}
40
41function init() {
avm999635a57c412020-12-27 00:26:45 +010042 i18n();
43 chrome.storage.sync.get(null, items => {
44 // If no settings are set
45 if (isEmpty(items)) {
46 items = {
47 translateinto: {},
avm999638824b8a2021-01-10 18:25:12 +010048 uniquetab: 'popup',
avm999635a57c412020-12-27 00:26:45 +010049 };
50 chrome.storage.sync.set(items);
51 }
avm999634a2a5d52016-06-04 16:17:29 +020052
avm999635a57c412020-12-27 00:26:45 +010053 // Check the checkbox of the window opening
54 if (items.uniquetab === 'yep') $('#uniquetab').checked = true;
55 if (items.uniquetab === '') $('#varioustabs').checked = true;
avm999638824b8a2021-01-10 18:25:12 +010056 if (items.uniquetab === 'panel' || items.uniquetab === 'popup')
57 $('#popup').checked = true;
avm999634a2a5d52016-06-04 16:17:29 +020058
avm999635a57c412020-12-27 00:26:45 +010059 // Add event listeners for certain buttons and links
avm999635a57c412020-12-27 00:26:45 +010060 $('#save').addEventListener('click', _ => {
avm99963a0036412020-12-29 13:39:05 +010061 saveOptions(true);
62 });
63
64 // Save automatically
65 $all('input[type="radio"]').forEach(radio => {
66 radio.addEventListener('change', _ => {
67 saveOptions();
68 });
avm999635a57c412020-12-27 00:26:45 +010069 });
70
71 // Print selected language list
72 var languages = items.translateinto;
73
74 if (languages) {
75 for (var language_id of Object.keys(languages)) {
76 var language = languages[language_id];
77 var el = document.createElement('li');
78 el.setAttribute('data-language', language);
79 el.setAttribute('data-id', language);
80 el.innerHTML = isoLangs[language]['name'] + ' (' +
81 isoLangs[language]['nativeName'] + ')' +
82 '<span data-language=\'' + language +
83 '\' class=\'delete\'>x</span>';
84 $('#languages').appendChild(el);
85 $('#languages li[data-language=' + language + '] .delete')
86 .addEventListener('click', function() {
87 $('#languages')
88 .removeChild($(
89 'li[data-language=' + this.getAttribute('data-language') +
90 ']'));
91 printListModal();
avm99963a0036412020-12-29 13:39:05 +010092 // Save automatically
93 saveOptions();
avm999635a57c412020-12-27 00:26:45 +010094 });
95 }
96 }
97
98 // Initiate Sortable
99 sortable = new Sortable($('#languages'), {animation: 150});
100
101 // Handling The Dialog
102 $('#languages_add').addEventListener('click', _ => {
103 $('dialog#languages_add_dialog').showModal();
104 $('#select_language').focus();
105 });
106
107 $('#languages_add_cancel').addEventListener('click', _ => {
108 $('dialog#languages_add_dialog').close();
109 });
110
111 $('#languages_add_ok').addEventListener('click', _ => {
112 var el = document.createElement('li');
113 var language = $('#select_language').value;
114 if (inArray(language, sortable.toArray())) {
115 return;
116 }
117 el.setAttribute('data-language', language);
118 el.setAttribute('data-id', language);
119 el.innerHTML = isoLangs[language]['name'] + ' (' +
120 isoLangs[language]['nativeName'] + ')' +
121 '<span data-language=\'' + language + '\' class=\'delete\'>x</span>';
122 $('#languages').appendChild(el);
123 var selection = $('#select_language option[value=' + language + ']');
124 selection.parentNode.removeChild(selection);
125 $('#languages li[data-id=' + language + '] .delete')
126 .addEventListener('click', function() {
127 $('#languages')
128 .removeChild(
129 $('li[data-language=' + this.getAttribute('data-language') +
130 ']'));
131 printListModal();
avm99963a0036412020-12-29 13:39:05 +0100132 // Save automatically
133 saveOptions();
avm999635a57c412020-12-27 00:26:45 +0100134 });
135 $('dialog').close();
avm99963a0036412020-12-29 13:39:05 +0100136
137 // Save automatically
138 saveOptions();
avm999635a57c412020-12-27 00:26:45 +0100139 });
140
141 // About credits...
avm999639bbb3a42020-12-29 03:29:44 +0100142 var normalCredits = fetch('json/credits.json').then(res => res.json());
143 var i18nCredits = fetch('json/i18n-credits.json').then(res => res.json());
144
145 Promise.all([normalCredits, i18nCredits])
146 .then(values => {
147 var credits = values[0];
148 var i18nCredits = values[1];
avm999635a57c412020-12-27 00:26:45 +0100149 var content = $('dialog#credits_dialog .content_area');
avm999639bbb3a42020-12-29 03:29:44 +0100150 credits.forEach(item => {
avm999635a57c412020-12-27 00:26:45 +0100151 var div = document.createElement('div');
avm999639bbb3a42020-12-29 03:29:44 +0100152 div.classList.add('entry');
avm999635a57c412020-12-27 00:26:45 +0100153 if (item.url) {
154 var a = document.createElement('a');
155 a.classList.add('homepage');
156 a.href = item.url;
157 a.target = '_blank';
158 a.textContent =
159 chrome.i18n.getMessage('options_credits_homepage');
160 div.append(a);
161 }
162
163 var h4 = document.createElement('h4');
164 h4.textContent = item.name;
165 div.append(h4);
166
167 if (item.author) {
168 var p = document.createElement('p');
169 p.classList.add('author');
170 p.textContent = chrome.i18n.getMessage('options_credits_by') +
171 ' ' + item.author +
172 (item.license ? ' - ' + item.license : '');
173 div.append(p);
174 }
175 content.append(div);
176 });
177
avm999639bbb3a42020-12-29 03:29:44 +0100178 var cList = document.getElementById('translators');
179 i18nCredits.forEach(contributor => {
180 var li = document.createElement('li');
181 var languages = [];
182 if (contributor.languages) {
183 contributor.languages.forEach(lang => {
184 languages.push(lang.name || 'undefined');
185 });
186 }
187
188 var name = document.createElement('span');
189 name.classList.add('name');
190 name.textContent = contributor.name || 'undefined';
191 li.append(name);
192
193 if (languages.length > 0) {
194 var languages =
195 document.createTextNode(': ' + languages.join(', '));
196 li.append(languages);
197 }
198
199 cList.append(li);
200 });
201
avm999635a57c412020-12-27 00:26:45 +0100202 window.onhashchange = function() {
203 if (location.hash == '#credits') {
avm999639bbb3a42020-12-29 03:29:44 +0100204 var credits = document.getElementById('credits_dialog');
205 credits.showModal();
206 credits.querySelector('.scrollable').scrollTo(0, 0);
avm999635a57c412020-12-27 00:26:45 +0100207 $('#credits_ok').focus();
208 }
209 };
210
211 if (location.hash == '#credits') {
212 $('dialog#credits_dialog').showModal();
213 $('#credits_ok').focus();
214 }
215
216 $('#credits_ok').addEventListener('click', _ => {
217 $('dialog#credits_dialog').close();
218 });
219 $('dialog#credits_dialog').addEventListener('close', _ => {
220 history.pushState(
221 '', document.title,
222 window.location.pathname + window.location.search);
223 });
224
225 // Print language list in the modal dialog
226 printListModal();
227 })
228 .catch(err => console.log(err));
229 });
avm999634a2a5d52016-06-04 16:17:29 +0200230}
231
avm99963a0036412020-12-29 13:39:05 +0100232function saveOptions(close = false) {
avm999635a57c412020-12-27 00:26:45 +0100233 var languages = document.getElementById('languages');
234 var options = {
235 uniquetab: '',
236 translateinto: {},
237 };
avm999634a2a5d52016-06-04 16:17:29 +0200238
avm999635a57c412020-12-27 00:26:45 +0100239 options.uniquetab = radioSelected('uniquetab');
avm999634a2a5d52016-06-04 16:17:29 +0200240
avm999635a57c412020-12-27 00:26:45 +0100241 var selected_languages = sortable.toArray();
avm999634a2a5d52016-06-04 16:17:29 +0200242
avm999635a57c412020-12-27 00:26:45 +0100243 var i = 0;
244 for (var language_id in selected_languages) {
245 var language = selected_languages[language_id];
246 options.translateinto[i] = language;
247 i++;
248 }
avm999634a2a5d52016-06-04 16:17:29 +0200249
avm999635a57c412020-12-27 00:26:45 +0100250 chrome.storage.sync.set(options, function() {
251 var background = chrome.extension.getBackgroundPage();
avm999634a2a5d52016-06-04 16:17:29 +0200252
avm999635a57c412020-12-27 00:26:45 +0100253 background.translator_tab = false;
254 background.translator_window = false;
avm99963a0036412020-12-29 13:39:05 +0100255 if (close) window.close();
avm999635a57c412020-12-27 00:26:45 +0100256 });
avm999634a2a5d52016-06-04 16:17:29 +0200257}
258
avm99963ce257a92020-12-27 00:07:13 +0100259function radioSelected(a) {
avm999635a57c412020-12-27 00:26:45 +0100260 var elements = document.getElementsByName(a);
avm999634a2a5d52016-06-04 16:17:29 +0200261
avm999635a57c412020-12-27 00:26:45 +0100262 for (var i = 0; i < elements.length; i++)
263 if (elements[i].checked) return elements[i].value;
avm999634a2a5d52016-06-04 16:17:29 +0200264}
265
266function inArray(needle, haystack) {
avm999635a57c412020-12-27 00:26:45 +0100267 var length = haystack.length;
268 for (var i = 0; i < length; i++) {
269 if (haystack[i] == needle) return true;
270 }
271 return false;
avm999634a2a5d52016-06-04 16:17:29 +0200272}
273
274window.addEventListener('load', init);