可以在初始DOM解析期间/之前修改DOM吗? [英] Possible to modify DOM during/before initial DOM parsing?

查看:68
本文介绍了可以在初始DOM解析期间/之前修改DOM吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在初始DOM解析期间或之前修改DOM?还是我必须等到DOM解析并构建之后再与之交互?更具体地说,是否有可能阻止DOM中的脚本元素使用用户脚本/内容脚本或chrome或firefox中的类似脚本运行?

解决方案

这是两个独立的问题:



1。



是的,是否可以在初始DOM解析期间或之前修改DOM?一旦浏览器构建了根元素,就可以开始查询和变异DOM。请注意,当脚本运行时,某些页面可能仍未解析,甚至可能仍在网络上传输。您的脚本通常可以访问在包含/调用脚本的script标签之前的源 中声明的任何元素。这包括包含您的脚本标记的父元素。



2。



否。是否有可能阻止DOM中的脚本元素使用用户脚本/内容脚本或类似内容在chrome或firefox中运行?所有脚本均已执行,并且一个脚本不能阻止另一个脚本的初始执行。但是,您也许可以返回并删除事件处理程序,否则尝试抵消脚本的效果。尽管这种情况似乎有些阴暗和/或与正常的JavaScript使用方式背道而驰。


Is it possible to modify the DOM during or before the initial DOM parsing? Or do I have to wait until the DOM is parsed and built before interacting with it? More specifically, is it possible to hinder a script element in DOM from running using userscripts/content scripts or similar in chrome or firefox?

Tried using eventListeners on DOMNodeInserted before the DOM is parsed, but these are only fired after the DOM is built.

解决方案

These are two separate questions:

1. Is it possible to modify the DOM during or before the initial DOM parsing?

Yes. As soon as the browser builds the root element, then you can start querying and mutating the DOM. Note that when your script runs, some of the page may still yet be unparsed, perhaps even still in transit on the network. Your script generally has access to any element declared in the source before the script tag containing/calling your script. This includes parent elements containing your script tag.

2. Is it possible to hinder a script element in DOM from running using userscripts/content scripts or similar in chrome or firefox?

No. All scripts are executed and one script can't prevent another script's initial execution. However, you can perhaps go back through and remove event handlers, and otherwise attempt to counteract the effects of a script. Although this scenario seems a bit shady and/or against the grain of normal JavaScript usage.

这篇关于可以在初始DOM解析期间/之前修改DOM吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆