Blocking dialogs in .NET WebBrowser control

2019-01-06 13:54发布

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 this application, I have set the WebBrowser control's ScriptErrorsSuppressed property to true, which the documentation included with VS 2005 states will [...]"hide all its dialog boxes that originate from the underlying ActiveX control, not just script errors." The MSDN article doesn't mention this, however. I have managed to cancel the NewWindow event, which prevents popups, so that's taken care of.

Anyone have any experience using one of these and successfully blocking all dialogs, script errors, etc?

EDIT

This isn't a standalone instance of IE, but an instance of a WebBrowser control living on a Windows Form application. Anyone have any experience with this control, or the underlying one, AxSHDocVW?

EDIT again

Sorry I forgot to mention this... I'm trying to block a JavaScript alert(), with just an OK button. Maybe I can cast into an IHTMLDocument2 object and access the scripts that way, I've used MSHTML a little bit, anyone know?

13条回答
看我几分像从前
2楼-- · 2019-01-06 14:12

The easiest way to do this is : In the : Webbrowser Control you have the procedure ( standard ) BeforeScriptExecute

( The parameter for BeforeScriptExecute is pdispwindow )

Add this :

pdispwindow.execscript("window.alert = function () { }")

In this way before any script execution on the page window alert will be suppressed by injected code.

查看更多
地球回转人心会变
3楼-- · 2019-01-06 14:13

Are you trying to implement a web robot? I have little experience in using the hosted IE control but I did completed a few Win32 projects tried to use the IE control. Disabling the popups should be done via the event handlers of the control as you already did, but I found that you also need to change the 'Disable script debugging xxxx' in the IE options (or you could modify the registry in your codes) as cjheath already pointed out. However I also found that extra steps needed to be done on checking the navigating url for any downloadable contents to prevent those open/save dialogs. But I do not know how to deal with streaming files since I cannot skip them by looking at the urls alone and in the end I turned to the Indy library saving me all the troubles in dealing with IE. Finally, I remember Microsoft did mention something online that IE is not designed to be used as an OLE control. According to my own experience, every time the control navigates to a new page did introduce memory leaks for the programs!

查看更多
劳资没心,怎么记你
4楼-- · 2019-01-06 14:13

Simply from the browser control properties: scriptErrorSupressed=true

查看更多
我想做一个坏孩纸
5楼-- · 2019-01-06 14:14

I just published an article on Code Project that may help you.

Please see - http://www.codeproject.com/KB/shell/WebBrowserControlDialogs.aspx

Hope this helps.

查看更多
Luminary・发光体
6楼-- · 2019-01-06 14:16

The InjectAlertBlocker is absolutely correct code is

private void InjectAlertBlocker() {
    HtmlElement head = webBrowser1.Document.GetElementsByTagName("head")[0];
    HtmlElement scriptEl = webBrowser1.Document.CreateElement("script");
    IHTMLScriptElement element = (IHTMLScriptElement)scriptEl.DomElement;
    string alertBlocker = "window.alert = function () { }";
    element.text = alertBlocker;
    head.AppendChild(scriptEl);
}

References needed to be added is

  1. Add a reference to MSHTML, which will probalby be called "Microsoft HTML Object Library" under COM references.

  2. Add using mshtml; to your namespaces.

  3. Get a reference to your script element's IHTMLElement:

Then you can use the Navigated event of webbrowser as:

private void InjectAlertBlocker()
{
    HtmlElement head = webBrowser1.Document.GetElementsByTagName("head")[0];
    HtmlElement scriptEl = webBrowser1.Document.CreateElement("script");
    IHTMLScriptElement element = (IHTMLScriptElement)scriptEl.DomElement;
    string alertBlocker = "window.alert = function () { }";
    element.text = alertBlocker;
    head.AppendChild(scriptEl);
}

private void webDest_Navigated(object sender, WebBrowserNavigatedEventArgs e)
{
    InjectAlertBlocker();
}
查看更多
Anthone
7楼-- · 2019-01-06 14:17

You may have to customize some things, take a look at IDocHostUIHandler, and then check out some of the other related interfaces. You can have a fair amount of control, even to the point of customizing dialog display/ui (I can't recall which interface does this). I'm pretty sure you can do what you want, but it does require mucking around in the internals of MSHTML and being able to implement the various COM interfaces.

Some other ideas: http://msdn.microsoft.com/en-us/library/aa770041.aspx

IHostDialogHelper
IDocHostShowUI

These may be the things you're looking at implementing.

查看更多
登录 后发表回答