I have a .NET 2.0 WebBrowser control used to navigate some pages with no user interaction (don\'t ask...long story). Because of the user-less nature of thi
I managed to inject the code above by creating an extended WebBroswer class and overriding the OnNavigated method.
This seemed to work quite well:
class WebBrowserEx : WebBrowser
{
public WebBrowserEx ()
{
}
protected override void OnNavigated( WebBrowserNavigatedEventArgs e )
{
HtmlElement he = this.Document.GetElementsByTagName( "head" )[0];
HtmlElement se = this.Document.CreateElement( "script" );
mshtml.IHTMLScriptElement element = (mshtml.IHTMLScriptElement)se.DomElement;
string alertBlocker = "window.alert = function () { }";
element.text = alertBlocker;
he.AppendChild( se );
base.OnNavigated( e );
}
}