Since fresh new Firebug 1.4a13 - the Net panel introduces, among other things, several new events that allow to easily collect all network requests and also related info gathered and computed by Firebug.
This functionality should be useful also in cases where Firebug extensions want to store network activity info into a local database or send it back to the server for further analysis (I am thinking about performance statistics here).
So, if you are interested to see a simple example that shows how a listener should be implemented and registered within the Net panel read more.
How to register a listener
A listener in Firebug's internal framework is registered by addListener and unregistered by removeListener methods. These methods are implemented by every object that fires an event(s). In this particular case, we are registering our listener into Firebug.NetMonitor object (module), but there is bunch of other objects.
Notice that we usually want to register the Net panel listener when Firebug is initialized (Firefox window opened) and unregister when Firebug is destroyed (Firefox window closed). See the following source code snippet.
// Register NetMonitor listener
this.netListener = new NetListener();
// Unregister NetMonitor listener
Implementation of the NetListner object is below.
Net panel events
In order to collect all network activity info we can utilize following events.
- onRequest(context, file) - A network request has been sent. This event is fired just after http-on-modify-request event.
- onExamineResponse(context, request) - A network response has been receivied, but not yet processed by Firebug. This event is fired just after http-on-examine-response event. This is the right place where response headers should be modified before they are read by Firebug.
- onResponse(context, file) - A network response has been received. Notice that response body doesn't have to be downloaded at this moment yet.
- onResponseBody(context, file) - Entire response body has been downloaded.
The purpose of our example listener is to store URL and elapse time of every request into a file. First of all take a look at the implementation.
var dirService = Cc["@mozilla.org/file/directory_service;1"]
// Get unique file within user profile directory.
var file = dirService.get("ProfD", Ci.nsIFile);
// Initialize output stream.
this.outputStream = Cc["@mozilla.org/network/file-output-stream;1"]
// write, create, truncate
this.outputStream.init(file, 0x02 | 0x08 | 0x20, 0666, 0);
onResponse: function(context, file)
var text = file.href + " (" + formatTime
(file.endTime - file.startTime) + ")\n";
A few notes.
- As you can see, the object doesn't have to implement methods for all events. Firebug frameworks always tests if the specific method exists and don't fire the event if it doesn't.
- Since we want to store all info into a file, we have to open a file output stream. This is done within NetListener's constructor. Every Firefox window session creates its own file. All these files are created within user profile directory under netlistener sub-directory.
- The outputStream should be closed within Firebug.NetListenerModule.shutdown method.
Complete example with FBTrace support included is here.