简体   繁体   English

获取 javascript 渲染页面的 html(与它交互后)

[英]Get the html of the javascript-rendered page (after interacting with it)

I would like to be able to save the state of the html page after I've interacted with it.我希望能够在与 html 页面交互后保存它的状态。

Say I click a checkbox, or the javascript set the values of various elements.假设我单击了一个复选框,或者 javascript 设置了各种元素的值。

How can I save the "javascript-rendered" page?如何保存“javascript-rendered”页面?

Thanks.谢谢。

In Chrome (and apparently Firefox), there is a special copy() method that will copy the rendered content to the clipboard.在 Chrome(显然是 Firefox)中,有一个特殊的 copy() 方法可以将渲染的内容复制到剪贴板。 Then you can do whatever you want by pasting it to your preferred text editor.然后您可以通过将其粘贴到您喜欢的文本编辑器来做任何您想做的事情。

https://developers.google.com/chrome-developer-tools/docs/commandline-api#copyobject https://developers.google.com/chrome-developer-tools/docs/commandline-api#copyobject

Console Example:控制台示例:

copy(document.body.innerHTML);

Note: I noticed Chrome reports undefined after the method is run, however, it seems to execute correctly and the right content is in the clipboard.注意:我注意到 Chrome 在方法运行后报告未定义,但是,它似乎正确执行并且正确的内容在剪贴板中。

那应该做并且将抓取所有页面而不仅仅是正文

console.log(document.getElementsByTagName('html')[0].innerHTML);

document.body.innerHTML will get you the HTML representation of the current document body. document.body.innerHTML将为您提供当前文档正文的 HTML 表示。

That will not necessarily include all internal state of DOM objects because the HTML contains the initial default state of objects, not necessarily the state that they may have been changed to.这不一定包括 DOM 对象的所有内部状态,因为 HTML 包含对象的初始默认状态,不一定是它们可能已更改为的状态。 The only way to guarantee you get all that state is to make a list of what state you want to save and actually programmatically get that state.保证您获得所有状态的唯一方法是列出您要保存的状态并实际以编程方式获得该状态。

To answer the part of your question about saving it, you'll have to describe more about what problem you're really trying to solve.要回答关于保存它的部分问题,您必须更多地描述您真正想要解决的问题。

To get the equivalent of view source with javascript rendered, including doctype and html tags, copy the command into the chrome console:要获得具有 javascript 渲染的等效视图源代码,包括 doctype 和 html 标签,请将命令复制到 chrome 控制台:

console.log(new XMLSerializer().serializeToString(document.doctype) + document.getElementsByTagName('html')[0].outerHTML);

In the chrome console, hover at the end of the output and click on the copy link to copy to the pasteboard.在 Chrome 控制台中,将鼠标悬停在输出的末尾并单击复制链接以复制到粘贴板。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM