Dmitriy Kozyatinskiy
Dmitriy Kozyatinskiy

Reputation: 170

Firefox add-on get the DOM window which made an HTTP request

I'm capturing the HTTP requests in a Firefox Add-on SDK extension. I need to get the DOM window associated with the request. However, I'm getting an NS_NOINTERFACE error.

Here is my code:

  var httpRequestObserver = {
    observe: function (subject, topic, data) {
      var httpRequest = subject.QueryInterface(Ci.nsIHttpChannel);
      var requestUrl = subject.URI.host;
      var domWin;
      var assWindow;
      console.log('URL: ', requestUrl);
      try {
        domWin = httpRequest.notificationCallbacks.getInterface(Ci.nsIDOMWindow);
        assWindow = httpChannel.notificationCallbacks.getInterface(Ci.nsILoadContext)
                               .associatedWindow;
        console.log(domWin);
      } catch (e) {
        console.log(e);
      }
      // console.log('TAB: ', tabsLib.getTabForWindow(domWin.top));

      var hostName = wn.domWindow.getBrowser().selectedBrowser.contentWindow.location.host;
      console.log('HOST: ', hostName);
    },

    get observerService() {
      return Cc['@mozilla.org/observer-service;1'].getService(Ci.nsIObserverService);
    },

    register: function () {
      this.observerService.addObserver(this, 'http-on-modify-request', false);
    },

    unregister: function () {
      this.observerService.removeObserver(this, 'http-on-modify-request');
    }
  };

  httpRequestObserver.register();

I've tried both nsIDOMWindow and nsILoadContext, but NS_NOINTERFACE error always appears on an attempt to get the window object.

Upvotes: 0

Views: 126

Answers (2)

the8472
the8472

Reputation: 43042

Since you already found how to get the <browser> from the request you can do the following to get back to SDK APIs:

let browser = ....topFrameElement
let xulTab = browser.ownerDocument.defaultView.gBrowser.getTabForBrowser(browser)
let sdkTab = modelFor(xulTab)

modelFor() is documented in the tabs module.

Upvotes: 0

Dmitriy Kozyatinskiy
Dmitriy Kozyatinskiy

Reputation: 170

I have finally managed to get the data I need via

httpRequest.notificationCallbacks.getInterface(Ci.nsILoadContext).topFrameElement

For example, to get url of the document which started the request, I used

httpRequest.notificationCallbacks.getInterface(Ci.nsILoadContext).topFrameElement._documentURI.href

Upvotes: 1

Related Questions