简体   繁体   English

如何提取页面的整个 HTML - 在 DOM 之前和之后?

[英]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.

相关问题 如何提取javascript生成的DOM对象的html代码? - How can I extract the html code for a DOM object generated by javascript? 如何在找到的HTML元素之前插入整个CSS类? - How can I insert a whole css class before an HTML element that I find? 我如何使整个页面变暗? (html) - How do i darken the whole page? ( html ) 我可以在加载整个页面之前运行 javascript 吗? - Can I run javascript before the whole page is loaded? 如何在 HTML 输入的字符前后添加空格 - How I can add spaces before and after character on HTML input 如何在处理 DOM 之前使用谷歌浏览器内容脚本修改 web 页面? - How can I modify a web page using google chrome content script before the DOM is processed? 如何在不刷新页面的情况下将内容附加到html页面并刷新DOM? - How can i append content to my html page and refresh the DOM without refresh the page? 在将按键事件应用于 DOM 之前,如何确定按键事件之后的值是什么? - How can I determine what a value would be after a keypress event BEFORE it is applied to the DOM? 如何将当前网页的整个HTML文档发送到服务器? - How can I send Current web page's whole HTML document to server? 如何将整个 html 页面调用到请求处理程序 JavaScript 文件? - How can i call the whole html page to a request handler JavaScript file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM