I'm coding a chrome extension that interacts with gmail api (chrome 45 is my version) and I am having issues sending a message from the background.js to my content-script. The async aspect is where the issue lies. How can I get the message to be sent after the callback?
//---------in content script---------
chrome.runtime.sendMessage({ messageId: _id }, function (response) {
console.log('the respose.messagePayload is: ' + response.messagePayload);
});
//---------in background script---------
chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
getMessage('me', request.messageId, function (payload) {
//i want to send my response here
//this executes and grabs the payload of data from the api, but isn't sending back the message to the content-script
sendResponse({ messagePayload: payload });
});
//this synchronous call sends a message to the content-script
//sendResponse({ messagePayload: "payload" });
return true;
});
function getMessage(userId, messageId,callback) {
var request = gapi.client.gmail.users.messages.get({
'userId': userId,
'id': messageId
});
request.execute(callback);
}