blob: 941e198a2c0b4557d1e0088c46303b8f8f533ebb [file] [log] [blame]
Adrià Vilanova Martínez5bdc4732022-05-31 20:12:21 +02001import actionApi from './common/actionApi';
2import {isoLangs} from './common/consts';
3import Options from './common/options';
4import ExtSessionStorage from './common/sessionStorage';
avm999634a2a5d52016-06-04 16:17:29 +02005
Adrià Vilanova Martínez5bdc4732022-05-31 20:12:21 +02006interface ContextMenuLangs {
7 [id: string]: string;
8}
9
10function getTranslationUrl(lang: string, text: string): string {
Adrià Vilanova Martínez51f151f2022-06-01 23:17:38 +020011 const params = new URLSearchParams({
avm999635a57c412020-12-27 00:26:45 +010012 sl: 'auto',
13 tl: lang,
14 text: text,
15 op: 'translate',
16 });
17 return 'https://translate.google.com/?' + params.toString();
avm99963ce257a92020-12-27 00:07:13 +010018}
19
Adrià Vilanova Martínez51f151f2022-06-01 23:17:38 +020020function translationClick(info: chrome.contextMenus.OnClickData): void {
21 const optionsPromise = Options.getOptions();
22 const ssPromise =
23 ExtSessionStorage.get(['contextMenuLangs', 'translatorTab']);
Adrià Vilanova Martínez86fda492022-05-31 15:05:21 +020024 Promise.all([optionsPromise, ssPromise])
25 .then(returnValues => {
26 const [options, sessionStorageItems] = returnValues;
Adrià Vilanova Martínez51f151f2022-06-01 23:17:38 +020027 const url = getTranslationUrl(
Adrià Vilanova Martínez86fda492022-05-31 15:05:21 +020028 sessionStorageItems.contextMenuLangs?.[info.menuItemId],
29 info.selectionText);
Adrià Vilanova Martínez51f151f2022-06-01 23:17:38 +020030 const settings_tab = {url};
Adrià Vilanova Martínez86fda492022-05-31 15:05:21 +020031 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ínez53f7a7f2022-05-30 13:59:59 +020045 } else if (options.uniqueTab == 'popup') {
Adrià Vilanova Martínezec599342022-05-31 11:57:35 +020046 chrome.windows.create({
47 type: 'popup',
48 url,
49 width: 1000,
50 height: 382,
51 });
Adrià Vilanova Martínez53f7a7f2022-05-30 13:59:59 +020052 } else {
53 chrome.tabs.create(settings_tab, function(tab) {
Adrià Vilanova Martínez86fda492022-05-31 15:05:21 +020054 ExtSessionStorage.set({translatorTab: tab.id});
avm999635a57c412020-12-27 00:26:45 +010055 });
Adrià Vilanova Martínez53f7a7f2022-05-30 13:59:59 +020056 }
57 })
58 .catch(err => {
Adrià Vilanova Martínez86fda492022-05-31 15:05:21 +020059 console.error('Error handling translation click', err);
avm999635a57c412020-12-27 00:26:45 +010060 });
avm999634a2a5d52016-06-04 16:17:29 +020061}
62
Adrià Vilanova Martínez5bdc4732022-05-31 20:12:21 +020063function createMenus(options: Options): Promise<void> {
Adrià Vilanova Martínez53f7a7f2022-05-30 13:59:59 +020064 chrome.contextMenus.removeAll();
avm999634a2a5d52016-06-04 16:17:29 +020065
Adrià Vilanova Martínez51f151f2022-06-01 23:17:38 +020066 const contextMenuLangs: ContextMenuLangs = {};
67 const langs = options.targetLangs;
68 const isSingleEntry = Object.values(langs).length == 1;
avm999634a2a5d52016-06-04 16:17:29 +020069
Adrià Vilanova Martínez53f7a7f2022-05-30 13:59:59 +020070 let parentEl;
71 if (!isSingleEntry) {
72 parentEl = chrome.contextMenus.create({
73 'id': 'parent',
avm999635a57c412020-12-27 00:26:45 +010074 'title': chrome.i18n.getMessage('contextmenu_title'),
75 'contexts': ['selection']
76 });
Adrià Vilanova Martínez53f7a7f2022-05-30 13:59:59 +020077 }
78
Adrià Vilanova Martínez51f151f2022-06-01 23:17:38 +020079 for (const language of Object.values(langs)) {
80 const languageDetails = isoLangs[language];
Adrià Vilanova Martínez53f7a7f2022-05-30 13:59:59 +020081 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 }
Adrià Vilanova Martínez51f151f2022-06-01 23:17:38 +020092 const id = chrome.contextMenus.create({
Adrià Vilanova Martínez53f7a7f2022-05-30 13:59:59 +020093 'id': 'tr_language_' + language,
94 'title': title,
95 'parentId': parentEl,
96 'contexts': ['selection']
97 });
Adrià Vilanova Martínez86fda492022-05-31 15:05:21 +020098 contextMenuLangs[id] = language;
Adrià Vilanova Martínez53f7a7f2022-05-30 13:59:59 +020099 }
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({
avm999635a57c412020-12-27 00:26:45 +0100109 'id': 'tr_options',
110 'title': chrome.i18n.getMessage('contextmenu_edit'),
Adrià Vilanova Martínez53f7a7f2022-05-30 13:59:59 +0200111 'parentId': parentEl,
avm999635a57c412020-12-27 00:26:45 +0100112 'contexts': ['selection']
113 });
114 }
Adrià Vilanova Martínez86fda492022-05-31 15:05:21 +0200115
116 return ExtSessionStorage.set({contextMenuLangs});
Adrià Vilanova Martínez53f7a7f2022-05-30 13:59:59 +0200117}
118
119chrome.storage.onChanged.addListener((changes, areaName) => {
120 if (areaName == 'sync') {
121 Options.getOptions(/* readOnly = */ false)
122 .then(options => {
Adrià Vilanova Martínez86fda492022-05-31 15:05:21 +0200123 return createMenus(options);
Adrià Vilanova Martínez53f7a7f2022-05-30 13:59:59 +0200124 })
125 .catch(err => {
126 console.error(
Adrià Vilanova Martínez86fda492022-05-31 15:05:21 +0200127 'Error setting up the extension after a change ' +
Adrià Vilanova Martínez53f7a7f2022-05-30 13:59:59 +0200128 'in the storage area.',
129 err);
130 });
131 }
avm999634a2a5d52016-06-04 16:17:29 +0200132});
133
Adrià Vilanova Martínez53f7a7f2022-05-30 13:59:59 +0200134Options.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ínez86fda492022-05-31 15:05:21 +0200146 return createMenus(options);
Adrià Vilanova Martínez53f7a7f2022-05-30 13:59:59 +0200147 })
148 .catch(err => {
Adrià Vilanova Martínez86fda492022-05-31 15:05:21 +0200149 console.error('Error initializing the extension.', err);
Adrià Vilanova Martínez53f7a7f2022-05-30 13:59:59 +0200150 });
151
avm99963ce257a92020-12-27 00:07:13 +0100152chrome.notifications.onClicked.addListener(notification_id => {
avm999635a57c412020-12-27 00:26:45 +0100153 switch (notification_id) {
154 case 'install':
Adrià Vilanova Martínez53f7a7f2022-05-30 13:59:59 +0200155 chrome.runtime.openOptionsPage();
avm999635a57c412020-12-27 00:26:45 +0100156 break;
157 }
158 chrome.notifications.clear(notification_id);
avm999634a2a5d52016-06-04 16:17:29 +0200159});
160
Adrià Vilanova Martínez51f151f2022-06-01 23:17:38 +0200161chrome.contextMenus.onClicked.addListener(info => {
avm999635a57c412020-12-27 00:26:45 +0100162 if (info.menuItemId == 'tr_options') {
Adrià Vilanova Martínez53f7a7f2022-05-30 13:59:59 +0200163 chrome.runtime.openOptionsPage();
avm999635a57c412020-12-27 00:26:45 +0100164 } else {
Adrià Vilanova Martínez51f151f2022-06-01 23:17:38 +0200165 translationClick(info);
avm999635a57c412020-12-27 00:26:45 +0100166 }
avm999634a2a5d52016-06-04 16:17:29 +0200167});
avm99963ce257a92020-12-27 00:07:13 +0100168
Adrià Vilanova Martínez51f151f2022-06-01 23:17:38 +0200169chrome.tabs.onRemoved.addListener(tabId => {
Adrià Vilanova Martínez86fda492022-05-31 15:05:21 +0200170 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ínezec599342022-05-31 11:57:35 +0200177});
178
Adrià Vilanova Martínez86fda492022-05-31 15:05:21 +0200179actionApi.onClicked.addListener(() => {
Adrià Vilanova Martínez53f7a7f2022-05-30 13:59:59 +0200180 chrome.runtime.openOptionsPage();
avm99963ce257a92020-12-27 00:07:13 +0100181});
Adrià Vilanova Martínez86fda492022-05-31 15:05:21 +0200182
Adrià Vilanova Martínez51f151f2022-06-01 23:17:38 +0200183chrome.runtime.onMessage.addListener(request => {
Adrià Vilanova Martínez86fda492022-05-31 15:05:21 +0200184 switch (request.action) {
185 case 'clearTranslatorTab':
186 ExtSessionStorage.set({translatorTab: null});
187 break;
188
189 default:
Adrià Vilanova Martínez51f151f2022-06-01 23:17:38 +0200190 console.error(
191 `Unknown action "${request.action}" received as a message.`);
Adrià Vilanova Martínez86fda492022-05-31 15:05:21 +0200192 }
Adrià Vilanova Martínez5bdc4732022-05-31 20:12:21 +0200193
194 return undefined;
Adrià Vilanova Martínez86fda492022-05-31 15:05:21 +0200195});