Search code examples
firefoxfirefox-addonfirefox-addon-sdk

Determine whether HTTP request / response is main frame


I am using an observer on "http-on-modify-request" to analyze HTTP requests (and responses with the corresponding other observers).

Is it possible to determine whether the HTTP request / response is the main frame loading (the actual page DOM)? As opposed to another resource (image, css, sub_frame, etc.).


Solution

  • The docs have most of the answer you're looking for here and I've modified it below for use with the addon-sdk.

    You can watch for an IFRAME by comparing the location with the top.document location.

    I don't think there's an easy way to detect loading of images, etc so you'll probably want to just watch for the first hit that's not an IFRAME and regard everything else as css/image/script content loading.

    var chrome = require("chrome");
    
    var httpmods = {
      observe : function(aSubject, aTopic, aData) {
        console.log("observer", aSubject, aTopic, aData);
          aSubject.QueryInterface(chrome.Ci.nsIHttpChannel);
          var url = aSubject.URI.spec;
    
          var dom = this.getBrowserFromChannel(aSubject);
          if (dom) { 
            if (dom.top.document && dom.location === dom.top.document.location) {
              console.log("ISN'T IFRAME");
            } else {
              console.log("IS IFRAME");
            }
          }
      },
      getBrowserFromChannel: function (aChannel) {
        try {
          var notificationCallbacks =
            aChannel.notificationCallbacks ? aChannel.notificationCallbacks : aChannel.loadGroup.notificationCallbacks;
    
          if (!notificationCallbacks)
            return null;
    
          var domWin = notificationCallbacks.getInterface(chrome.Ci.nsIDOMWindow);
          return domWin;
        }
        catch (e) {
          dump(e + "\n");
          return null;
        }
      }
    }
    
    require("observer-service").add("http-on-modify-request", httpmods.observe, httpmods);