-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.js
157 lines (134 loc) · 5.97 KB
/
main.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
chrome.contextMenus.onClicked.addListener(onClickContextMenuOptions);
chrome.runtime.onInstalled.addListener(createContextMenuOptions);
function createContextMenuOptions() {
chrome.contextMenus.create({
"title": "Çevir",
"id": "translate",
"contexts": [
"selection"
]
});
}
function doInCurrentTab(tabCallback) {
chrome.tabs.query(
{currentWindow: true, active: true},
function (tabArray) {
tabCallback(tabArray[0]);
}
);
}
function onClickContextMenuOptions(info, tab) {
if (info.menuItemId === "translate") {
chrome.tabs.executeScript(tab.id, {file: 'balloon.js', allFrames: true}, function () {
search(info.selectionText, tab.id);
});
console.log(info.selectionText);
}
}
const selectedTextGetter = function () {
const selection = window.getSelection();
return (selection.rangeCount > 0) ? selection.toString() : '';
};
chrome.commands.onCommand.addListener(function (command) {
console.log('command:', command);
if (command === "search-phrase") {
doInCurrentTab(function (tab) {
chrome.tabs.executeScript(null, {
code: ';(' + selectedTextGetter + ')();',
allFrames: true
},
function (selectedTextPerFrame) {
if ((selectedTextPerFrame.length > 0) && (typeof (selectedTextPerFrame[0]) === 'string')) {
const text = selectedTextPerFrame[0];
chrome.tabs.executeScript(tab.id, {file: 'balloon.js', allFrames: true}, function () {
search(text, tab.id);
});
}
});
});
}
});
function search(searchString, tabId) {
if (!searchString) {
return;
}
chrome.tabs.sendMessage(tabId, {method: 'createBalloon'});
const request = new XMLHttpRequest();
request.open("GET", getSearchURL(searchString), true);
let completed = false;
request.onreadystatechange = function () {
if (!completed && request.readyState === 4 && request.status === 200) {
const responseText = request.responseText;
chrome.tabs.sendMessage(tabId, {
method: 'showResult',
string: getResultsForResponseText(responseText, getSearchURL(searchString))
});
completed = true;
}
if (request.readyState === 4 && (request.status === 0 || request.status === 404)) {
chrome.tabs.sendMessage(tabId, {
method: 'showResult',
string: '<p style=\'margin: 0; padding: 0; color: black; font: italic normal 14px Verdana, sans-serif;\'>Tureng ile bağlantı kurulamadı!</p>'
});
}
}
request.send();
}
function getSearchURL(searchString) {
return "http://tureng.com/search/" + searchString;
}
function getResultsForResponseText(responseText, url) {
let result = "";
const tables = $($.parseHTML(responseText)).find('#englishResultsTable');
const isThereCrossLanguageResult = tables.length === 3;
const englishResultsTable = $(tables[0]);
if (englishResultsTable != null) {
let resultsForLanguage = getResultsFromTags(englishResultsTable.find("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.find("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='" + url + "' 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;
}