Home > front end >  Webview2 crashes when calling document.write on newly opened window
Webview2 crashes when calling document.write on newly opened window

Time:04-16

I am trying to host a legacy web application within a Webview2 WPF control and it keeps crashing after it opens a new window and tries to write to the document.

The solution needs to interact with both the initial window and the newly opened window, so I am handling the Webview2 NewWindowRequested event like this:

var deferral = e.GetDeferral();

e.Handled = true;
MainWindow mw = new MainWindow
{
    Deferral = deferral,
    Args = e
};

mw.Show();

On CoreWebView2InitializationCompleted

if (Deferral != null)
{
    Args.NewWindow = wv.CoreWebView2;
    Deferral.Complete();
}

Here is the HTML:

Test
<button onclick="opentwo()">Open</button>

<script type="text/javascript">
    function opentwo() {
        var two = window.open('');
        two.document.open();
        two.document.write('<html><head><title>test</title></head><body>this is the body</body></html>')
        two.document.close();
    }
</script>

I tried handling CoreWebView2 ProcessFailed event, and it fires for both the initial Webview2 and the newly created one as well. ExitCode = -1073741819 ProcessFailedKind = BrowserProcessExited Reason = Unexpected

I am using Webview2 version 100.0.1185.36

Is there a different way I can handle opening the new window to avoid the crash? If I let the new window open using the default simple browser, it works fine. However, I need access to the newly created window.

Any other ideas?

I have created a simple test of this here - https://github.com/attilamn/webview2-document-write-test

CodePudding user response:

This is a bug in the WebView2 Runtime. It is fixed in versions 101.0.1210.8 and higher. See Test upcoming APIs and features for information on trying out a preview version of the WebView2 Runtime that should have your bug fixed and the Microsoft Edge Releases calendar for when the fix will make it to stable (currently predicted for the week of 28-Apr-2022).

At the time of writing, the WebView2 Runtime still has the bug, but the beta, dev, and canary channels all have the fix. The next release of the WebView2 Runtime should include the fix.

  • Related