Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Problem/Bug]: WebResourceResponseReceived for a new window doesn't fire for initial navigation #4799

Open
pushkin- opened this issue Sep 9, 2024 · 0 comments
Assignees
Labels
bug Something isn't working

Comments

@pushkin-
Copy link

pushkin- commented Sep 9, 2024

What happened?

I'm trying to detect if a window (result of window.open(...)) contains a PDF.

When I create my WebView2 control, I add a WebResourceResponseReceived event handler, but it doesn't fire when the WebView2 finishes navigating (to the PDF or to another site)

I have found that the only wait to get it to fire is to cancel the initial navigation and navigate it myself via CoreWebView2::Navigate(...) (code below)

Importance

Moderate. My app's user experience is affected, but still usable.

Runtime Channel

Stable release (WebView2 Runtime)

Runtime Version

128.0.2739.67

SDK Version

1.0.2535.41

Framework

Winforms

Operating System

Windows 11

OS Version

22631.4037

Repro steps

If you open the WinForms sample and:
In BrowserForm.cs::WebView2Control_CoreWebView2InitializationCompleted, add this.webView2Control.CoreWebView2.NewWindowRequested += CoreWebView2_NewWindowRequested;

	private async void CoreWebView2_NewWindowRequested(object sender, CoreWebView2NewWindowRequestedEventArgs e)
	{
		e.Handled = true;
		using (e.GetDeferral())
		{
			Form form = new Form();
			WebView2 webView = new WebView2();
			await webView.EnsureCoreWebView2Async();
			form.Controls.Add(webView);
			form.Visible = true;
			webView.Dock = DockStyle.Fill;
			webView.CoreWebView2.WebResourceResponseReceived += CoreWebView2_WebResourceResponseReceived;
			webView.CoreWebView2.NavigationCompleted += CoreWebView2_NavigationCompleted;
			e.NewWindow = webView.CoreWebView2;
		}
	}

	private void CoreWebView2_NavigationCompleted(object sender, CoreWebView2NavigationCompletedEventArgs e)
	{
		// this fires
	}

	private void CoreWebView2_WebResourceResponseReceived(object sender, CoreWebView2WebResourceResponseReceivedEventArgs e)
	{
		bool loadingPDF = e.Response.StatusCode == 200 && e.Response.Headers.Contains("Content-Type") && e.Response.Headers.GetHeader("Content-Type") == "application/pdf";
		// doesn't fire
	}
  1. start app
  2. open devtools
  3. window.open("https://google.com");
  4. the webresourceResponseReceived handler doesn't fire

For PDFs specifically, you can open a PDF to, say, https://www.sos.state.tx.us/corp/status-example.pdf

I have found that if I cancel the initial navigation and navigate it myself, it work fine. So in CoreWebView2_NewWindowRequested, add webView.CoreWebView2.NavigationStarting +=(sender2, e2) => CoreWebView2_NavigationStarting(webView, e2); and then add:

	private Dictionary<WebView2, bool> _initialNavigationStopped = new Dictionary<WebView2, bool>();

	private void CoreWebView2_NavigationStarting(WebView2 webView, CoreWebView2NavigationStartingEventArgs e)
	{
		if (!_initialNavigationStopped.ContainsKey(webView))
		{
			_initialNavigationStopped[webView] = true;
			webView.CoreWebView2.WebResourceResponseReceived += CoreWebView2_WebResourceResponseReceived;

			e.Cancel = true;
			Task.Run(() =>
			{
				webView.Invoke(new Action(() => { webView.CoreWebView2.Navigate(e.Uri); }));
			});
		}
	}

Then, the WebResourceResponseReceived handler fires and loadingPDF is true.

Repros in Edge Browser

No, issue does not reproduce in the corresponding Edge version

Regression

No, this never worked

Last working version (if regression)

No response

@pushkin- pushkin- added the bug Something isn't working label Sep 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants