blob: 1770c14ac1a36d87a10dd00f4b7ba7b46e078d40 [file] [log] [blame]
avm99963ce257a92020-12-27 00:07:13 +01001var array_elements = [], translator_tab = null, translator_window = null;
avm999634a2a5d52016-06-04 16:17:29 +02002
3function isEmpty(obj) {
avm999635a57c412020-12-27 00:26:45 +01004 return Object.keys(obj).length === 0;
avm999634a2a5d52016-06-04 16:17:29 +02005}
6
avm99963f65e9bf2020-12-29 12:57:16 +01007function inObject(hayStack, el) {
8 for (var i of Object.keys(hayStack)) {
9 if (hayStack[i] == el) return true;
10 }
11 return false;
12}
13
avm99963ce257a92020-12-27 00:07:13 +010014function getTranslationUrl(lang, text) {
avm999635a57c412020-12-27 00:26:45 +010015 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();
avm99963ce257a92020-12-27 00:07:13 +010022}
23
24function translationClick(info, tab) {
avm999635a57c412020-12-27 00:26:45 +010025 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 });
avm999638824b8a2021-01-10 18:25:12 +010042 } else if (items.uniquetab == 'panel' || items.uniquetab == 'popup') {
avm999635a57c412020-12-27 00:26:45 +010043 chrome.windows.create(
44 {
avm999638824b8a2021-01-10 18:25:12 +010045 type: 'popup',
avm999635a57c412020-12-27 00:26:45 +010046 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 });
avm999634a2a5d52016-06-04 16:17:29 +020073}
74
avm99963ce257a92020-12-27 00:07:13 +010075function openOptionsPage() {
avm999635a57c412020-12-27 00:26:45 +010076 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 }
avm999634a2a5d52016-06-04 16:17:29 +020090}
91
92function createmenus() {
avm999635a57c412020-12-27 00:26:45 +010093 chrome.storage.sync.get('translateinto', function(items) {
94 chrome.contextMenus.removeAll();
avm999634a2a5d52016-06-04 16:17:29 +020095
avm999635a57c412020-12-27 00:26:45 +010096 var count = 0, singleone = true;
avm999634a2a5d52016-06-04 16:17:29 +020097
avm999635a57c412020-12-27 00:26:45 +010098 for (var language of Object.keys(items.translateinto)) {
99 if (count == 0) {
100 count++;
101 } else {
102 singleone = false;
103 break;
104 }
105 }
avm999634a2a5d52016-06-04 16:17:29 +0200106
avm999635a57c412020-12-27 00:26:45 +0100107 if (singleone) {
108 for (var language_id of Object.keys(items.translateinto)) {
109 var language = items.translateinto[language_id];
110 var languagem = isoLangs[language];
avm99963f65e9bf2020-12-29 12:57:16 +0100111 if (languagem === undefined) {
112 console.error(language + ' doesn\'t exist!');
113 continue;
114 }
avm999635a57c412020-12-27 00:26:45 +0100115 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];
avm99963f65e9bf2020-12-29 12:57:16 +0100132 if (languagem === undefined) {
133 console.error(language + ' doesn\'t exist!');
134 continue;
135 }
avm999635a57c412020-12-27 00:26:45 +0100136 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 });
avm999634a2a5d52016-06-04 16:17:29 +0200160}
161
162chrome.runtime.onInstalled.addListener(function(details) {
avm999635a57c412020-12-27 00:26:45 +0100163 chrome.storage.sync.get(null, function(items) {
164 if (details.reason == 'install') {
165 if (isEmpty(items)) {
avm999638824b8a2021-01-10 18:25:12 +0100166 var settings = {'translateinto': {}, 'uniquetab': 'popup'},
avm99963f65e9bf2020-12-29 12:57:16 +0100167 default_language_1 =
168 chrome.i18n.getMessage('@@ui_locale').replace('_', '-'),
169 default_language_2 =
avm999635a57c412020-12-27 00:26:45 +0100170 chrome.i18n.getMessage('@@ui_locale').split('_')[0];
avm99963ce257a92020-12-27 00:07:13 +0100171
avm99963f65e9bf2020-12-29 12:57:16 +0100172 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;
avm99963ce257a92020-12-27 00:07:13 +0100176
avm999635a57c412020-12-27 00:26:45 +0100177 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('.');
avm99963ce257a92020-12-27 00:07:13 +0100190
avm999635a57c412020-12-27 00:26:45 +0100191 // 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];
avm99963ce257a92020-12-27 00:07:13 +0100199
avm99963f65e9bf2020-12-29 12:57:16 +0100200 if (isoLangs[default_language] != undefined)
avm999635a57c412020-12-27 00:26:45 +0100201 settings.languages[default_language] = default_language;
avm99963ce257a92020-12-27 00:07:13 +0100202
Adrià Vilanova Martínez203bc432021-06-08 16:35:00 +0200203 chrome.storage.sync.set(settings);
avm999635a57c412020-12-27 00:26:45 +0100204 }
avm99963ce257a92020-12-27 00:07:13 +0100205
avm999635a57c412020-12-27 00:26:45 +0100206 // Updating from a version previous to v0.7
207 if (version[0] == '0' && version[1] < '7') {
208 items.translateinto = {};
209 var i = 0;
210 for (var language in items.languages) {
211 items.translateinto[i] = items.languages[language];
212 i++;
213 }
214 delete items.languages;
Adrià Vilanova Martínez203bc432021-06-08 16:35:00 +0200215 chrome.storage.sync.set(items);
avm999635a57c412020-12-27 00:26:45 +0100216 }
avm99963f65e9bf2020-12-29 12:57:16 +0100217
218 // Remove non-existent languages or change with correct language code
219 if (items.translateinto) {
220 var modified = false;
221 for (var language_id of Object.keys(items.translateinto)) {
222 var language = items.translateinto[language_id];
223 if (isoLangs[language] === undefined) {
224 if (convertLanguages[language] === undefined) {
225 // The language doesn't exist
226 console.log(
227 'Deleting ' + language +
228 ' from items.translateinto because it doesn\'t exist.');
229 delete items.translateinto[language_id];
230 } else {
231 // The language doesn't exist but a known replacement is known
232 var newLanguage = convertLanguages[language];
233 console.log('Replacing ' + language + ' with ' + newLanguage);
234
235 // If the converted language is already on the list, just remove
236 // the wrong language, otherwise convert the language
237 if (inObject(items.translateinto, newLanguage))
238 delete items.translateinto[language_id];
239 else
240 items.translateinto[language_id] = newLanguage;
241 }
242 modified = true;
243 }
244 }
245 if (modified) chrome.storage.sync.set(items);
246 } else {
247 console.log('items.translateinto doesn\'t exist: let\'s create it.');
248 items['translateinto'] = {};
249 chrome.storage.sync.set(items);
250 }
avm999635a57c412020-12-27 00:26:45 +0100251 }
252 });
avm999634a2a5d52016-06-04 16:17:29 +0200253});
254
avm99963ce257a92020-12-27 00:07:13 +0100255chrome.storage.onChanged.addListener((changes, areaName) => {
avm999635a57c412020-12-27 00:26:45 +0100256 if (areaName == 'sync') createmenus();
avm999634a2a5d52016-06-04 16:17:29 +0200257});
258
avm99963ce257a92020-12-27 00:07:13 +0100259chrome.storage.sync.get(null, items => {
avm999635a57c412020-12-27 00:26:45 +0100260 if (items.translateinto) {
261 createmenus();
262 } else {
263 chrome.contextMenus.removeAll();
264 var parent = chrome.contextMenus.create({
265 'id': 'tr_parent',
266 'title': chrome.i18n.getMessage('contextmenu_title'),
267 'contexts': ['selection']
268 });
269 var id = chrome.contextMenus.create({
270 'id': 'tr_options',
271 'title': chrome.i18n.getMessage('contextmenu_edit'),
272 'parentId': parent,
273 'contexts': ['selection']
274 });
275 }
avm999634a2a5d52016-06-04 16:17:29 +0200276});
277
avm99963ce257a92020-12-27 00:07:13 +0100278chrome.notifications.onClicked.addListener(notification_id => {
avm999635a57c412020-12-27 00:26:45 +0100279 switch (notification_id) {
280 case 'install':
avm999635a57c412020-12-27 00:26:45 +0100281 openOptionsPage();
282 break;
283 }
284 chrome.notifications.clear(notification_id);
avm999634a2a5d52016-06-04 16:17:29 +0200285});
286
avm99963ce257a92020-12-27 00:07:13 +0100287chrome.contextMenus.onClicked.addListener((info, tab) => {
avm999635a57c412020-12-27 00:26:45 +0100288 if (info.menuItemId == 'tr_options') {
289 openOptionsPage();
290 } else {
291 translationClick(info, tab);
292 }
avm999634a2a5d52016-06-04 16:17:29 +0200293});
avm99963ce257a92020-12-27 00:07:13 +0100294
295chrome.browserAction.onClicked.addListener(_ => {
avm999635a57c412020-12-27 00:26:45 +0100296 openOptionsPage();
avm99963ce257a92020-12-27 00:07:13 +0100297});