我正在实现一个铬扩展应用程序。我想用“#”替换标签中的href属性(在我的webapp主页上)。的问题是,这个标记可能是由ajax动态加载的,并且可以通过用户操作重新加载。对于如何让chrome扩展检测ajax加载的html内容有什么建议吗?
发布于 2013-08-02 12:43:48
有两种方法,
第一个解决方案是处理ajax请求。
.ajaxComplete()函数在jQuery中处理页面上的所有ajax请求。
在content script中,
var actualCode = '(' + function() {
$(document).ajaxComplete(function() {
alert('content has just been changed, you should change href tag again');
// chaging href tag code will be here
});
} + ')();';
var script = document.createElement('script');
script.textContent = actualCode;
(document.head||document.documentElement).appendChild(script);
script.parentNode.removeChild(script);第二个解决方案是监听内容更改。
这在突变事件中是可能的,在content script中也是如此。
$(document).bind("DOMSubtreeModified", function() {
alert("something has been changed on page, you should update href tag");
});您可以使用一些不同的选择器来限制控制更改的元素。
$("body").bind("DOMSubtreeModified", function() {}); // just listen changes on body content
$("#mydiv").bind("DOMSubtreeModified", function() {}); // just listen changes on #mydiv contenthttps://stackoverflow.com/questions/17986020
复制相似问题