Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

96
Views
chrome.tabs.sendMessage not working with background script and iframe

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

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!