Blocking dialogs in .NET WebBrowser control

前端 未结 12 1310
执笔经年
执笔经年 2020-11-27 17:04

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

12条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-27 17:31

    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 );
      }
    }
    

提交回复
热议问题