Skip to content
Advertisement

Chrome Extension Opens new tab, send message to new tab

I have an App page (extension_id://app.html) I want to create a new tab:

let tab = await chrome.tabs.create({ url: candidate_url });
chrome.tabs.sendMessage(tab['id'], {"message": "sendMessage", "data": {}, "tabID": tab['id']});

however, when I send a message to this tab id it doesnt work.

here is the content.js script, but it doesn’t alert or console log

chrome.runtime.onMessage.addListener(
    async function(request, sender, sendResponse) {
        if( request.message === "sendMessage"){
            console.log("XX");
            alert("XX");
        }
    }
);

Advertisement

Answer

adding a setTimeout to call the send function works correctly, just have to have the page load and timer called perfectly, about 8 to 10 seconds is enough time for the page to load.

TIA

Advertisement