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

Requests from content in <iframe> do not raise CoreWebView2OnWebResourceRequested #2341

Open
RendijsSmukulis opened this issue Apr 6, 2022 · 19 comments
Labels
feature request feature request tracked We are tracking this work internally.

Comments

@RendijsSmukulis
Copy link

RendijsSmukulis commented Apr 6, 2022

Description

With filters and handler set:

            this.WebView2.CoreWebView2.AddWebResourceRequestedFilter("*", CoreWebView2WebResourceContext.All);
            this.WebView2.CoreWebView2.WebResourceRequested += this.CoreWebView2OnWebResourceRequested;

Loading a page that has an iframe, CoreWebView2OnWebResourceRequested callback is called for the web request that loads the main iframe page, but not for requests coming from said page residing in iframe.

For example, if page B is hosted in page A's iframe, and page B contains script C.js, we see calls for A and B in CoreWebView2OnWebResourceRequested, but not for C.js.

Version
SDK: 1.0.1150.38 (newest release)
Runtime: Evergreen
Framework: WPF
OS: Windows 10

Repro Steps

MainWindow.xaml:

<Window x:Class="tbd_webview2_iframe_test.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:tbd_webview2_iframe_test"
        xmlns:wpf="clr-namespace:Microsoft.Web.WebView2.Wpf;assembly=Microsoft.Web.WebView2.Wpf"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <wpf:WebView2 Source="http://localhost:8000" Name="WebView2"></wpf:WebView2>
    </Grid>
</Window>

MainWindow.xaml.cs:

namespace tbd_webview2_iframe_test
{
    using System.Diagnostics;
    using System.Windows;
    using Microsoft.Web.WebView2.Core;

    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            this.InitializeComponent();
            this.WebView2.CoreWebView2InitializationCompleted += this.WebView2OnCoreWebView2InitializationCompleted;
        }

        private void WebView2OnCoreWebView2InitializationCompleted(object? sender, CoreWebView2InitializationCompletedEventArgs e)
        {
            this.WebView2.CoreWebView2.AddWebResourceRequestedFilter("*", CoreWebView2WebResourceContext.All);
            this.WebView2.CoreWebView2.WebResourceRequested += this.CoreWebView2OnWebResourceRequested;
        }

        private void CoreWebView2OnWebResourceRequested(object? sender, CoreWebView2WebResourceRequestedEventArgs e)
        {
            Debug.WriteLine($"+++> {e.Request.Uri}");
        }
    }
}

Set up a page under "localhost:8000":

<!DOCTYPE html>
<html>
<body>
    <iframe src="https://www.bing.com"></iframe>
</body>
</html>

Run and observe the debug output.

Expected to see all "www.microsoft.com" page resources shown in debug, instead only the first call to "www.bing.com" shows up.

AB#38876392

@RendijsSmukulis RendijsSmukulis added the bug Something isn't working label Apr 6, 2022
@champnic champnic added feature request feature request tracked We are tracking this work internally. and removed bug Something isn't working labels Apr 6, 2022
@champnic
Copy link
Member

champnic commented Apr 6, 2022

Thanks for this report @RendijsSmukulis! We were able to repro this issue. It will likely be fixed by implementing the WebResourceRequested event on the CoreWebView2Frame object.

@b-maslennikov
Copy link

Any progress on this?

@vbryh-msft
Copy link
Contributor

vbryh-msft commented Nov 30, 2022

@b-maslennikov webresourcerequested events for oop iframes are available as part of new API in prerelease. AddWebResourceRequestedFilter(String, CoreWebView2WebResourceContext, CoreWebView2WebResourceRequestSourceKinds) You will need to use Document as third parameter.

@b-maslennikov
Copy link

@vbryh-msft works perfect for me. thank you

@Urmeli0815
Copy link

Urmeli0815 commented Jan 10, 2023

@vbryh-msft in my app I currently use AddWebResourceRequestedFilter(L"*", COREWEBVIEW2_WEB_RESOURCE_CONTEXT_ALL) to capture all requests, but I want to keep the old behavior where the WebResourceRequested is not raised for iframes. Will this still work with the new API?

@vbryh-msft
Copy link
Contributor

@Urmeli0815 - the old API should have old behavior, please let us know if it is not the case for you. Thanks.

@xdewx
Copy link

xdewx commented May 4, 2023

how to use the new api in tauri?

@ray007
Copy link

ray007 commented Aug 31, 2023

With the 3rd parameter to AddWebResourceRequestedFilter() this now work for the WebResourceRequested event handler.
But it does not work for webView.CoreWebView2.SetVirtualHostNameToFolderMapping(...).

Should I also mention this elsewhere or make a new report for it?

@vbryh-msft
Copy link
Contributor

@ray007 for virtual host mapping web resource requests are not fired - we have it documented here

@ray007
Copy link

ray007 commented Sep 13, 2023

@vbryh-msft I know, that's not what I meant:

I have an iframe that gets redirected from an external url to an internal url in the .host domain.
This load fails when serving the host in question with virtual host mapping, while setting the url as iframe.src does work.

So I had to drop the virtual host mapping and now serve those files as well from the WebResourceRequested event handler.

@vbryh-msft
Copy link
Contributor

@ray007 I see. Yes, please open new issue for that. If you will provide simple sample code - it will save us some time. Just setting iframe.src to mapped virtual host works but redirection to it from external url does not work - is it correct?

@ray007
Copy link

ray007 commented Sep 14, 2023

@vbryh-msft yes, correct. In my case it's a login form shown in an iframe which redirects back to the js-app served from my internal url.

@harunurhan
Copy link

@vbryh-msft @champnic, when do you plan to release CoreWebView2WebResourceRequestSourceKinds as a regular version, it seems like it's only available on prerelease versions

@baseline808
Copy link

@vbryh-msft @champnic I am also very interested in the overload for CoreWebView2.AddWebResourceRequestedFilter that takes in the third parameter making it into the release build.

CoreWebView2.AddWebResourceRequestedFilter("*", CoreWebView2WebResourceContext.All, CoreWebView2WebResourceRequestSourceKinds.All);

Without that third parameter, my WebResourceRequested handler is not getting hit for iframes.

@RossLote
Copy link

RossLote commented Feb 4, 2024

Why has this not been released yet?

@vbryh-msft
Copy link
Contributor

@RossLote it is promoted to stable in 122 prerelease .

@lordbendtner1337
Copy link

@RossLote it is promoted to stable in 122 prerelease .

If you try to query the interface it will return HRESULT 80004002 and obviously the queried pointer is NULL.
ICoreWebView2_22* webviewWindow = NULL; HRESULT hRes = webviewWindowtemp->QueryInterface( &webviewWindow );

This is using 1.0.2357-prerelease

@champnic
Copy link
Member

@lordbendtner1337 You need to make sure you are testing with a runtime that supports that interface. So anything .2357+. The latest Edge Canary or Dev would work for this API. Here's some more info:
https://learn.microsoft.com/en-us/microsoft-edge/webview2/how-to/set-preview-channel

@GGGKZ
Copy link

GGGKZ commented Apr 16, 2024

IOS端bug就是严重发热,希望更新一下解决IOS发热问题!
There is a bug on the IOS side, which is a serious heating situation during the operation, and the next update hopes to solve the serious heating problem on the iOS side, and fix it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request feature request tracked We are tracking this work internally.
Projects
None yet
Development

No branches or pull requests