How to transfer data from a remote window to a chrome extension background page?

I have a chrome extension, and from my background page I open a remote window:

chrome.windows.create({ type : 'popup', url : "https://www.example.com/mypage.html" }, function(newWindow) { }); 

On my remote page ( https://www.example.com/mypage.html ) I wait for the user to complete the action. When this action is completed, I need to transfer some data back to the extension.

How can i do this? I did not find anything suitable in the docs ( http://developer.chrome.com/extensions/messaging.html )

+8
google-chrome-extension message-passing
source share
2 answers

In principle, this is possible. You should use the script content as a bridge between your newly created window and the script background. For example:

Your background script:

 chrome.runtime.onMessage.addListener(function(message, sender, sendResponse) { alert("message received"); }); chrome.windows.create({ type : 'popup', url : "http://yoursite.com/page.html", type: "popup" }, function(newWindow) { }); 

Your content script:

 document.addEventListener("hello", function(data) { chrome.runtime.sendMessage("test"); }) 

page.html:

 <script> var go = function() { var event = document.createEvent('Event'); event.initEvent('hello'); document.dispatchEvent(event); } </script> <a href="javascript:go();">Click me</a> 

So, the idea is to send an event from a page using a document object. The content of the script listens for this event and once sends a message to the background image of the script where your code is originally.

+9
source share

I believe that messaging is not needed for what you do. (If I am missing something.) I am experimenting with creating an extension and have to create a popup to request some information from the user. I used messaging first, then read the following page:

http://developer.chrome.com/extensions/event_pages.html

Enable js on the html page that uses either the .getBackgroundPage extension or runtime.getBackgroundPage [use the latter if you use a fickle background] to get the background script. It can be just as simple to call a function on your background page as follows:

extension.getBackgroundPage () sendDataOrWhateverYouCallIt (data) ;.

0
source share

All Articles