blob: 286ec97c2caae6f2af7a6239e0859f3abe8e3d80 [file] [log] [blame]
Adrià Vilanova Martíneza197d862022-05-27 17:33:20 +02001import {convertLanguages, isoLangs} from './common/consts.js';
2
avm99963ce257a92020-12-27 00:07:13 +01003var array_elements = [], translator_tab = null, translator_window = null;
avm999634a2a5d52016-06-04 16:17:29 +02004
5function isEmpty(obj) {
avm999635a57c412020-12-27 00:26:45 +01006 return Object.keys(obj).length === 0;
avm999634a2a5d52016-06-04 16:17:29 +02007}
8
avm99963f65e9bf2020-12-29 12:57:16 +01009function inObject(hayStack, el) {
10 for (var i of Object.keys(hayStack)) {
11 if (hayStack[i] == el) return true;
12 }
13 return false;
14}
15
avm99963ce257a92020-12-27 00:07:13 +010016function getTranslationUrl(lang, text) {
avm999635a57c412020-12-27 00:26:45 +010017 var params = new URLSearchParams({
18 sl: 'auto',
19 tl: lang,
20 text: text,
21 op: 'translate',
22 });
23 return 'https://translate.google.com/?' + params.toString();
avm99963ce257a92020-12-27 00:07:13 +010024}
25
26function translationClick(info, tab) {
avm999635a57c412020-12-27 00:26:45 +010027 chrome.storage.sync.get('uniquetab', items => {
28 var url = getTranslationUrl(
29 array_elements[info.menuItemId]['langCode'], info.selectionText);
30 var settings_tab = {url};
31 if (translator_tab && items.uniquetab == 'yep') {
32 chrome.tabs.update(translator_tab, settings_tab, tab => {
33 chrome.tabs.highlight(
34 {
35 windowId: tab.windowId,
36 tabs: tab.index,
37 },
38 _ => {
39 chrome.windows.update(tab.windowId, {
40 focused: true,
41 });
42 });
43 });
avm999638824b8a2021-01-10 18:25:12 +010044 } else if (items.uniquetab == 'panel' || items.uniquetab == 'popup') {
avm999635a57c412020-12-27 00:26:45 +010045 chrome.windows.create(
46 {
avm999638824b8a2021-01-10 18:25:12 +010047 type: 'popup',
avm999635a57c412020-12-27 00:26:45 +010048 url,
49 width: 1000,
50 height: 382,
51 },
52 function(tab) {
53 translator_window = tab.windowId;
54 translator_tab = tab.id;
55 chrome.windows.onRemoved.addListener(function(windowId) {
56 if (windowId == translator_window) {
57 translator_window = null;
58 translator_tab = null;
59 }
60 });
61 });
62 } else {
63 chrome.tabs.create(settings_tab, function(tab) {
64 translator_window = tab.windowId;
65 translator_tab = tab.id;
66 chrome.tabs.onRemoved.addListener(function(tabId, removeInfo) {
67 if (tabId == translator_tab) {
68 translator_window = null;
69 translator_tab = null;
70 }
71 });
72 });
73 }
74 });
avm999634a2a5d52016-06-04 16:17:29 +020075}
76
avm99963ce257a92020-12-27 00:07:13 +010077function openOptionsPage() {
avm999635a57c412020-12-27 00:26:45 +010078 if (chrome.runtime.openOptionsPage) {
79 // New way to open options pages, if supported (Chrome 42+).
80 chrome.runtime.openOptionsPage();
81 } else {
82 chrome.tabs.create(
83 {
84 'url': 'chrome-extension://' +
85 chrome.i18n.getMessage('@@extension_id') + '/options.html',
86 'active': true
87 },
88 tab => {
89 chrome.windows.update(tab.windowId, {focused: true});
90 });
91 }
avm999634a2a5d52016-06-04 16:17:29 +020092}
93
94function createmenus() {
avm999635a57c412020-12-27 00:26:45 +010095 chrome.storage.sync.get('translateinto', function(items) {
96 chrome.contextMenus.removeAll();
avm999634a2a5d52016-06-04 16:17:29 +020097
avm999635a57c412020-12-27 00:26:45 +010098 var count = 0, singleone = true;
avm999634a2a5d52016-06-04 16:17:29 +020099
avm999635a57c412020-12-27 00:26:45 +0100100 for (var language of Object.keys(items.translateinto)) {
101 if (count == 0) {
102 count++;
103 } else {
104 singleone = false;
105 break;
106 }
107 }
avm999634a2a5d52016-06-04 16:17:29 +0200108
avm999635a57c412020-12-27 00:26:45 +0100109 if (singleone) {
110 for (var language_id of Object.keys(items.translateinto)) {
111 var language = items.translateinto[language_id];
112 var languagem = isoLangs[language];
avm99963f65e9bf2020-12-29 12:57:16 +0100113 if (languagem === undefined) {
114 console.error(language + ' doesn\'t exist!');
115 continue;
116 }
avm999635a57c412020-12-27 00:26:45 +0100117 var id = chrome.contextMenus.create({
118 'id': 'tr_single_parent',
119 'title': chrome.i18n.getMessage('contextmenu_title2', languagem.name),
120 'contexts': ['selection'],
121 });
122 array_elements[id] = new Array();
123 array_elements[id]['langCode'] = language;
124 }
125 } else {
126 var parentEl = chrome.contextMenus.create({
127 'id': 'parent',
128 'title': chrome.i18n.getMessage('contextmenu_title'),
129 'contexts': ['selection']
130 });
131 for (var language_id of Object.keys(items.translateinto)) {
132 var language = items.translateinto[language_id];
133 var languagem = isoLangs[language];
avm99963f65e9bf2020-12-29 12:57:16 +0100134 if (languagem === undefined) {
135 console.error(language + ' doesn\'t exist!');
136 continue;
137 }
avm999635a57c412020-12-27 00:26:45 +0100138 var title = languagem.name + ' (' + languagem.nativeName + ')';
139 var id = chrome.contextMenus.create({
140 'id': 'tr_language_' + language,
141 'title': title,
142 'parentId': parentEl,
143 'contexts': ['selection']
144 });
145 array_elements[id] = new Array();
146 array_elements[id]['langCode'] = language;
147 }
148 chrome.contextMenus.create({
149 'id': 'tr_separator',
150 'type': 'separator',
151 'parentId': parentEl,
152 'contexts': ['selection']
153 });
154 chrome.contextMenus.create({
155 'id': 'tr_options',
156 'title': chrome.i18n.getMessage('contextmenu_edit'),
157 'parentId': parentEl,
158 'contexts': ['selection']
159 });
160 }
161 });
avm999634a2a5d52016-06-04 16:17:29 +0200162}
163
164chrome.runtime.onInstalled.addListener(function(details) {
avm999635a57c412020-12-27 00:26:45 +0100165 chrome.storage.sync.get(null, function(items) {
166 if (details.reason == 'install') {
167 if (isEmpty(items)) {
avm999638824b8a2021-01-10 18:25:12 +0100168 var settings = {'translateinto': {}, 'uniquetab': 'popup'},
avm99963f65e9bf2020-12-29 12:57:16 +0100169 default_language_1 =
170 chrome.i18n.getMessage('@@ui_locale').replace('_', '-'),
171 default_language_2 =
avm999635a57c412020-12-27 00:26:45 +0100172 chrome.i18n.getMessage('@@ui_locale').split('_')[0];
avm99963ce257a92020-12-27 00:07:13 +0100173
avm99963f65e9bf2020-12-29 12:57:16 +0100174 if (isoLangs[default_language_1] != undefined)
175 settings.translateinto['0'] = default_language_1;
176 else if (isoLangs[default_language_2] != undefined)
177 settings.translateinto['0'] = default_language_2;
avm99963ce257a92020-12-27 00:07:13 +0100178
avm999635a57c412020-12-27 00:26:45 +0100179 chrome.storage.sync.set(settings, function() {
180 chrome.notifications.create('install', {
181 type: 'basic',
182 iconUrl: 'icons/translate-128.png',
183 title: chrome.i18n.getMessage('notification_install_title'),
184 message: chrome.i18n.getMessage('notification_install_message'),
185 isClickable: true
186 });
187 });
188 }
189 }
190 if (details.reason == 'update') {
191 var version = details.previousVersion.split('.');
avm99963ce257a92020-12-27 00:07:13 +0100192
avm999635a57c412020-12-27 00:26:45 +0100193 // Updating from a version previous to v0.6
194 if (version[0] == '0' && version[1] < '6') {
195 var settings = {
196 languages: {},
197 uniquetab: '',
198 };
199 var default_language =
200 chrome.i18n.getMessage('@@ui_locale').split('_')[0];
avm99963ce257a92020-12-27 00:07:13 +0100201
avm99963f65e9bf2020-12-29 12:57:16 +0100202 if (isoLangs[default_language] != undefined)
avm999635a57c412020-12-27 00:26:45 +0100203 settings.languages[default_language] = default_language;
avm99963ce257a92020-12-27 00:07:13 +0100204
Adrià Vilanova Martínez203bc432021-06-08 16:35:00 +0200205 chrome.storage.sync.set(settings);
avm999635a57c412020-12-27 00:26:45 +0100206 }
avm99963ce257a92020-12-27 00:07:13 +0100207
avm999635a57c412020-12-27 00:26:45 +0100208 // Updating from a version previous to v0.7
209 if (version[0] == '0' && version[1] < '7') {
210 items.translateinto = {};
211 var i = 0;
212 for (var language in items.languages) {
213 items.translateinto[i] = items.languages[language];
214 i++;
215 }
216 delete items.languages;
Adrià Vilanova Martínez203bc432021-06-08 16:35:00 +0200217 chrome.storage.sync.set(items);
avm999635a57c412020-12-27 00:26:45 +0100218 }
avm99963f65e9bf2020-12-29 12:57:16 +0100219
220 // Remove non-existent languages or change with correct language code
221 if (items.translateinto) {
222 var modified = false;
223 for (var language_id of Object.keys(items.translateinto)) {
224 var language = items.translateinto[language_id];
225 if (isoLangs[language] === undefined) {
226 if (convertLanguages[language] === undefined) {
227 // The language doesn't exist
228 console.log(
229 'Deleting ' + language +
230 ' from items.translateinto because it doesn\'t exist.');
231 delete items.translateinto[language_id];
232 } else {
233 // The language doesn't exist but a known replacement is known
234 var newLanguage = convertLanguages[language];
235 console.log('Replacing ' + language + ' with ' + newLanguage);
236
237 // If the converted language is already on the list, just remove
238 // the wrong language, otherwise convert the language
239 if (inObject(items.translateinto, newLanguage))
240 delete items.translateinto[language_id];
241 else
242 items.translateinto[language_id] = newLanguage;
243 }
244 modified = true;
245 }
246 }
247 if (modified) chrome.storage.sync.set(items);
248 } else {
249 console.log('items.translateinto doesn\'t exist: let\'s create it.');
250 items['translateinto'] = {};
251 chrome.storage.sync.set(items);
252 }
avm999635a57c412020-12-27 00:26:45 +0100253 }
254 });
avm999634a2a5d52016-06-04 16:17:29 +0200255});
256
avm99963ce257a92020-12-27 00:07:13 +0100257chrome.storage.onChanged.addListener((changes, areaName) => {
avm999635a57c412020-12-27 00:26:45 +0100258 if (areaName == 'sync') createmenus();
avm999634a2a5d52016-06-04 16:17:29 +0200259});
260
avm99963ce257a92020-12-27 00:07:13 +0100261chrome.storage.sync.get(null, items => {
avm999635a57c412020-12-27 00:26:45 +0100262 if (items.translateinto) {
263 createmenus();
264 } else {
265 chrome.contextMenus.removeAll();
266 var parent = chrome.contextMenus.create({
267 'id': 'tr_parent',
268 'title': chrome.i18n.getMessage('contextmenu_title'),
269 'contexts': ['selection']
270 });
271 var id = chrome.contextMenus.create({
272 'id': 'tr_options',
273 'title': chrome.i18n.getMessage('contextmenu_edit'),
274 'parentId': parent,
275 'contexts': ['selection']
276 });
277 }
avm999634a2a5d52016-06-04 16:17:29 +0200278});
279
avm99963ce257a92020-12-27 00:07:13 +0100280chrome.notifications.onClicked.addListener(notification_id => {
avm999635a57c412020-12-27 00:26:45 +0100281 switch (notification_id) {
282 case 'install':
avm999635a57c412020-12-27 00:26:45 +0100283 openOptionsPage();
284 break;
285 }
286 chrome.notifications.clear(notification_id);
avm999634a2a5d52016-06-04 16:17:29 +0200287});
288
avm99963ce257a92020-12-27 00:07:13 +0100289chrome.contextMenus.onClicked.addListener((info, tab) => {
avm999635a57c412020-12-27 00:26:45 +0100290 if (info.menuItemId == 'tr_options') {
291 openOptionsPage();
292 } else {
293 translationClick(info, tab);
294 }
avm999634a2a5d52016-06-04 16:17:29 +0200295});
avm99963ce257a92020-12-27 00:07:13 +0100296
297chrome.browserAction.onClicked.addListener(_ => {
avm999635a57c412020-12-27 00:26:45 +0100298 openOptionsPage();
avm99963ce257a92020-12-27 00:07:13 +0100299});