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));
});