I am facing a challenge with transmitting data from my background script to the script for my pageAction
. The content script I have adds an <iframe />
, and the JavaScript within the <iframe />
successfully receives the data from the background script, but it appears that the data is not being retrieved in my pageAction
.
In the background script, my code looks something like this:
chrome.tabs.sendMessage(senderTab.tab.id,
{
foo:bar
});
where senderTab.tab.id
acts as the "sender" in the onMessage
listener within my background script.
The JavaScript loaded by the <iframe />
injected by my content script includes code similar to:
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
console.log("received in iframe:", request);
}
});
The <iframe />
successfully receives the message as expected.
I included the same JavaScript in my page_action.js
, but it does not receive any data from the background script. The pageAction is triggered using
chrome.pageAction.show(senderTab.tab.id);
before calling chrome.tabs.sendMessage(senderTab.tab.id ...
Is the HTML page associated with my pageAction not part of the same tab? Given that this tabId
allows me to activate/"show" the icon, I would expect the JavaScript listener for the pageAction to also receive information from
chrome.tabs.sendMessage(senderTab.tab.id ...
In my content script, I use the following method to send data to the background script:
chrome.runtime.sendMessage({
foo: bar
});
When the content script sends the above message, the pageAction JavaScript is able to retrieve it.
How can I ensure that the background script effectively transmits data to my pageAction? I prefer not to rely on pageAction requesting or polling, rather I want pageAction to simply listen and receive updates. For instance, if the pageAction's HTML is displayed, it should be capable of updating in real time as the background page implements changes.