我有一个Chrome扩展程序,可在网站上进行一些更改(编辑评论)。
在网站上最近的更改(网站不是我的)之后 - 使用ajax加载注释块(在整个页面重新加载之前它是简单的发布请求之前)。
现在,如果我第一次加载页面 - 内容脚本有效,但是当我转到下一页时,说第2页 - 使用ajax添加注释,扩展脚本不再运行。所以评论不会改变我想要的方式。
有没有简单的方法来监听页面更改DOM并再次应用扩展脚本?
清单文件中的我有:
{
"name": "Name",
"version": "1.0",
"description": "Description",
"icons": {"16":"16.png",
"48":"48.png",
"32":"32.png",
"128":"128.png"},
"browser_action": {
"default_title": "Default title",
"default_icon": "48.png",
"popup": "popup.html"
},
"permissions": [
"tabs",
"http://www.site.com/*",
"notifications",
"unlimitedStorage"
],
"options_page": "options.html",
"background_page": "background.html",
"content_scripts": [
{
"matches": ["http://www.site.com/*","https://www.site.com/*"],
"js": ["jquery-1.7.1.min.js","content.js"],
"run_at": "document_end"
}]
}
答案 0 :(得分:8)
您可以在名为DOMSubtreeModified
的事件上添加侦听器。将函数绑定到它,只要有变化就会调用它。
在jQuery中:
$('#ContentContainer').bind('DOMSubtreeModified',modifyComments);
更新:
如果事件多次触发,请在第一次删除事件绑定,并在超过一定时间后,您可以调用modifyComments并重新绑定事件。
function DOMModificationHandler(){
$(this).unbind('DOMSubtreeModified.event1');
setTimeout(function(){
modifyComments();
$('#ContentContainer').bind('DOMSubtreeModified.event1',DOMModificationHandler);
},1000);
}
//after document-load
$('#ContentContainer').bind('DOMSubtreeModified.event1',DOMModificationHandler);