background.js
1.47 KB
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
var popup = undefined;
var selectedText = undefined;
// Creates the context menu to translate texts
chrome.contextMenus.create({
id: 'translate_contextmenu',
title: 'Traduzir \'%s\' para LIBRAS',
contexts: ['selection']
}, function () {
if ( chrome.runtime.lastError ) console.log(chrome.runtime.lastError.message);
});
// Listening the event click
chrome.contextMenus.onClicked.addListener( function (info) {
// Creates the window if it exists
if ( popup === undefined ) {
chrome.windows.create({
url: "app/player/window.html",
top: 10,
left: 10,
width: 535,
height: 500,
type: "popup"
}, function (w) {
popup = w;
selectedText = info.selectionText;
});
} else {
chrome.windows.update(popup.id, {focused: true}, function () {
chrome.runtime.sendMessage({selectedText: info.selectionText});
});
}
});
// Frees variable if the popup doesn't to exist anymore
chrome.windows.onRemoved.addListener( function (windowId) {
if (windowId == popup.id) {
popup = undefined;
}
});
// Listening the ready event of the popup
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
if (request.ready === true && selectedText !== undefined) {
chrome.runtime.sendMessage({selectedText: selectedText});
selectedText = undefined;
}
});