Skip to content Skip to sidebar Skip to footer

Chrome Extension Sending Message From Iframe To Event Page Then To Content Script

I have inserted an iframe from content script. It works fine. But if I want to display parent's html content on iframe, I have to use messaging to communicate between iframe and co

Solution 1:

I found a related question. https://stackoverflow.com/a/20077854/772481

From the documentation for chrome.runtime.onMessage.addListener:

This function becomes invalid when the event listener returns, unless you return true from the event listener to indicate you wish to send a response asynchronously (this will keep the message channel open to the other end until sendResponse is called).

So I have to return true to indicate the sendResponse is async.

event page:

chrome.runtime.onMessage.addListener(function(msg, sender, sendResponse) {
  if (msg.from === 'popup' && msg.method === 'ping') {
    chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
       chrome.tabs.sendMessage(tabs[0].id, {
        from: 'event',
        method:'ping'}, function(response) {
          sendResponse(response.data);
      });
    });
    returntrue; // <-- Indicate that sendResponse will be async
  }
});

Post a Comment for "Chrome Extension Sending Message From Iframe To Event Page Then To Content Script"