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