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 | |
avm99963 | 5a57c41 | 2020-12-27 00:26:45 +0100 | [diff] [blame] | 203 | chrome.storage.sync.set(settings, function() { |
| 204 | chrome.notifications.create('upgradedtostorage', { |
| 205 | type: 'basic', |
| 206 | iconUrl: 'icons/translate-128.png', |
| 207 | title: |
| 208 | chrome.i18n.getMessage('notification_upgradedtostorage_title'), |
| 209 | message: chrome.i18n.getMessage( |
| 210 | 'notification_upgradedtostorage_message'), |
| 211 | isClickable: true, |
| 212 | }); |
| 213 | }); |
| 214 | } |
avm99963 | ce257a9 | 2020-12-27 00:07:13 +0100 | [diff] [blame] | 215 | |
avm99963 | 5a57c41 | 2020-12-27 00:26:45 +0100 | [diff] [blame] | 216 | // Updating from a version previous to v0.7 |
| 217 | if (version[0] == '0' && version[1] < '7') { |
| 218 | items.translateinto = {}; |
| 219 | var i = 0; |
| 220 | for (var language in items.languages) { |
| 221 | items.translateinto[i] = items.languages[language]; |
| 222 | i++; |
| 223 | } |
| 224 | delete items.languages; |
| 225 | chrome.storage.sync.set(items, _ => { |
| 226 | chrome.notifications.create('reorder', { |
| 227 | type: 'basic', |
| 228 | iconUrl: 'icons/translate-128.png', |
| 229 | title: chrome.i18n.getMessage('notification_reorder_title'), |
| 230 | message: chrome.i18n.getMessage('notification_reorder_message'), |
| 231 | isClickable: true |
| 232 | }); |
| 233 | }); |
| 234 | } |
avm99963 | f65e9bf | 2020-12-29 12:57:16 +0100 | [diff] [blame] | 235 | |
| 236 | // Remove non-existent languages or change with correct language code |
| 237 | if (items.translateinto) { |
| 238 | var modified = false; |
| 239 | for (var language_id of Object.keys(items.translateinto)) { |
| 240 | var language = items.translateinto[language_id]; |
| 241 | if (isoLangs[language] === undefined) { |
| 242 | if (convertLanguages[language] === undefined) { |
| 243 | // The language doesn't exist |
| 244 | console.log( |
| 245 | 'Deleting ' + language + |
| 246 | ' from items.translateinto because it doesn\'t exist.'); |
| 247 | delete items.translateinto[language_id]; |
| 248 | } else { |
| 249 | // The language doesn't exist but a known replacement is known |
| 250 | var newLanguage = convertLanguages[language]; |
| 251 | console.log('Replacing ' + language + ' with ' + newLanguage); |
| 252 | |
| 253 | // If the converted language is already on the list, just remove |
| 254 | // the wrong language, otherwise convert the language |
| 255 | if (inObject(items.translateinto, newLanguage)) |
| 256 | delete items.translateinto[language_id]; |
| 257 | else |
| 258 | items.translateinto[language_id] = newLanguage; |
| 259 | } |
| 260 | modified = true; |
| 261 | } |
| 262 | } |
| 263 | if (modified) chrome.storage.sync.set(items); |
| 264 | } else { |
| 265 | console.log('items.translateinto doesn\'t exist: let\'s create it.'); |
| 266 | items['translateinto'] = {}; |
| 267 | chrome.storage.sync.set(items); |
| 268 | } |
avm99963 | 5a57c41 | 2020-12-27 00:26:45 +0100 | [diff] [blame] | 269 | } |
| 270 | }); |
avm99963 | 4a2a5d5 | 2016-06-04 16:17:29 +0200 | [diff] [blame] | 271 | }); |
| 272 | |
avm99963 | ce257a9 | 2020-12-27 00:07:13 +0100 | [diff] [blame] | 273 | chrome.storage.onChanged.addListener((changes, areaName) => { |
avm99963 | 5a57c41 | 2020-12-27 00:26:45 +0100 | [diff] [blame] | 274 | if (areaName == 'sync') createmenus(); |
avm99963 | 4a2a5d5 | 2016-06-04 16:17:29 +0200 | [diff] [blame] | 275 | }); |
| 276 | |
avm99963 | ce257a9 | 2020-12-27 00:07:13 +0100 | [diff] [blame] | 277 | chrome.storage.sync.get(null, items => { |
avm99963 | 5a57c41 | 2020-12-27 00:26:45 +0100 | [diff] [blame] | 278 | if (items.translateinto) { |
| 279 | createmenus(); |
| 280 | } else { |
| 281 | chrome.contextMenus.removeAll(); |
| 282 | var parent = chrome.contextMenus.create({ |
| 283 | 'id': 'tr_parent', |
| 284 | 'title': chrome.i18n.getMessage('contextmenu_title'), |
| 285 | 'contexts': ['selection'] |
| 286 | }); |
| 287 | var id = chrome.contextMenus.create({ |
| 288 | 'id': 'tr_options', |
| 289 | 'title': chrome.i18n.getMessage('contextmenu_edit'), |
| 290 | 'parentId': parent, |
| 291 | 'contexts': ['selection'] |
| 292 | }); |
| 293 | } |
avm99963 | 4a2a5d5 | 2016-06-04 16:17:29 +0200 | [diff] [blame] | 294 | }); |
| 295 | |
avm99963 | ce257a9 | 2020-12-27 00:07:13 +0100 | [diff] [blame] | 296 | chrome.notifications.onClicked.addListener(notification_id => { |
avm99963 | 5a57c41 | 2020-12-27 00:26:45 +0100 | [diff] [blame] | 297 | switch (notification_id) { |
| 298 | case 'install': |
| 299 | case 'upgradedtostorage': |
| 300 | case 'reorder': |
| 301 | openOptionsPage(); |
| 302 | break; |
| 303 | } |
| 304 | chrome.notifications.clear(notification_id); |
avm99963 | 4a2a5d5 | 2016-06-04 16:17:29 +0200 | [diff] [blame] | 305 | }); |
| 306 | |
avm99963 | ce257a9 | 2020-12-27 00:07:13 +0100 | [diff] [blame] | 307 | chrome.contextMenus.onClicked.addListener((info, tab) => { |
avm99963 | 5a57c41 | 2020-12-27 00:26:45 +0100 | [diff] [blame] | 308 | if (info.menuItemId == 'tr_options') { |
| 309 | openOptionsPage(); |
| 310 | } else { |
| 311 | translationClick(info, tab); |
| 312 | } |
avm99963 | 4a2a5d5 | 2016-06-04 16:17:29 +0200 | [diff] [blame] | 313 | }); |
avm99963 | ce257a9 | 2020-12-27 00:07:13 +0100 | [diff] [blame] | 314 | |
| 315 | chrome.browserAction.onClicked.addListener(_ => { |
avm99963 | 5a57c41 | 2020-12-27 00:26:45 +0100 | [diff] [blame] | 316 | openOptionsPage(); |
avm99963 | ce257a9 | 2020-12-27 00:07:13 +0100 | [diff] [blame] | 317 | }); |