blob: a639c5691517c4a0356b89cc71988fd0e442f98d [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...
132 fetch('json/credits.json')
133 .then(res => res.json())
134 .then(json => {
135 var content = $('dialog#credits_dialog .content_area');
136 json.forEach(item => {
137 var div = document.createElement('div');
138 if (item.url) {
139 var a = document.createElement('a');
140 a.classList.add('homepage');
141 a.href = item.url;
142 a.target = '_blank';
143 a.textContent =
144 chrome.i18n.getMessage('options_credits_homepage');
145 div.append(a);
146 }
147
148 var h4 = document.createElement('h4');
149 h4.textContent = item.name;
150 div.append(h4);
151
152 if (item.author) {
153 var p = document.createElement('p');
154 p.classList.add('author');
155 p.textContent = chrome.i18n.getMessage('options_credits_by') +
156 ' ' + item.author +
157 (item.license ? ' - ' + item.license : '');
158 div.append(p);
159 }
160 content.append(div);
161 });
162
163 window.onhashchange = function() {
164 if (location.hash == '#credits') {
165 $('dialog#credits_dialog').showModal();
166 $('#credits_ok').focus();
167 }
168 };
169
170 if (location.hash == '#credits') {
171 $('dialog#credits_dialog').showModal();
172 $('#credits_ok').focus();
173 }
174
175 $('#credits_ok').addEventListener('click', _ => {
176 $('dialog#credits_dialog').close();
177 });
178 $('dialog#credits_dialog').addEventListener('close', _ => {
179 history.pushState(
180 '', document.title,
181 window.location.pathname + window.location.search);
182 });
183
184 // Print language list in the modal dialog
185 printListModal();
186 })
187 .catch(err => console.log(err));
188 });
avm999634a2a5d52016-06-04 16:17:29 +0200189}
190
avm99963ce257a92020-12-27 00:07:13 +0100191function saveOptions() {
avm999635a57c412020-12-27 00:26:45 +0100192 var languages = document.getElementById('languages');
193 var options = {
194 uniquetab: '',
195 translateinto: {},
196 };
avm999634a2a5d52016-06-04 16:17:29 +0200197
avm999635a57c412020-12-27 00:26:45 +0100198 options.uniquetab = radioSelected('uniquetab');
avm999634a2a5d52016-06-04 16:17:29 +0200199
avm999635a57c412020-12-27 00:26:45 +0100200 var selected_languages = sortable.toArray();
avm999634a2a5d52016-06-04 16:17:29 +0200201
avm999635a57c412020-12-27 00:26:45 +0100202 var i = 0;
203 for (var language_id in selected_languages) {
204 var language = selected_languages[language_id];
205 options.translateinto[i] = language;
206 i++;
207 }
avm999634a2a5d52016-06-04 16:17:29 +0200208
avm999635a57c412020-12-27 00:26:45 +0100209 chrome.storage.sync.set(options, function() {
210 var background = chrome.extension.getBackgroundPage();
avm999634a2a5d52016-06-04 16:17:29 +0200211
avm999635a57c412020-12-27 00:26:45 +0100212 background.translator_tab = false;
213 background.translator_window = false;
214 window.close();
215 });
avm999634a2a5d52016-06-04 16:17:29 +0200216}
217
avm99963ce257a92020-12-27 00:07:13 +0100218function radioSelected(a) {
avm999635a57c412020-12-27 00:26:45 +0100219 var elements = document.getElementsByName(a);
avm999634a2a5d52016-06-04 16:17:29 +0200220
avm999635a57c412020-12-27 00:26:45 +0100221 for (var i = 0; i < elements.length; i++)
222 if (elements[i].checked) return elements[i].value;
avm999634a2a5d52016-06-04 16:17:29 +0200223}
224
225function inArray(needle, haystack) {
avm999635a57c412020-12-27 00:26:45 +0100226 var length = haystack.length;
227 for (var i = 0; i < length; i++) {
228 if (haystack[i] == needle) return true;
229 }
230 return false;
avm999634a2a5d52016-06-04 16:17:29 +0200231}
232
233window.addEventListener('load', init);