/
background.js
174 lines (149 loc) · 5.16 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
RegExp.quote = function(str) {
return (str+'').replace(/([.?*+^$[\]\\(){}|-])/g, "\\$1");
};
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-46863240-1']);
_gaq.push(['_trackPageview']);
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);
function translate(word, sl, tl, last_translation, onresponse, sendResponse, ga_event_name) {
var options = {
url: "https://translate.googleapis.com/translate_a/single?dt=t&dt=bd",
data: {
client: 'gtx',
q: word,
sl: sl,
tl: tl,
dj: 1,
source: 'bubble'
},
dataType: 'json',
success: function on_success(data) {
onresponse(data, word, tl, last_translation, sendResponse, ga_event_name);
},
error: function(xhr, status, e) {
console.log({e: e, xhr: xhr});
}
};
$.ajax(options);
}
function figureOutSlTl(tab_lang) {
var res = {};
if (Options.target_lang() == tab_lang && Options.reverse_lang()) {
res.tl = Options.reverse_lang();
res.sl = Options.target_lang();
console.log('reverse translate into: ', {tl: res.tl, sl: res.sl});
}
else {
res.tl = Options.target_lang();
res.sl = Options.from_lang();
console.log('normal translate into:', {tl: res.tl, sl: res.sl});
}
return res;
}
function on_translation_response(data, word, tl, last_translation, sendResponse, ga_event_name) {
var output, translation = {tl: tl};
console.log('raw_translation: ', data);
if (!data.dict && !data.sentences ||
(data.sentences && data.sentences[0].trans.match(new RegExp(TransOver.regexp_escape(word), 'i')))) {
translation.succeeded = false;
if (data.src == tl || Options.do_not_show_oops()) {
output = '';
}
else {
output = 'Oops.. No translation found.';
}
}
else {
translation.succeeded = true;
translation.word = word;
output = [];
if (data.dict) { // full translation
data.dict.forEach(function(t) {
output.push({pos: t.pos, meanings: t.terms});
});
} else { // single word or sentence(s)
data.sentences.forEach(function(s) {
output.push(s.trans)
})
output = output.join(" ")
}
translation.sl = data.src;
}
if (! output instanceof String) {
output = JSON.stringify(output);
}
translation.translation = output;
$.extend(last_translation, translation);
_gaq.push(['_trackEvent', ga_event_name, translation.sl, translation.tl]);
console.log('response: ', translation);
sendResponse(translation);
}
var last_translation = {};
chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
switch (request.handler) {
case 'get_last_tat_sl_tl':
console.log('get_last_tat_sl_tl');
sendResponse({last_tl: localStorage['last_tat_tl'], last_sl: localStorage['last_tat_sl']});
break;
case 'get_options':
sendResponse({
options: JSON.stringify({
except_urls: Options.except_urls(),
only_urls: Options.only_urls(),
target_lang: Options.target_lang(),
reverse_lang: Options.reverse_lang(),
delay: Options.delay(),
word_key_only: Options.word_key_only(),
selection_key_only: Options.selection_key_only(),
tts: Options.tts(),
tts_key: Options.tts_key(),
popup_show_trigger: Options.popup_show_trigger(),
translate_by: Options.translate_by()
})
});
break;
case 'translate':
console.log("received to translate: " + request.word);
chrome.tabs.detectLanguage(null, function(tab_lang) {
var sl, tl;
// hack: presence of request.tl/sl means this came from popup translate
if (request.tl && request.sl) {
localStorage['last_tat_tl'] = request.tl;
localStorage['last_tat_sl'] = request.sl;
sl = request.sl;
tl = request.tl;
} else {
var sltl = figureOutSlTl(tab_lang);
sl = sltl.sl;
tl = sltl.tl;
}
translate(request.word, sl, tl, last_translation, on_translation_response, sendResponse, Options.translate_by());
});
break;
case 'tts':
if (last_translation.succeeded) {
console.log("tts: " + last_translation.word + ", sl: " + last_translation.sl);
_gaq.push(['_trackEvent', 'tts', last_translation.sl, last_translation.tl]);
var msg = new SpeechSynthesisUtterance();
msg.lang = last_translation.sl;
msg.text = last_translation.word;
msg.rate = 0.7;
speechSynthesis.speak(msg);
}
sendResponse({});
break;
default:
console.log("Error! Unknown handler");
sendResponse({});
}
});
chrome.browserAction.onClicked.addListener(function(tab) {
chrome.tabs.sendRequest(tab.id, 'open_type_and_translate');
});
chrome.runtime.onInstalled.addListener(function(details) {
if (details.reason == 'install') {
chrome.tabs.create({url: chrome.extension.getURL('options.html')});
}
});