-
Notifications
You must be signed in to change notification settings - Fork 0
/
content.js
39 lines (36 loc) · 1.06 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
$(document).ready(function () {
$("body").keyup(function (e) {
var selection = window.getSelection();
var text = selection.toString().trim();
switch (e.which) {
case 110: // n letter
case 78: // N letter
if (text) {
chrome.runtime.sendMessage({ method: "translate", word: text, langDirection: 12 },
function (response) {
tooltip.show(JSON.stringify(response.response.translations[0]['translation']));
});
}
break;
case 109: // m letter
case 77: // M letter
if (text) {
chrome.runtime.sendMessage({ method: "translate", word: text, langDirection: 21 },
function (response) {
tooltip.show(JSON.stringify(response.response.translations[0]['translation']));
});
}
break;
}
});
$("body").click(function (e) {
tooltip.hide();
});
chrome.extension.onRequest.addListener(function (request, sender, responseCallback) {
if (request.action === "getSelectedText") {
responseCallback({ selectedText: window.getSelection().toString() });
} else {
responseCallback({});
}
});
});