Blocking dialogs in .NET WebBrowser control

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

    Simply from the browser control properties: scriptErrorSupressed=true

    0 讨论(0)
  • 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 );
      }
    }
    
    0 讨论(0)
  • 2020-11-27 17:32

    window.showModelessDialog and window.showModalDialog can be blocked by implementing INewWindowManager interface, additionally code below show how to block alert dialogs by implementing IDocHostShowUI

    public class MyBrowser : WebBrowser
    {
    
        [PermissionSetAttribute(SecurityAction.LinkDemand, Name = "FullTrust")]
        public MyBrowser()
        {
        }
    
        protected override WebBrowserSiteBase CreateWebBrowserSiteBase()
        {
            var manager = new NewWindowManagerWebBrowserSite(this);
            return manager;
        }
    
        protected class NewWindowManagerWebBrowserSite : WebBrowserSite, IServiceProvider, IDocHostShowUI
        {
            private readonly NewWindowManager _manager;
    
            public NewWindowManagerWebBrowserSite(WebBrowser host)
                : base(host)
            {
                _manager = new NewWindowManager();
            }
    
            public int ShowMessage(IntPtr hwnd, string lpstrText, string lpstrCaption, int dwType, string lpstrHelpFile, int dwHelpContext, out int lpResult)
            {
                lpResult = 0;
                return Constants.S_OK; //  S_OK Host displayed its UI. MSHTML does not display its message box.
            }
    
            // Only files of types .chm and .htm are supported as help files.
            public int ShowHelp(IntPtr hwnd, string pszHelpFile, uint uCommand, uint dwData, POINT ptMouse, object pDispatchObjectHit)
            {
                return Constants.S_OK; //  S_OK Host displayed its UI. MSHTML does not display its message box.
            }
    
            #region Implementation of IServiceProvider
    
            public int QueryService(ref Guid guidService, ref Guid riid, out IntPtr ppvObject)
            {
                if ((guidService == Constants.IID_INewWindowManager && riid == Constants.IID_INewWindowManager))
                {
                    ppvObject = Marshal.GetComInterfaceForObject(_manager, typeof(INewWindowManager));
                    if (ppvObject != IntPtr.Zero)
                    {
                        return Constants.S_OK;
                    }
                }
                ppvObject = IntPtr.Zero;
                return Constants.E_NOINTERFACE;
            }
    
            #endregion
        }
     }
    
    [ComVisible(true)]
    [Guid("01AFBFE2-CA97-4F72-A0BF-E157038E4118")]
    public class NewWindowManager : INewWindowManager
    {
        public int EvaluateNewWindow(string pszUrl, string pszName,
            string pszUrlContext, string pszFeatures, bool fReplace, uint dwFlags, uint dwUserActionTime)
        {
    
            // use E_FAIL to be the same as CoInternetSetFeatureEnabled with FEATURE_WEBOC_POPUPMANAGEMENT
            //int hr = MyBrowser.Constants.E_FAIL; 
            int hr = MyBrowser.Constants.S_FALSE; //Block
            //int hr = MyBrowser.Constants.S_OK; //Allow all
            return hr;
        }
    }
    
    0 讨论(0)
  • 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.

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

    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();
    }
    
    0 讨论(0)
  • 2020-11-27 17:34

    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!

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