-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathballoon.js
221 lines (196 loc) · 9.09 KB
/
balloon.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
if (!window.isClicklateLoaded) {
function createBalloon() {
var selection = window.getSelection && window.getSelection();
if (!selection || selection.rangeCount < 1) {
return;
}
var rect = selection.getRangeAt(0).getBoundingClientRect();
var span = document.createElement("span");
var tail = document.createElement("span");
var loading = document.createElement("img");
var content = document.createElement("span");
/* Balloon Span */
span.style.backgroundAttachment = "scroll";
span.style.backgroundClip = "border-box";
span.style.backgroundImage = "none";
span.style.backgroundOrigin = "padding-box";
span.style.boxShadow = "7px 7px 7px rgba(0, 0, 0, 0.3)";
span.style.border = "2px solid #bd1e2c";
span.style.borderTopColor = "rgb(1, 153, 241)";
span.style.borderBottomColor = "rgb(120, 191, 35)";
span.style.borderRightColor = "rgb(235, 147, 22)";
span.style.borderLeftColor = "rgb(229, 35, 80)";
span.style.borderRadius = "0px";
span.style.cursor = "auto";
span.style.display = "block";
span.style.margin = "0px";
span.style.padding = "10px";
span.style.zIndex = "100000";
span.style.background = "white";
span.style.position = "absolute";
span.style.height = "auto";
span.style.width = "175px";
span.style.right = "3px";
span.style.whiteSpace = "pre-wrap";
span.style.textAlign = "left";
span.style.color = "black";
span.style.font = "italic normal 14px Verdana, sans-serif;";
span.style.left = (rect.left - 15) + "px";
span.style.top = (rect.top + rect.height + window.pageYOffset + 11) + "px";
/* Loading Image */
loading.src = chrome.runtime.getURL("preloader.gif");
loading.style.backgroundAttachment = "scroll";
loading.style.backgroundClip = "border-box";
loading.style.backgroundColor = "transparent";
loading.style.backgroundImage = "none";
loading.style.backgroundOrigin = "padding-box";
loading.style.bordeStyle = "none";
loading.style.border = "none";
loading.style.color = "white";
loading.style.cursor = "auto";
loading.style.display = "block";
loading.style.left = "auto";
loading.style.lineHeight = "normal";
loading.style.margin = "0px auto";
loading.style.padding = "0px";
loading.style.width = "14px";
loading.style.height = "14px";
loading.style.position = "static";
loading.style.zIndex = "auto";
/* Tail Image */
tail.style.backgroundAttachment = "scroll";
tail.style.backgroundClip = "border-box";
tail.style.backgroundColor = "transparent";
tail.style.backgroundImage = "none";
tail.style.backgroundOrigin = "padding-box";
tail.style.border = "none";
tail.style.width = "0px";
tail.style.height = "0px";
tail.style.borderBottom = "10px solid rgb(1, 153, 241)";
tail.style.borderLeft = "10px solid transparent";
tail.style.borderRight = "10px solid transparent";
tail.style.color = "white";
tail.style.cursor = "auto";
tail.style.display = "block";
tail.style.fontFamily = "sans-serif";
tail.style.fontSize = "14px";
tail.style.fontStyle = "normal";
tail.style.fontVariant = "normal";
tail.style.fontWeight = "normal";
tail.style.left = "-22px";
tail.style.lineHeight = "normal";
tail.style.margin = "0px";
tail.style.outlineColor = "white";
tail.style.outlineStyle = "none";
tail.style.outlineWidth = "0px";
tail.style.padding = "0";
tail.style.position = "absolute";
tail.style.right = "auto";
tail.style.textAlign = "left";
tail.style.left = "15px";
tail.style.top = "-12px";
tail.style.verticalAlign = "baseline";
tail.style.zIndex = "auto";
span.appendChild(content);
span.appendChild(loading);
span.appendChild(tail);
document.body.appendChild(span);
span.addEventListener("click", function () {
balloon.close();
}, false);
let balloon = {
setText: function (text) {
span.removeChild(loading);
content.innerHTML = text;
},
close: function () {
span.parentNode.removeChild(span);
}
};
return balloon;
}
function getResultsForResponseText(responseText, searchURL) {
let result = "";
const parser = new DOMParser();
const doc = parser.parseFromString(responseText, "text/html");
const tables = doc.querySelectorAll("#englishResultsTable");
const isThereCrossLanguageResult = tables.length === 3;
const englishResultsTable = tables[0];
if (englishResultsTable) {
const resultsForLanguage = getResultsFromTags(englishResultsTable.querySelectorAll("a"));
if (resultsForLanguage != "") {
if (isThereCrossLanguageResult) {
result += "<p style='border-bottom: 1px solid rgba(0, 0, 0, 0.75); margin: 0; padding: 0; color: black; font: italic bold 14px Verdana, sans-serif;'>İngilizce - Türkçe</p>";
}
result += resultsForLanguage;
}
}
if (isThereCrossLanguageResult) {
const turkishResultsTable = tables[1];
if (turkishResultsTable != null) {
let resultsForLanguage = getResultsFromTags(turkishResultsTable.querySelectorAll("a"));
if (resultsForLanguage !== "") {
if (result !== "") {
result += "<p style='border-bottom: 1px solid rgba(0, 0, 0, 0.75); margin: 5px 0 0 0; padding: 0; color: black; font: italic bold 14px Verdana, sans-serif;'>Türkçe - İngilizce</p>";
} else {
result += "<p style='border-bottom: 1px solid rgba(0, 0, 0, 0.75); margin: 0; padding: 0; color: black; font: italic bold 14px Verdana, sans-serif;'>Türkçe - İngilizce</p>";
}
result += resultsForLanguage;
}
}
}
if (result !== "") {
result += "<a href='" + searchURL + "' target='_blank' style='border: none; float: right; line-height: 0; height: 7px; margin: 0; padding: 0; color: rgba(0, 0, 0, 0.75); font: italic bold 10px Verdana, sans-serif; text-align: center; text-decoration: none;'>...</p>";
return result;
}
return "<p style='margin: 0; padding: 0; color: black; font: italic normal 14px Verdana, sans-serif;'>Çeviri bulunamadı...</p>";
}
function getResultsFromTags(aTags) {
let resultsForLanguage = "";
let uniqueResults = getUniqueResultsExactOrder(aTags);
for (let i = 0; i < uniqueResults.length; i++) {
if (i >= 4 || (i + 1 === uniqueResults.length && i < 4)) {
resultsForLanguage += "<p style='margin: 0; padding: 0; color: black; font: italic normal 14px Verdana, sans-serif;'>" + uniqueResults[i] + "</p>";
break;
}
resultsForLanguage += "<p style='border-bottom: 1px solid rgba(0, 0, 0, 0.15); margin: 0; padding: 0; color: black; font: italic normal 14px Verdana, sans-serif;'>" + uniqueResults[i] + "</p>";
}
return resultsForLanguage;
}
function getUniqueResultsExactOrder(aTags) {
const uniqueResults = [];
for (let i = 1; i < aTags.length; i = i + 2) {
if (uniqueResults.indexOf(aTags[i].text) === -1) {
uniqueResults.push(aTags[i].text);
}
}
return uniqueResults;
}
chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
if (request.method === "showResult") {
if (!balloon) {
// ignore
return;
}
if (!request.externalRequestSucceeded) {
request.resultText = '<p style=\'margin: 0; padding: 0; color: black; font: italic normal 14px Verdana, sans-serif;\'>Tureng ile bağlantı kurulamadı!</p>'
} else {
request.resultText = getResultsForResponseText(request.rawResponseText, request.searchURL);
}
balloon.setText(request.resultText);
sendResponse({ "message": "Result text is displayed" });
}
if (request.method === "createBalloon") {
try {
if (balloon) {
balloon.close()
}
} catch (e) {
// ignore
}
balloon = createBalloon();
sendResponse({ "message": "Balloon is created" });
}
});
}
window.isClicklateLoaded = true;