Chrome extension: sendResponse not working properly

I am taking a screenshot in the viewport using the built-in chrome extension function. In the background script, when a message is received to capture a screenshot, I take it and send dataURI back to the beginning using sendResponse. When I console.log URI in the background of the script, it works fine, but when I get the answer at the beginning of the script, it turns out to be undefined. What am I doing wrong?

Background script:

 chrome.runtime.onMessage.addListener(function(request,sender,sendResponse) { console.log("huf"); if(request.capture) { chrome.tabs.query({active: true, currentWindow: true}, function(tabs) { chrome.tabs.captureVisibleTab(null, {}, function(dataUrl) { if(dataUrl) { console.log(dataUrl); sendResponse({"screenshot":dataUrl}); } }); }); } return true; }); 

The contents of the script:

 chrome.runtime.sendMessage({"capture":true},function(response) { console.log(response.screenshot); }); 
+5
source share

All Articles