Chrome Extension : Modify DOM before loading

老子叫甜甜 提交于 2019-12-12 10:48:25

问题


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 ?


回答1:


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);

 



来源:https://stackoverflow.com/questions/9778637/chrome-extension-modify-dom-before-loading

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!