0

I ham having an issue with receiving responses to message in my case below. I have a popup.html and popup.js file which uses angularjs (not sure if that is relevant). The code below for the popup.js is run in the init of the angular app.

I store config settings in the chrome sync storage and am trying to have the popup and the contentscript javascript file request these settings from the background page.

When I put the 'sendResponse({})' call outside of the async chrome.storage.sync.get call, it works fine and the popup.js receives the response. However, when I 'sendResponse' call within the chrome.storage.sync.get callback, the popup message response handler is not executed. Is there something that I am doing incorrectly with message passing?

background page.js:

chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
  var resp= {greeting: "hi"};
  // when the response is sent here, the popup.js file writes to the console
  sendResponse(resp);

  // The below code displays the alert but the popup.js does not display the console.log
  //chrome.storage.sync.get("isActive", function (isActiveData) {
  //
  //  sendResponse(resp);
  //  alert('response sent: ' + JSON.stringify(resp));;
  //});
});

popup.js:

chrome.runtime.sendMessage({action: "getConfig"}, function (getConfigResponse) {
    console.log("getConfigResponse: " + JSON.stringify(getConfigResponse));
});
B.McCarthy
  • 123
  • 1
  • 1
  • 13
  • Err. `chrome.storage` is freely available in both your content script and the popup. Just saying: you're overcomplicating things. This is precisely why the `storage` API was added, in my opinion. – Xan Dec 17 '14 at 00:15
  • And on topic of your error, it's a duplicate of http://stackoverflow.com/questions/20077487/chrome-extension-message-passing-response-not-sent – Xan Dec 17 '14 at 00:16

0 Answers0