Blocking dialogs in .NET WebBrowser control

前端 未结 12 1143
执笔经年
执笔经年 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:07
    webBrowser1.ScriptErrorsSuppressed = true;
    

    Just add that to your entry level function. After alot of research is when I came across this method, and touch wood till now its worked. Cheers!!

    0 讨论(0)
  • 2020-11-27 17:08

    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.

    0 讨论(0)
  • 2020-11-27 17:09

    I had bigger problems with this: loading a webpage that is meant for printing and it displays annoying Print dialog. The InjectBlocker was the only way that worked, but fairly unreliable. Under certain conditions (I am considering it's due that WebBrowser control uses IE engine and this depends on installed IE version) the print dialog still appears. This is a major problem, the solution works on Win7 with IE9 installed, but WinXP with IE8 displays the dialog, no matter what.

    I believe the solution is in modifying source code and removing the print javascript, before control renders the page. However I tried that with: DocumentText property of the webbrowser control and it is not working. The property is not read only, but it has no effect, when I modify the source.

    The solution I found for my problem is the Exec script:

    string alertBlocker = "window.print = function emptyMethod() { }; window.alert = function emptyMethod() { }; window.open = function emptyMethod() { };";    
    this.Document.InvokeScript("execScript", new Object[] { alertBlocker, "JavaScript" });
    
    0 讨论(0)
  • 2020-11-27 17:16

    And for an easy way to inject that magic line of javascript, read how to inject javascript into webbrowser control.

    Or just use this complete code:

    private void InjectAlertBlocker() {
        HtmlElement head = webBrowser1.Document.GetElementsByTagName("head")[0];
        HtmlElement scriptEl = webBrowser1.Document.CreateElement("script");
        string alertBlocker = "window.alert = function () { }";
        scriptEl.SetAttribute("text", alertBlocker);
        head.AppendChild(scriptEl);
    }
    
    0 讨论(0)
  • 2020-11-27 17:24

    Bulletproof alert blocker:

    Browser.Navigated +=
        new WebBrowserNavigatedEventHandler(
            (object sender, WebBrowserNavigatedEventArgs args) => {
                Action<HtmlDocument> blockAlerts = (HtmlDocument d) => {
                    HtmlElement h = d.GetElementsByTagName("head")[0];
                    HtmlElement s = d.CreateElement("script");
                    IHTMLScriptElement e = (IHTMLScriptElement)s.DomElement;
                    e.text = "window.alert=function(){};";
                    h.AppendChild(s);
                };
                WebBrowser b = sender as WebBrowser;
                blockAlerts(b.Document);
                for (int i = 0; i < b.Document.Window.Frames.Count; i++)
                    try { blockAlerts(b.Document.Window.Frames[i].Document); }
                    catch (Exception) { };
            }
        );
    

    This sample assumes you have Microsoft.mshtml reference added, "using mshtml;" in your namespaces and Browser is your WebBrowser instance.

    Why is it bulletproof? First, it handles scripts inside frames. Then, it doesn't crash when a special "killer frame" exists in document. A "killer frame" is a frame which raises an exception on attempt to use it as HtmlWindow object. Any "foreach" used on Document.Window.Frames would cause an exception, so safer "for" loop must be used with try / catch block.

    Maybe it's not the most readable piece of code, but it works with real life, ill-formed pages.

    0 讨论(0)
  • 2020-11-27 17:24

    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.

    0 讨论(0)
提交回复
热议问题