Google Chrome Extension: Get Current Page HTML From Context Menu Item

4,205 views
Skip to first unread message

infl8o...@gmail.com

unread,
Jul 18, 2014, 9:01:58 AM7/18/14
to chromium-...@chromium.org
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-extension

This 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?

Benjamin Kalman

unread,
Jul 18, 2014, 11:16:45 AM7/18/14
to infl8o...@gmail.com, chromium-...@chromium.org
It looks like you're calling chrome.extension.getBackgroundPage() from a content script, but that method only works from extension pages like options pages and extension popups.

To communicate back to the options page you should be specifying the sendResponse variable of chrome.runtime.onMessage[1] and calling it with message.innerText.

Of course, note to be careful of XSS when sending back raw HTML to your background page.


[1] chrome.runtime.onMessage is preferable to chrome.extension.onMessage, though they do the exact same thing.


--
You received this message because you are subscribed to the Google Groups "Chromium-extensions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extens...@chromium.org.
To post to this group, send email to chromium-...@chromium.org.
Visit this group at http://groups.google.com/a/chromium.org/group/chromium-extensions/.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/chromium-extensions/4b66acb1-ab9f-45a7-b50a-d7723a3b8e99%40chromium.org.
For more options, visit https://groups.google.com/a/chromium.org/d/optout.

infl8o...@gmail.com

unread,
Jul 18, 2014, 4:36:57 PM7/18/14
to chromium-...@chromium.org, infl8o...@gmail.com
No, the first code snippet (which includes the getBackgroundPage()) comes from the Popup.js (used in Popup.html), so it is not a content script. It's on the extension popup.

This code works, and it does set correctly the global var. globalCurrentPageHTML (defined separately in Background.js). There is no getBackgroundPage() in any of the Background scripts. But it only works when the Popup is opened. What I need to do is to fill the variable when the extension's context right-click menu is clicked. When I handle the context menu, I find that the variable hasn't been set yet; it only gets set after I open the extension's popup (with its DIV injection).
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extensions+unsub...@chromium.org.
Reply all
Reply to author
Forward
0 new messages