我正在开发一个Firefox附加组件,有了它,我需要监视特定站点的内容并对DOM更改做出反应。目前,我正在使用gBrowser.contentDocument.getElementsByClassName("class")
的组合并附加DOMSubteeeModified
事件。但我注意到它仅在选项卡处于活动状态时才有效。当我离开使用另一个选项卡,并且DOM在非活动选项卡中更改时,它不起作用。我该如何解决这个问题? Firefox MDN相当分散(有时过时),对新手来说非常令人沮丧。
以下是我正在做的简化版本:
var MyExtension = {
init() : function() {
if("gBrowser" in window) {
gBrowser.addEventListener("DOMContentLoaded", function(e){this.onPageLoad(e);},false);
},
onPageLoad: function(e) {
var doc = e.originalTarget;
if((/http://xxxx.xyz/v/[0-9a-z]/).test(doc.location.href)) {
MyExtension.XXX.handler(e);
}
e.originalTarget.defaultView.addEventListener("unload", function(e){MyExtension.onUnload(e);}, false);
},
onUnload: function(e) {
if((/http://xxxx.xyz/v/[0-9a-z]/).test(e.originalTarget.location.href)) {
//remove listeners and nullify references to dom objects
}
};
MyExtension.XXX = {
handler : function(e) {
//get dom element with gBrowser.contentDocument.getElementsByClassName("class");
//bind DOMSubtreeModified listener, attach a function to handle the event
}
};
window.addEventListener("load", function(e) {
window.removeEventListener("load", load, false);
MyExtension.init();
}, false);
答案 0 :(得分:0)
以下是我正在关注的技术,使用gBrowser.selectedBrowser
作为记录here
var MyExtension = {
tab : null, //<---- added this line
init() : function() {
if("gBrowser" in window) {
gBrowser.addEventListener("DOMContentLoaded", function(e){this.onPageLoad(e);},false);
},
onPageLoad: function(e) {
var doc = e.originalTarget;
if((/http://xxxx.xyz/v/[0-9a-z]/).test(doc.location.href)) {
this.tab = gBrowser.selectedBrowser; //<--- GET REFERENCE TO CURRENT TAB
MyExtension.XXX.handler(e);
}
e.originalTarget.defaultView.addEventListener("unload", function(e){MyExtension.onUnload(e);}, false);
},
onUnload: function(e) {
if((/http://xxxx.xyz/v/[0-9a-z]/).test(e.originalTarget.location.href)) {
//remove listeners and nullify references to dom objects
}
};
MyExtension.XXX = {
handler : function(e) {
//get dom element
MyExtension.tab.contentDocument.getElementsByClassName("class"); //<--- GET CONTENT DOCUMENT INSIDE DESIRED TAB
//bind DOMSubtreeModified listener, attach a function to handle the event
}
};