Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chrome Dev Tools export Elements HTML

To debug my chromium-embedded application I am looking for a function to get the source code of the web page from withing the chrome developer tools. I basically want the HTML tree shown in the 'Elements' tab, the actual HTML DOM, as HTML text. Does this functionality exist? How can I use it?

As I use CEF I do only have the chrome dev tools available and not the full browser. I cannot use the right-click context menu because I want to see the current manipulated DOM and not the original source.

I want to use this functionality for debugging purpose so that I can diff two different HTML trees.

like image 835
Sjoerd222888 Avatar asked Jan 26 '15 13:01

Sjoerd222888


People also ask

How do I copy HTML from DevTools?

From the top menu, select Tools > Web Developer > Page Source. A new tab will open with the page's code, which you can copy by highlighting a specific area or by right-clicking to Select All if you want all of the code. Press Ctrl+C or Command+C on your keyboard and paste it into a text or document file.

How do you save elements in DevTools?

Right click on the element, go to Copy > Copy element. Then paste the clipboard content into your favorite editor or IDE and save. Not as convenient as saving from the Sources tab, but it does allow you to get/save all the changes you've made in the Elements tab.


3 Answers

Select the top node and choose "Copy". You'll have to re-add the Doctype, though.

Exporting HTML from Chrome DevTools

Alternatively, you could click on "edit as HTML" and copy it from there.

like image 115
Sebb Avatar answered Oct 06 '22 01:10

Sebb


update: extension has been released! Named Dump Dom
chrome store
github source

I found a better way to dump the current dom tree to a html file ( to persist your changes to the dom tree in the element tab ),just paste the code below to the console, and a dom.html file would be downloaded.

filename = "dom";
var html = '',
  node = document.firstChild
while (node) {
  switch (node.nodeType) {
    case Node.ELEMENT_NODE:
      html += node.outerHTML
      break
    case Node.TEXT_NODE:
      html += node.nodeValue
      break
    case Node.CDATA_SECTION_NODE:
      html += '<![CDATA[' + node.nodeValue + ']]>'
      break
    case Node.COMMENT_NODE:
      html += '<!--' + node.nodeValue + '-->'
      break
    case Node.DOCUMENT_TYPE_NODE:
      // (X)HTML documents are identified by public identifiers
      html +=
        '<!DOCTYPE ' +
        node.name +
        (node.publicId ? ' PUBLIC "' + node.publicId + '"' : '') +
        (!node.publicId && node.systemId ? ' SYSTEM' : '') +
        (node.systemId ? ' "' + node.systemId + '"' : '') +
        '>\n'
      break
  }
  node = node.nextSibling
}


var file = new Blob([html], {
  type: 'text/html'
});
if (window.navigator.msSaveOrOpenBlob) // IE10+
  window.navigator.msSaveOrOpenBlob(file, filename);
else { // Others
  var a = document.createElement("a"),
    url = URL.createObjectURL(file);
  a.href = url;
  a.download = filename;
  document.body.appendChild(a);
  a.click();
  setTimeout(function () {
    document.body.removeChild(a);
    window.URL.revokeObjectURL(url);
  }, 0);
}

enter image description here Inspired from this project: https://github.com/wingleung/save-page-state. And I would develop an extention to make on-click-dump functional later.

like image 32
傅继晗 Avatar answered Oct 06 '22 01:10

傅继晗


You can try the following:

enter image description here

All you have to do is right click the element and copy the outerHTML.

like image 35
G. LC Avatar answered Oct 06 '22 00:10

G. LC