-
Notifications
You must be signed in to change notification settings - Fork 1
/
background.js
90 lines (73 loc) · 2.39 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
function getDictionaryOption(){
return $('#dict').text();
}
function setDictonayOption( option ){
$('#dict').text(option);
}
function XmlHTTPRequest( URL, callback ){
var xhr = new XMLHttpRequest();
xhr.open("GET", URL, false);
xhr.onreadystatechange = function() {
if (xhr.readyState == 4 && xhr.status==200) {
//document.getElementById("add").innerHTML = xhr.responseText;
callback(xhr.responseText);
}
};
xhr.send();
}
function composeURL( dictType, selectedWord ) {
if( dictType === "0.0" ) {
return "http://www.gujaratilexicon.com/dictionary/EG/" + selectedWord;
}
else if( dictType === "1.1" ) {
return "http://www.gujaratilexicon.com/dictionary/GG/" + selectedWord;
}
else if( dictType === "1.2" ) {
return "http://www.gujaratilexicon.com/dictionary/GE/" + selectedWord;
}
else if(dictType === "2.0") {
return "http://www.gujaratilexicon.com/dictionary/HG/" + selectedWord;
}
}
function sendTranslation( responsehandler, selectedWord, exactWordInWebPage, pronunciation, finalTranslation ) {
if(finalTranslation !== "") {
if(selectedWord.toLowerCase() === exactWordInWebPage.split(' ').join('')) {
responsehandler({ TW: finalTranslation,
PRONUNCIATION : pronunciation });
}
else {
responsehandler({TW: "No Exact Match Found.!!"});
}
}
else {
responsehandler({TW: "Not Found.!!"});
}
}
chrome.extension.onMessage.addListener(
function( request, sender, sendResponse ) {
if(request.getDictOpt) {
sendResponse({ curDictOpt: getDictionaryOption() });
}
else if( request.setDictOpt ) {
setDictonayOption(request.setDictOpt);
sendResponse({Not: "null"});
}
if(request.SW) {
var tempDictAndWord = request.SW;
// var dictionaryType = parseInt(tempDictAndWord[0]);
//if(dictionaryType === 1)
{
var tempSelectedWord = tempDictAndWord.substr(3, tempDictAndWord.length);
dictionaryType = tempDictAndWord.substr(0 , 3);
}
var URL = composeURL(dictionaryType, tempSelectedWord);
XmlHTTPRequest(URL, function(tempWebpage) {
document.getElementById('ab').innerHTML = tempWebpage;
var selectAllMetaTag = $('#ab meta');
var exactWordInWebpage = $('#ab #details .rc_border .wordbox h1').text();
var pronunciation = $( $('#details .rc_border table tbody td')[2] ).text();
var finalTranslation = selectAllMetaTag[1]["content"];
sendTranslation( sendResponse , tempSelectedWord , exactWordInWebpage , pronunciation, finalTranslation );
});
}
});