I need to analyze the current page HTML in my Google Chrome extension. I followed this instruction on how to obtain the source HTML in the extension:
http://stackoverflow.com/questions/11684454/getting-the-source-html-of-the-current-page-from-chrome-extensionThis solution works, but it injects the HTML into the Popup (an invisible Div in the Popup). I need to store the HTML in a global variable when I click the right-click context menu of my extension. I don't want to open a popup everytime I need to get the HTML. Instead, there is a global variable defined in background.js, which I need to fill every time the right-click context menu is clicked.
When I tried retrieving it the following way, it didn't work.
1) From the Popup solution: The Listener is added
// Injection Script Start
chrome.extension.onMessage.addListener(function(request, sender) {
if (request.action == "getSource") {
message.innerText = request.source;
// Set current HTML into Global Background variable
var bg = chrome.extension.getBackgroundPage();
bg.globalCurrentPageHTML = message.innerText;
}
});
2) From the right-click menu handler in background.js, retrieve the global variable:
function clickContextMenuHandler() {
alert('Context Menu clicked. Current HTML = ' + globalCurrentPageHTML);
};
This does not work for some reason, but the Popup DIV injection/retrieval works. Any suggestions?