可以在初始 DOM 解析期间/之前修改 DOM 吗?
是否可以在初始 DOM 解析期间或之前修改 DOM?或者我是否必须等到 DOM 被解析和构建之后才能与其交互?更具体地说,是否有可能阻止 DOM 中的脚本元素使用用户脚本/内容脚本或 Chrome 或 Firefox 中的类似脚本运行?
在解析 DOM 之前尝试在 DOMNodeInserted 上使用 eventListeners,但这些事件监听器仅在 DOM 构建后才会触发。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是两个独立的问题:
1。是否可以在初始 DOM 解析期间或之前修改 DOM?
是的。一旦浏览器构建了根元素,您就可以开始查询和改变 DOM。请注意,当您的脚本运行时,某些页面可能仍未解析,甚至可能仍在网络上传输。您的脚本通常可以访问源代码中包含/调用脚本的脚本标记之前声明的任何元素。这包括包含脚本标记的父元素。
2.是否可以在 chrome 或 firefox 中使用用户脚本/内容脚本或类似脚本来阻止 DOM 中的脚本元素运行? 不会。
所有脚本都会执行,并且一个脚本无法阻止另一脚本的初始执行。但是,您也许可以返回并删除事件处理程序,或者尝试抵消脚本的影响。尽管这种情况看起来有点阴暗和/或违背了 JavaScript 的正常使用。
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.
实际上,我在为 Chrome 开发广告拦截器时对此进行了很多研究。基本上,您不能仅使用 Javascript 来停止 Javascript,特别是因为它会与相关脚本元素同时运行。因此,即使它有效,也会导致竞争条件。
I've actually looked into this a lot while developing an adblocker for Chrome. Basically, you can't use just Javascript to stop Javascript, especially since it would run simultaneously with the script element in question. So even if it would work it would cause a race condition.
你可以在文档解析过程中修改元素,但是在这个元素添加到dom之后。
例如
you can modify elements during document parsing, but after than this element has added to dom.
for example
是的,这是可能的。
首先完全阻止文档被完全解析,然后在侧面获取相同的文档,对此文档进行任何处理,然后将生成的文档注入页面中。
这是我目前的做法 https://stackoverflow.com/a/36097573/6085033
Yes it is possible.
Start by completely preventing the document from getting parsed altogether then on the side, fetch the same document, do any processing on this document and then inject the resulting document in the page.
Here is how I currently do just that https://stackoverflow.com/a/36097573/6085033