Firefox addon - monitoring network

后端 未结 1 1452
南旧
南旧 2020-12-06 13:44

If I add an nsIHttpChannel observer, is there any way I can know what initiated the HTTP request(script, iframe, image etc..)

In chrome when monitoring the network f

相关标签:
1条回答
  • 2020-12-06 14:14

    Don't accept this as solution yet. I hope some other people can come and help build this solution.

    I know this is for sure correct:

    • TEST FOR: XHR - identify XHR (ajax) response while listening to http response in firefox addon
    • Get load context of request (like which tab, which html window, which xul window) - Firefox add-on pageMod, catch ajax done in contentScriptFile (this is marked as optional though in the code below it requires a helper function: https://gist.github.com/Noitidart/644494bdc26f996739ef )

    This I think is correct, by this i mean it works in my test cases but I'm not sure if its the recommended way:

    • TEST FOR: Frame or full page load - Can we differentiate between frame and non-frame loads with Ci in firefox addon

    This I don't know how to do so I need help from community on this:

    • TEST FOR image - Comment on "identify XHR (ajax) response while listening to http response in firefox addon"
      • Image detection can be done through the MIME type. Check channel.contentType


    Solution in progress:

    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
    }
    

    Of course to start observing:

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

    and to stop:

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

    To start observing

    To start start obseving all requests do this (for example on startup of your addon)

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

    To stop observing

    Its important to stop observring (make sure to run this at least on shutdown of addon, you dont want to leave the observer registered for memory reasons)

    for (var o in observers) {
        observers[o].unreg();
    }
    
    0 讨论(0)
提交回复
热议问题