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... |
| 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 | }); |
avm99963 | 4a2a5d5 | 2016-06-04 16:17:29 +0200 | [diff] [blame] | 189 | } |
| 190 | |
avm99963 | ce257a9 | 2020-12-27 00:07:13 +0100 | [diff] [blame] | 191 | function saveOptions() { |
avm99963 | 5a57c41 | 2020-12-27 00:26:45 +0100 | [diff] [blame] | 192 | var languages = document.getElementById('languages'); |
| 193 | var options = { |
| 194 | uniquetab: '', |
| 195 | translateinto: {}, |
| 196 | }; |
avm99963 | 4a2a5d5 | 2016-06-04 16:17:29 +0200 | [diff] [blame] | 197 | |
avm99963 | 5a57c41 | 2020-12-27 00:26:45 +0100 | [diff] [blame] | 198 | options.uniquetab = radioSelected('uniquetab'); |
avm99963 | 4a2a5d5 | 2016-06-04 16:17:29 +0200 | [diff] [blame] | 199 | |
avm99963 | 5a57c41 | 2020-12-27 00:26:45 +0100 | [diff] [blame] | 200 | var selected_languages = sortable.toArray(); |
avm99963 | 4a2a5d5 | 2016-06-04 16:17:29 +0200 | [diff] [blame] | 201 | |
avm99963 | 5a57c41 | 2020-12-27 00:26:45 +0100 | [diff] [blame] | 202 | 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 | } |
avm99963 | 4a2a5d5 | 2016-06-04 16:17:29 +0200 | [diff] [blame] | 208 | |
avm99963 | 5a57c41 | 2020-12-27 00:26:45 +0100 | [diff] [blame] | 209 | chrome.storage.sync.set(options, function() { |
| 210 | var background = chrome.extension.getBackgroundPage(); |
avm99963 | 4a2a5d5 | 2016-06-04 16:17:29 +0200 | [diff] [blame] | 211 | |
avm99963 | 5a57c41 | 2020-12-27 00:26:45 +0100 | [diff] [blame] | 212 | background.translator_tab = false; |
| 213 | background.translator_window = false; |
| 214 | window.close(); |
| 215 | }); |
avm99963 | 4a2a5d5 | 2016-06-04 16:17:29 +0200 | [diff] [blame] | 216 | } |
| 217 | |
avm99963 | ce257a9 | 2020-12-27 00:07:13 +0100 | [diff] [blame] | 218 | function radioSelected(a) { |
avm99963 | 5a57c41 | 2020-12-27 00:26:45 +0100 | [diff] [blame] | 219 | var elements = document.getElementsByName(a); |
avm99963 | 4a2a5d5 | 2016-06-04 16:17:29 +0200 | [diff] [blame] | 220 | |
avm99963 | 5a57c41 | 2020-12-27 00:26:45 +0100 | [diff] [blame] | 221 | for (var i = 0; i < elements.length; i++) |
| 222 | if (elements[i].checked) return elements[i].value; |
avm99963 | 4a2a5d5 | 2016-06-04 16:17:29 +0200 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | function inArray(needle, haystack) { |
avm99963 | 5a57c41 | 2020-12-27 00:26:45 +0100 | [diff] [blame] | 226 | var length = haystack.length; |
| 227 | for (var i = 0; i < length; i++) { |
| 228 | if (haystack[i] == needle) return true; |
| 229 | } |
| 230 | return false; |
avm99963 | 4a2a5d5 | 2016-06-04 16:17:29 +0200 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | window.addEventListener('load', init); |