how to get fully computed HTML (instead of source HTML)?

人盡茶涼 提交于 2019-12-17 17:38:37

问题


Given a webpage that uses lots of javascript to generate its HTML, how can I get the final computed HTML being parsed by the browser instead of the source HTML? In other words, presume a page has lots of tags surrounding javascript functions that, when called, return some HTML. When I view the source of the page, I see the script function call, not the HTML it produces.

How could I get all of the HTML produced by a webpage?

I've noticed that Firebug appears able to see the HTML instead of the scripts, but it doesn't appear to have any way to save the whole page, only little segments of it.

Update:

Thanks for all the answers. However, I'm still not getting the HTML I see in Firebug's console with any of those techniques. For my example page, I'm using the 'Info' tab of my own Facebook profile. If you view source on that page, you'll see lots of scripts with the title 'big_pipe.onPageletArrive()'. However, if you look at it in Firebug, each of those function calls renders out to HTML. I tried the right-click on the tag in Firebug, the View Generated Source in the Webdev Toolbar, and the Chrome suggestion, but they all give me the script call, not the HTML.

Any other ideas?

Update 2:

When I said each of those functions renders out to HTML in Firebug, I wasn't quite correct. They only render out if I select them in the page and right click->Inspect Element. Then it appears to render it out. So maybe my question has become how do you get Firebug to automatically render out all of the HTML so you can select and save it? (Or I'm open to any other solution for grabbing this HTML).


回答1:


With Firebug's HTML tab, you can right click on the <html> element, and click "Copy HTML".

You can do the same thing with Developer Tools in Chrome/Safari.




回答2:


The Web Developer Toolbar for Firefox has a "View Generated Source" option which provides this functionality.




回答3:


with (window.open("")) {
    document.open("text/html");
    document.write("<!--\n"); //for live version delete this line
    document.write(opener.document.documentElement.outerHTML.replace(/</g,"<").replace(/>/g, ">"));
    document.write("\n//-->"); //for live version delete this line
    document.close();
    document.title = "DOM Snapshot:" + opener.document.title;
    focus();
}
  1. Open console
  2. copy paste the above code and execute
  3. it opens an empty page,
  4. now inspect the page with right click or f12,
  5. copy outerhtml of the comment
  6. paste wherever you want
  7. optionally remove the comment at the start and end

If you want a live version that is clickable, then simple leave out the comment tags in the above code.




回答4:


document.getElementById('awesomeness').textContent = document.documentElement.outerHTML.replace(/<\/\w+>/g, (e) => e + '\r\n');
<div id="awesomeness" style="overflow:scroll;width:100%;height:100%;white-space:pre;"/>

so yea, use that...




回答5:


It is not possible generally. Here is excerpt from my bookmarklet which relies on non-standard outerHTML:

with (window.open("")) {
    document.open("text/html");
    document.write("<PRE>");
    document.write(opener.document.documentElement.outerHTML.replace(/</g,"<").replace(/>/g, ">"));
    document.write("</PRE>");
    document.close();
    document.title = "DOM Snapshot:" + opener.document.title;
    focus();
}

Note: DTD is missing and not retrievable at all.



来源:https://stackoverflow.com/questions/6364138/how-to-get-fully-computed-html-instead-of-source-html

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!