-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpopup.js
More file actions
131 lines (117 loc) · 3.76 KB
/
popup.js
File metadata and controls
131 lines (117 loc) · 3.76 KB
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
// var btn = document.getElementById('search');
if (localStorage.fromLang == undefined || "undefined") {
localStorage.fromLang = "en";
}
if (localStorage.dstLang == undefined || "undefined") {
localStorage.dstLang = 'en';
}
if (localStorage.limit == undefined || "undefined"){
localStorage.limit = 5;
}
var default_out = "<p class=\"tips mr-side-m\">Tip: This is a work in progress chrome extenction. For more detail visit our <a href=\"http://www.github.com/prabinzz/\">repo</a> on GitHub.</p>"
var btn = document.getElementById("search");
btn.addEventListener("click", function(){
if(document.getElementById('word').value != ''){
getMeaning(word.value);
}
});
function getMeaning(w){
xreq = new XMLHttpRequest();
xreq.onerror= function(){
dataError("Error While loading.");
}
xreq.onreadystatechange = function(){
if (this.readyState == 4 && this.status == 200) {
// Typical action to be performed when the document is ready:
data = JSON.parse(xreq.responseText);
if(data.result == "ok" && data.hasOwnProperty("tuc")){
dataParse(data);
}else{
dataError(data);
}
}
else if (this.readyState < 4) {
onLoading();
}
}
xreq.open("get","https://glosbe.com/gapi/translate?from="+
localStorage.fromLang+"&dest="+localStorage.toLang+"&format=json&phrase="+
w+"&pretty=true", true);
xreq.send(null);
}
function onLoading(){
output.innerHTML = "<img class='loading' src='./img/loading.gif' alt='loading'>";
}
function dataParse(data){
if(data == "" || data == null || data == undefined){
dataError();
return;
}else{
console.log(data);
data_clean = getData(data);
console.log(data_clean);
var temp = '';
temp+= "<h3>"+data.phrase+"</h3>";
temp+="<ol>";
var appended = 0;
for (var i = 0; i < data_clean.length && appended < localStorage.limit; i++) {
if(data_clean[i].language == localStorage.toLang){
temp+="<li>"+data_clean[i].text+"</li>";
appended ++;
}
}
// if(appended == 0){
// dataError(data);
// return;
// }
temp+="</ol>";
temp +="<a class='center' href='http://www.dictionary.com/browse/"+data.phrase+"?s=t'>"+
"more>> </a>";
output.innerHTML = temp;
linkInit();
}
}
function dataError(data){
var temp = "";
if(typeof(data)=='string'){
temp += "<h4 class='center no-match'>"+data+"</h4>";
}else{
temp += "<h4 class='center no-match'> No match found for \""+data.phrase+"\"</h4>";
temp += "<ul class='reasons'>"+"<li> Word Not available in current language</li>"
+"<li> Given word is invalid. </li> "+
"<li> Server is busy.</li>"
+"</ul>"
temp +="<h5> Search in: </h5><ul class='no-bul'>";
temp +="<a href='http://www.dictionary.com/browse/"+data.phrase+"?s=t'>"+
"<li>dictionary.com</li></a>";
temp +="<a href='http://www.google.com/search?q="+data.phrase+"'>"+
"<li>google.com</li></a>";
temp += "</ul>";
}
output.innerHTML = temp;
linkInit();
}
function newTab(obj, add){
obj.addEventListener("click", function(){
chrome.tabs.create({"url": add});
})
}
function linkInit(){
var links = document.getElementsByTagName('a');
for (var i = 0; i < links.length; i++) {
newTab(links[i],links[i].href);
}
}
function getData(data){
var temp = [];
for(var i = 0; i<data.tuc.length; i++){
if (data.tuc[i].hasOwnProperty("meanings")) {
for(var j= 0; j<data.tuc[i].meanings.length; j++){
temp.push(data.tuc[i].meanings[j]);
}
}
}
return temp;
}
// _____________________________________________________
linkInit();