Firefox插件 - 监控网络

时间:2014-12-15 12:03:20

标签: firefox-addon

如果我添加一个nsIHttpChannel观察者,有什么方法可以知道发起HTTP请求的内容(脚本,iframe,图像等等)。

在Chrome中,当从后台页面监控网络时,您会看到请求类型,告诉您它是来自iframe,脚本等......

1 个答案:

答案 0 :(得分:4)

不要接受此作为解决方案。我希望其他人能够帮助我们建立这个解决方案。

我知道肯定是正确:

这个我认为是正确的,我的意思是它适用于我的测试用例,但我不确定它是否是推荐的方式:

我不知道该怎么做,我需要社区的帮助:


正在进行的解决方案:

var myobserve = function(aSubject, aTopic, aData) {
    var httpChannel = aSubject.QueryInterface(Ci.nsIHttpChannel);


    //start - test if xhr
    var isXHR;
    try {
        var callbacks = httpChannel.notificationCallbacks;
        var xhr = callbacks ? callbacks.getInterface(Ci.nsIXMLHttpRequest) : null;
        isXHR = !!xhr;
    } catch (e) {
        isXHR = false;
    }
    //end - test if xhr

    //start - test if frame OR full page load
    var isFrameLoad;
    var isFullPageLoad;
    if (httpChannel.loadFlags & Ci.nsIHttpChannel.LOAD_INITIAL_DOCUMENT_URI) {
        isFullPageLoad = true;
        isFrameLoad = false;
    } else if (httpChannel.loadFlags & Ci.nsIHttpChannel.LOAD_DOCUMENT_URI) {
        isFrameLoad = true;
        isFullPageLoad = false;
    }
    //end - test if frame OR full page load

    //start - test if image
    var isImg;
    var isCss;
    var isJs;
    var isAudio;
    //can keep going here
    var mimeType = httpChannel.contentType;
    if (/^image/i.test(mimeType)) {
        isImg = true;
    }
    if (/^audio/i.test(mimeType)) {
        isAudio = true;
    }
    if (/\/css$/i.test(mimeType)) {
        isCss = true;
    }
    if (/\/js$/i.test(mimeType)) {
        isJs = true;
    }
    //end - test if image

    //start - OPTIONAL use loadContext to get a bunch of good stuff
    //must paste the function from here: https://gist.github.com/Noitidart/644494bdc26f996739ef somewhere in your code
    var goodies = loadContextAndGoodies(aSubject, true);
    /*
    //goodies is an object that holds the following information:
    var goodies = {
        loadContext: loadContext,
        DOMWindow: DOMWindow,
        gBrowser: gBrowser,
        contentWindow: contentWindow,
        browser: browser,
        tab: tab
    };
    */
    // test if resource (such as image, or whatever) is being loaded is going into a frame [can also be used as altnerative way to test if frame load or full page]
    var itemDestinationIsFrame;
    var itemDestinationIsTopWin;
    if (goodies.contentWindow) {
        if (goodies.contentWindow.frameElement) {
            itemDestinationIsFrame = true;
            itemDestinationIsTopWin = false;
        } else {
            itemDestinationIsFrame = false;
            itemDestinationIsTopWin = true;
        }
    }
    //end - OPTIONAL use loadContext to get a bunch of good stuff
}

当然要开始观察:

Services.obs.addObserver(myobserve, 'http-on-modify-request', false);

并停止:

Services.obs.removeObserver(myobserve, 'http-on-modify-request', false);

开始观察

要开始观察所有请求,请执行此操作(例如,在启动插件时)

for (var o in observers) {
    observers[o].reg();
}

停止观察

重要的是要停止观察(确保至少在关闭插件时运行它,你不想让观察者因记忆原因而注册)

for (var o in observers) {
    observers[o].unreg();
}