Progress listeners: Difference between revisions
mNo edit summary |
GypsyDoctor (talk | contribs) |
||
Line 53: | Line 53: | ||
Components.interfaces.nsIWebProgress.NOTIFY_STATE_DOCUMENT);</pre> | Components.interfaces.nsIWebProgress.NOTIFY_STATE_DOCUMENT);</pre> | ||
The second argument is a [http://www.xulplanet.com/references/xpcomref/comps/c_appshellcomponentbrowserstatusfilter1.html status filter] which determines the type of events that will be recieved. | The second argument is a [http://www.xulplanet.com/references/xpcomref/comps/c_appshellcomponentbrowserstatusfilter1.html status filter] which determines the type of events that will be recieved. All six progress handler functions must be defined, even if filters are used. | ||
</li> | </li> | ||
</ol> | </ol> |
Revision as of 13:50, 17 December 2005
This page is part of the extension development documentation project.
Ask your questions in MozillaZine Forums. Also try browsing example code.
Note: development documentation is in process of being moved to Mozilla Development Center (MDC).
Progress Listeners
Progress listeners allow extensions to be notified of events associated with documents loading in the browser and with tab switching events. Progress listeners implement the nsIWebProgressListener
interface.
Note that if you just want to execute your code each time a page loads, you can use an an easier method.
Example
- Create an object which implements
nsIWebProgressListener
:const STATE_START = Components.interfaces.nsIWebProgressListener.STATE_START; const STATE_STOP = Components.interfaces.nsIWebProgressListener.STATE_STOP; var myListener = { QueryInterface: function(aIID) { if (aIID.equals(Components.interfaces.nsIWebProgressListener) || aIID.equals(Components.interfaces.nsISupportsWeakReference) || aIID.equals(Components.interfaces.nsISupports)) return this; throw Components.results.NS_NOINTERFACE; }, onStateChange: function(aProgress, aRequest, aFlag, aStatus) { if(aFlag & STATE_START) { // This fires when the load event is initiated } if(aFlag & STATE_STOP) { // This fires when the load finishes } return 0; }, onLocationChange: function(aProgress, aRequest, aURI) { // This fires when the location bar changes i.e load event is confirmed // or when the user switches tabs return 0; }, // For definitions of the remaining functions see XulPlanet.com onProgressChange: function() {return 0;}, onStatusChange: function() {return 0;}, onSecurityChange: function() {return 0;}, onLinkIconAvailable: function() {return 0;} }
- Attach the progress listener to a <browser> or a <tabbrowser> element, e.g. for Firefox put the following code in a
load
listener of a main window:gBrowser.addProgressListener(myListener, Components.interfaces.nsIWebProgress.NOTIFY_STATE_DOCUMENT);
The second argument is a status filter which determines the type of events that will be recieved. All six progress handler functions must be defined, even if filters are used.
Remember to change myListener
to an unique identifier.