I am trying to send messages from background script of an extension to an iframe that is being rendered on a webpage as part of the contentScript but it is only being received by iframe 4 out of 10 times. The reason for failure is not clear as there are no error logs. Any idea what might be going wrong ?
background.js
chrome.tabs.getCurrent((currentTab) => {
chrome.tabs.sendMessage(
currentTab.id,
{
message: "Generic Message",
},
function () {}
);
});
iframe.js
chrome.runtime.onMessage.addListener(function (request) {
// Not able to receive here in most of the cases
})
I am required to pass messages from background to iframe and vice-versa and that too is highly dynamic in a way that messages from background script would be fired based on events received through websocket connection. Hence, information would be passed back and forth between back and forth between Iframe (rendered inside contentscipt) and background script. So, I also added event listeners in both background and iframe.js and handled only certain cases that need to be handled.
Event listener for background.js
chrome.runtime.onMessage.addListener(function (request) {
// request.sender is just a property that I added to identify if this event was called from background context or iframe context.
if (request.sender === "Sender 1") {
// Execute this
} else {
// Execute this
}
})
So now I have 2 files or contexts and both of them are passing messages and both of them are receiving messages as well.
P.S. I am using manifest v3