我有一个 chrome 扩展程序,它在页面加载后将 iFrame 注入(inject) dom。
我还想监听 dom 更改,但我想忽略因注入(inject) iFrame 而被触发的 DOMSubtreeModified
。因此,在我注入(inject) iFrame 之前或之后的任何事件,我都想捕获。
这是查看 dom 更改的内容脚本的基础知识:
var processRequest = function() {
$('body').bind('DOMSubtreeModified', DOMModificationHandler);
return 'complete';
}
function DOMModificationHandler() {
$(this).unbind('DOMSubtreeModified');
setTimeout(function() {
//skip this if it was my iFrame injection that caused this event
//someLocalFunction();
$('#ContentContainer').bind('DOMSubtreeModified', DOMModificationHandler);
},1000);
}
当 DOMModificationHandler
触发时,我能做些什么来区分我的注入(inject)和其他 dom 事件吗?
**更新:我考虑过向 iFrame 添加某种类型的 id 或数据元素,但这只是告诉我它就在那里,而不是 iFrame 导致了事件。特别是当有一个 AJAX 请求更新了部分 dom 时。任何 id 或数据属性可能已经存在,所以我仍然不知道圆顶变化的来源。
最佳答案
查看 http://www.w3.org/TR/DOM-Level-3-Events/#event-type-DOMSubtreeModified随后 http://www.w3.org/TR/DOM-Level-3-Events/#events-event-type-target .您应该能够根据目标过滤事件。在这种情况下,目标是正在修改的父子树。
关于javascript - 检测在我的 Chrome 扩展之外生成的 DOM 更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13059419/