My main objective was to capture a screenshot through the background page using:
http://developer.chrome.com/extensions/tabs.html#method-captureVisibleTab
and then transmit it to the content script for further analysis and customization utilizing the page's HTML DOM.
However, I encountered difficulty in passing the dataUrl back to the content script via Message Passing :
http://developer.chrome.com/extensions/messaging.html
I attempted JSON.stringify() without success.
The code below is functioning perfectly:
background.js
chrome.runtime.onMessage.addListener(
function(request,sender,sendResponse){
sendResponse({imgSrc:'hello'});
});
But when I switch to the following code, nothing seems to work:
background.js
chrome.runtime.onMessage.addListener(
function(request,sender,sendResponse){
chrome.tabs.captureVisibleTab(
null,
{},
function(dataUrl) {
sendResponse({imgSrc:dataUrl});
}
)}
);
The only evidence that the background page has successfully captured a screenshot is if I execute:
chrome.tabs.captureVisibleTab(null,{},function(dataUrl){console.log(dataUrl);});
and I witness
"data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAA....etc..."
printed in background.html, confirming its validity
My query is: How can I transfer this URL to the content script?
I would rather not conduct all the processing on the background page which lacks control over the actual visible page.