[Project_owners] How to obtain the scrollbar positions of a webpage?

Jaap Haitsma jaap at haitsma.org
Mon Aug 9 00:56:46 EDT 2004


Neil wrote:
> Jaap Haitsma wrote:
> 
>> So my problem is how to do a loadURI and know that that specific 
>> loadURI has finished.
> 
> 
> Aha, well, in that case, assuming your tab doesn't get deleted in the 
> mean time, I think the best you can do is
> getBrowser().webProgress.addProgressListener(myProgressListener, 
> notifyFlags);
> This returns notifications for the tab that you issue loadURI on. You 
> can choose the notifications by combining the flags (they are properties 
> on nsIWebProgress). However your progress listener does need a 
> QueryInterface method where it should claim to support nsISupports and 
> nsISupportsWeakReference as well as nsIWebProgressListener of course. 
> Don't forget to remove the progress listener when you have finished.

Thanks Neil!!!

I've got it working. I now have the code below. Posting it in case
somebody ever googles for it.

Cheers,

Jaap

BTW The onProgressChange always gives back -1 for aMaxSelfProgress
and aMaxTotalProgress in my case. According to the documentation on 
xulplanet they are -1 if progress is not supported. Seems a bit silly to
me that in this case it is not supported.


<snip>
tab.reloadEveryProgessListener = ReloadEveryProgressListener(tab);
</snip>

<snip>
tab.curScrollX = tab.contentWindow.scrollX;
tab.curScrollY = tab.contentWindow.scrollY;

var notifyFlags = Components.interfaces.nsIWebProgress.NOTIFY_ALL;
 
tab.webProgress.addProgressListener(tab.reloadEveryProgessListener, 
notifyFlags);

tab.webNavigation.loadURI(tab.reloadEveryURL, loadFlags, null, 
entry.postData, null);

</snip>

<snip>
function ReloadEveryProgressListener(aTab){
     return ({

     tab : aTab,
     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;
	},
     onProgressChange : function (aWebProgress, aRequest, 
aCurSelfProgress, aMaxSelfProgress,
                                  aCurTotalProgress, aMaxTotalProgress){
     },

     onStateChange : function(aWebProgress, aRequest, aStateFlags, 
aStatus){
         if (aStateFlags & 
Components.interfaces.nsIWebProgressListener.STATE_IS_WINDOW){
             //log("ReloadEveryProgressListener(): page loaded " + tab.id);
 
tab.webProgress.removeProgressListener(tab.reloadEveryProgessListener);
             tab.contentWindow.scrollTo(tab.curScrollX, tab.curScrollY);
             tab.reloadEveryTimerID=setTimeout("reloadPage(\"" + tab.id 
+ "\");", tab.reloadEveryReloadTime*1000);
         }

     },

     onLocationChange : function(aWebProgress, aRequest, aLocation){
     },

     onStatusChange : function(aWebProgress, aRequest, aStatus, aMessage){
     },

     onSecurityChange : function(aWebProgress, aRequest, aState){
     }
     });		
}


</snip>


More information about the Project_owners mailing list