background.js 1.47 KB
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;
          }
        });