-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontent.js
158 lines (115 loc) · 4.1 KB
/
content.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
var index = 0;
var my_json;
var im_data;
var tr_data;
var API_KEY = 'YOUR PIXABAY IPA KEY HERE';
var YANTRAN_KEY ='YOUR YANDEX TRANSLATE API KEY HERE';
var $div = $("<div>",{id: "ppp", class:'popup'});
$div.hide();
var $yan_credit = $("<a id='credit' href='http://www.translate.yandex.com'>powered by Yandex Translate</a>");
var $dspan = $("<span>",{id: "mypopup", class:"popuptext"});
var $spandiv = $("<div>");
$cred_div = $("<div>");
var $im_content = $("<div>",{class: "display-container"});
var $pb_logo = $('<img id="pb-logo" src="" height= 20px width= 75px />');
$pb_logo.attr('src',chrome.extension.getURL("pixabay-logo.png"));
var result = $('<img id="payload" src="" />');
var $butt_left = $("<input/>",{class: "ext-left-button", type:"button", value:'previous' });
var $butt_right = $("<input/>", {class:'ext-right-button', type:"button", value:'next'});
$("body").append($div);
$(".popup").append($yan_credit);
$(".popup").append($cred_div);
$(".popup").append($dspan);
$(".popup").append($spandiv);
$(".popup").append(result);
$(".popup").append($pb_logo);
$(".popup").append($im_content);
$(".popup").append($butt_left);
$(".popup").append($butt_right);
$($butt_left).click(function(){advanceIm(-1);});
$($butt_right).click(function(){advanceIm(1);});
$("body").dblclick(function(e){
if ($(e.target).attr('class') == 'ext-right-button' || $(event.target).attr('class')=='ext-left-button'){return;}
$(".display-container").attr('src','');
index = 0;
var popupd = $(".popup");
var $popup = $("#mypopup");
var $payload = $("#payload");
//is our popup already displayed?
var isDisplayed = ($popup.attr("Class").split(' '));
//get the word double clicked on
var highlighted = window.getSelection().toString().trim();
//is selection from dblclick a text element? if so show, else close popup box if it is open
if (highlighted.length < 1 || highlighted == ' ' || window.getSelection().anchorNode.toString() == "[object HTMLBodyElement]"){
if(isDisplayed.length == 2){
$popup.toggleClass("Show");
}
return;
}
else{
//show popup box
$(".popup").show();
var URL = "https://pixabay.com/api/?key="+API_KEY+"&q="+encodeURIComponent(highlighted) + "&image_type=photo";
var yanURL = 'https://translate.yandex.net/api/v1.5/tr.json/translate?key=' + YANTRAN_KEY + '&text=' + highlighted +'&lang=ru-en'
//request json
$.ajax({
url:yanURL,
async: false,
crossDomain: true,
dataType: 'json',
success: function(json){
myCallback(json);
}
});
$.ajax({
url:URL,
async: false,
crossDomain: true,
dataType: 'json',
success: function(json){
callback(json);
}
});
}
if(tr_data['code'] == 200){
$dspan.text(highlighted + ': ' + tr_data['text'][0]);
}
else{
$dspan.text(highlighted + ': ' + 'error: could not translate');
}
if (im_data.total == 0){
result.attr('src','#');
}
else{
result.attr('src',im_data.hits[index].previewURL);}
//display popup where the mouse was clicked
popupd.offset({left:e.pageX, top:e.pageY});
if(isDisplayed.length == 1){
$popup.toggleClass("show");
}
//can't remember why this else if statement is important.
else if(isDisplayed.length == 2 && isDisplayed[2] == "show"){
$popup.toggleClass("show");
}
});
function advanceIm(n){
if((index + n) < 0 || (index + n) >= im_data.hits.length){
index = 0;
}
else{
index = index + n;
}
result.attr('src',im_data.hits[index].previewURL);
};
$(document).click(function(event){
if ($(event.target).attr('class') == 'ext-right-button' || $(event.target).attr('class')=='ext-left-button'){
}
else{
$(".popup").hide();}
});
function callback(response){
im_data = response;
}
function myCallback(data){
tr_data = data;
}