Firefox中的Window Observer

时间:2013-07-25 18:29:45

标签: javascript firefox firefox-addon add-on

我正在构建一个我希望捕获窗口事件的扩展。我有这个代码: .....................................

  function MyWindowObserver() {
    this.observe=function(aSubject, aTopic, aData) {
        if(aSubject.innerWidth!=1536)
            aSubject.close();
    }
}

function myObserver(){}

myObserver.prototype = {
    observe: function(subject, topic, data) {

},
    register: function() {
        var observerService = Components.classes["@mozilla.org/observer-service;1"]
            .getService(Components.interfaces.nsIObserverService);
        observerService.addObserver(this, "http-on-modify-request", false);

    },
    unregister: function() {
        var observerService = Components.classes["@mozilla.org/observer-service;1"]
            .getService(Components.interfaces.nsIObserverService);
        observerService.removeObserver(this, "http-on-modify-request");
    }
}

function installButton()
{
    var id = "button_cs";
    var toolbarId = "nav-bar";

    var toolbar = document.getElementById(toolbarId);

    //add the button at the end of the navigation toolbar
    toolbar.insertItem(id, toolbar.lastChild);
    toolbar.setAttribute("currentset", toolbar.currentSet);
    document.persist(toolbar.id, "currentset");

    //if the navigation toolbar is hidden,
    //show it, so the user can see your button
    toolbar.collapsed = false;
}

function firstRun(extensions) {
    var extension = extensions.get("bak@asda.com");

    if (extension.firstRun) {
        installButton();
    }
}

var myExtension={

    Observers:null,
    prefs:null,
    clock:null,
    prevwidth:null,
    prevon:null,
    BrosNumber:null,

    init:function(){

        if (Application.extensions)
            firstRun(Application.extensions);
        else
            Application.getExtensions(firstRun);

        this.prefs = Components.classes["@mozilla.org/preferences-service;1"]
            .getService(Components.interfaces.nsIPrefService).getBranch("accessibility.");

        this.Observers = new myObserver();

        this.Observers.register();
    }
};

myExtension.init();

window.addEventListener('load', function(win) {
    var observerService = Components.classes["@mozilla.org/observer-service;1"]
        .getService(Components.interfaces.nsIObserverService);
    observerService.addObserver(new MyWindowObserver(), "toplevel-window-ready", false);
},false);

我做错了什么? aSubject不是最后一个打开的窗口?

有什么想法吗?

2 个答案:

答案 0 :(得分:0)

这会关注http-on-modify-requesttoplevel-window-ready

观察者不是基于每个窗口的。你只需在全球注册它们。

var{interfaces: Ci, utils: Cu} = Components;
Cu.import('resource://gre/modules/Services.jsm');

var observers = {
    httpModReq: {
        observe: function (aSubject, aTopic, aData) {
            console.log('incoming observe httpModReq - aSubject: "' + aSubject + '" | aTopic: "' + aTopic + '" | aData: "' + aData + '"');
            var httpChannel, requestURL;
            httpChannel = subject.QueryInterface(Ci.nsIHttpChannel);
            requestURL = httpChannel.URI.spec;

            if (requestURL.indexOf('google.com') > -1) {
                //they are loading google
                //httpChannel.setRequestHeader('MyCustomRequestHeader', 'hiiii', false);
                /*
                try {
                   var Host = httpChannel.getRequestHeader('Host');
                } catch (ex) {
                   //if doesnt have Host header it throws exception "NS_ERROR_NOT_AVAILABLE"
                    var Host = 'NULL';
                }
                console.log('REQUEST Header "Host" = ' + Host);
                */
            }
        },
        register: function () {
            Services.obs.addObserver(observer.httpModReq, 'http-on-modify-request', false);
        },
        unregister: function () {
            Services.obs.removeObserver(observer.httpModReq, 'http-on-modify-request');
        }
    },
    topWinRdy: {
        observe: function (aSubject, aTopic, aData) {
            console.log('incoming observe topWinRdy - aSubject: "' + aSubject + '" | aTopic: "' + aTopic + '" | aData: "' + aData + '"');
        },
        register: function () {
            Services.obs.addObserver(observer.topWinRdy, 'toplevel-window-ready', false);
        },
        unregister: function () {
            Services.obs.removeObserver(observer.topWinRdy, 'toplevel-window-ready');
        }
    }
};


//run this block to start observing
for (var o in observers) {
    observers[o].register();
}

/*//run this block when you want to remove the observers
for (var o in observers) {
    observers[o].unregister();
}
*/

您可以将其粘贴到暂存器中,并在环境设置为浏览器的情况下运行它。它会正常工作。

答案 1 :(得分:0)

只是修复@Notidart 代码

const { interfaces: Ci, utils: Cu } = Components;
Cu.import('resource://gre/modules/Services.jsm');
var observers = {
    httpModReq: {
        observe: function (aSubject, aTopic, aData) {
            console.log('incoming observe httpModReq - aSubject: "' + aSubject + '" | aTopic: "' + aTopic + '" | aData: "' + aData + '"');
            var httpChannel, requestURL;
            httpChannel = aSubject.QueryInterface(Ci.nsIHttpChannel);
            requestURL = httpChannel.URI.spec;

            console.log('requestURL', requestURL)
        },
        register: function () {
            Services.obs.addObserver(this, 'http-on-modify-request', false);
        },
        unregister: function () {
            Services.obs.removeObserver(this, 'http-on-modify-request');
        }
    },
    topWinRdy: {
        observe: function (aSubject, aTopic, aData) {
            console.log('incoming observe topWinRdy - aSubject: "' + aSubject + '" | aTopic: "' + aTopic + '" | aData: "' + aData + '"');
        },
        register: function () {
            Services.obs.addObserver(this, 'toplevel-window-ready', false);
        },
        unregister: function () {
            Services.obs.removeObserver(this, 'toplevel-window-ready');
        }
    }
};


//run this block to start observing
for (var o in observers) {
    observers[o].register();
}

示例输出

incoming observe httpModReq - aSubject: "[xpconnect wrapped nsIRequest]" | aTopic: "http-on-modify-request" | aData: "null" userChrome.js:14:21
requestURL https://www.youtube.com/ userChrome.js:19:21
incoming observe httpModReq - aSubject: "[xpconnect wrapped (nsIRequest, nsIHttpChannel)]" | aTopic: "http-on-modify-request" | aData: "null" userChrome.js:14:21
requestURL https://www.youtube.com/ userChrome.js:19:21
incoming observe httpModReq - aSubject: "[xpconnect wrapped nsISupports]" | aTopic: "http-on-modify-request" | aData: "null" userChrome.js:14:21
requestURL http://ocsp.pki.goog/gts1c3 userChrome.js:19:21
incoming observe httpModReq - aSubject: "[xpconnect wrapped nsIHttpChannel]" | aTopic: "http-on-modify-request" | aData: "null" userChrome.js:14:21
requestURL http://ocsp.pki.goog/gts1c3 userChrome.js:19:21
incoming observe httpModReq - aSubject: "[xpconnect wrapped nsISupports]" | aTopic: "http-on-modify-request" | aData: "null" userChrome.js:14:21
requestURL http://ocsp.pki.goog/gts1c3 userChrome.js:19:21
incoming observe httpModReq - aSubject: "[xpconnect wrapped nsIHttpChannel]" | aTopic: "http-on-modify-request" | aData: "null" userChrome.js:14:21
requestURL http://ocsp.pki.goog/gts1c3 userChrome.js:19:21
incoming observe httpModReq - aSubject: "[xpconnect wrapped nsIRequest]" | aTopic: "http-on-modify-request" | aData: "null"