1

I have a WebBrowser in WPF and I keep getting this popup after successful login to a website

enter image description here

I've tried to suppress this using both hide script and an alert blocker without success.

Is there a way to suppress this popup in code only?

Code:

        <WebBrowser x:Name="wpfBrowser"
                    Height="450"
                    MinHeight="450"
                    Width="825"
                    MinWidth="825"
                    view:WebBrowserBehaviors.BindableSource="{Binding LoginViewModel.URL}" 
                    Navigated="wpfBrowser_Navigated"
                    Loaded="wpfBrowser_Loaded"
                    LoadCompleted="wpfBrowser_LoadCompleted" KeyUp="wpfBrowser_KeyUp">                
        </WebBrowser>


    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 });
    }

    private void InjectAlertBlocker()
    {
        mshtml.HTMLDocument document = (mshtml.HTMLDocument)wpfBrowser.Document;
        mshtml.IHTMLElement head = document.getElementsByTagName("head").Cast<mshtml.IHTMLElement>().First();
        var script = (mshtml.IHTMLScriptElement)document.createElement("script");
        string alertBlocker = "window.alert = function () { }";
        ((mshtml.HTMLHeadElement)head).appendChild((mshtml.IHTMLDOMNode)script);
    }

    private void wpfBrowser_Loaded(object sender, RoutedEventArgs e)
    {
        HideScriptErrors(wpfBrowser, true);
    }

    private void wpfBrowser_Navigated(object sender, NavigationEventArgs e)
    {
        try
        {
            InjectAlertBlocker();
        }
        catch (Exception ex)
        {
            NLog.Log(LogLevel.Error, ex, "Logs");
        }
    }
  • I've gone through all of the examples in that link and none block this dialog. –  Dec 14 '17 at 20:02
  • This is definitely not a duplicate to https://stackoverflow.com/questions/77659/blocking-dialogs-in-net-webbrowser-control!!! Because: The other question is about JavaScript Dialogs alert() - but the Dialog in this question has nothing do to with JavaScript Alert!!! – Markus Dec 14 '17 at 21:47

1 Answers1

0

This popup is NOT from JavaScript... It's from IE.

You can try this, if you have access to the registry from your app:

https://support.microsoft.com/en-us/help/229940/how-to-disable-internet-explorer-password-caching

BUT: This is also canceling this Popup for the normal IE Browser outside your app!!!

Markus
  • 2,184
  • 2
  • 22
  • 32
  • Thanks, this helps alot. Introduces many more problems but does answer the question. Will probably have to go with CefSharp instead. Not sure why they closed it as it's not related. –  Dec 14 '17 at 23:20