-
Notifications
You must be signed in to change notification settings - Fork 0
/
extension.js
72 lines (52 loc) · 2.12 KB
/
extension.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
var key = 'trnsl.1.1.20160531T091436Z.507c846faf27c7d3.070941436e4c223200d2d95c18988163e3f62b2f';
var apiHost = 'https://translate.yandex.net/api/v1.5/tr.json/translate';
var lang = encodeURIComponent('en-ru');
init();
function init(){
document.addEventListener('mouseup', function(){
var selection = window.getSelection();
var selectionStr = selection.toString();
selectionStr = selectionStr.replace(/[а-яё]*/ig, '').trim();
if (!selectionStr) return;
insertScript(selectionStr);
});
var container = document.createElement('div');
container.className = '__y-translate-container';
var clientScript = document.createElement('script');
clientScript.innerHTML = `
var yTranslatePlugin = {
getData: function(data){
console.log('data');
console.log(data);
yTranslatePlugin.showResult(data.text.join(' '));
},
showResult: function(text){
console.log('text');
console.log(text);
clearTimeout(this.showTimer);
var showContainer = document.querySelector('.__y-translate-container');
showContainer.textContent = text;
showContainer.className = '__y-translate-container __y-translate-container_showed_yes';
this.showTimer = setTimeout(function(){
showContainer.className = '__y-translate-container';
}, 3000);
},
showTimer: 3000
};
`;
document.addEventListener('DOMContentLoaded', function() {
document.querySelector('body').appendChild(clientScript);
document.querySelector('body').appendChild(container);
});
}
function insertScript(selectionStr){
var script = buildScript(selectionStr);
document
.querySelector('body')
.appendChild(script);
}
function buildScript(selectionStr){
var script = document.createElement('script');
script.src=`${apiHost}?key=${key}&text=${selectionStr}&lang=${lang}&format=plain&callback=yTranslatePlugin.getData`;
return script;
}