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