forked from amielboim/Xpress
-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
270 lines (201 loc) · 7.93 KB
/
background.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/**
* Created by Apptory on 8/3/2015.
*/
var translatePort = false;
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
/* Check if is redirect request */
if(typeof Redirect[request.type] == 'function')Redirect[request.type](request,sender);
/* translate string */
if(request.type == 'translate') {
var sender_id = (sender.tab == undefined || sender.tab.id == undefined) ? undefined : sender.tab.id;
if(!translatePort) {
sendResponse({status: 'opening'})
setTranslateTab(function() {
translatePort.postMessage({method: 'translate',string: request.string, sender_id: sender_id});
if(request.string.length > 140 || request.toGT == true)Redirect.goToGT(request,sender);
});
} else {
translatePort.postMessage({method: 'translate',string: request.string, sender_id: sender_id});
if(request.string.length > 140 || request.toGT == true)Redirect.goToGT(request,sender);
}
}
});
/**
* Return all details of google translate tab. is this tub not exist it will open.
* */
function setTranslateTab(callback){
chrome.tabs.query({
url: "*://translate.google.com/*"
}, function (tabs) {
if (tabs.length == 0) {
chrome.tabs.create({
index: 0,
url: "https://translate.google.com/",
active: false
}, function (tab) {
var handlerInterval = setInterval(function () {
chrome.tabs.get(tab.id, function (tab) {
if (tab.status === 'complete') {
connectToGoogleTranslate(tab);
callback();
clearInterval(handlerInterval);
}
})
}, 50);
})
} else {
connectToGoogleTranslate(tabs[0]);
callback();
}
});
}
/**
* create port with gooale translate
* */
function connectToGoogleTranslate(tab){
translatePort = chrome.tabs.connect(tab.id);
chrome.storage.sync.set({translateTab:tab});
translatePort.onMessage.addListener(function(msg) {
if(msg.sender_id != undefined)chrome.tabs.sendMessage(msg.sender_id, msg.result);
});
translatePort.onDisconnect.addListener(function(tab1){
translatePort = false;
chrome.storage.sync.set({translateTab:false});
});
/*
* listener when change tabs.
* For disappear last tab icon in google translate.
* */
chrome.tabs.onHighlighted.addListener(function(highlightInfo){
chrome.storage.sync.get('translateTab', function(data){
if(data.translateTab == undefined || data.translateTab == false)return;
if(data.translateTab.id != highlightInfo.tabIds){
if((translatePort != undefined || translatePort != false) && typeof translatePort.postMessage == 'function')translatePort.postMessage({method: "disappearIconTab"});
}
});
});
}
var Redirect = (function(){
/**
* redirect to Google Translate Page
* */
function goToGT(request, sender){
chrome.storage.sync.get('translateTab', function(data) {
if (!data.translateTab) {
chrome.tabs.create({
index: 0,
url: "https://translate.google.com/",
active: true
});
}else {
chrome.tabs.get(data.translateTab.id
, function (tab) {
if(tab.windowId == sender.tab.windowId){
chrome.tabs.update(tab.id, {active:true});
}else {
chrome.tabs.move(tab.id, {windowId:sender.tab.windowId, index:0}, function(tab){
chrome.tabs.update(tab.id, {active:true});
});
}
})
translatePort.postMessage({method: "setNewTab",comeFrom: sender});
}
});
}
/*
* Highlight specific tab
* */
function goBack(request){
chrome.tabs.update(request.tabId, {active:true});
}
/**
* open page practice in new tab
* */
function goToPractice(){
if (chrome.runtime.openOptionsPage) {
// New way to open options pages, if supported (Chrome 42+).
chrome.runtime.openOptionsPage();
} else {
// Reasonable fallback.
window.open(chrome.runtime.getURL('practice.html'));
}
}
/*
* close google translate
* */
function closeGt(){
chrome.storage.sync.get('translateTab', function(data) {
if (!data.translateTab)return;
chrome.tabs.remove(data.translateTab.id);
});
}
function updatePractice(request){
chrome.tabs.query({url: chrome.runtime.getURL('practice.html')}, function(tabs){
tabs.forEach(function(tab, key){
chrome.tabs.sendMessage(tab.id, {practicePageNewRecord:request.newRecord});
});
});
}
return{
goToGT:goToGT,
goBack:goBack,
goToPractice: goToPractice,
closeGt:closeGt,
updatePractice:updatePractice
}
})();
/* Start up actions */
chrome.runtime.onInstalled.addListener(function(){
chrome.storage.sync.get('lang_from', function(data) {
if(data.lang_from != undefined)return;
/* Starting configs */
chrome.storage.sync.set({autoClosing: false, is_from_english: true, lang_from: {dir: 'ltr', lang: 'English', code: 'en'}, lang_to: {dir: 'ltr', lang: 'English', code: 'en'}, dictionary: []});
/* Notification with first explains */
chrome.notifications.create({
type: 'image',
iconUrl: chrome.runtime.getURL('img/logo_128.png'),
title: "Xpress - Welcome!!",
message: "Thank you for the installation!! \n First - Please config your languages.",
imageUrl: chrome.runtime.getURL('img/select_lang.png'),
priority: 2
}, function (notificationId) {
chrome.notifications.onClosed.addListener(function (notId, byUser) {
if (notId === notificationId) {
chrome.notifications.create({
type: 'image',
iconUrl: chrome.runtime.getURL('img/logo_128.png'),
title: "Xpress - Enjoy!!",
message: "After this, you can begin.. reload open windows.. Select some words and click.. ",
imageUrl: chrome.runtime.getURL('img/selection_2.png'),
priority: 2
});
}
});
chrome.tabs.query({
url: "https://translate.google.com/*"
}, function (tabs) {
chrome.tabs.reload(tabs[0].id);
});
});
/* inject to all tabs */
/* chrome.permissions.onAdded.addListener(function(){
chrome.tabs.query({url:'<all_urls>'}, function(tabs){
tabs.forEach(function(tab, key){
chrome.tabs.insertCSS(tab.id, {file:chrome.runtime.getURL('css/content.css')}, function(){
chrome.tabs.executeScript(tab.id, {file:chrome.runtime.getURL('js/inject.js')});
});
});
});
});*/
});
});
/* google analytics */
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-73749689-1']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = 'https://ssl.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();