1

I am building a Chrome extension and am trying to send a message to my event background.js page from inside of chrome.tabs.captureVisibleTab(). For some reason the message will not send to my content script...

chrome.runtime.onMessage.addListener(
 function(request, sender, sendResponse) {
 var responseObj = new Object();
 if(request.screenshotRequest == true){
  chrome.tabs.captureVisibleTab({ format: "png"}, function(dataUrl){
    responseObj.screenshotRequest = dataUrl;
    alert(sendResponse);
    sendResponse(responseObj);
   });
  }
});

My manifest.json allows for the correct permissions to allow this message passing to happen. When I try and pass messages using sendResponse() outside of the chrome.tabs.captureVisibleTab() the message passes correctly and I can access it from the content script. Any idea why this isn't passing?

UPDATE: alert(sendResponse); proves that chrome.tabs.captureVisibleTabs() has access to sendResponse because the object is correctly displayed in the alert message.

Brannon
  • 1,286
  • 2
  • 21
  • 36

1 Answers1

4

I dug into the console of the unpacked extension (apparently it is different from the regular extension's console: Where to read console messages from background.js in a Chrome extension?) and it said that chrome.runtime.onMessage.addListener needed to return true if sendResponse was called inside of a callback in the function. It worked!

Community
  • 1
  • 1
Brannon
  • 1,286
  • 2
  • 21
  • 36