blob: b7857c8c151e829fbf26842add214dffb4206d0f [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
avm99963ce257a92020-12-27 00:07:13 +01007function getTranslationUrl(lang, text) {
avm999635a57c412020-12-27 00:26:45 +01008 var params = new URLSearchParams({
9 sl: 'auto',
10 tl: lang,
11 text: text,
12 op: 'translate',
13 });
14 return 'https://translate.google.com/?' + params.toString();
avm99963ce257a92020-12-27 00:07:13 +010015}
16
17function translationClick(info, tab) {
avm999635a57c412020-12-27 00:26:45 +010018 console.log(info.selectionText);
19 chrome.storage.sync.get('uniquetab', items => {
20 var url = getTranslationUrl(
21 array_elements[info.menuItemId]['langCode'], info.selectionText);
22 var settings_tab = {url};
23 if (translator_tab && items.uniquetab == 'yep') {
24 chrome.tabs.update(translator_tab, settings_tab, tab => {
25 chrome.tabs.highlight(
26 {
27 windowId: tab.windowId,
28 tabs: tab.index,
29 },
30 _ => {
31 chrome.windows.update(tab.windowId, {
32 focused: true,
33 });
34 });
35 });
36 } else if (items.uniquetab == 'panel') {
37 chrome.windows.create(
38 {
39 type: 'panel',
40 url,
41 width: 1000,
42 height: 382,
43 },
44 function(tab) {
45 translator_window = tab.windowId;
46 translator_tab = tab.id;
47 chrome.windows.onRemoved.addListener(function(windowId) {
48 if (windowId == translator_window) {
49 translator_window = null;
50 translator_tab = null;
51 }
52 });
53 });
54 } else {
55 chrome.tabs.create(settings_tab, function(tab) {
56 translator_window = tab.windowId;
57 translator_tab = tab.id;
58 chrome.tabs.onRemoved.addListener(function(tabId, removeInfo) {
59 if (tabId == translator_tab) {
60 translator_window = null;
61 translator_tab = null;
62 }
63 });
64 });
65 }
66 });
avm999634a2a5d52016-06-04 16:17:29 +020067}
68
avm99963ce257a92020-12-27 00:07:13 +010069function openOptionsPage() {
avm999635a57c412020-12-27 00:26:45 +010070 if (chrome.runtime.openOptionsPage) {
71 // New way to open options pages, if supported (Chrome 42+).
72 chrome.runtime.openOptionsPage();
73 } else {
74 chrome.tabs.create(
75 {
76 'url': 'chrome-extension://' +
77 chrome.i18n.getMessage('@@extension_id') + '/options.html',
78 'active': true
79 },
80 tab => {
81 chrome.windows.update(tab.windowId, {focused: true});
82 });
83 }
avm999634a2a5d52016-06-04 16:17:29 +020084}
85
86function createmenus() {
avm999635a57c412020-12-27 00:26:45 +010087 chrome.storage.sync.get('translateinto', function(items) {
88 chrome.contextMenus.removeAll();
avm999634a2a5d52016-06-04 16:17:29 +020089
avm999635a57c412020-12-27 00:26:45 +010090 var count = 0, singleone = true;
avm999634a2a5d52016-06-04 16:17:29 +020091
avm999635a57c412020-12-27 00:26:45 +010092 for (var language of Object.keys(items.translateinto)) {
93 if (count == 0) {
94 count++;
95 } else {
96 singleone = false;
97 break;
98 }
99 }
avm999634a2a5d52016-06-04 16:17:29 +0200100
avm999635a57c412020-12-27 00:26:45 +0100101 if (singleone) {
102 for (var language_id of Object.keys(items.translateinto)) {
103 var language = items.translateinto[language_id];
104 var languagem = isoLangs[language];
105 var id = chrome.contextMenus.create({
106 'id': 'tr_single_parent',
107 'title': chrome.i18n.getMessage('contextmenu_title2', languagem.name),
108 'contexts': ['selection'],
109 });
110 array_elements[id] = new Array();
111 array_elements[id]['langCode'] = language;
112 }
113 } else {
114 var parentEl = chrome.contextMenus.create({
115 'id': 'parent',
116 'title': chrome.i18n.getMessage('contextmenu_title'),
117 'contexts': ['selection']
118 });
119 for (var language_id of Object.keys(items.translateinto)) {
120 var language = items.translateinto[language_id];
121 var languagem = isoLangs[language];
122 var title = languagem.name + ' (' + languagem.nativeName + ')';
123 var id = chrome.contextMenus.create({
124 'id': 'tr_language_' + language,
125 'title': title,
126 'parentId': parentEl,
127 'contexts': ['selection']
128 });
129 array_elements[id] = new Array();
130 array_elements[id]['langCode'] = language;
131 }
132 chrome.contextMenus.create({
133 'id': 'tr_separator',
134 'type': 'separator',
135 'parentId': parentEl,
136 'contexts': ['selection']
137 });
138 chrome.contextMenus.create({
139 'id': 'tr_options',
140 'title': chrome.i18n.getMessage('contextmenu_edit'),
141 'parentId': parentEl,
142 'contexts': ['selection']
143 });
144 }
145 });
avm999634a2a5d52016-06-04 16:17:29 +0200146}
147
148chrome.runtime.onInstalled.addListener(function(details) {
avm999635a57c412020-12-27 00:26:45 +0100149 chrome.storage.sync.get(null, function(items) {
150 if (details.reason == 'install') {
151 if (isEmpty(items)) {
152 var settings = {'translateinto': {}, 'uniquetab': ''},
153 default_language =
154 chrome.i18n.getMessage('@@ui_locale').split('_')[0];
avm99963ce257a92020-12-27 00:07:13 +0100155
avm999635a57c412020-12-27 00:26:45 +0100156 if (isoLangs[default_language] != 'undefined') {
157 settings.translateinto[default_language] = default_language;
158 }
avm99963ce257a92020-12-27 00:07:13 +0100159
avm999635a57c412020-12-27 00:26:45 +0100160 chrome.storage.sync.set(settings, function() {
161 chrome.notifications.create('install', {
162 type: 'basic',
163 iconUrl: 'icons/translate-128.png',
164 title: chrome.i18n.getMessage('notification_install_title'),
165 message: chrome.i18n.getMessage('notification_install_message'),
166 isClickable: true
167 });
168 });
169 }
170 }
171 if (details.reason == 'update') {
172 var version = details.previousVersion.split('.');
avm99963ce257a92020-12-27 00:07:13 +0100173
avm999635a57c412020-12-27 00:26:45 +0100174 // Updating from a version previous to v0.6
175 if (version[0] == '0' && version[1] < '6') {
176 var settings = {
177 languages: {},
178 uniquetab: '',
179 };
180 var default_language =
181 chrome.i18n.getMessage('@@ui_locale').split('_')[0];
avm99963ce257a92020-12-27 00:07:13 +0100182
avm999635a57c412020-12-27 00:26:45 +0100183 if (isoLangs[default_language] != 'undefined')
184 settings.languages[default_language] = default_language;
avm99963ce257a92020-12-27 00:07:13 +0100185
avm999635a57c412020-12-27 00:26:45 +0100186 chrome.storage.sync.set(settings, function() {
187 chrome.notifications.create('upgradedtostorage', {
188 type: 'basic',
189 iconUrl: 'icons/translate-128.png',
190 title:
191 chrome.i18n.getMessage('notification_upgradedtostorage_title'),
192 message: chrome.i18n.getMessage(
193 'notification_upgradedtostorage_message'),
194 isClickable: true,
195 });
196 });
197 }
avm99963ce257a92020-12-27 00:07:13 +0100198
avm999635a57c412020-12-27 00:26:45 +0100199 // Updating from a version previous to v0.7
200 if (version[0] == '0' && version[1] < '7') {
201 items.translateinto = {};
202 var i = 0;
203 for (var language in items.languages) {
204 items.translateinto[i] = items.languages[language];
205 i++;
206 }
207 delete items.languages;
208 chrome.storage.sync.set(items, _ => {
209 chrome.notifications.create('reorder', {
210 type: 'basic',
211 iconUrl: 'icons/translate-128.png',
212 title: chrome.i18n.getMessage('notification_reorder_title'),
213 message: chrome.i18n.getMessage('notification_reorder_message'),
214 isClickable: true
215 });
216 });
217 }
218 }
219 });
avm999634a2a5d52016-06-04 16:17:29 +0200220});
221
avm99963ce257a92020-12-27 00:07:13 +0100222chrome.storage.onChanged.addListener((changes, areaName) => {
avm999635a57c412020-12-27 00:26:45 +0100223 if (areaName == 'sync') createmenus();
avm999634a2a5d52016-06-04 16:17:29 +0200224});
225
avm99963ce257a92020-12-27 00:07:13 +0100226chrome.storage.sync.get(null, items => {
avm999635a57c412020-12-27 00:26:45 +0100227 if (items.translateinto) {
228 createmenus();
229 } else {
230 chrome.contextMenus.removeAll();
231 var parent = chrome.contextMenus.create({
232 'id': 'tr_parent',
233 'title': chrome.i18n.getMessage('contextmenu_title'),
234 'contexts': ['selection']
235 });
236 var id = chrome.contextMenus.create({
237 'id': 'tr_options',
238 'title': chrome.i18n.getMessage('contextmenu_edit'),
239 'parentId': parent,
240 'contexts': ['selection']
241 });
242 }
avm999634a2a5d52016-06-04 16:17:29 +0200243});
244
avm99963ce257a92020-12-27 00:07:13 +0100245chrome.notifications.onClicked.addListener(notification_id => {
avm999635a57c412020-12-27 00:26:45 +0100246 switch (notification_id) {
247 case 'install':
248 case 'upgradedtostorage':
249 case 'reorder':
250 openOptionsPage();
251 break;
252 }
253 chrome.notifications.clear(notification_id);
avm999634a2a5d52016-06-04 16:17:29 +0200254});
255
avm99963ce257a92020-12-27 00:07:13 +0100256chrome.contextMenus.onClicked.addListener((info, tab) => {
avm999635a57c412020-12-27 00:26:45 +0100257 if (info.menuItemId == 'tr_options') {
258 openOptionsPage();
259 } else {
260 translationClick(info, tab);
261 }
avm999634a2a5d52016-06-04 16:17:29 +0200262});
avm99963ce257a92020-12-27 00:07:13 +0100263
264chrome.browserAction.onClicked.addListener(_ => {
avm999635a57c412020-12-27 00:26:45 +0100265 openOptionsPage();
avm99963ce257a92020-12-27 00:07:13 +0100266});