blob: 7058875ed03ccd6560334abfbd86afe992ad09c7 [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
avm999635a57c412020-12-27 00:26:45 +0100203 chrome.storage.sync.set(settings, function() {
204 chrome.notifications.create('upgradedtostorage', {
205 type: 'basic',
206 iconUrl: 'icons/translate-128.png',
207 title:
208 chrome.i18n.getMessage('notification_upgradedtostorage_title'),
209 message: chrome.i18n.getMessage(
210 'notification_upgradedtostorage_message'),
211 isClickable: true,
212 });
213 });
214 }
avm99963ce257a92020-12-27 00:07:13 +0100215
avm999635a57c412020-12-27 00:26:45 +0100216 // Updating from a version previous to v0.7
217 if (version[0] == '0' && version[1] < '7') {
218 items.translateinto = {};
219 var i = 0;
220 for (var language in items.languages) {
221 items.translateinto[i] = items.languages[language];
222 i++;
223 }
224 delete items.languages;
225 chrome.storage.sync.set(items, _ => {
226 chrome.notifications.create('reorder', {
227 type: 'basic',
228 iconUrl: 'icons/translate-128.png',
229 title: chrome.i18n.getMessage('notification_reorder_title'),
230 message: chrome.i18n.getMessage('notification_reorder_message'),
231 isClickable: true
232 });
233 });
234 }
avm99963f65e9bf2020-12-29 12:57:16 +0100235
236 // Remove non-existent languages or change with correct language code
237 if (items.translateinto) {
238 var modified = false;
239 for (var language_id of Object.keys(items.translateinto)) {
240 var language = items.translateinto[language_id];
241 if (isoLangs[language] === undefined) {
242 if (convertLanguages[language] === undefined) {
243 // The language doesn't exist
244 console.log(
245 'Deleting ' + language +
246 ' from items.translateinto because it doesn\'t exist.');
247 delete items.translateinto[language_id];
248 } else {
249 // The language doesn't exist but a known replacement is known
250 var newLanguage = convertLanguages[language];
251 console.log('Replacing ' + language + ' with ' + newLanguage);
252
253 // If the converted language is already on the list, just remove
254 // the wrong language, otherwise convert the language
255 if (inObject(items.translateinto, newLanguage))
256 delete items.translateinto[language_id];
257 else
258 items.translateinto[language_id] = newLanguage;
259 }
260 modified = true;
261 }
262 }
263 if (modified) chrome.storage.sync.set(items);
264 } else {
265 console.log('items.translateinto doesn\'t exist: let\'s create it.');
266 items['translateinto'] = {};
267 chrome.storage.sync.set(items);
268 }
avm999635a57c412020-12-27 00:26:45 +0100269 }
270 });
avm999634a2a5d52016-06-04 16:17:29 +0200271});
272
avm99963ce257a92020-12-27 00:07:13 +0100273chrome.storage.onChanged.addListener((changes, areaName) => {
avm999635a57c412020-12-27 00:26:45 +0100274 if (areaName == 'sync') createmenus();
avm999634a2a5d52016-06-04 16:17:29 +0200275});
276
avm99963ce257a92020-12-27 00:07:13 +0100277chrome.storage.sync.get(null, items => {
avm999635a57c412020-12-27 00:26:45 +0100278 if (items.translateinto) {
279 createmenus();
280 } else {
281 chrome.contextMenus.removeAll();
282 var parent = chrome.contextMenus.create({
283 'id': 'tr_parent',
284 'title': chrome.i18n.getMessage('contextmenu_title'),
285 'contexts': ['selection']
286 });
287 var id = chrome.contextMenus.create({
288 'id': 'tr_options',
289 'title': chrome.i18n.getMessage('contextmenu_edit'),
290 'parentId': parent,
291 'contexts': ['selection']
292 });
293 }
avm999634a2a5d52016-06-04 16:17:29 +0200294});
295
avm99963ce257a92020-12-27 00:07:13 +0100296chrome.notifications.onClicked.addListener(notification_id => {
avm999635a57c412020-12-27 00:26:45 +0100297 switch (notification_id) {
298 case 'install':
299 case 'upgradedtostorage':
300 case 'reorder':
301 openOptionsPage();
302 break;
303 }
304 chrome.notifications.clear(notification_id);
avm999634a2a5d52016-06-04 16:17:29 +0200305});
306
avm99963ce257a92020-12-27 00:07:13 +0100307chrome.contextMenus.onClicked.addListener((info, tab) => {
avm999635a57c412020-12-27 00:26:45 +0100308 if (info.menuItemId == 'tr_options') {
309 openOptionsPage();
310 } else {
311 translationClick(info, tab);
312 }
avm999634a2a5d52016-06-04 16:17:29 +0200313});
avm99963ce257a92020-12-27 00:07:13 +0100314
315chrome.browserAction.onClicked.addListener(_ => {
avm999635a57c412020-12-27 00:26:45 +0100316 openOptionsPage();
avm99963ce257a92020-12-27 00:07:13 +0100317});