Reputation: 19684
I am responding to the System.Windows.WebBrowser.DocumentComplete event in my application. However this event fires every time an ajax call completes on the page. I would like to know when the document is ready. Meaning when all calls out have returned. Any ideas?
void WebBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
//Can fire many times if page has async calls.
}
Upvotes: 0
Views: 1458
Reputation: 15281
There isn't an event that is fired when the scripts are done (well, there is IActiveScriptSite::OnLeaveScript, but you can't get your own code into IE's scripting host)
I think you can use IDispatchEx to override the appendChild method and removeChild method for each DOM node (or hook up the Mutation events if you are on IE9) and inject a call to your code (e.g. an IAmNotDoneYet function) after calling IE's implementation of these method. The original properties and methods should be accessible via COM interfaces (e.g. IHTMLElement.AppendChild).
You probably want to override the setter method for InnerHTML and OuterHTML properties too. If you want to monitor property change like css/directshow transitions then there are too many methods/properties to hook without scarifying performance.
Upvotes: 1
Reputation: 1566
As everyone else has said, there really isnt a particular event you can watch for. If you know what particular element you are waiting for, you can wait until that element appears. IE
while (element == null)
application.doevents
Or something. But that's really about it, honestly. If you find a better way, let me know cause i'd be able to use the hell out of that.
Upvotes: 0
Reputation: 19684
After much searching, trail and error I decided to add a manual delay. I simple spin up a worker thread and then put this tread to sleep for a couple of seconds. This allows the main ui to work while adding the needed delay.
Upvotes: 0
Reputation: 3371
If you control the site you're loading you could create an object for scripting that can be called from your javascript when the page is done loading. See WebBrowser.ObjectForScripting....
EDIT: Per OP comments. He doesn't control the site.
Upvotes: 0
Reputation: 2455
You can check the Url property of WebBrowserDocumentCompletedEventArgs
if (!e.Url.AbsoluteUri.Contains(".js"))
return;
I haven't tested this for ajax calls, but it works very well for iframes.
Upvotes: 0