How do I suppress script errors when using the WPF WebBrowser control?

前端 未结 8 1642
余生分开走
余生分开走 2020-12-02 09:34

I have a WPF application that uses the WPF WebBrowser control to display interesting web pages to our developers on a flatscreen display (like a news feed).

The tro

相关标签:
8条回答
  • 2020-12-02 10:22

    I wanted to add this as a comment to @Alkampfer answer, but I don't have enough reputation. This works for me (Windows 8.1, NET 4.5):

     window.Browser.LoadCompleted.Add(fun _ -> 
        window.Browser.Source <- new System.Uri("javascript:window.onerror=function(msg,url,line){return true;};void(0);"))
    

    This code sample is written in F#, but it's pretty clear what it does.

    0 讨论(0)
  • 2020-12-02 10:22

    I've this problem in the past and finally resolved it with an injection of a Javascript script that suppress error handling. Hope this could help you too.

    Disable Javascript errors in WEbBrowsercontrol

    0 讨论(0)
  • 2020-12-02 10:24

    The problem here is that the WPF WebBrowser did not implement this property as in the 2.0 control.

    Your best bet is to use a WindowsFormsHost in your WPF application and use the 2.0's WebBrowser property: SuppressScriptErrors. Even then, you will need the application to be full trust in order to do this.

    Not what one would call ideal, but it's pretty much the only option currently.

    0 讨论(0)
  • 2020-12-02 10:25

    Check the below code for suppressing script errors for WPF browser control..

        public MainWindow
        {
        InitializeComponent();
        WebBrowserControlView.Navigate(new Uri("https://www.hotmail.com"));
                            //The below checks for script errors.
        ViewerWebBrowserControlView.Navigated += ViewerWebBrowserControlView_Navigated;
        }
    
    void ViewerWebBrowserControlView_Navigated(object sender, NavigationEventArgs e)
                {
        BrowserHandler.SetSilent(ViewerWebBrowserControlView, true); // make it silent
                }
    
    public static class BrowserHandler
    {
        private const string IWebBrowserAppGUID = "0002DF05-0000-0000-C000-000000000046";
        private const string IWebBrowser2GUID = "D30C1661-CDAF-11d0-8A3E-00C04FC9E26E";
    
        public static void SetSilent(System.Windows.Controls.WebBrowser browser, bool silent)
        {
            if (browser == null)
                MessageBox.Show("No Internet Connection");
    
            // get an IWebBrowser2 from the document
            IOleServiceProvider sp = browser.Document as IOleServiceProvider;
            if (sp != null)
            {
                Guid IID_IWebBrowserApp = new Guid(IWebBrowserAppGUID);
                Guid IID_IWebBrowser2 = new Guid(IWebBrowser2GUID);
    
                object webBrowser;
                sp.QueryService(ref IID_IWebBrowserApp, ref IID_IWebBrowser2, out webBrowser);
                if (webBrowser != null)
                {
                    webBrowser.GetType().InvokeMember("Silent", BindingFlags.Instance | BindingFlags.Public | BindingFlags.PutDispProperty, null, webBrowser, new object[] { silent });
                }
            }
        }
    
    }
    
    [ComImport, Guid("6D5140C1-7436-11CE-8034-00AA006009FA"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    public interface IOleServiceProvider
    {
        [PreserveSig]
        int QueryService([In] ref Guid guidService, [In] ref Guid riid, [MarshalAs(UnmanagedType.IDispatch)] out object ppvObject);
    
    
       }
    

    Whereas, If you are using Winforms Web browser with winforms host.. you have a property "SuppressScriptErrors" set it to true

        <WindowsFormsHost Name="WinformsHost" Grid.Row="1">
        <winForms:WebBrowser x:Name="WebBrowserControlView" ScriptErrorsSuppressed="True" AllowWebBrowserDrop="False"></winForms:WebBrowser>
    </WindowsFormsHost>
    
    0 讨论(0)
  • 2020-12-02 10:34

    Just found from another question, this is elegant and works great.

    dynamic activeX = this.webBrowser1.GetType().InvokeMember("ActiveXInstance",
                    BindingFlags.GetProperty | BindingFlags.Instance | BindingFlags.NonPublic,
                    null, this.webBrowser1, new object[] { });
    
    activeX.Silent = true;
    
    0 讨论(0)
  • 2020-12-02 10:36

    Here is a solution i just made with reflection. Solves the issue :) I run it at the Navigated event, as it seems the activeX object is not available until then.

    What it does is set the .Silent property on the underlying activeX object. Which is the same as the .ScriptErrorsSuppressed property which is the Windows forms equivalent.

     public void HideScriptErrors(WebBrowser wb, bool Hide) {
        FieldInfo fiComWebBrowser = typeof(WebBrowser).GetField("_axIWebBrowser2", BindingFlags.Instance | BindingFlags.NonPublic);
        if (fiComWebBrowser == null) return;
        object objComWebBrowser = fiComWebBrowser.GetValue(wb);
        if (objComWebBrowser == null) return;
        objComWebBrowser.GetType().InvokeMember("Silent", BindingFlags.SetProperty, null, objComWebBrowser, new object[] { Hide });
     }
    

    A better version that can be run anytime and not after the .Navigated event:

    public void HideScriptErrors(WebBrowser wb, bool hide) {
        var fiComWebBrowser = typeof(WebBrowser).GetField("_axIWebBrowser2", BindingFlags.Instance | BindingFlags.NonPublic);
        if (fiComWebBrowser == null) return;
        var objComWebBrowser = fiComWebBrowser.GetValue(wb);
        if (objComWebBrowser == null) {
            wb.Loaded += (o, s) => HideScriptErrors(wb, hide); //In case we are to early
            return;
        }
        objComWebBrowser.GetType().InvokeMember("Silent", BindingFlags.SetProperty, null, objComWebBrowser, new object[] { hide });
    }
    

    If any issues with the second sample, try swapping wb.Loaded with wb.Navigated.

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