The typical way to use this interface is as follows:
- Register for the "http-on-examine-response" notification to track all HTTP responses;
- Skip redirects (
responseStatus= 3xx on nsIHttpChannel), since otherwise you may end up with two listeners registered for a channel;
- QI the channel passed as the "
subject" to your observer to
nsITraceableChannel, and replace the default
nsIStreamListener(that passes the data to the original requester - e.g. to XMLHttpRequest or to the browser tab that made the request) with your own implementation (see "Implementing nsIStreamListener" below).
After that your
nsIStreamListener implementation will get the response data and will be able to pass the data on to the original nsIStreamListener (possibly modifying it).
See nsITraceableChannel, Intercept HTTP Traffic for a more detailed description with code samples.
See Modify URL before loading page in firefox for an overview of how to modify a request before it is made.
Replaces the channel's current listener with a new one, returning the listener previously assigned to the channel.
nsIStreamListener setNewListener( in nsIStreamListener aListener );
nsIStreamListenerto be notified of events on the HTTP channel.
The previous listener for the channel. Each listener call through to the previous listener for every call, in order to establish a call chain to allow all interested parties a chance to act on each event.
nsIStreamListener passed to
setNewListener() should implement the following methods, which are called to notify it of events that occur on the HTTP stream:
onStartRequest: An HTTP request is beginning.
onDataAvailable: Data is arriving on the HTTP channel.
onStopRequest: The HTTP request is ending.
Channels may restrict when you may replace the listener. In particular, listeners typically should not be replaced after
onStartRequest has been called.