Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

95
Visualizações
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
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda