Adrià Vilanova Martínez | 5bdc473 | 2022-05-31 20:12:21 +0200 | [diff] [blame] | 1 | import actionApi from './common/actionApi'; |
| 2 | import {isoLangs} from './common/consts'; |
| 3 | import Options from './common/options'; |
| 4 | import ExtSessionStorage from './common/sessionStorage'; |
avm99963 | 4a2a5d5 | 2016-06-04 16:17:29 +0200 | [diff] [blame] | 5 | |
Adrià Vilanova Martínez | 5bdc473 | 2022-05-31 20:12:21 +0200 | [diff] [blame] | 6 | interface ContextMenuLangs { |
| 7 | [id: string]: string; |
| 8 | } |
| 9 | |
| 10 | function getTranslationUrl(lang: string, text: string): string { |
| 11 | let params = new URLSearchParams({ |
avm99963 | 5a57c41 | 2020-12-27 00:26:45 +0100 | [diff] [blame] | 12 | sl: 'auto', |
| 13 | tl: lang, |
| 14 | text: text, |
| 15 | op: 'translate', |
| 16 | }); |
| 17 | return 'https://translate.google.com/?' + params.toString(); |
avm99963 | ce257a9 | 2020-12-27 00:07:13 +0100 | [diff] [blame] | 18 | } |
| 19 | |
Adrià Vilanova Martínez | 5bdc473 | 2022-05-31 20:12:21 +0200 | [diff] [blame] | 20 | function translationClick( |
| 21 | info: chrome.contextMenus.OnClickData, tab: chrome.tabs.Tab): void { |
Adrià Vilanova Martínez | 86fda49 | 2022-05-31 15:05:21 +0200 | [diff] [blame] | 22 | let optionsPromise = Options.getOptions(); |
| 23 | let ssPromise = ExtSessionStorage.get(['contextMenuLangs', 'translatorTab']); |
| 24 | Promise.all([optionsPromise, ssPromise]) |
| 25 | .then(returnValues => { |
| 26 | const [options, sessionStorageItems] = returnValues; |
Adrià Vilanova Martínez | ec59934 | 2022-05-31 11:57:35 +0200 | [diff] [blame] | 27 | let url = getTranslationUrl( |
Adrià Vilanova Martínez | 86fda49 | 2022-05-31 15:05:21 +0200 | [diff] [blame] | 28 | sessionStorageItems.contextMenuLangs?.[info.menuItemId], |
| 29 | info.selectionText); |
Adrià Vilanova Martínez | ec59934 | 2022-05-31 11:57:35 +0200 | [diff] [blame] | 30 | let settings_tab = {url}; |
Adrià Vilanova Martínez | 86fda49 | 2022-05-31 15:05:21 +0200 | [diff] [blame] | 31 | if (sessionStorageItems.translatorTab && options.uniqueTab == 'yep') { |
| 32 | chrome.tabs.update( |
| 33 | sessionStorageItems.translatorTab, settings_tab, tab => { |
| 34 | chrome.tabs.highlight( |
| 35 | { |
| 36 | windowId: tab.windowId, |
| 37 | tabs: tab.index, |
| 38 | }, |
| 39 | () => { |
| 40 | chrome.windows.update(tab.windowId, { |
| 41 | focused: true, |
| 42 | }); |
| 43 | }); |
| 44 | }); |
Adrià Vilanova Martínez | 53f7a7f | 2022-05-30 13:59:59 +0200 | [diff] [blame] | 45 | } else if (options.uniqueTab == 'popup') { |
Adrià Vilanova Martínez | ec59934 | 2022-05-31 11:57:35 +0200 | [diff] [blame] | 46 | chrome.windows.create({ |
| 47 | type: 'popup', |
| 48 | url, |
| 49 | width: 1000, |
| 50 | height: 382, |
| 51 | }); |
Adrià Vilanova Martínez | 53f7a7f | 2022-05-30 13:59:59 +0200 | [diff] [blame] | 52 | } else { |
| 53 | chrome.tabs.create(settings_tab, function(tab) { |
Adrià Vilanova Martínez | 86fda49 | 2022-05-31 15:05:21 +0200 | [diff] [blame] | 54 | ExtSessionStorage.set({translatorTab: tab.id}); |
avm99963 | 5a57c41 | 2020-12-27 00:26:45 +0100 | [diff] [blame] | 55 | }); |
Adrià Vilanova Martínez | 53f7a7f | 2022-05-30 13:59:59 +0200 | [diff] [blame] | 56 | } |
| 57 | }) |
| 58 | .catch(err => { |
Adrià Vilanova Martínez | 86fda49 | 2022-05-31 15:05:21 +0200 | [diff] [blame] | 59 | console.error('Error handling translation click', err); |
avm99963 | 5a57c41 | 2020-12-27 00:26:45 +0100 | [diff] [blame] | 60 | }); |
avm99963 | 4a2a5d5 | 2016-06-04 16:17:29 +0200 | [diff] [blame] | 61 | } |
| 62 | |
Adrià Vilanova Martínez | 5bdc473 | 2022-05-31 20:12:21 +0200 | [diff] [blame] | 63 | function createMenus(options: Options): Promise<void> { |
Adrià Vilanova Martínez | 53f7a7f | 2022-05-30 13:59:59 +0200 | [diff] [blame] | 64 | chrome.contextMenus.removeAll(); |
avm99963 | 4a2a5d5 | 2016-06-04 16:17:29 +0200 | [diff] [blame] | 65 | |
Adrià Vilanova Martínez | 5bdc473 | 2022-05-31 20:12:21 +0200 | [diff] [blame] | 66 | let contextMenuLangs: ContextMenuLangs = {}; |
Adrià Vilanova Martínez | 53f7a7f | 2022-05-30 13:59:59 +0200 | [diff] [blame] | 67 | let langs = options.targetLangs; |
| 68 | let isSingleEntry = Object.values(langs).length == 1; |
avm99963 | 4a2a5d5 | 2016-06-04 16:17:29 +0200 | [diff] [blame] | 69 | |
Adrià Vilanova Martínez | 53f7a7f | 2022-05-30 13:59:59 +0200 | [diff] [blame] | 70 | let parentEl; |
| 71 | if (!isSingleEntry) { |
| 72 | parentEl = chrome.contextMenus.create({ |
| 73 | 'id': 'parent', |
avm99963 | 5a57c41 | 2020-12-27 00:26:45 +0100 | [diff] [blame] | 74 | 'title': chrome.i18n.getMessage('contextmenu_title'), |
| 75 | 'contexts': ['selection'] |
| 76 | }); |
Adrià Vilanova Martínez | 53f7a7f | 2022-05-30 13:59:59 +0200 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | for (let [index, language] of Object.entries(langs)) { |
| 80 | let languageDetails = isoLangs[language]; |
| 81 | if (languageDetails === undefined) { |
| 82 | console.error(language + ' doesn\'t exist!'); |
| 83 | continue; |
| 84 | } |
| 85 | let title; |
| 86 | if (isSingleEntry) { |
| 87 | title = |
| 88 | chrome.i18n.getMessage('contextmenu_title2', languageDetails.name); |
| 89 | } else { |
| 90 | title = languageDetails.name + ' (' + languageDetails.nativeName + ')'; |
| 91 | } |
| 92 | let id = chrome.contextMenus.create({ |
| 93 | 'id': 'tr_language_' + language, |
| 94 | 'title': title, |
| 95 | 'parentId': parentEl, |
| 96 | 'contexts': ['selection'] |
| 97 | }); |
Adrià Vilanova Martínez | 86fda49 | 2022-05-31 15:05:21 +0200 | [diff] [blame] | 98 | contextMenuLangs[id] = language; |
Adrià Vilanova Martínez | 53f7a7f | 2022-05-30 13:59:59 +0200 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | if (!isSingleEntry) { |
| 102 | chrome.contextMenus.create({ |
| 103 | 'id': 'tr_separator', |
| 104 | 'type': 'separator', |
| 105 | 'parentId': parentEl, |
| 106 | 'contexts': ['selection'] |
| 107 | }); |
| 108 | chrome.contextMenus.create({ |
avm99963 | 5a57c41 | 2020-12-27 00:26:45 +0100 | [diff] [blame] | 109 | 'id': 'tr_options', |
| 110 | 'title': chrome.i18n.getMessage('contextmenu_edit'), |
Adrià Vilanova Martínez | 53f7a7f | 2022-05-30 13:59:59 +0200 | [diff] [blame] | 111 | 'parentId': parentEl, |
avm99963 | 5a57c41 | 2020-12-27 00:26:45 +0100 | [diff] [blame] | 112 | 'contexts': ['selection'] |
| 113 | }); |
| 114 | } |
Adrià Vilanova Martínez | 86fda49 | 2022-05-31 15:05:21 +0200 | [diff] [blame] | 115 | |
| 116 | return ExtSessionStorage.set({contextMenuLangs}); |
Adrià Vilanova Martínez | 53f7a7f | 2022-05-30 13:59:59 +0200 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | chrome.storage.onChanged.addListener((changes, areaName) => { |
| 120 | if (areaName == 'sync') { |
| 121 | Options.getOptions(/* readOnly = */ false) |
| 122 | .then(options => { |
Adrià Vilanova Martínez | 86fda49 | 2022-05-31 15:05:21 +0200 | [diff] [blame] | 123 | return createMenus(options); |
Adrià Vilanova Martínez | 53f7a7f | 2022-05-30 13:59:59 +0200 | [diff] [blame] | 124 | }) |
| 125 | .catch(err => { |
| 126 | console.error( |
Adrià Vilanova Martínez | 86fda49 | 2022-05-31 15:05:21 +0200 | [diff] [blame] | 127 | 'Error setting up the extension after a change ' + |
Adrià Vilanova Martínez | 53f7a7f | 2022-05-30 13:59:59 +0200 | [diff] [blame] | 128 | 'in the storage area.', |
| 129 | err); |
| 130 | }); |
| 131 | } |
avm99963 | 4a2a5d5 | 2016-06-04 16:17:29 +0200 | [diff] [blame] | 132 | }); |
| 133 | |
Adrià Vilanova Martínez | 53f7a7f | 2022-05-30 13:59:59 +0200 | [diff] [blame] | 134 | Options.getOptions(/* readOnly = */ false) |
| 135 | .then(options => { |
| 136 | if (options.isFirstRun) { |
| 137 | chrome.notifications.create('install', { |
| 138 | type: 'basic', |
| 139 | iconUrl: 'icons/translate-128.png', |
| 140 | title: chrome.i18n.getMessage('notification_install_title'), |
| 141 | message: chrome.i18n.getMessage('notification_install_message'), |
| 142 | isClickable: true |
| 143 | }); |
| 144 | } |
| 145 | |
Adrià Vilanova Martínez | 86fda49 | 2022-05-31 15:05:21 +0200 | [diff] [blame] | 146 | return createMenus(options); |
Adrià Vilanova Martínez | 53f7a7f | 2022-05-30 13:59:59 +0200 | [diff] [blame] | 147 | }) |
| 148 | .catch(err => { |
Adrià Vilanova Martínez | 86fda49 | 2022-05-31 15:05:21 +0200 | [diff] [blame] | 149 | console.error('Error initializing the extension.', err); |
Adrià Vilanova Martínez | 53f7a7f | 2022-05-30 13:59:59 +0200 | [diff] [blame] | 150 | }); |
| 151 | |
avm99963 | ce257a9 | 2020-12-27 00:07:13 +0100 | [diff] [blame] | 152 | chrome.notifications.onClicked.addListener(notification_id => { |
avm99963 | 5a57c41 | 2020-12-27 00:26:45 +0100 | [diff] [blame] | 153 | switch (notification_id) { |
| 154 | case 'install': |
Adrià Vilanova Martínez | 53f7a7f | 2022-05-30 13:59:59 +0200 | [diff] [blame] | 155 | chrome.runtime.openOptionsPage(); |
avm99963 | 5a57c41 | 2020-12-27 00:26:45 +0100 | [diff] [blame] | 156 | break; |
| 157 | } |
| 158 | chrome.notifications.clear(notification_id); |
avm99963 | 4a2a5d5 | 2016-06-04 16:17:29 +0200 | [diff] [blame] | 159 | }); |
| 160 | |
avm99963 | ce257a9 | 2020-12-27 00:07:13 +0100 | [diff] [blame] | 161 | chrome.contextMenus.onClicked.addListener((info, tab) => { |
avm99963 | 5a57c41 | 2020-12-27 00:26:45 +0100 | [diff] [blame] | 162 | if (info.menuItemId == 'tr_options') { |
Adrià Vilanova Martínez | 53f7a7f | 2022-05-30 13:59:59 +0200 | [diff] [blame] | 163 | chrome.runtime.openOptionsPage(); |
avm99963 | 5a57c41 | 2020-12-27 00:26:45 +0100 | [diff] [blame] | 164 | } else { |
| 165 | translationClick(info, tab); |
| 166 | } |
avm99963 | 4a2a5d5 | 2016-06-04 16:17:29 +0200 | [diff] [blame] | 167 | }); |
avm99963 | ce257a9 | 2020-12-27 00:07:13 +0100 | [diff] [blame] | 168 | |
Adrià Vilanova Martínez | ec59934 | 2022-05-31 11:57:35 +0200 | [diff] [blame] | 169 | chrome.tabs.onRemoved.addListener((tabId, removeInfo) => { |
Adrià Vilanova Martínez | 86fda49 | 2022-05-31 15:05:21 +0200 | [diff] [blame] | 170 | ExtSessionStorage.get('translatorTab') |
| 171 | .then(items => { |
| 172 | if (tabId == items.translatorTab) { |
| 173 | ExtSessionStorage.set({translatorTab: null}); |
| 174 | } |
| 175 | }) |
| 176 | .catch(err => console.log(err)); |
Adrià Vilanova Martínez | ec59934 | 2022-05-31 11:57:35 +0200 | [diff] [blame] | 177 | }); |
| 178 | |
Adrià Vilanova Martínez | 86fda49 | 2022-05-31 15:05:21 +0200 | [diff] [blame] | 179 | actionApi.onClicked.addListener(() => { |
Adrià Vilanova Martínez | 53f7a7f | 2022-05-30 13:59:59 +0200 | [diff] [blame] | 180 | chrome.runtime.openOptionsPage(); |
avm99963 | ce257a9 | 2020-12-27 00:07:13 +0100 | [diff] [blame] | 181 | }); |
Adrià Vilanova Martínez | 86fda49 | 2022-05-31 15:05:21 +0200 | [diff] [blame] | 182 | |
| 183 | chrome.runtime.onMessage.addListener((request, sender, sendResponse) => { |
| 184 | switch (request.action) { |
| 185 | case 'clearTranslatorTab': |
| 186 | ExtSessionStorage.set({translatorTab: null}); |
| 187 | break; |
| 188 | |
| 189 | default: |
Adrià Vilanova Martínez | 5bdc473 | 2022-05-31 20:12:21 +0200 | [diff] [blame] | 190 | console.error(`Unknown action "${request.action}" received as a message.`); |
Adrià Vilanova Martínez | 86fda49 | 2022-05-31 15:05:21 +0200 | [diff] [blame] | 191 | } |
Adrià Vilanova Martínez | 5bdc473 | 2022-05-31 20:12:21 +0200 | [diff] [blame] | 192 | |
| 193 | return undefined; |
Adrià Vilanova Martínez | 86fda49 | 2022-05-31 15:05:21 +0200 | [diff] [blame] | 194 | }); |