4

I'm developing a firefox extension which requires me to intercept page loads by filtering out some HTTPRequests. I did that using the instructions given here. Please note that my question draws from the content of this link.

I used the method given under the section of HTTPObservers. And it worked, I am indeed able to extract the respective urls of the Requests being sent out.

However, another thing which I really require is to get the target DOM Window where the contents pertaining to the HTTPRequest were about to be loaded. Is it possible using HTTPObservers?

In the link above, another way has been described using WebProgressListeners.

I tried that out as well. The onLocationChange() method only returns location changes in the url bar. Is it somehow possible to get the HTTPRequest urls using any of these progress listeners? Because if so, then if I understand correctly, aWebProgress.DOMWindow would give me the window I require.

Note: I am using gwt for the extension and the JSNI for the above mentioned part.

Saurabh Agarwal
  • 507
  • 2
  • 5
  • 16

1 Answers1

7

You can usually do that by using nsILoadContext interface (sadly barely documented) attached to the request or its load group. Here is how you would do that:

function getWindowForRequest(request)
{
  if (request instanceof Components.interfaces.nsIRequest)
  {
    try
    {
      if (request.notificationCallbacks)
      {
        return request.notificationCallbacks
                      .getInterface(Components.interfaces.nsILoadContext)
                      .associatedWindow;
      }
    } catch(e) {}

    try
    {
      if (request.loadGroup && request.loadGroup.notificationCallbacks)
      {
        return request.loadGroup.notificationCallbacks
                      .getInterface(Components.interfaces.nsILoadContext)
                      .associatedWindow;
      }
    } catch(e) {}
  }

  return null;
}

Note that this function is expected to return null occasionally - not every HTTP request is associated with a window.

Wladimir Palant
  • 56,865
  • 12
  • 98
  • 126
  • Thanks a lot for the reply! I had a small question. Is this method supposed to be used along the HTTPObserver method mentioned in the question? If so, for the parameter 'request' in the function, would it suffice to pass the parameter aSubject (in the context of [HTTPObsevers](https://developer.mozilla.org/en/XUL_School/Intercepting_Page_Loads#HTTP_Observers) )? – Saurabh Agarwal May 23 '12 at 12:44
  • 1
    @SaurabhAgarwal: Yes on both questions - it is supposed to get an `nsIRequest`/`nsIChannel` which corresponds to the `aSubject` parameter in an `http-on-modify-request` observer. – Wladimir Palant May 23 '12 at 12:54
  • is it possible to get docShell from loadContext using this method? i want to get the xul elements of the tab it loaded in. i can get all the html windows and dom with loadContext fine https://github.com/Noitidart/loadcontext-and-http-modify-observer – Noitidart Feb 06 '14 at 17:44
  • 1
    @Noit: You can always get the docShell for a window. However, if I understand correctly and your goal is finding the tab element for a window - see [here](http://stackoverflow.com/a/6489272/785541). Change `QueryInterface(Ci.nsIDocShellTreeItem)` in the example code there to `QueryInterface(Ci.nsIDocShell)` if you really need a docShell. – Wladimir Palant Feb 07 '14 at 07:21
  • 1
    @WladimirPalant a sincere thank you! I will try it out and fix that botched example I posted on MDN. I really enjoy learning and updating MDN and helping others while making some cool addons. I'm a FF loyalist, anti google chrome haha – Noitidart Feb 07 '14 at 11:24