Hi all,
Thanks very much for all the replies on this one. They all helped...
I managed to get it working about 45 mins ago :)
I used B�atrice's code snippets to get this working but I had to make
two changes as his code wouldn't work for me as it was. I've included a
cut-down version of my function again so people can see what I did -
I've commented the two places where I had to make changes to get this
working.
I've quickly tested this with Firefox v1.0.2, Netscape v7.2 and Mozilla
v1.7.6 - It worked perfectly with all the above.
Again, thanks all for the help.
Damien
==================
NS_IMETHODIMP
MyMozilla::OnStateChange( nsIWebProgress *aWebProgress,
nsIRequest *aRequest, PRUint32 aStateFlags,
nsresult aStatus )
{
try
{
if (aStateFlags & STATE_START)
{
if (aStateFlags & STATE_IS_NETWORK)
{
mTotalRequests = 0;
mFinishedRequests = 0;
}
if (aStateFlags & STATE_IS_REQUEST)
{
++mTotalRequests;
}
}
else
{
if (aStateFlags & STATE_STOP)
{
//************************
//In code snip from B�atrice this was
STATE_IS_REQUEST not STATE_IS_DOCUMENT
//as I have. Without this change I never
entered this section of code.
if (aStateFlags & STATE_IS_DOCUMENT)
{
//************************
//I found that when starting
mozilla my home page was not being captured.
//I discovered that this was
happening because at startup time I was
//getting more STATE_STOP's than
STATE_START's and so the test later in
//code to see if
mFinishedRequests == mTotalRequests was failing. This
//if statement means that I
can't have more STOPs than STARTs (which I
//think makes sense?) I'm not
sure why I get more STOPs than STARTs at
//browser startup but whatever
the reason this "if" statement solves the
//problem and doesn't seem to
cause any side effects when loading pages
//after startup.
if ( (mFinishedRequests + 1) <=
mTotalRequests )
{
++mFinishedRequests;
}
}
}
}
if ( (aStateFlags & nsIWebProgressListener::STATE_START)
&& (aStateFlags &
nsIWebProgressListener::STATE_IS_DOCUMENT) )
{
return NS_OK;
}
if (!aWebProgress)
{
return NS_OK;
}
else
{
PRBool docLoading;
aWebProgress->GetIsLoadingDocument(&docLoading);
if (docLoading == PR_TRUE)
{
OutputDebugString(_T("Our page is still
loading...\n"));
return NS_OK;
}
}
if ( (mFinishedRequests == mTotalRequests)
&& ( (aStateFlags &
nsIWebProgressListener::STATE_STOP) |
(aStateFlags &
nsIWebProgressListener::STATE_IS_NETWORK) )
&& (aStatus == NS_OK))
{
OutputDebugString(_T("Our page is now
loaded...\n"));
}
}
catch(...)
{
OutputDebugString(_T("CRASH!!!\n"));
}
return NS_OK;
}
> -----Original Message-----
> From: [EMAIL PROTECTED]
> [mailto:[EMAIL PROTECTED] On Behalf Of vonuyx
> Sent: 21 April 2005 15:48
> To: [email protected]
> Subject: Re: How can I determine when a page is finished loading?
>
>
> Hi,
> It seems to me that everytime when a whole page is loaded (within a
> single window), OnStateChange is called twice with some
> speciale values
> of aStateFlags.
> In your example (http://www.cnn.com/), you should have:
>
> 1. First, OnStateChange is called with aStateFlags contains
> STATE_STOP
> |STATE_IS_DOCUMENT and in aRequest you have aRequest::GetName() ==
> http://www.cnn.com/
> 2. Next, OnStateChange is called again with aStateFlags contains
> STATE_STOP | STATE_IS_NETWORK | STATE_IS_WINDOW and the same URI in
> aRequest.
>
> I think you can depent on this to determine whether the whole
> page was
> loaded or not.
>
> It works for Firefox 1.x
>
> Vonuyx
>
>
>
> Damien O'Brien wrote:
>
> > Hi,
> > I'm working on a C++ XPCOM component on WinXP. I want to
> get the title
> > and URL of pages viewed in a browser. To do this I using
> the function
> > OnStateChange() from nsIWebProgressListener. This all seems to be
> > working fine, however I've discovered a problem when
> visiting certain
> > web pages (e.g. www.cnn.com). There are several references to other
> > pages that need to be downloded within this page. So the
> browser needs
> > to download each of these "embedded" pages in turn. The
> problem with
> > this is my plugin thinks each of these is a new page and so
> > OnStateChange() gets called for each embedded page. I am testing
> > aStateFlags to determine the download state but this can't
> tell me if
> > a page is an "embedded" page. So instead of me seeing just one page
> > being loaded when I go to www.cnn.com - I see several pages e.g
> >
> >
> http://ar.atwola.com/html/93182780/547029716/aol?SNM=HI&width=120&heig
> > ht=90&
> > target=_blank&TZ=-60&TVAR=class%3Dintl&CT=I
> >
> http://cnn.dyn.cnn.com/weatherBox.html?domId=weatherBox&time=1
> 114017300788
> > And finally the page that I'm really only interested in;
> > http://www.cnn.com/
> >
> > What I need is a way for me to determine when the page is finished
> > loading. Does anyone have any idea how I can do this?
> >
> > I've included a cutdown version of my OnStateChange()
> implementation
> > in case this helps. Thanks
> > Damien
> >
> > NS_IMETHODIMP
> > MyMozilla::OnStateChange( nsIWebProgress *aWebProgress,
> > nsIRequest *aRequest, PRUint32 aStateFlags,
> > nsresult aStatus )
> > {
> > // First check that this is a document load that's occuring.
> > if (aStateFlags & STATE_IS_DOCUMENT)
> > {
> > // Now check if we've finished loading the document
> > if (aStateFlags & STATE_STOP)
> > {
> > HWND hWnd = NULL;
> > nsCOMPtr<nsIDOMWindow> window;
> > nsresult rv =
> > aWebProgress->GetDOMWindow(getter_AddRefs(window));
> >
> > if (NS_FAILED(rv))
> > {
> > return rv;
> > }
> >
> > nsIDOMDocument *domDocument;
> > rv = window->GetDocument(&domDocument);
> >
> > if (NS_FAILED(rv))
> > {
> > return rv;
> > }
> >
> > nsCOMPtr<nsIDOMHTMLDocument>
> > htmlDomDocument(do_QueryInterface(domDocument));
> >
> > if (htmlDomDocument)
> > {
> > nsEmbedString strTitleText;
> > htmlDomDocument->GetTitle
> (strTitleText);
> > const PRUnichar *strTitle =
> > strTitleText.get();
> > nsEmbedString strURLText;
> > htmlDomDocument->GetURL(strURLText);
> > const PRUnichar *strURL =
> strURLText.get();
> >
> > // DO STUFF HERE.....
> > }
> > }
> > }
> >
> > return NS_OK;
> > }
> >
>
> _______________________________________________
> Mozilla-xpcom mailing list
> [email protected]
> http://mail.mozilla.org/listinfo/mozilla-xpcom
>
_______________________________________________
Mozilla-xpcom mailing list
[email protected]
http://mail.mozilla.org/listinfo/mozilla-xpcom