The chrome.tabs.sendMessage call function is not called. What for?

I have the following method called from the background script of my Chrome extension. The goal is to send a message to a specific tab and then call the provided callback method with the result. The important part is that it callbackDone

should always be called at some point in time. The way it is:

function sendToTab(nTabID, callbackDone)
{
    (function()
    {
        chrome.tabs.sendMessage(nTabID, {
            action: "update01"
        }, 
        function(response) 
        {
            if(chrome.runtime.lastError)
            {
                //Failed to send message to the page
                if(callbackDone)
                    callbackDone(nTabID, null); //Page never received the message
            }
            else
            {
                //Sent message OK
                if(response.result === true)
                {
                    if(callbackDone)
                        callbackDone(nTabID, true); //Success!
                }
                else
                {
                    if(callbackDone)
                        callbackDone(nTabID, false);    //Page returns failure
                }
            }
        });
    }());
}

      

Then, from within the page handling the post (may be injected content script

), I handle it as such:

chrome.runtime.onMessage.addListener(onMessageProc);

function onMessageProc(request, sender, sendResponse)
{
    if(request.action == "update01")
    {
        //Do processing .... that sets `bResult`

        sendResponse({result: bResult});
    }
}

      

The above approach works pretty well, except ... Let's say there is a page like the Script Options Page that doesn't process my message update01

, and instead it processes its own message as such:

chrome.runtime.onMessage.addListener(onMessageProc);

function onMessageProc(request, sender, sendResponse)
{
    if(request.action == "update02")   //Note different action ID
    {
        //Does some other actions...
    }
}

      

In this case, when my first method sendToTab

is called for that tab, mine is callbackDone

never called, i.e. chrome.tabs.sendMessage

is called and it returns immediately, but its callback function is never called.

So what am I missing here?

+3


source to share


1 answer


You see the expected behavior.

The documentation says about the callback function:

If you give the parameter responseCallback, it should be a function that looks like this:

function(any response) {...};

any response


The JSON response object sent by the message handler. If an error occurs while connecting to the specified tab, the callback will be called with no arguments and runtime.lastError

an error message will be set.



There are 3 possible outcomes sendMessage

.

  • There was a listener and it was called sendResponse

    .
    The callback is then called with the response as a parameter.

  • There was a listener and it completed without being called sendResponse

    (synchronously or asynchronously).
    Then the callback is not called at all .

  • There was some kind of error sending your message.
    The callback is then called with no arguments and chrome.runtime.lastError

    .

If you need your callback to execute under any circumstance, you need a "default" case in your listeners that calls sendResponse

.

+5


source







All Articles