JavaScript alert method in WebView2 cause application crash

aluzi liu 486 Reputation points
2023-03-10T04:58:22.2866667+00:00

I have a WPF project that using Webview2 control(Microsoft.Web.WebView2.Wpf) and .NET 6, I implement the "alert" javascript method like this:

        private void W_ScriptDialogOpening(object? sender, Microsoft.Web.WebView2.Core.CoreWebView2ScriptDialogOpeningEventArgs e)
        {
            switch (e.Kind)
            {
                case Microsoft.Web.WebView2.Core.CoreWebView2ScriptDialogKind.Alert:
                    MessageBox.Show(e.Message, "MyTitle", MessageBoxButton.OK, MessageBoxImage.Information);
                    break;
                case Microsoft.Web.WebView2.Core.CoreWebView2ScriptDialogKind.Confirm:
                    if (MessageBox.Show(e.Message, "MyTitle", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
                        e.Accept();
                    break;
                default:
                    MessageBox.Show("Hi");
                    break;
            }
        }

And then I run this project and open a html page with only one button, this button running javascript:alert("OK")

When I click this button, MessageBox.Show executed, I can see the dialog.

The problem is, if you don't close the dialog, left the dialog keep showing, about 25 seconds later the application will crash without any message, how to fix it?

Developer technologies Windows Presentation Foundation
Developer technologies .NET Other
Developer technologies C#
{count} votes

Accepted answer
  1. Viorel 122.5K Reputation points
    2023-03-10T20:51:06.3233333+00:00

    According to https://github.com/MicrosoftEdge/WebView2Feedback/issues/1966 (which is a different issue), try an experimental workaround:

    private void CoreWebView2_ScriptDialogOpening( object? sender, CoreWebView2ScriptDialogOpeningEventArgs e )
    {
        CoreWebView2Deferral d = e.GetDeferral();
                
        Dispatcher.BeginInvoke( ( ) => ShowDialog( e, d ) );
    }
    
    void ShowDialog( CoreWebView2ScriptDialogOpeningEventArgs e, CoreWebView2Deferral d )
    {
        switch( e.Kind )
        {
        case CoreWebView2ScriptDialogKind.Alert:
            MessageBox.Show( e.Message, "MyTitle", MessageBoxButton.OK, MessageBoxImage.Information );
            break;
        case CoreWebView2ScriptDialogKind.Confirm:
            if( MessageBox.Show( e.Message, "MyTitle", MessageBoxButton.YesNo, MessageBoxImage.Question ) == MessageBoxResult.Yes )
            {
                e.Accept( );
            }
            break;
        default:
            MessageBox.Show( "Hi" );
            break;
        }
    
        d.Complete();
        webView.Focus();
    }
    
    

    If it works, it probably can be extended for other kinds of alerts. The flow can be simplified if the debugger is not detected.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.