Call an event when the current url loads (excluding iframe websites, etc) (repeated)

Add-in Express™ Support Service
That's what is more important than anything else

Call an event when the current url loads (excluding iframe websites, etc) (repeated)
 
Konrad Gorski




Posts: 6
Joined: 2012-07-23
Hi,
I want to inject some js's after page is loaded.
I found quite good answer for my problem here but on some url's event DocumentComplete is fired once (mostly, ok), two times (very strange, happens few times) or sometimes never! Especially by page reloads event don't fire every so often (using F5, 1-3 times for 10 reloads).
I'm stuck: can understand 'never fired' or 'always fired' but sometimes?
Example url:
some product on Amazon

What is a proper way to determine when top document is loaded?

Regards
Konrad
ps. used browser IE9, code:
private void IEModule_DocumentComplete(object pDisp, string url)
{
if (pDisp is IE.IWebBrowser2)
{
IE.IWebBrowser2 browser = pDisp as IE.IWebBrowser2;

int hwnd = 0;

try
{
hwnd = browser.HWND;
}
catch (Exception)
{
}

if (hwnd != 0)
{
// run custom code
MessageBox.Show("IEModule_DocumentComplete: " + url);

}
}
}
Posted 12 Sep, 2012 15:08:36 Top
Konrad Gorski




Posts: 6
Joined: 2012-07-23
I just found that it's not url-dependant effect.
Same problem is on www.google.pl, for example.

Regards
Konrad
Posted 12 Sep, 2012 15:28:29 Top
Eugene Astafiev


Add-in Express team


Posts: 8540
Joined: 2007-07-25
Hi Konrad,

The following code works like a charm on my PC with Internet Explorer 9 installed:

private void IEModule_DocumentComplete(object pDisp, string url)
{            
   if (pDisp is IE.IWebBrowser2)
   {
       IE.IWebBrowser2 browser = pDisp as IE.IWebBrowser2;
       int hwnd = 0;
       try
       {
           hwnd = browser.HWND;
       }
       catch (Exception)
       {
       }
       if (hwnd != 0)
       {
           // run custom code
           System.Diagnostics.Debug.WriteLine("IEModule_DocumentComplete: " + url); 
       }
   }
}


Please try to replace the MessageBox.Show method with the System.Diagnostics.Debug.WriteLine statement. Do you get "the custom code" running now?

BTW Sometimes it is fired twice. It looks like this depends on the web page. However, I always get the custom code running on my PC (even with your URL).
.NET, Extended MAPI, Windows Phone & RT
Posted 13 Sep, 2012 10:46:26 Top
Konrad Gorski




Posts: 6
Joined: 2012-07-23
Hi Eugene,

I found "temporary" solution here and it's stable: event DownloadComplete and check IeApp.ReadyState for READYSTATE_COMPLETE.

Your code still don't work for me. As I see, problem is on my side only so I'll check it on others PC's with IE9.

Thanks for fast replay.

Regards
Konrad
Posted 13 Sep, 2012 13:29:12 Top
Eugene Astafiev


Add-in Express team


Posts: 8540
Joined: 2007-07-25
Good news, Konrad!

Thank you for letting us know and good luck with your add-in project.
.NET, Extended MAPI, Windows Phone & RT
Posted 14 Sep, 2012 03:35:21 Top
Sergey Grischenko


Add-in Express team


Posts: 7202
Joined: 2004-07-05
Hi Konrad,

The latest version provides the DocumentComplete2 event. The event contains the 'rootDocLoaded' parameter.
It is set to true if the root document is completely loaded. Also, to determine if the user refreshes the web page, you can use the OnBeforeRefresh event handler.
Posted 14 Sep, 2012 05:47:23 Top