[英]How can I extract the whole HTML of a page - before and after the DOM?
I'm trying to find a way to extract the HTML code of a web page using JavaScript in two editions:我试图找到一种方法来使用两个版本的 JavaScript 提取 web 页面的 HTML 代码:
Before the DOM / Before JS is applied After the DOM / After JS is applied DOM 应用前 / JS 应用前 DOM 后 / JS 应用后
All the JavaScript methods that I know just take it from the DOM element, like document.body or document.all... but non of them work specifically for before or after the DOM.我所知道的所有 JavaScript 方法都只是从 DOM 元素中获取它,例如 document.body 或 document.all... 但它们都不是专门针对 DOM 之前或之后工作的。
Added: Just to focus the question further, this is not my page so I can't install any on the page, this is for any random web page.补充:为了进一步关注问题,这不是我的页面,所以我无法在页面上安装任何内容,这是针对任何随机 web 页面的。
Can you point me in the right direction?你能为我指出正确的方向吗? is there a specific method/command/process that is used in JavaScript and can do that? JavaScript中是否有特定的方法/命令/过程可以做到? maybe I should stop the page load at specific point and take the code and then let it continue loading and take the code with the JS included?也许我应该在特定点停止页面加载并获取代码,然后让它继续加载并获取包含 JS 的代码?
Well, If you put a <script />
tag in the middle of your HTML
your code will get executed before the continuation of the rest of your HTML.好吧,如果您在HTML
的中间放置一个<script />
标记,您的代码将在 HTML 的 rest 继续之前执行。
also you can use你也可以使用
document.addEventListener("DOMContentLoaded", fn);
to call your function when the DOM
is ready (it comes in the name of the event listener so sorry for over-explaination!)在DOM
准备就绪时致电您的 function(它以事件侦听器的名称出现,很抱歉过度解释!)
Getting all the stuff is easy via the window
object, can not imagine anything else you might need.通过window
object 很容易获得所有东西,无法想象您可能还需要什么。
So if you want to do something before the DOM
loads just put a <script />
tag at the top.所以如果你想在DOM
加载之前做一些事情,只需在顶部放置一个<script />
标签。 if you want the dom to be ready use the DOMContentLoaded
.如果您希望 dom 准备就绪,请使用DOMContentLoaded
。
You can find great and complete documentation on this subject of script, async, defer on javascript.info .您可以在javascript.info上找到有关脚本、异步、延迟这一主题的出色而完整的文档。 if there is anything that my explanation did not cover for you.如果有任何我的解释没有为您涵盖的内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.