Adrià Vilanova Martínez | a197d86 | 2022-05-27 17:33:20 +0200 | [diff] [blame] | 1 | import {convertLanguages, isoLangs} from './common/consts.js'; |
| 2 | |
avm99963 | ce257a9 | 2020-12-27 00:07:13 +0100 | [diff] [blame] | 3 | var array_elements = [], translator_tab = null, translator_window = null; |
avm99963 | 4a2a5d5 | 2016-06-04 16:17:29 +0200 | [diff] [blame] | 4 | |
| 5 | function isEmpty(obj) { |
avm99963 | 5a57c41 | 2020-12-27 00:26:45 +0100 | [diff] [blame] | 6 | return Object.keys(obj).length === 0; |
avm99963 | 4a2a5d5 | 2016-06-04 16:17:29 +0200 | [diff] [blame] | 7 | } |
| 8 | |
avm99963 | f65e9bf | 2020-12-29 12:57:16 +0100 | [diff] [blame] | 9 | function inObject(hayStack, el) { |
| 10 | for (var i of Object.keys(hayStack)) { |
| 11 | if (hayStack[i] == el) return true; |
| 12 | } |
| 13 | return false; |
| 14 | } |
| 15 | |
avm99963 | ce257a9 | 2020-12-27 00:07:13 +0100 | [diff] [blame] | 16 | function getTranslationUrl(lang, text) { |
avm99963 | 5a57c41 | 2020-12-27 00:26:45 +0100 | [diff] [blame] | 17 | var params = new URLSearchParams({ |
| 18 | sl: 'auto', |
| 19 | tl: lang, |
| 20 | text: text, |
| 21 | op: 'translate', |
| 22 | }); |
| 23 | return 'https://translate.google.com/?' + params.toString(); |
avm99963 | ce257a9 | 2020-12-27 00:07:13 +0100 | [diff] [blame] | 24 | } |
| 25 | |
| 26 | function translationClick(info, tab) { |
avm99963 | 5a57c41 | 2020-12-27 00:26:45 +0100 | [diff] [blame] | 27 | chrome.storage.sync.get('uniquetab', items => { |
| 28 | var url = getTranslationUrl( |
| 29 | array_elements[info.menuItemId]['langCode'], info.selectionText); |
| 30 | var settings_tab = {url}; |
| 31 | if (translator_tab && items.uniquetab == 'yep') { |
| 32 | chrome.tabs.update(translator_tab, settings_tab, tab => { |
| 33 | chrome.tabs.highlight( |
| 34 | { |
| 35 | windowId: tab.windowId, |
| 36 | tabs: tab.index, |
| 37 | }, |
| 38 | _ => { |
| 39 | chrome.windows.update(tab.windowId, { |
| 40 | focused: true, |
| 41 | }); |
| 42 | }); |
| 43 | }); |
avm99963 | 8824b8a | 2021-01-10 18:25:12 +0100 | [diff] [blame] | 44 | } else if (items.uniquetab == 'panel' || items.uniquetab == 'popup') { |
avm99963 | 5a57c41 | 2020-12-27 00:26:45 +0100 | [diff] [blame] | 45 | chrome.windows.create( |
| 46 | { |
avm99963 | 8824b8a | 2021-01-10 18:25:12 +0100 | [diff] [blame] | 47 | type: 'popup', |
avm99963 | 5a57c41 | 2020-12-27 00:26:45 +0100 | [diff] [blame] | 48 | url, |
| 49 | width: 1000, |
| 50 | height: 382, |
| 51 | }, |
| 52 | function(tab) { |
| 53 | translator_window = tab.windowId; |
| 54 | translator_tab = tab.id; |
| 55 | chrome.windows.onRemoved.addListener(function(windowId) { |
| 56 | if (windowId == translator_window) { |
| 57 | translator_window = null; |
| 58 | translator_tab = null; |
| 59 | } |
| 60 | }); |
| 61 | }); |
| 62 | } else { |
| 63 | chrome.tabs.create(settings_tab, function(tab) { |
| 64 | translator_window = tab.windowId; |
| 65 | translator_tab = tab.id; |
| 66 | chrome.tabs.onRemoved.addListener(function(tabId, removeInfo) { |
| 67 | if (tabId == translator_tab) { |
| 68 | translator_window = null; |
| 69 | translator_tab = null; |
| 70 | } |
| 71 | }); |
| 72 | }); |
| 73 | } |
| 74 | }); |
avm99963 | 4a2a5d5 | 2016-06-04 16:17:29 +0200 | [diff] [blame] | 75 | } |
| 76 | |
avm99963 | ce257a9 | 2020-12-27 00:07:13 +0100 | [diff] [blame] | 77 | function openOptionsPage() { |
avm99963 | 5a57c41 | 2020-12-27 00:26:45 +0100 | [diff] [blame] | 78 | if (chrome.runtime.openOptionsPage) { |
| 79 | // New way to open options pages, if supported (Chrome 42+). |
| 80 | chrome.runtime.openOptionsPage(); |
| 81 | } else { |
| 82 | chrome.tabs.create( |
| 83 | { |
| 84 | 'url': 'chrome-extension://' + |
| 85 | chrome.i18n.getMessage('@@extension_id') + '/options.html', |
| 86 | 'active': true |
| 87 | }, |
| 88 | tab => { |
| 89 | chrome.windows.update(tab.windowId, {focused: true}); |
| 90 | }); |
| 91 | } |
avm99963 | 4a2a5d5 | 2016-06-04 16:17:29 +0200 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | function createmenus() { |
avm99963 | 5a57c41 | 2020-12-27 00:26:45 +0100 | [diff] [blame] | 95 | chrome.storage.sync.get('translateinto', function(items) { |
| 96 | chrome.contextMenus.removeAll(); |
avm99963 | 4a2a5d5 | 2016-06-04 16:17:29 +0200 | [diff] [blame] | 97 | |
avm99963 | 5a57c41 | 2020-12-27 00:26:45 +0100 | [diff] [blame] | 98 | var count = 0, singleone = true; |
avm99963 | 4a2a5d5 | 2016-06-04 16:17:29 +0200 | [diff] [blame] | 99 | |
avm99963 | 5a57c41 | 2020-12-27 00:26:45 +0100 | [diff] [blame] | 100 | for (var language of Object.keys(items.translateinto)) { |
| 101 | if (count == 0) { |
| 102 | count++; |
| 103 | } else { |
| 104 | singleone = false; |
| 105 | break; |
| 106 | } |
| 107 | } |
avm99963 | 4a2a5d5 | 2016-06-04 16:17:29 +0200 | [diff] [blame] | 108 | |
avm99963 | 5a57c41 | 2020-12-27 00:26:45 +0100 | [diff] [blame] | 109 | if (singleone) { |
| 110 | for (var language_id of Object.keys(items.translateinto)) { |
| 111 | var language = items.translateinto[language_id]; |
| 112 | var languagem = isoLangs[language]; |
avm99963 | f65e9bf | 2020-12-29 12:57:16 +0100 | [diff] [blame] | 113 | if (languagem === undefined) { |
| 114 | console.error(language + ' doesn\'t exist!'); |
| 115 | continue; |
| 116 | } |
avm99963 | 5a57c41 | 2020-12-27 00:26:45 +0100 | [diff] [blame] | 117 | var id = chrome.contextMenus.create({ |
| 118 | 'id': 'tr_single_parent', |
| 119 | 'title': chrome.i18n.getMessage('contextmenu_title2', languagem.name), |
| 120 | 'contexts': ['selection'], |
| 121 | }); |
| 122 | array_elements[id] = new Array(); |
| 123 | array_elements[id]['langCode'] = language; |
| 124 | } |
| 125 | } else { |
| 126 | var parentEl = chrome.contextMenus.create({ |
| 127 | 'id': 'parent', |
| 128 | 'title': chrome.i18n.getMessage('contextmenu_title'), |
| 129 | 'contexts': ['selection'] |
| 130 | }); |
| 131 | for (var language_id of Object.keys(items.translateinto)) { |
| 132 | var language = items.translateinto[language_id]; |
| 133 | var languagem = isoLangs[language]; |
avm99963 | f65e9bf | 2020-12-29 12:57:16 +0100 | [diff] [blame] | 134 | if (languagem === undefined) { |
| 135 | console.error(language + ' doesn\'t exist!'); |
| 136 | continue; |
| 137 | } |
avm99963 | 5a57c41 | 2020-12-27 00:26:45 +0100 | [diff] [blame] | 138 | var title = languagem.name + ' (' + languagem.nativeName + ')'; |
| 139 | var id = chrome.contextMenus.create({ |
| 140 | 'id': 'tr_language_' + language, |
| 141 | 'title': title, |
| 142 | 'parentId': parentEl, |
| 143 | 'contexts': ['selection'] |
| 144 | }); |
| 145 | array_elements[id] = new Array(); |
| 146 | array_elements[id]['langCode'] = language; |
| 147 | } |
| 148 | chrome.contextMenus.create({ |
| 149 | 'id': 'tr_separator', |
| 150 | 'type': 'separator', |
| 151 | 'parentId': parentEl, |
| 152 | 'contexts': ['selection'] |
| 153 | }); |
| 154 | chrome.contextMenus.create({ |
| 155 | 'id': 'tr_options', |
| 156 | 'title': chrome.i18n.getMessage('contextmenu_edit'), |
| 157 | 'parentId': parentEl, |
| 158 | 'contexts': ['selection'] |
| 159 | }); |
| 160 | } |
| 161 | }); |
avm99963 | 4a2a5d5 | 2016-06-04 16:17:29 +0200 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | chrome.runtime.onInstalled.addListener(function(details) { |
avm99963 | 5a57c41 | 2020-12-27 00:26:45 +0100 | [diff] [blame] | 165 | chrome.storage.sync.get(null, function(items) { |
| 166 | if (details.reason == 'install') { |
| 167 | if (isEmpty(items)) { |
avm99963 | 8824b8a | 2021-01-10 18:25:12 +0100 | [diff] [blame] | 168 | var settings = {'translateinto': {}, 'uniquetab': 'popup'}, |
avm99963 | f65e9bf | 2020-12-29 12:57:16 +0100 | [diff] [blame] | 169 | default_language_1 = |
| 170 | chrome.i18n.getMessage('@@ui_locale').replace('_', '-'), |
| 171 | default_language_2 = |
avm99963 | 5a57c41 | 2020-12-27 00:26:45 +0100 | [diff] [blame] | 172 | chrome.i18n.getMessage('@@ui_locale').split('_')[0]; |
avm99963 | ce257a9 | 2020-12-27 00:07:13 +0100 | [diff] [blame] | 173 | |
avm99963 | f65e9bf | 2020-12-29 12:57:16 +0100 | [diff] [blame] | 174 | if (isoLangs[default_language_1] != undefined) |
| 175 | settings.translateinto['0'] = default_language_1; |
| 176 | else if (isoLangs[default_language_2] != undefined) |
| 177 | settings.translateinto['0'] = default_language_2; |
avm99963 | ce257a9 | 2020-12-27 00:07:13 +0100 | [diff] [blame] | 178 | |
avm99963 | 5a57c41 | 2020-12-27 00:26:45 +0100 | [diff] [blame] | 179 | chrome.storage.sync.set(settings, function() { |
| 180 | chrome.notifications.create('install', { |
| 181 | type: 'basic', |
| 182 | iconUrl: 'icons/translate-128.png', |
| 183 | title: chrome.i18n.getMessage('notification_install_title'), |
| 184 | message: chrome.i18n.getMessage('notification_install_message'), |
| 185 | isClickable: true |
| 186 | }); |
| 187 | }); |
| 188 | } |
| 189 | } |
| 190 | if (details.reason == 'update') { |
| 191 | var version = details.previousVersion.split('.'); |
avm99963 | ce257a9 | 2020-12-27 00:07:13 +0100 | [diff] [blame] | 192 | |
avm99963 | 5a57c41 | 2020-12-27 00:26:45 +0100 | [diff] [blame] | 193 | // Updating from a version previous to v0.6 |
| 194 | if (version[0] == '0' && version[1] < '6') { |
| 195 | var settings = { |
| 196 | languages: {}, |
| 197 | uniquetab: '', |
| 198 | }; |
| 199 | var default_language = |
| 200 | chrome.i18n.getMessage('@@ui_locale').split('_')[0]; |
avm99963 | ce257a9 | 2020-12-27 00:07:13 +0100 | [diff] [blame] | 201 | |
avm99963 | f65e9bf | 2020-12-29 12:57:16 +0100 | [diff] [blame] | 202 | if (isoLangs[default_language] != undefined) |
avm99963 | 5a57c41 | 2020-12-27 00:26:45 +0100 | [diff] [blame] | 203 | settings.languages[default_language] = default_language; |
avm99963 | ce257a9 | 2020-12-27 00:07:13 +0100 | [diff] [blame] | 204 | |
Adrià Vilanova Martínez | 203bc43 | 2021-06-08 16:35:00 +0200 | [diff] [blame] | 205 | chrome.storage.sync.set(settings); |
avm99963 | 5a57c41 | 2020-12-27 00:26:45 +0100 | [diff] [blame] | 206 | } |
avm99963 | ce257a9 | 2020-12-27 00:07:13 +0100 | [diff] [blame] | 207 | |
avm99963 | 5a57c41 | 2020-12-27 00:26:45 +0100 | [diff] [blame] | 208 | // Updating from a version previous to v0.7 |
| 209 | if (version[0] == '0' && version[1] < '7') { |
| 210 | items.translateinto = {}; |
| 211 | var i = 0; |
| 212 | for (var language in items.languages) { |
| 213 | items.translateinto[i] = items.languages[language]; |
| 214 | i++; |
| 215 | } |
| 216 | delete items.languages; |
Adrià Vilanova Martínez | 203bc43 | 2021-06-08 16:35:00 +0200 | [diff] [blame] | 217 | chrome.storage.sync.set(items); |
avm99963 | 5a57c41 | 2020-12-27 00:26:45 +0100 | [diff] [blame] | 218 | } |
avm99963 | f65e9bf | 2020-12-29 12:57:16 +0100 | [diff] [blame] | 219 | |
| 220 | // Remove non-existent languages or change with correct language code |
| 221 | if (items.translateinto) { |
| 222 | var modified = false; |
| 223 | for (var language_id of Object.keys(items.translateinto)) { |
| 224 | var language = items.translateinto[language_id]; |
| 225 | if (isoLangs[language] === undefined) { |
| 226 | if (convertLanguages[language] === undefined) { |
| 227 | // The language doesn't exist |
| 228 | console.log( |
| 229 | 'Deleting ' + language + |
| 230 | ' from items.translateinto because it doesn\'t exist.'); |
| 231 | delete items.translateinto[language_id]; |
| 232 | } else { |
| 233 | // The language doesn't exist but a known replacement is known |
| 234 | var newLanguage = convertLanguages[language]; |
| 235 | console.log('Replacing ' + language + ' with ' + newLanguage); |
| 236 | |
| 237 | // If the converted language is already on the list, just remove |
| 238 | // the wrong language, otherwise convert the language |
| 239 | if (inObject(items.translateinto, newLanguage)) |
| 240 | delete items.translateinto[language_id]; |
| 241 | else |
| 242 | items.translateinto[language_id] = newLanguage; |
| 243 | } |
| 244 | modified = true; |
| 245 | } |
| 246 | } |
| 247 | if (modified) chrome.storage.sync.set(items); |
| 248 | } else { |
| 249 | console.log('items.translateinto doesn\'t exist: let\'s create it.'); |
| 250 | items['translateinto'] = {}; |
| 251 | chrome.storage.sync.set(items); |
| 252 | } |
avm99963 | 5a57c41 | 2020-12-27 00:26:45 +0100 | [diff] [blame] | 253 | } |
| 254 | }); |
avm99963 | 4a2a5d5 | 2016-06-04 16:17:29 +0200 | [diff] [blame] | 255 | }); |
| 256 | |
avm99963 | ce257a9 | 2020-12-27 00:07:13 +0100 | [diff] [blame] | 257 | chrome.storage.onChanged.addListener((changes, areaName) => { |
avm99963 | 5a57c41 | 2020-12-27 00:26:45 +0100 | [diff] [blame] | 258 | if (areaName == 'sync') createmenus(); |
avm99963 | 4a2a5d5 | 2016-06-04 16:17:29 +0200 | [diff] [blame] | 259 | }); |
| 260 | |
avm99963 | ce257a9 | 2020-12-27 00:07:13 +0100 | [diff] [blame] | 261 | chrome.storage.sync.get(null, items => { |
avm99963 | 5a57c41 | 2020-12-27 00:26:45 +0100 | [diff] [blame] | 262 | if (items.translateinto) { |
| 263 | createmenus(); |
| 264 | } else { |
| 265 | chrome.contextMenus.removeAll(); |
| 266 | var parent = chrome.contextMenus.create({ |
| 267 | 'id': 'tr_parent', |
| 268 | 'title': chrome.i18n.getMessage('contextmenu_title'), |
| 269 | 'contexts': ['selection'] |
| 270 | }); |
| 271 | var id = chrome.contextMenus.create({ |
| 272 | 'id': 'tr_options', |
| 273 | 'title': chrome.i18n.getMessage('contextmenu_edit'), |
| 274 | 'parentId': parent, |
| 275 | 'contexts': ['selection'] |
| 276 | }); |
| 277 | } |
avm99963 | 4a2a5d5 | 2016-06-04 16:17:29 +0200 | [diff] [blame] | 278 | }); |
| 279 | |
avm99963 | ce257a9 | 2020-12-27 00:07:13 +0100 | [diff] [blame] | 280 | chrome.notifications.onClicked.addListener(notification_id => { |
avm99963 | 5a57c41 | 2020-12-27 00:26:45 +0100 | [diff] [blame] | 281 | switch (notification_id) { |
| 282 | case 'install': |
avm99963 | 5a57c41 | 2020-12-27 00:26:45 +0100 | [diff] [blame] | 283 | openOptionsPage(); |
| 284 | break; |
| 285 | } |
| 286 | chrome.notifications.clear(notification_id); |
avm99963 | 4a2a5d5 | 2016-06-04 16:17:29 +0200 | [diff] [blame] | 287 | }); |
| 288 | |
avm99963 | ce257a9 | 2020-12-27 00:07:13 +0100 | [diff] [blame] | 289 | chrome.contextMenus.onClicked.addListener((info, tab) => { |
avm99963 | 5a57c41 | 2020-12-27 00:26:45 +0100 | [diff] [blame] | 290 | if (info.menuItemId == 'tr_options') { |
| 291 | openOptionsPage(); |
| 292 | } else { |
| 293 | translationClick(info, tab); |
| 294 | } |
avm99963 | 4a2a5d5 | 2016-06-04 16:17:29 +0200 | [diff] [blame] | 295 | }); |
avm99963 | ce257a9 | 2020-12-27 00:07:13 +0100 | [diff] [blame] | 296 | |
| 297 | chrome.browserAction.onClicked.addListener(_ => { |
avm99963 | 5a57c41 | 2020-12-27 00:26:45 +0100 | [diff] [blame] | 298 | openOptionsPage(); |
avm99963 | ce257a9 | 2020-12-27 00:07:13 +0100 | [diff] [blame] | 299 | }); |