-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopup.js
58 lines (47 loc) · 1.28 KB
/
popup.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
function Translate(){
var self = this;
var baseUrlService = "http://api.mymemory.translated.net/get"
var textAreaTranslated = $('#textTranslated')
var selectedText = ""
var showDefaultError = function(e){
console.log(e)
alert('Oops, ocorreu um erro. Avise-nos sobre isso.')
}
var remoteTranslate = function(from, to, text){
var params = {
'q': text,
'langpair': from + "|" + to
}
$.ajax({
url: baseUrlService,
data: params,
success: function(result){
textAreaTranslated.text(result.responseData.translatedText)
},
error: function(e){
showDefaultError(e)
}
})
}
self.translate = function(from, to, text){
if(!from || !from.trim() || !to || !to.trim()){
return
}
if(!text){
// Captura o texto que está selecionado na página
chrome.tabs.getSelected(null, function(tab){
chrome.tabs.executeScript(tab.id, {code: "document.getSelection().toString()"}, function(response) {
selectedText = response[0]
if(selectedText){
remoteTranslate(from, to, selectedText)
}
});
});
}
}
}
// Run our kitten generation script as soon as the document's DOM is ready.
document.addEventListener('DOMContentLoaded', function () {
$translate = new Translate()
$translate.translate('en', 'pt')
});