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

244
Visualizações
How to send message to newly created window tab using chrome.windows.create

I created a new chrome window tab via extension but I can't send message to its content script, the event never fired

background.js

chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
    if (request.action == "run") {
        chrome.tabs.create({
            url: 'https://vnexpress.net/',
            active: false
        }, function (tab) {
            chrome.windows.create({
                tabId: tab.id,
                type: 'popup',
                focused: true
            }, function (win) {
                chrome.tabs.sendMessage(win.tabId, { action: "scrape" });
            });
        });
    }

    sendResponse();
})

content_script.js

chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
    if (request.action == "scrape") {
        alert("Scraping!!!");
        console.log("Scraping")
        //This never run
    }
});
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The logic seems to be correct, but probably sendMessage is being called before content_script.js is run.

To verify this, use console.log(new Date) before the sendMessage line and another one in the content script. You can then compare the dates.

If that's the issue, you could have the content script message the background script when it loads instead, so there's no race condition.

about 4 years ago · Juan Pablo Isaza Relatório
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