avm99963 | 4a2a5d5 | 2016-06-04 16:17:29 +0200 | [diff] [blame] | 1 | function $(selector) { |
avm99963 | 5a57c41 | 2020-12-27 00:26:45 +0100 | [diff] [blame] | 2 | return document.querySelector(selector); |
avm99963 | 4a2a5d5 | 2016-06-04 16:17:29 +0200 | [diff] [blame] | 3 | } |
| 4 | |
| 5 | function $all(selector) { |
avm99963 | 5a57c41 | 2020-12-27 00:26:45 +0100 | [diff] [blame] | 6 | return document.querySelectorAll(selector); |
avm99963 | 4a2a5d5 | 2016-06-04 16:17:29 +0200 | [diff] [blame] | 7 | } |
| 8 | |
| 9 | function isEmpty(obj) { |
avm99963 | 5a57c41 | 2020-12-27 00:26:45 +0100 | [diff] [blame] | 10 | return Object.keys(obj).length === 0; |
avm99963 | 4a2a5d5 | 2016-06-04 16:17:29 +0200 | [diff] [blame] | 11 | } |
| 12 | |
| 13 | function i18n() { |
avm99963 | 5a57c41 | 2020-12-27 00:26:45 +0100 | [diff] [blame] | 14 | $all('[data-i18n]').forEach(el => { |
| 15 | el.innerHTML = |
| 16 | chrome.i18n.getMessage('options_' + el.getAttribute('data-i18n')); |
| 17 | }); |
avm99963 | 4a2a5d5 | 2016-06-04 16:17:29 +0200 | [diff] [blame] | 18 | } |
| 19 | |
avm99963 | ce257a9 | 2020-12-27 00:07:13 +0100 | [diff] [blame] | 20 | function printListModal() { |
avm99963 | 5a57c41 | 2020-12-27 00:26:45 +0100 | [diff] [blame] | 21 | $('#select_language').textContent = ''; |
| 22 | var heysortable = sortable.toArray(); |
avm99963 | 0f96116 | 2020-12-27 13:12:27 +0100 | [diff] [blame] | 23 | 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)) { |
avm99963 | 5a57c41 | 2020-12-27 00:26:45 +0100 | [diff] [blame] | 33 | var el = document.createElement('option'); |
avm99963 | 0f96116 | 2020-12-27 13:12:27 +0100 | [diff] [blame] | 34 | el.setAttribute('value', language['code']); |
| 35 | el.textContent = language['name'] + ' (' + language['nativeName'] + ')'; |
avm99963 | 5a57c41 | 2020-12-27 00:26:45 +0100 | [diff] [blame] | 36 | $('#select_language').appendChild(el); |
avm99963 | 4a2a5d5 | 2016-06-04 16:17:29 +0200 | [diff] [blame] | 37 | } |
avm99963 | 0f96116 | 2020-12-27 13:12:27 +0100 | [diff] [blame] | 38 | }); |
avm99963 | 4a2a5d5 | 2016-06-04 16:17:29 +0200 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | function init() { |
avm99963 | 5a57c41 | 2020-12-27 00:26:45 +0100 | [diff] [blame] | 42 | 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 | } |
avm99963 | 4a2a5d5 | 2016-06-04 16:17:29 +0200 | [diff] [blame] | 52 | |
avm99963 | 5a57c41 | 2020-12-27 00:26:45 +0100 | [diff] [blame] | 53 | // 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; |
avm99963 | 4a2a5d5 | 2016-06-04 16:17:29 +0200 | [diff] [blame] | 57 | |
avm99963 | 5a57c41 | 2020-12-27 00:26:45 +0100 | [diff] [blame] | 58 | // Add event listeners for certain buttons and links |
| 59 | $('#panelsflag').addEventListener('click', _ => { |
| 60 | event.preventDefault(); |
| 61 | chrome.tabs.create({url: 'chrome://flags/#enable-panels'}); |
avm99963 | 4a2a5d5 | 2016-06-04 16:17:29 +0200 | [diff] [blame] | 62 | }); |
avm99963 | 5a57c41 | 2020-12-27 00:26:45 +0100 | [diff] [blame] | 63 | |
| 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... |
avm99963 | 9bbb3a4 | 2020-12-29 03:29:44 +0100 | [diff] [blame] | 132 | 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]; |
avm99963 | 5a57c41 | 2020-12-27 00:26:45 +0100 | [diff] [blame] | 139 | var content = $('dialog#credits_dialog .content_area'); |
avm99963 | 9bbb3a4 | 2020-12-29 03:29:44 +0100 | [diff] [blame] | 140 | credits.forEach(item => { |
avm99963 | 5a57c41 | 2020-12-27 00:26:45 +0100 | [diff] [blame] | 141 | var div = document.createElement('div'); |
avm99963 | 9bbb3a4 | 2020-12-29 03:29:44 +0100 | [diff] [blame] | 142 | div.classList.add('entry'); |
avm99963 | 5a57c41 | 2020-12-27 00:26:45 +0100 | [diff] [blame] | 143 | 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 | |
avm99963 | 9bbb3a4 | 2020-12-29 03:29:44 +0100 | [diff] [blame] | 168 | 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 | |
avm99963 | 5a57c41 | 2020-12-27 00:26:45 +0100 | [diff] [blame] | 192 | window.onhashchange = function() { |
| 193 | if (location.hash == '#credits') { |
avm99963 | 9bbb3a4 | 2020-12-29 03:29:44 +0100 | [diff] [blame] | 194 | var credits = document.getElementById('credits_dialog'); |
| 195 | credits.showModal(); |
| 196 | credits.querySelector('.scrollable').scrollTo(0, 0); |
avm99963 | 5a57c41 | 2020-12-27 00:26:45 +0100 | [diff] [blame] | 197 | $('#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 | }); |
avm99963 | 4a2a5d5 | 2016-06-04 16:17:29 +0200 | [diff] [blame] | 220 | } |
| 221 | |
avm99963 | ce257a9 | 2020-12-27 00:07:13 +0100 | [diff] [blame] | 222 | function saveOptions() { |
avm99963 | 5a57c41 | 2020-12-27 00:26:45 +0100 | [diff] [blame] | 223 | var languages = document.getElementById('languages'); |
| 224 | var options = { |
| 225 | uniquetab: '', |
| 226 | translateinto: {}, |
| 227 | }; |
avm99963 | 4a2a5d5 | 2016-06-04 16:17:29 +0200 | [diff] [blame] | 228 | |
avm99963 | 5a57c41 | 2020-12-27 00:26:45 +0100 | [diff] [blame] | 229 | options.uniquetab = radioSelected('uniquetab'); |
avm99963 | 4a2a5d5 | 2016-06-04 16:17:29 +0200 | [diff] [blame] | 230 | |
avm99963 | 5a57c41 | 2020-12-27 00:26:45 +0100 | [diff] [blame] | 231 | var selected_languages = sortable.toArray(); |
avm99963 | 4a2a5d5 | 2016-06-04 16:17:29 +0200 | [diff] [blame] | 232 | |
avm99963 | 5a57c41 | 2020-12-27 00:26:45 +0100 | [diff] [blame] | 233 | 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 | } |
avm99963 | 4a2a5d5 | 2016-06-04 16:17:29 +0200 | [diff] [blame] | 239 | |
avm99963 | 5a57c41 | 2020-12-27 00:26:45 +0100 | [diff] [blame] | 240 | chrome.storage.sync.set(options, function() { |
| 241 | var background = chrome.extension.getBackgroundPage(); |
avm99963 | 4a2a5d5 | 2016-06-04 16:17:29 +0200 | [diff] [blame] | 242 | |
avm99963 | 5a57c41 | 2020-12-27 00:26:45 +0100 | [diff] [blame] | 243 | background.translator_tab = false; |
| 244 | background.translator_window = false; |
| 245 | window.close(); |
| 246 | }); |
avm99963 | 4a2a5d5 | 2016-06-04 16:17:29 +0200 | [diff] [blame] | 247 | } |
| 248 | |
avm99963 | ce257a9 | 2020-12-27 00:07:13 +0100 | [diff] [blame] | 249 | function radioSelected(a) { |
avm99963 | 5a57c41 | 2020-12-27 00:26:45 +0100 | [diff] [blame] | 250 | var elements = document.getElementsByName(a); |
avm99963 | 4a2a5d5 | 2016-06-04 16:17:29 +0200 | [diff] [blame] | 251 | |
avm99963 | 5a57c41 | 2020-12-27 00:26:45 +0100 | [diff] [blame] | 252 | for (var i = 0; i < elements.length; i++) |
| 253 | if (elements[i].checked) return elements[i].value; |
avm99963 | 4a2a5d5 | 2016-06-04 16:17:29 +0200 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | function inArray(needle, haystack) { |
avm99963 | 5a57c41 | 2020-12-27 00:26:45 +0100 | [diff] [blame] | 257 | var length = haystack.length; |
| 258 | for (var i = 0; i < length; i++) { |
| 259 | if (haystack[i] == needle) return true; |
| 260 | } |
| 261 | return false; |
avm99963 | 4a2a5d5 | 2016-06-04 16:17:29 +0200 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | window.addEventListener('load', init); |