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

234
Views
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 answers
Answer question

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 Report
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!