forked from artemave/translate_onhover
-
Notifications
You must be signed in to change notification settings - Fork 2
/
contentscript.js
340 lines (273 loc) · 10.5 KB
/
contentscript.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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
var debug = false;
function log() {
if (debug) {
console.log(arguments);
}
}
chrome.extension.sendRequest({handler: 'get_options'}, function(response) {
function process(e) {
function getHitWord(e) {
function escape_html(text) {
return text.replace(XRegExp("(<|>|&)", 'g'), function ($0, $1) {
switch ($1) {
case '<': return "<";
case '>': return ">";
case '&': return "&";
}
});
}
function restorable(node, do_stuff) {
$(node).wrap('<transwrapper />');
var res = do_stuff(node);
$('transwrapper').replaceWith(escape_html( $('transwrapper').text() ));
return res;
}
function getExactTextNode(nodes, e) {
$(text_nodes).wrap('<transblock />');
var hit_text_node = document.elementFromPoint(e.clientX, e.clientY);
//means we hit between the lines
if (hit_text_node.nodeName != 'TRANSBLOCK') {
$(text_nodes).unwrap();
return null;
}
hit_text_node = hit_text_node.childNodes[0];
$(text_nodes).unwrap();
return hit_text_node;
}
var hit_elem = $(document.elementFromPoint(e.clientX, e.clientY));
var word_re = "\\p{L}{2,}";
var parent_font_style = {
'line-height': hit_elem.css('line-height'),
'font-size': '1em',
'font-family': hit_elem.css('font-family')
};
var text_nodes = hit_elem.contents().filter(function(){
return this.nodeType == Node.TEXT_NODE && XRegExp(word_re).test( this.nodeValue )
});
if (text_nodes.length == 0) {
log('no text');
return '';
}
var hit_text_node = getExactTextNode(text_nodes, e);
if (!hit_text_node) {
log('hit between lines');
return '';
}
var hit_word = restorable(hit_text_node, function(node) {
var hw = '';
function getHitText(node, parent_font_style) {
log("getHitText: '" + node.textContent + "'");
if (XRegExp(word_re).test( node.textContent )) {
$(node).replaceWith(function() {
return this.textContent.replace(XRegExp("^(.{"+Math.round( node.textContent.length/2 )+"}\\p{L}*)(.*)", 's'), function($0, $1, $2) {
return '<transblock>'+escape_html($1)+'</transblock><transblock>'+escape_html($2)+'</transblock>';
});
});
$('transblock').css(parent_font_style);
var next_node = document.elementFromPoint(e.clientX, e.clientY).childNodes[0];
if (next_node.textContent == node.textContent) {
return next_node;
}
else {
return getHitText(next_node, parent_font_style);
}
}
else {
return null;
}
}
var minimal_text_node = getHitText(hit_text_node, parent_font_style);
if (minimal_text_node) {
//wrap words inside text node into <transover> element
$(minimal_text_node).replaceWith(function() {
return this.textContent.replace(XRegExp("(<|>|&|\\p{L}+)", 'g'), function ($0, $1) {
switch ($1) {
case '<': return "<";
case '>': return ">";
case '&': return "&";
default: return '<transover>'+$1+'</transover>';
}
});
});
$('transover').css(parent_font_style);
//get the exact word under cursor
var hit_word_elem = document.elementFromPoint(e.clientX, e.clientY);
//no word under cursor? we are done
if (hit_word_elem.nodeName != 'TRANSOVER') {
log("missed!");
}
else {
hw = $(hit_word_elem).text();
log("got it: "+hw);
}
}
return hw;
});
return hit_word;
}
var selection = window.getSelection();
var hit_elem = document.elementFromPoint(e.clientX, e.clientY);
//don't mess around with html inputs
if (/INPUT|TEXTAREA/.test( hit_elem.nodeName )) {
return;
}
//and editable divs
if (hit_elem.getAttribute('contenteditable') == 'true' || $(hit_elem).parents('[contenteditable=true]').length > 0) {
return;
}
var word = '';
if (selection.toString()) {
if (options.selection_key_only) {
log('Skip because "selection_key_only"');
return;
}
log('Got selection: ' + selection.toString());
var sel_container = selection.getRangeAt(0).commonAncestorContainer;
while (sel_container.nodeType != Node.ELEMENT_NODE) {
sel_container = sel_container.parentNode;
}
if (
// only choose selection if mouse stopped within immediate parent of selection
( $(hit_elem).is(sel_container) || $.contains(sel_container, hit_elem) )
// and since it can still be quite a large area
// narrow it down by only choosing selection if mouse points at the element that is (partially) inside selection
&& selection.containsNode(hit_elem, true)
// But what is the point for the first part of condition? Well, without it, pointing at body for instance would also satisfy the second part
// resulting in selection translation showing up in random places
) {
word = selection.toString();
}
else if (options.translate_by == 'point') {
word = getHitWord(e);
}
}
else {
word = getHitWord(e);
}
if (word != '') {
chrome.extension.sendRequest({handler: 'translate', word: word}, function(response) {
log('response: ', response);
var translation = TransOver.deserialize(response.translation);
if (!translation) {
log('skipping empty translation');
return;
}
tooltip.show(e.clientX, e.clientY, TransOver.formatTranslation(translation), getLangDirection(response.tl));
});
}
}
var start_tip_has_already_popped_up = false;
function withOptionsSatisfied(e, do_stuff) {
if (options.target_lang) {
//respect 'translate only when alt pressed' option
if (options.word_key_only && !show_popup_key_pressed) { return }
//respect "don't translate these sites"
if ($.grep(options.except_urls, function(url) { return RegExp(url).test(window.location.href) }).length > 0) { return }
do_stuff();
}
else {
if (start_tip.is_hidden() && !start_tip_has_already_popped_up) {
var text = 'Please, <a target="_blank" href="'+chrome.extension.getURL('options.html')+'">choose language</a> to translate into.';
start_tip.show(e.clientX, e.clientY, '<div class="pos_translation">'+text+'</div>');
start_tip_has_already_popped_up = true;
}
}
}
var options = JSON.parse( response.options );
var tooltip = new Tooltip({dismiss_on: 'mousemove'});
var start_tip = new Tooltip({dismiss_on: 'escape'});
$(document).on('mousestop', function(e) {
withOptionsSatisfied(e, function() {
// translate selection unless 'translate selection on alt only' is set
if (window.getSelection().toString()) {
if (!options.selection_key_only) {
process(e);
}
} else {
if (options.translate_by == 'point') {
process(e);
}
}
});
});
$(document).click(function(e) {
withOptionsSatisfied(e, function() {
if (options.translate_by != 'click')
return
if ($(e.target).closest('a').length > 0)
return
process(e);
});
return true;
});
var show_popup_key_pressed = false;
$(document)
.keydown(function(e) {
if (TransOver.modifierKeys[event.keyCode] == options.popup_show_trigger) {
show_popup_key_pressed = true;
var selection = window.getSelection().toString();
if (options.selection_key_only && selection) {
log('Got selection_key_only');
chrome.extension.sendRequest({handler: 'translate', word: selection}, function(response) {
log('response: ', response);
var translation = TransOver.deserialize(response.translation);
if (!translation) {
log('skipping empty translation');
return;
}
tooltip.show(last_mouse_stop.x, last_mouse_stop.y, TransOver.formatTranslation(translation), getLangDirection(response.tl));
});
}
}
// text-to-speech on ctrl press
if (e.keyCode == 17 && options.tts && (tooltip.is_visible() || type_and_translate_tooltip.is_visible())) {
log("tts");
chrome.extension.sendRequest({handler: 'tts'});
}
}).keyup(function(event) {
if (TransOver.modifierKeys[event.keyCode] == options.popup_show_trigger) {
show_popup_key_pressed = false;
}
});
function hasMouseReallyMoved(e) { //or is it a tremor?
var left_boundry = parseInt(last_mouse_stop.x) - 5,
right_boundry = parseInt(last_mouse_stop.x) + 5,
top_boundry = parseInt(last_mouse_stop.y) - 5,
bottom_boundry = parseInt(last_mouse_stop.y) + 5;
return e.clientX > right_boundry || e.clientX < left_boundry || e.clientY > bottom_boundry || e.clientY < top_boundry;
}
$(document).mousemove(function(e) {
if (hasMouseReallyMoved(e)) {
var mousemove_without_noise = new $.Event('mousemove_without_noise');
mousemove_without_noise.clientX = e.clientX;
mousemove_without_noise.clientY = e.clientY;
$(document).trigger(mousemove_without_noise);
}
})
var timer25;
var last_mouse_stop = {x: 0, y: 0};
// setup mousestop event
$(document).on('mousemove_without_noise', function(e){
clearTimeout(timer25);
var delay = options.delay;
if (window.getSelection().toString()) {
if (options.selection_key_only) {
delay = 200;
}
} else {
if (options.word_key_only) {
delay = 200;
}
}
timer25 = setTimeout(function() {
var mousestop = new $.Event("mousestop");
last_mouse_stop.x = mousestop.clientX = e.clientX;
last_mouse_stop.y = mousestop.clientY = e.clientY;
$(document).trigger(mousestop);
}, delay);
});
if (window == window.top) {
var type_and_translate_tooltip = new Tooltip({dismiss_on: 'escape'});
new TypeAndTranslate(chrome, type_and_translate_tooltip, options, log);
}
});