Search This Blog

Friday, November 1, 2013

Disable Script Errors WebBrowser WPF

WPF > Controls  > WebBrowser > Disable Script Errors

WebBrowser WPF control doesn't have property to disable script errors.
Using reflection we can solve this problem.

Solution:

private void Window_Loaded(object sender, RoutedEventArgs e)
{
   wb1.Navigated += new NavigatedEventHandler(WebBrowser_Navigated);
}

void WebBrowser_Navigated(object sender, NavigationEventArgs e)
{
   HideJsScriptErrors((WebBrowser) sender);
}

public void HideJsScriptErrors(WebBrowser wb)
{
   // IWebBrowser2 interface
   // Exposes methods that are implemented by the WebBrowser control 
   // Searches for the specified field, using the specified binding constraints.
   FieldInfo fld = typeof(WebBrowser).GetField("_axIWebBrowser2", BindingFlags.Instance | BindingFlags.NonPublic);
   if (fld == null)
       return;
   object obj = fld.GetValue(wb);
   if (obj == null)
       return;
// Silent: Sets or gets a value that indicates whether the object can display dialog boxes.
// HRESULT IWebBrowser2::get_Silent(VARIANT_BOOL *pbSilent);HRESULT IWebBrowser2::put_Silent(VARIANT_BOOL bSilent);
     obj.GetType().InvokeMember("Silent", BindingFlags.SetProperty, null, obj, new object[] { true });
}