168

I am trying to pass messages between content script and the extension

Here is what I have in content-script

chrome.runtime.sendMessage({type: "getUrls"}, function(response) {
  console.log(response)
});

And in the background script I have

chrome.runtime.onMessage.addListener(
  function(request, sender, sendResponse) {
    if (request.type == "getUrls"){
      getUrls(request, sender, sendResponse)
    }
});

function getUrls(request, sender, sendResponse){
  var resp = sendResponse;
  $.ajax({
    url: "http://localhost:3000/urls",
    method: 'GET',
    success: function(d){
      resp({urls: d})
    }
  });

}

Now if I send the response before the ajax call in the getUrls function, the response is sent successfully, but in the success method of the ajax call when I send the response it doesn't send it, when I go into debugging I can see that the port is null inside the code for sendResponse function.

2
  • Storing a reference to the sendResponse parameter is critical. Without it, the response object goes out of scope and cannot be called. Thanks for the code which hinted me towards fixing my problem! Jan 30, 2018 at 12:01
  • maybe another solution is to wrap everything inside an async function with Promise and call await for the async methods?
    – Enrique
    Jan 1, 2019 at 13:08

3 Answers 3

381

From the documentation for chrome.runtime.onMessage.addListener:

If you want to asynchronously use sendResponse(), add return true; to the onMessage event handler.

So you just need to add return true; after the call to getUrls to indicate that you'll call the response function asynchronously.

Note this isn't mentioned on other documentation (for example the onMessage documentation) so it's possible developers miss this.

9
  • 66
    +1 for this. It has saved me after wasting 2 days trying to debug this issue. I can't believe that this is not mentioned at all in the message passing guide at: developer.chrome.com/extensions/messaging
    – funforums
    Nov 13, 2014 at 12:18
  • 8
    I've apparently had this issue before; came back to realize I had already upvoted this. This needs to be in bold in big <blink> and <marquee> tags somewhere on the page. Aug 14, 2015 at 16:54
  • 2
    @funforums FYI, this behavior is now documented in the messaging documentation (the difference is here: codereview.chromium.org/1874133002/patch/80001/90002).
    – Rob W
    Jun 16, 2016 at 22:56
  • 20
    I swear this is the most unintuitive API I've ever used. Jul 17, 2016 at 8:06
  • 3
    Unfortunately this doesn't seem to work with async/await... Feb 7, 2022 at 7:57
9

The accepted answer is correct, I just wanted to add sample code that simplifies this. The problem is that the API (in my view) is not well designed because it forces us developers to know if a particular message will be handled async or not. If you handle many different messages this becomes an impossible task because you never know if deep down some function a passed-in sendResponse will be called async or not. Consider this:

chrome.extension.onMessage.addListener(function (request, sender, sendResponseParam) {
if (request.method == "method1") {
    handleMethod1(sendResponse);
}

How can I know if deep down handleMethod1 the call will be async or not? How can someone that modifies handleMethod1 knows that it will break a caller by introducing something async?

My solution is this:

chrome.extension.onMessage.addListener(function (request, sender, sendResponseParam) {

    var responseStatus = { bCalled: false };

    function sendResponse(obj) {  //dummy wrapper to deal with exceptions and detect async
        try {
            sendResponseParam(obj);
        } catch (e) {
            //error handling
        }
        responseStatus.bCalled= true;
    }

    if (request.method == "method1") {
        handleMethod1(sendResponse);
    }
    else if (request.method == "method2") {
        handleMethod2(sendResponse);
    }
    ...

    if (!responseStatus.bCalled) { //if its set, the call wasn't async, else it is.
        return true;
    }

});

This automatically handles the return value, regardless of how you choose to handle the message. Note that this assumes that you never forget to call the response function. Also note that chromium could have automated this for us, I don't see why they didn't.

14
  • One issue is that sometimes you will not want to call the response function, and in those cases you should return false. If you don't, you are preventing Chrome from freeing up resources associated to the message.
    – rsanchez
    May 2, 2014 at 18:24
  • yes, thats why I said to not forget calling the callback. That special case you menion can be handled by having a convention that the handler (handleMethod1 etc) return false to indicate the "no response" case (though Id rather just always make a response, even an empty one). This way the maintainability problem is only localized to those special "no return" cases.
    – Zig Mandel
    May 2, 2014 at 20:01
  • 8
    Don't re-invent the wheel. The deprecated chrome.extension.onRequest / chrome.exension.sendRequest methods behaves exactly as you describe. These methods are deprecated because it turns out that many extension developers did NOT close the message port. The current API (requiring return true) is a better design, because failing hard is better than leaking silently.
    – Rob W
    Oct 31, 2014 at 17:41
  • @RobW but whats the issue then? my answer prevents the dev from forgetting to return true.
    – Zig Mandel
    Aug 13, 2015 at 14:42
  • 1
    true. Im just too accostumed to hungarian, I actually have it as "fCalled" in my code but made it b for this post :)
    – Zig Mandel
    Mar 28, 2023 at 16:05
2

You can use my library https://github.com/lawlietmester/webextension to make this work in both Chrome and FF with Firefox way without callbacks.

Your code will look like:

Browser.runtime.onMessage.addListener( request => new Promise( resolve => {
    if( !request || typeof request !== 'object' || request.type !== "getUrls" ) return;

    $.ajax({
        'url': "http://localhost:3000/urls",
        'method': 'GET'
    }).then( urls => { resolve({ urls }); });
}) );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.