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.
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.
Share Improve this question edited Dec 20, 2013 at 1:14 abraham 47.9k10 gold badges106 silver badges158 bronze badges asked Dec 19, 2013 at 23:27 bkvchubkvchu 431 silver badge3 bronze badges 3- possible duplicate of Chrome Extension Message passing between extension(background) and content script – rsanchez Commented Dec 19, 2013 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 Commented Dec 20, 2013 at 0: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 Commented Dec 20, 2013 at 0:47
1 Answer
Reset to default 21According to the docs on `chrome.runtime.onMessage:
sendResponse ( function )
[...]This function bees 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`
});