0

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.

  • *Please*, [edit] the question to put console message information **in the question as text, contained in a `code block`**. If the error generates something that text can not convey, then an image of the error may be useful. However, in most cases, having an image of console messages is *significantly* less helpful than having them as text. When in text format, they can be copied, pasted, and searched. As text, they are **much** more useful both for answering the question and for people trying to find the answer to their similar problem in the future. – Makyen Jul 25 '16 at 18:12
  • I have down-voted and voted to close due to not having the error information contained in the question as text. If you put the error messages into the question as text, I will remove my down-vote and retract my close vote. In that case, I may up-vote. Please leave a comment with `@Makyen` so that I am notified of the change. – Makyen Jul 25 '16 at 18:17
  • @Makyen thanks for help, updated. – Dmitriy Kozyatinskiy Jul 25 '16 at 20:38
  • Perhaps I was not clear. I did not mean just remove the image of the console messages. Please put the console messages in the question as text. Console messages as text >> image of console error messages >>>>>> nothing (or little/incomplete information). If the image of the console errors has information you are not putting in the question as text, please leave the image of the console in the question. – Makyen Jul 25 '16 at 20:59
  • @Makyen that console message is not important. It's a common one for firefox sdk and its name tells everything. – Dmitriy Kozyatinskiy Jul 27 '16 at 06:35

2 Answers2

1

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
  • 1
    Awesome job! Here is some documentation that might help out - https://developer.mozilla.org/en-US/Add-ons/Code_snippets/Tabbed_browser#Getting_the_browser_that_fires_the_http-on-modify-request_notification_(example_code_updated_for_loadContext) – Noitidart Jul 25 '16 at 17:09
  • 1
    Do note though that this is not very e10s friendly. As you are working in process manager. It is much preferable to use nsIWebProgressListener from the framescript of each tab - https://github.com/Noitidart/Full-Stop/blob/master/resources/scripts/framescript.js – Noitidart Jul 25 '16 at 17:11
  • the URI can be obtained without using frame scripts. but touching the dom window will need them under e10s. – the8472 Jul 25 '16 at 19:56
0

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.

the8472
  • 40,999
  • 5
  • 70
  • 122