Chrome扩展程序:在加载前修改DOM [英] Chrome Extension : Modify DOM before loading

查看:214
本文介绍了Chrome扩展程序:在加载前修改DOM的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



例如,我的Chrome扩展程序在DOM完全构建之前应该修改DOM元素,并且在最佳情况下,如果我有一个在我的文档,我想等到它被构造,然后直接修改它,然后构建其余的DOM。



但我只管理在元素构造之前和构建整个DOM之后访问DOM。



那么如何聆听特殊元素的构造?

解决方案

您可以使用 document.addEventListener 以及 DOMNodeInserted 事件。这些节点将被构造,您将有机会在插入到DOM之前进行修改。

  function nodeInsertedCallback(event){
console.log(event);
};
document.addEventListener('DOMNodeInserted',nodeInsertedCallback);

 


My Chrome Extension should modify a DOM element before the DOM is fully constructed, and in the best scenario, right after this DOM element is constructed.

For example, if I have a in my document, I want to wait until it is constructed, then directly modify it, before the rest of the DOM is constructed.

But I only managed to access the DOM before the element is constructed and after the entire DOM is constructed.

So how do I listen to the construction of a special element ?

解决方案

You can use document.addEventListener with the DOMNodeInserted event. The nodes will be constructed and you will have a chance to modify them before they are inserted into the DOM. Something like the following should work.

function nodeInsertedCallback(event) {
  console.log(event);
};
document.addEventListener('DOMNodeInserted', nodeInsertedCallback);

 

这篇关于Chrome扩展程序:在加载前修改DOM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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