Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I pass back data from a remote window to a chrome extension's 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 am waiting for the user to perform an action. When this action is performed, I need to pass back to the extension some data.

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

like image 845
abinop Avatar asked Aug 22 '13 07:08

abinop


People also ask

Does Chrome extension work in background?

Extensions monitor these events in their background script, then react with specified instructions. A background page is loaded when it is needed, and unloaded when it goes idle. Some examples of events include: The extension is first installed or updated to a new version.

How do I send data between Chrome extension scripts?

When sending a message to the content script, we need to specify which tab to send it to. Therefore, we need to retrieve the active tab information first, and then use tabs. sendMessage . To use the tabs API and to have access to the active tab, you need to add tabs and activeTab under permissions in your manifest.

What is a Chrome background script?

Background scripts or a background page enable you to monitor and react to events in the browser, such as navigating to a new page, removing a bookmark, or closing a tab. Background scripts or a page are: Persistent – loaded when the extension starts and unloaded when the extension is disabled or uninstalled.

Do extensions run in the background?

On the contrary, background scripts depend upon your extension, so as long as it remains installed, then the scripts will be running in the background in a daemon fashion.


1 Answers

It is basically possible. What you should do is to use the content script as a bridge between your newly created window and your background script. 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 dispatch an event from the page using document object. The content script listens for that event and once occur send a message to the background script where your code is originally.

like image 124
Krasimir Avatar answered Sep 28 '22 05:09

Krasimir