Progress listeners: Difference between revisions

From MozillaZine Knowledge Base
Jump to navigationJump to search
(move from Dev_:_Extensions_:_Example_Code)
 
(fix some typos, code and markup; add a link to On Tab Load)
Line 1: Line 1:
==Progress Listeners==
==Progress Listeners==
Progress Listeners alllow extensions to be notified of events assosiated with documents loading in the browser and with tab switching events. Progress Listeners implement the [http://www.xulplanet.com/references/xpcomref/ifaces/nsIWebProgressListener.html nsIWebProgressListener] interface.
Progress Listeners allow extensions to be notified of events assosiated with documents loading in the browser and with tab switching events. Progress Listeners implement the [http://www.xulplanet.com/references/xpcomref/ifaces/nsIWebProgressListener.html nsIWebProgressListener] interface.
 
Note that if you just want to execute your code each time a page loads, you can use an [[Dev : Extensions : Example Code : On Tab Load|an easier method]].
 
==Example==
<ol><li>Create an object which implements nsIWebProgressListener:
<ol><li>Create an object which implements nsIWebProgressListener:
const STATE_START = Components.interfaces.nsIWebProgressListener.STATE_START;
<pre>const STATE_START = Components.interfaces.nsIWebProgressListener.STATE_START;
const STATE_STOP = Components.interfaces.nsIWebProgressListener.STATE_STOP;  
const STATE_STOP = Components.interfaces.nsIWebProgressListener.STATE_STOP;
var myListener =
var myListener =
{
{
  QueryInterface : function(aIID)
  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)
   {
   {
     if (aIID.equals(Components.interfaces.nsIWebProgressListener) ||
     // This fires when the load event is initiated
        aIID.equals(Components.interfaces.nsISupportsWeakReference) ||
   }
        aIID.equals(Components.interfaces.nsISupports))
   if(aFlag & STATE_STOP)
      return this;
    throw Components.results.NS_NOINTERFACE;
   },
   onStateChange:function(aProgress,aRequest,aFlag,aStatus)
   {
   {
     if(aFlag & STATE_START)
     // This fires when the load finishes
    {
  }
      //This fires when the load event is initiated
  return 0;
    }
  },
    if(aFlag & STATE_STOP)
 
    {
  onLocationChange: function(aProgress, aRequest, aURI)
      //This fires when the load finishes
  {
    }
  // This fires when the location bar changes i.e load event is confirmed
    return 0;
  // or when the user switches tabs
  },
  return 0;
  onLocationChange:function(aProgress webProgress , aRequest, aURI)
  },
  {
 
      //This fires when the location bar changes i.e load event is confirmed or when the user switches tabs
  // For definitions of the remaining functions see XulPlanet.com
      //return 0;
  onProgressChange: function() {return 0;},
  },
  onStatusChange: function() {return 0;},
  //For defnitiions of the remaining functions see XulPlanet
  onSecurityChange: function() {return 0;},
  onProgressChange:function(a,b,c,d,e,f){},
  onLinkIconAvailable: function() {return 0;}
  onStatusChange:function(a,b,c,d){},
}</pre>
  onSecurityChange:function(a,b,c){},
</li>
  onLinkIconAvailable:function(a){}
 
}</li>
<li>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:
<li>Attach the progressListener to a <browser> object, e.g. for firefox:
<pre>const NOTIFY_STATE_DOCUMENT = Components.interfaces.nsIWebProgress.NOTIFY_STATE_DOCUMENT;
const NOTIFY_STATE_DOCUMENT = Components.interfaces.nsIWebProgress.NOTIFY_STATE_DOCUMENT;  
window.getBrowser().addProgressListener(myListener, NOTIFY_STATE_DOCUMENT);</pre>
window.getBrowser().addProgressListener(myListener , NOTIFY_STATE_DOCUMENT);
 
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.</li>
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.
</li>
</ol>
</ol>
Remember to change ''myListener'' to an [[Dev : Javascript coding guidelines|unique identifier]].

Revision as of 10:47, 31 October 2004

Progress Listeners

Progress Listeners allow extensions to be notified of events assosiated 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

  1. 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;}
    }
  2. 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:
    const NOTIFY_STATE_DOCUMENT = Components.interfaces.nsIWebProgress.NOTIFY_STATE_DOCUMENT;
    window.getBrowser().addProgressListener(myListener, NOTIFY_STATE_DOCUMENT);

    The second argument is a status filter which determines the type of events that will be recieved.

Remember to change myListener to an unique identifier.