4

As described in the title, I'm trying to write a background script that will listen for load requests from either the popup.js or contentscript.js. When it receives a load, it get the content of chrome.storage.local, performs a little bit of data processing(for loop) and sends it to the requester.

The current issue, is my code receives the request, but fails to send back the data. My code is listed below:

popup.js:

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

background.js:

chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
  if(request.greeting=='Load') {
    chrome.storage.local.get(null,function(storeObject){
      var newList=[];
      //Perform some dataprocessing to store part of storeObject into newList
      sendResponse(newList);
    });
  }
});

I think the issue is related to scope, since after debugging it looks like sendResponse is trying to send from chrome.storage instead of background.js. Also, if I send the message outside (before/after) the chrome.storage.local callback, popup.js receives the message.Regardless I'm pretty lost at what to do to get this message passing working and would appreciate any help.

abraham
  • 46,583
  • 10
  • 100
  • 152
bkvchu
  • 43
  • 1
  • 3
  • possible duplicate of [Chrome Extension Message passing between extension(background) and content script](http://stackoverflow.com/questions/20077487/chrome-extension-message-passing-between-extensionbackground-and-content-scrip) – rsanchez Dec 19 '13 at 23:44
  • Thanks for the quick reply rsanchez. This was part of the problem, now background.js shows an error message: "An error occurred: Could not establish connection. Receiving end does not exist." This is probably related to the scope issue I hypothesized. The port numbers are not set when sendMessage is called within thechrome.storage.local.get callback versus outside of the callback. – bkvchu Dec 20 '13 at 00:36
  • Error message was because I called sendResponse twice. Once I fixed that, there is still no message being sent nor is there any error message. – bkvchu Dec 20 '13 at 00:47

1 Answers1

21

According to the docs on `chrome.runtime.onMessage:

sendResponse ( function )
[...]This function becomes invalid when the event listener returns, unless you return true from the event listener to indicate you wish to send a response asynchronously (this will keep the message channel open to the other end until sendResponse is called).

Since, sendResponse is called asynchronously in chrome.storage.local.get()'s callback, you need to return true from the onMessage listener to prevent the function from being invalidated.

chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
    if (request.greeting === 'Load') {
        chrome.storage.local.get(null, function(storeObject) {
            ...
            sendResponse(newList);
        });
        return true;   // <-- I intend to call `sendResponse` later
    }
    return false;   // <-- I do NOT intend to call `sendResponse`
});
gkalpak
  • 47,844
  • 8
  • 105
  • 118
  • Thanks ExpertSystem. The location of the return true made all the difference. Originally I placed return true inside of the if statement, when it was outside it started working! – bkvchu Dec 20 '13 at 02:47