1

In my Google Chrome extension, I need to be able to get the current page HTML, including any updated Ajax HTML (unlike the browser's View Source command, which doesn't update it).

Is there a way to get it as a string in my Extension?

Suppose my extension is a right-click context menu called "View Actual HTML Source" which should print the current HTML to the console, or maybe count the number of certain tags there. I wasn't able to find an easy answer to this.

gene b.
  • 10,512
  • 21
  • 115
  • 227

2 Answers2

0

You can get the current state of the DOM as HTML programmatically using document.documentElement.innerHTML

Or just use Developer Tools

Scott
  • 604
  • 3
  • 7
  • No, that did not work for me. When I put this in my Context Menu Handler, I get this HTML no matter what: " (size 65). This is not the HTML of any page I'm viewing. – gene b. Jul 12 '14 at 21:30
  • 1
    That is a different issue. You are running it on the background page. Use `chrome.runtime.executeScript` or communicate with a content script to run it on the page. – Scott Jul 12 '14 at 21:38
  • You mean chrome.tabs.executeScript (not runtime)? Some details or examples of how to use this would be appreciated. – gene b. Jul 12 '14 at 21:43
0

I followed the exact solution here, and this gave me the Page Source HTML:

Getting the source HTML of the current page from chrome extension

The solution is to inject the HTML into the Popup.

Community
  • 1
  • 1
gene b.
  • 10,512
  • 21
  • 115
  • 227