2

Say, I am clicking on some tennis match on Flashscore web site. A new window pops up. I want to capture that second window in WebView2:

browser.CoreWebView2Ready += delegate
{ 
    browser.CoreWebView2.NewWindowRequested += OnNewWindowRequested;
};

private async void OnNewWindowRequested(object sender, CoreWebView2NewWindowRequestedEventArgs e)
{
    var newWindow = e.NewWindow; //null
}

However, newWindow is null. At the same time, using WindowFeatures, I can get height or width of the new window:

uint height = e.WindowFeatures.Height;
uint width = e.WindowFeatures.Width;

How can I capture the reference to the second window?

JohnyL
  • 6,894
  • 3
  • 22
  • 41

1 Answers1

7

The NewWindowRequested event can let you cancel opening a new window or replace the window with your own, but you cannot have WebView2 open a new window for you and get a reference to that new window.

NewWindowRequested event scenarios:

  1. Take no action in the event handler or don't subscribe. WebView2 will open a new window with minimal UI that is out of control of the end developer.
  2. Set the Handled property on the event args to true. Cancels opening of the new window. If window.open is what was creating the new window, then the return value is null.
  3. Set the Handled property on the event args to true, and use the Uri property to open the URI in the user's default browser or otherwise handle the new window outside of WebView2.
  4. Set the Handled property on the event args to true, and set the NewWindow property to a new CoreWebView2 that you create. If window.open is what was creating the new window, then the return value is a new window object corresponding to the CoreWebView2 that you provided. You can obtain a new CoreWebView2 by creating one from a CoreWebView2Environment class or if you're using a WebView2 element in WPF, WinForms, or WinUI, you can create a new WebView2 element and use its CoreWebView2 property.

You can see some example of using the NewWindowRequested event in our sample app.

David Risney
  • 3,886
  • 15
  • 16
  • I guess, I need option 4. When I'm doing this in event handler: `e.NewWindow = browser.CoreWebView2;`, then I begin managing this new window. So, this one line does the trick. I didn't understand _and set the `NewWindow` property to a new CoreWebView2 that you create_. But CoreWebView2 isn't creatable. Could you elaborate more on this since that one line of code works? Thanks! – JohnyL Dec 04 '20 at 11:25
  • I've updated the answer to point out the two main ways you can get a new CoreWebView2. We also need to update our .NET samples to show how to use NewWindowRequested. Thx! – David Risney Dec 04 '20 at 21:14
  • Now I get it! Thanks for explanation! – JohnyL Dec 05 '20 at 06:57
  • I apologize that I'm asking here, but is there way to get notifications from CoreWebView2 when HTML changes? :) I could create a new question and promise to mark your answer as accepted. The thing is that during time the page gets modified, and I want to receive brand-new HTML. Thanks! – JohnyL Dec 05 '20 at 09:47
  • 1
    There's no explicit WebView2 API for that. You can use ExecuteScriptAsync to inject JavaScript that uses MutationObserver https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver to detect whatever changes you like, and then use WebMessageReceived / chrome.webview.postMessage to post a message from script to native when the change occurs. Or if you mean when navigating to a new HTML document, you can use the NavigationStarting event. – David Risney Dec 07 '20 at 21:51
  • Thanks for a tip! I will take a look. ))) – JohnyL Dec 09 '20 at 05:52
  • maybe i missunderstand somthing in this c++ code. in C# NewWindowRequested event handler returns BEFORE m_onWebViewFirstInitialized event handler is called. any hints? – Kux Sep 29 '21 at 00:59