Progress listeners: Difference between revisions

From MozillaZine Knowledge Base
Jump to navigationJump to search
Line 61: Line 61:
Also remember that if you're adding listeners inside a function, make sure to have a reference to your listener object that stays in memory (this is due to the required nsISupportsWeakReference interface).
Also remember that if you're adding listeners inside a function, make sure to have a reference to your listener object that stays in memory (this is due to the required nsISupportsWeakReference interface).


In order to only receive a change location event whenever the address bar changes (the simple example referenced above fires onload events only - not fired when the user uses the back button and loads a page from cache) use the follow code...
== Example: OnChangeLocation ==
In order to only receive a change location event whenever the address bar changes (the "easier method" linked above fires onload events only - not fired when the user uses the back button and loads a page from cache) use the follow code...
<pre>
<pre>
    var oldURL = null;
var oldURL = null;
        var urlBarListener =
var urlBarListener =
    {
{
        QueryInterface: function(aIID)
  QueryInterface: function(aIID)
        {
  {
    if (aIID.equals(Components.interfaces.nsIWebProgressListener) ||
  if (aIID.equals(Components.interfaces.nsIWebProgressListener) ||
      aIID.equals(Components.interfaces.nsISupportsWeakReference) ||
      aIID.equals(Components.interfaces.nsISupportsWeakReference) ||
      aIID.equals(Components.interfaces.nsISupports))
      aIID.equals(Components.interfaces.nsISupports))
    return this;
    return this;
    throw Components.results.NS_NOINTERFACE;
  throw Components.results.NS_NOINTERFACE;
        },
  },
        onLocationChange: function( aProgress, aRequest, aURI )
        {
    processNewURL(aURI);
    return 0;
        }
    }
    document.addEventListener( "load", initializeClient, true );


    function initializeClient()
  onStateChange: function(aProgress, aRequest, aFlag, aStatus)
    {
  {
        // Listen for webpage loads
  if(aFlag & STATE_START)
        document.getElementById( "content" ).addProgressListener( urlBarListener,
  {
        Components.interfaces.nsIWebProgress.NOTIFY_STATE_DOCUMENT);
    // This fires when the load event is initiated
    }
  }
  if(aFlag & STATE_STOP)
  {
    // This fires when the load finishes
  }
  return 0;
  },


    function processNewURL(aURI) {
  onLocationChange: function( aProgress, aRequest, aURI )
  {
    processNewURL(aURI);
    return 0;
  },


        if (aURI.spec == oldURL){
  // For definitions of the remaining functions see XulPlanet.com
    return;
  onProgressChange: function() {return 0;},
        }
  onStatusChange: function() {return 0;},
  onSecurityChange: function() {return 0;},
  onLinkIconAvailable: function() {return 0;}
}


        // now we know the url is new...
document.addEventListener( "load", initializeClient, true );
   
 
        alert(aURI.spec);
function initializeClient()
        oldURL = new String(aURI.spec);
{
        return;
  // Listen for webpage loads
    }      
  gBrowser.addProgressListener( urlBarListener,
      Components.interfaces.nsIWebProgress.NOTIFY_STATE_DOCUMENT);
}
 
function processNewURL(aURI)
{
  if (aURI.spec == oldURL)
    return;
 
  // now we know the url is new...
  alert(aURI.spec);
  oldURL = aURI.spec;
  return;
}      
</pre>
</pre>




[[Category:Example code]] [[Category:XPCOM example code]] [[Category:JavaScript example code]]
[[Category:Example code]] [[Category:XPCOM example code]] [[Category:JavaScript example code]]

Revision as of 01:00, 15 January 2006

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

  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:
    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.

Also remember that if you're adding listeners inside a function, make sure to have a reference to your listener object that stays in memory (this is due to the required nsISupportsWeakReference interface).

Example: OnChangeLocation

In order to only receive a change location event whenever the address bar changes (the "easier method" linked above fires onload events only - not fired when the user uses the back button and loads a page from cache) use the follow code...

var oldURL = null;
var urlBarListener =
{
  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 )
  {
    processNewURL(aURI);
    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;}
}

document.addEventListener( "load", initializeClient, true );

function initializeClient()
{
  // Listen for webpage loads
  gBrowser.addProgressListener( urlBarListener,
      Components.interfaces.nsIWebProgress.NOTIFY_STATE_DOCUMENT);
}

function processNewURL(aURI)
{
  if (aURI.spec == oldURL)
    return;
  
  // now we know the url is new...
  alert(aURI.spec);
  oldURL = aURI.spec;
  return;
}