Forward message to tab

675 views
Skip to first unread message

GMathew

unread,
Jul 24, 2017, 10:56:07 AM7/24/17
to Chromium-Extensions-Announce
I am loading an iframe inside background.html page. iframe passes a message to background.js , which then needs to be sent to a tab. 

/* background.js */
window.addEventListener('message', function (e) {
    console.log("We got product! , Pass to tab")
   // This is not sending message to tab
    chrome.tabs.sendMessage(
        currentTabId,
        {
            action: 'message',
            title: "test",
        }, function (response) { }); 
}); 

// in webpage where I receive message , which doesn't get executed 
 window.addEventListener('message', function (e) {
        console.log("We got product on page!");
    }, false);


Is there a different mechanisam to get mssage ? I have checked documents quite few times but I am confused with the worflow.

Muhammad Qayyum Abro

unread,
Jul 24, 2017, 11:20:03 AM7/24/17
to Chromium-Extensions-Announce
In content.js 

chrome.runtime.onMessage.addListener(function(msg, sender, sendResponse)  
{
   if(msg.action == "message")
   {
     //CODE...
   }
});

GMathew

unread,
Jul 24, 2017, 11:24:57 AM7/24/17
to Chromium-Extensions-Announce
To clarify, I have a tab open , in background.js I need to call a function located in that tab. I can do that by injecting code or sending a message from background ?

PhistucK

unread,
Jul 24, 2017, 12:39:06 PM7/24/17
to GMathew, Chromium-Extensions-Announce
The background page cannot communicate with the tab itself, it has to communicate with code that runs in the world of the extension.
The world of the extension consists of content scripts or scripts that were executed using chrome.tabs.executeScript.
Either way, in order to get the messages, you have to add a listener to onMessage, like Muhammad Qayyum Abro showed.
In order to communicate with the tab itself (from the content script or executed script), you can use the usual postMessage API.


PhistucK

--
You received this message because you are subscribed to the Google Groups "Chromium-Extensions-Announce" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extensions+unsub...@chromium.org.
To post to this group, send email to chromium-extensions@chromium.org.
Visit this group at https://groups.google.com/a/chromium.org/group/chromium-extensions/.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/chromium-extensions/4c6a295d-5e25-4ce3-b25a-0eec5ad40017%40chromium.org.

PhistucK

unread,
Jul 26, 2017, 8:40:10 AM7/26/17
to GMathew, Chromium-Extensions-Announce
Sorry, I might have been wrong.
I wrote this in another thread -
You cannot use the standard web message mechanism to communicate between a background page and a web page.


PhistucK

On Mon, Jul 24, 2017 at 7:38 PM, PhistucK <phis...@gmail.com> wrote:
The background page cannot communicate with the tab itself, it has to communicate with code that runs in the world of the extension.
The world of the extension consists of content scripts or scripts that were executed using chrome.tabs.executeScript.
Either way, in order to get the messages, you have to add a listener to onMessage, like Muhammad Qayyum Abro showed.
In order to communicate with the tab itself (from the content script or executed script), you can use the usual postMessage API.


PhistucK

On Mon, Jul 24, 2017 at 6:24 PM, GMathew <sonesh...@gmail.com> wrote:
To clarify, I have a tab open , in background.js I need to call a function located in that tab. I can do that by injecting code or sending a message from background ?

On Monday, July 24, 2017 at 11:20:03 AM UTC-4, Muhammad Qayyum Abro wrote:
In content.js 

chrome.runtime.onMessage.addListener(function(msg, sender, sendResponse)  
{
   if(msg.action == "message")
   {
     //CODE...
   }
});

--
You received this message because you are subscribed to the Google Groups "Chromium-Extensions-Announce" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extensions+unsubscribe...@chromium.org.
Reply all
Reply to author
Forward
0 new messages