From 02d2f40a8e5936c67b0c64a17b1f1a089e10d67f Mon Sep 17 00:00:00 2001 From: David Guilherme Date: Wed, 29 Apr 2015 20:59:08 -0300 Subject: [PATCH] Sends the selected text to popup script --- app/main.js | 16 ++++++++++++---- background.js | 20 ++++++++++++++++---- 2 files changed, 28 insertions(+), 8 deletions(-) diff --git a/app/main.js b/app/main.js index ef8ace0..4129f44 100644 --- a/app/main.js +++ b/app/main.js @@ -1,6 +1,14 @@ document.addEventListener("DOMContentLoaded", function(event) { - var $player = document.getElementById('canvas'); + var $player = document.getElementById('canvas'); - $player.width = window.innerWidth; - $player.height = window.innerHeight; -}); \ No newline at end of file + if ( $player ) { + $player.width = window.innerWidth; + $player.height = window.innerHeight; + } + +}); + +chrome.runtime.onMessage.addListener( + function(request, sender, sendResponse) { + console.log(request.selectedText); + }); diff --git a/background.js b/background.js index 33c1d8c..cc5269f 100644 --- a/background.js +++ b/background.js @@ -1,5 +1,7 @@ var popup = undefined; +var port = undefined; +// Creates the context menu to translate texts chrome.contextMenus.create({ id: 'translate_contextmenu', title: 'Traduzir \'%s\' para LIBRAS', @@ -8,24 +10,34 @@ chrome.contextMenus.create({ if ( chrome.runtime.lastError ) console.log(chrome.runtime.lastError.message); }); -chrome.contextMenus.onClicked.addListener( function () { - console.log(popup); +// Listening the event click +chrome.contextMenus.onClicked.addListener( function (info) { + // Creates the window if it exists if ( popup === undefined ) { chrome.windows.create({ url: "app/window.html", top: 10, left: 10, + width: 600, + height: 600, type: "popup" }, function (w) { popup = w; + + chrome.runtime.sendMessage({selectedText: info.selectionText}); }); } else { - chrome.windows.update(popup.id, {focused: true}); + 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; } -}); \ No newline at end of file +}); + +chrome.runtime.sendMe \ No newline at end of file -- libgit2 0.21.2