blob: ca66004a040804a41328e4349353ad70c8f160d8 [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: {},
48 uniquetab: '',
49 };
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;
56 if (items.uniquetab === 'panel') $('#panel').checked = true;
avm999634a2a5d52016-06-04 16:17:29 +020057
avm999635a57c412020-12-27 00:26:45 +010058 // Add event listeners for certain buttons and links
59 $('#panelsflag').addEventListener('click', _ => {
60 event.preventDefault();
61 chrome.tabs.create({url: 'chrome://flags/#enable-panels'});
avm999634a2a5d52016-06-04 16:17:29 +020062 });
avm999635a57c412020-12-27 00:26:45 +010063
64 $('#save').addEventListener('click', _ => {
65 saveOptions();
66 });
67
68 // Print selected language list
69 var languages = items.translateinto;
70
71 if (languages) {
72 for (var language_id of Object.keys(languages)) {
73 var language = languages[language_id];
74 var el = document.createElement('li');
75 el.setAttribute('data-language', language);
76 el.setAttribute('data-id', language);
77 el.innerHTML = isoLangs[language]['name'] + ' (' +
78 isoLangs[language]['nativeName'] + ')' +
79 '<span data-language=\'' + language +
80 '\' class=\'delete\'>x</span>';
81 $('#languages').appendChild(el);
82 $('#languages li[data-language=' + language + '] .delete')
83 .addEventListener('click', function() {
84 $('#languages')
85 .removeChild($(
86 'li[data-language=' + this.getAttribute('data-language') +
87 ']'));
88 printListModal();
89 });
90 }
91 }
92
93 // Initiate Sortable
94 sortable = new Sortable($('#languages'), {animation: 150});
95
96 // Handling The Dialog
97 $('#languages_add').addEventListener('click', _ => {
98 $('dialog#languages_add_dialog').showModal();
99 $('#select_language').focus();
100 });
101
102 $('#languages_add_cancel').addEventListener('click', _ => {
103 $('dialog#languages_add_dialog').close();
104 });
105
106 $('#languages_add_ok').addEventListener('click', _ => {
107 var el = document.createElement('li');
108 var language = $('#select_language').value;
109 if (inArray(language, sortable.toArray())) {
110 return;
111 }
112 el.setAttribute('data-language', language);
113 el.setAttribute('data-id', language);
114 el.innerHTML = isoLangs[language]['name'] + ' (' +
115 isoLangs[language]['nativeName'] + ')' +
116 '<span data-language=\'' + language + '\' class=\'delete\'>x</span>';
117 $('#languages').appendChild(el);
118 var selection = $('#select_language option[value=' + language + ']');
119 selection.parentNode.removeChild(selection);
120 $('#languages li[data-id=' + language + '] .delete')
121 .addEventListener('click', function() {
122 $('#languages')
123 .removeChild(
124 $('li[data-language=' + this.getAttribute('data-language') +
125 ']'));
126 printListModal();
127 });
128 $('dialog').close();
129 });
130
131 // About credits...
avm999639bbb3a42020-12-29 03:29:44 +0100132 var normalCredits = fetch('json/credits.json').then(res => res.json());
133 var i18nCredits = fetch('json/i18n-credits.json').then(res => res.json());
134
135 Promise.all([normalCredits, i18nCredits])
136 .then(values => {
137 var credits = values[0];
138 var i18nCredits = values[1];
avm999635a57c412020-12-27 00:26:45 +0100139 var content = $('dialog#credits_dialog .content_area');
avm999639bbb3a42020-12-29 03:29:44 +0100140 credits.forEach(item => {
avm999635a57c412020-12-27 00:26:45 +0100141 var div = document.createElement('div');
avm999639bbb3a42020-12-29 03:29:44 +0100142 div.classList.add('entry');
avm999635a57c412020-12-27 00:26:45 +0100143 if (item.url) {
144 var a = document.createElement('a');
145 a.classList.add('homepage');
146 a.href = item.url;
147 a.target = '_blank';
148 a.textContent =
149 chrome.i18n.getMessage('options_credits_homepage');
150 div.append(a);
151 }
152
153 var h4 = document.createElement('h4');
154 h4.textContent = item.name;
155 div.append(h4);
156
157 if (item.author) {
158 var p = document.createElement('p');
159 p.classList.add('author');
160 p.textContent = chrome.i18n.getMessage('options_credits_by') +
161 ' ' + item.author +
162 (item.license ? ' - ' + item.license : '');
163 div.append(p);
164 }
165 content.append(div);
166 });
167
avm999639bbb3a42020-12-29 03:29:44 +0100168 var cList = document.getElementById('translators');
169 i18nCredits.forEach(contributor => {
170 var li = document.createElement('li');
171 var languages = [];
172 if (contributor.languages) {
173 contributor.languages.forEach(lang => {
174 languages.push(lang.name || 'undefined');
175 });
176 }
177
178 var name = document.createElement('span');
179 name.classList.add('name');
180 name.textContent = contributor.name || 'undefined';
181 li.append(name);
182
183 if (languages.length > 0) {
184 var languages =
185 document.createTextNode(': ' + languages.join(', '));
186 li.append(languages);
187 }
188
189 cList.append(li);
190 });
191
avm999635a57c412020-12-27 00:26:45 +0100192 window.onhashchange = function() {
193 if (location.hash == '#credits') {
avm999639bbb3a42020-12-29 03:29:44 +0100194 var credits = document.getElementById('credits_dialog');
195 credits.showModal();
196 credits.querySelector('.scrollable').scrollTo(0, 0);
avm999635a57c412020-12-27 00:26:45 +0100197 $('#credits_ok').focus();
198 }
199 };
200
201 if (location.hash == '#credits') {
202 $('dialog#credits_dialog').showModal();
203 $('#credits_ok').focus();
204 }
205
206 $('#credits_ok').addEventListener('click', _ => {
207 $('dialog#credits_dialog').close();
208 });
209 $('dialog#credits_dialog').addEventListener('close', _ => {
210 history.pushState(
211 '', document.title,
212 window.location.pathname + window.location.search);
213 });
214
215 // Print language list in the modal dialog
216 printListModal();
217 })
218 .catch(err => console.log(err));
219 });
avm999634a2a5d52016-06-04 16:17:29 +0200220}
221
avm99963ce257a92020-12-27 00:07:13 +0100222function saveOptions() {
avm999635a57c412020-12-27 00:26:45 +0100223 var languages = document.getElementById('languages');
224 var options = {
225 uniquetab: '',
226 translateinto: {},
227 };
avm999634a2a5d52016-06-04 16:17:29 +0200228
avm999635a57c412020-12-27 00:26:45 +0100229 options.uniquetab = radioSelected('uniquetab');
avm999634a2a5d52016-06-04 16:17:29 +0200230
avm999635a57c412020-12-27 00:26:45 +0100231 var selected_languages = sortable.toArray();
avm999634a2a5d52016-06-04 16:17:29 +0200232
avm999635a57c412020-12-27 00:26:45 +0100233 var i = 0;
234 for (var language_id in selected_languages) {
235 var language = selected_languages[language_id];
236 options.translateinto[i] = language;
237 i++;
238 }
avm999634a2a5d52016-06-04 16:17:29 +0200239
avm999635a57c412020-12-27 00:26:45 +0100240 chrome.storage.sync.set(options, function() {
241 var background = chrome.extension.getBackgroundPage();
avm999634a2a5d52016-06-04 16:17:29 +0200242
avm999635a57c412020-12-27 00:26:45 +0100243 background.translator_tab = false;
244 background.translator_window = false;
245 window.close();
246 });
avm999634a2a5d52016-06-04 16:17:29 +0200247}
248
avm99963ce257a92020-12-27 00:07:13 +0100249function radioSelected(a) {
avm999635a57c412020-12-27 00:26:45 +0100250 var elements = document.getElementsByName(a);
avm999634a2a5d52016-06-04 16:17:29 +0200251
avm999635a57c412020-12-27 00:26:45 +0100252 for (var i = 0; i < elements.length; i++)
253 if (elements[i].checked) return elements[i].value;
avm999634a2a5d52016-06-04 16:17:29 +0200254}
255
256function inArray(needle, haystack) {
avm999635a57c412020-12-27 00:26:45 +0100257 var length = haystack.length;
258 for (var i = 0; i < length; i++) {
259 if (haystack[i] == needle) return true;
260 }
261 return false;
avm999634a2a5d52016-06-04 16:17:29 +0200262}
263
264window.addEventListener('load', init);