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: {}, |
avm99963 | 8824b8a | 2021-01-10 18:25:12 +0100 | [diff] [blame] | 48 | uniquetab: 'popup', |
avm99963 | 5a57c41 | 2020-12-27 00:26:45 +0100 | [diff] [blame] | 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; |
avm99963 | 8824b8a | 2021-01-10 18:25:12 +0100 | [diff] [blame] | 56 | if (items.uniquetab === 'panel' || items.uniquetab === 'popup') |
| 57 | $('#popup').checked = true; |
avm99963 | 4a2a5d5 | 2016-06-04 16:17:29 +0200 | [diff] [blame] | 58 | |
avm99963 | 5a57c41 | 2020-12-27 00:26:45 +0100 | [diff] [blame] | 59 | // Add event listeners for certain buttons and links |
avm99963 | 5a57c41 | 2020-12-27 00:26:45 +0100 | [diff] [blame] | 60 | $('#save').addEventListener('click', _ => { |
avm99963 | a003641 | 2020-12-29 13:39:05 +0100 | [diff] [blame] | 61 | saveOptions(true); |
| 62 | }); |
| 63 | |
| 64 | // Save automatically |
| 65 | $all('input[type="radio"]').forEach(radio => { |
| 66 | radio.addEventListener('change', _ => { |
| 67 | saveOptions(); |
| 68 | }); |
avm99963 | 5a57c41 | 2020-12-27 00:26:45 +0100 | [diff] [blame] | 69 | }); |
| 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(); |
avm99963 | a003641 | 2020-12-29 13:39:05 +0100 | [diff] [blame] | 92 | // Save automatically |
| 93 | saveOptions(); |
avm99963 | 5a57c41 | 2020-12-27 00:26:45 +0100 | [diff] [blame] | 94 | }); |
| 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(); |
avm99963 | a003641 | 2020-12-29 13:39:05 +0100 | [diff] [blame] | 132 | // Save automatically |
| 133 | saveOptions(); |
avm99963 | 5a57c41 | 2020-12-27 00:26:45 +0100 | [diff] [blame] | 134 | }); |
| 135 | $('dialog').close(); |
avm99963 | a003641 | 2020-12-29 13:39:05 +0100 | [diff] [blame] | 136 | |
| 137 | // Save automatically |
| 138 | saveOptions(); |
avm99963 | 5a57c41 | 2020-12-27 00:26:45 +0100 | [diff] [blame] | 139 | }); |
| 140 | |
| 141 | // About credits... |
avm99963 | 9bbb3a4 | 2020-12-29 03:29:44 +0100 | [diff] [blame] | 142 | 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]; |
avm99963 | 5a57c41 | 2020-12-27 00:26:45 +0100 | [diff] [blame] | 149 | var content = $('dialog#credits_dialog .content_area'); |
avm99963 | 9bbb3a4 | 2020-12-29 03:29:44 +0100 | [diff] [blame] | 150 | credits.forEach(item => { |
avm99963 | 5a57c41 | 2020-12-27 00:26:45 +0100 | [diff] [blame] | 151 | var div = document.createElement('div'); |
avm99963 | 9bbb3a4 | 2020-12-29 03:29:44 +0100 | [diff] [blame] | 152 | div.classList.add('entry'); |
avm99963 | 5a57c41 | 2020-12-27 00:26:45 +0100 | [diff] [blame] | 153 | 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 | |
avm99963 | 9bbb3a4 | 2020-12-29 03:29:44 +0100 | [diff] [blame] | 178 | 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 | |
avm99963 | 5a57c41 | 2020-12-27 00:26:45 +0100 | [diff] [blame] | 202 | window.onhashchange = function() { |
| 203 | if (location.hash == '#credits') { |
avm99963 | 9bbb3a4 | 2020-12-29 03:29:44 +0100 | [diff] [blame] | 204 | var credits = document.getElementById('credits_dialog'); |
| 205 | credits.showModal(); |
| 206 | credits.querySelector('.scrollable').scrollTo(0, 0); |
avm99963 | 5a57c41 | 2020-12-27 00:26:45 +0100 | [diff] [blame] | 207 | $('#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 | }); |
avm99963 | 4a2a5d5 | 2016-06-04 16:17:29 +0200 | [diff] [blame] | 230 | } |
| 231 | |
avm99963 | a003641 | 2020-12-29 13:39:05 +0100 | [diff] [blame] | 232 | function saveOptions(close = false) { |
avm99963 | 5a57c41 | 2020-12-27 00:26:45 +0100 | [diff] [blame] | 233 | var languages = document.getElementById('languages'); |
| 234 | var options = { |
| 235 | uniquetab: '', |
| 236 | translateinto: {}, |
| 237 | }; |
avm99963 | 4a2a5d5 | 2016-06-04 16:17:29 +0200 | [diff] [blame] | 238 | |
avm99963 | 5a57c41 | 2020-12-27 00:26:45 +0100 | [diff] [blame] | 239 | options.uniquetab = radioSelected('uniquetab'); |
avm99963 | 4a2a5d5 | 2016-06-04 16:17:29 +0200 | [diff] [blame] | 240 | |
avm99963 | 5a57c41 | 2020-12-27 00:26:45 +0100 | [diff] [blame] | 241 | var selected_languages = sortable.toArray(); |
avm99963 | 4a2a5d5 | 2016-06-04 16:17:29 +0200 | [diff] [blame] | 242 | |
avm99963 | 5a57c41 | 2020-12-27 00:26:45 +0100 | [diff] [blame] | 243 | 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 | } |
avm99963 | 4a2a5d5 | 2016-06-04 16:17:29 +0200 | [diff] [blame] | 249 | |
avm99963 | 5a57c41 | 2020-12-27 00:26:45 +0100 | [diff] [blame] | 250 | chrome.storage.sync.set(options, function() { |
| 251 | var background = chrome.extension.getBackgroundPage(); |
avm99963 | 4a2a5d5 | 2016-06-04 16:17:29 +0200 | [diff] [blame] | 252 | |
avm99963 | 5a57c41 | 2020-12-27 00:26:45 +0100 | [diff] [blame] | 253 | background.translator_tab = false; |
| 254 | background.translator_window = false; |
avm99963 | a003641 | 2020-12-29 13:39:05 +0100 | [diff] [blame] | 255 | if (close) window.close(); |
avm99963 | 5a57c41 | 2020-12-27 00:26:45 +0100 | [diff] [blame] | 256 | }); |
avm99963 | 4a2a5d5 | 2016-06-04 16:17:29 +0200 | [diff] [blame] | 257 | } |
| 258 | |
avm99963 | ce257a9 | 2020-12-27 00:07:13 +0100 | [diff] [blame] | 259 | function radioSelected(a) { |
avm99963 | 5a57c41 | 2020-12-27 00:26:45 +0100 | [diff] [blame] | 260 | var elements = document.getElementsByName(a); |
avm99963 | 4a2a5d5 | 2016-06-04 16:17:29 +0200 | [diff] [blame] | 261 | |
avm99963 | 5a57c41 | 2020-12-27 00:26:45 +0100 | [diff] [blame] | 262 | for (var i = 0; i < elements.length; i++) |
| 263 | if (elements[i].checked) return elements[i].value; |
avm99963 | 4a2a5d5 | 2016-06-04 16:17:29 +0200 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | function inArray(needle, haystack) { |
avm99963 | 5a57c41 | 2020-12-27 00:26:45 +0100 | [diff] [blame] | 267 | var length = haystack.length; |
| 268 | for (var i = 0; i < length; i++) { |
| 269 | if (haystack[i] == needle) return true; |
| 270 | } |
| 271 | return false; |
avm99963 | 4a2a5d5 | 2016-06-04 16:17:29 +0200 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | window.addEventListener('load', init); |