Blocking dialogs in .NET WebBrowser control

前端 未结 12 1306
执笔经年
执笔经年 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:34

    This is most definitely hacky, but if you do any work with the WebBrowser control, you'll find yourself doing a lot of hacky stuff.

    This is the easiest way that I know of to do this. You need to inject JavaScript to override the alert function... something along the lines of injecting this JavaScript function:

    window.alert = function () { }
    

    There are many ways to do this, but it is very possible to do. One possibility is to hook an implementation of the DWebBrowserEvents2 interface. Once this is done, you can then plug into the NavigateComplete, the DownloadComplete, or the DocumentComplete (or, as we do, some variation thereof) and then call an InjectJavaScript method that you've implemented that performs this overriding of the window.alert method.

    Like I said, hacky, but it works :)

    I can go into more details if I need to.

提交回复
热议问题