0

我正在开发一个 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);
4

1 回答 1

0

这是我正在遵循的技术,gBrowser.selectedBrowser按照此处记录的方式使用

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
    }
};
于 2013-06-24T18:54:46.860 回答