-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
39 lines (35 loc) · 1.15 KB
/
background.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
// Log creation status
function onCreated() {
if (chrome.runtime.lastError) {
console.log(`Error: ${chrome.runtime.lastError}`);
} else {
console.log("Menu item slovnikcz-selection created.");
}
}
// Log all errors
function onError(error) {
console.log(`Error: ${error}`);
}
// Create the context menu item.
chrome.contextMenus.create({
id: "slovnikcz-selection",
title: chrome.i18n.getMessage("title"),
contexts: ["selection"]
}, onCreated);
// Add click event
chrome.contextMenus.onClicked.addListener((info, tab) => {
sText = info.selectionText;
slovnikURL = "http://www.slovnik.cz/bin/mld.fpl?vcb=" + encodeURIComponent(sText) + "&dictdir=encz.en&lines=10&js=0";
xhr = new XMLHttpRequest();
xhr.open("GET", slovnikURL, true);
xhr.onreadystatechange = function() {
if (xhr.readyState == 4) {
chrome.tabs.sendMessage(tab.id,
{action: "t",
response: xhr.responseText},
function(response) {});
}
};
chrome.tabs.sendMessage(tab.id, {action: "p"}, function(response) {});
xhr.send();
});