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

Unable to get callbacks via add_WebResourceRequested for WebView2 created via add_NewWindowRequested #616

Closed
pontusn opened this issue Nov 9, 2020 · 9 comments
Labels
bug Something isn't working tracked We are tracking this work internally.

Comments

@pontusn
Copy link

pontusn commented Nov 9, 2020

We use a callbackhook via add_WebResourceRequested for adding request headers and serving some content from local folders.

This works perfect until we try to use windows created via window.open.

Exactly the same code for hosting a second WebView2 in a separate dialog does not give any callbacks for requests when sent back via put_NewWindow. Other callbacks, like navigation and dev protocol, works perfect. Navigating works also, minus our headers and local content.

Attaching multiple callbacks via add_WebResourceRequested in our code yields similar behaviour. Our suspision is that (conflicting) callbacks are used internally.

Version
SDK: 1.0.622.22 (GA)
Runtime: GA
Framework: Win32
OS: Win10

@pontusn pontusn added the bug Something isn't working label Nov 9, 2020
@champnic champnic added the tracked We are tracking this work internally. label Nov 10, 2020
@champnic
Copy link
Member

Thanks for the report, I've added this bug to our backlog!

@yildirimcagri-msft
Copy link
Member

Hi, if I understood your issue correctly, I can see the WebResourceRequested events firing on a opened WebView so I cannot repro this. Are you calling AddWebResourceRequestedFilter separately and adding a separate WebResourceRequested handler on the new WebView that you created for the NewWindowRequested as well? Otherwise, it would be good to see some sample code from your side to understand the issue better.

@pontusn
Copy link
Author

pontusn commented Nov 17, 2020

We create a new controller and WebView2 window. The new window is setup separately with filter and callbacks. The callbacks for web requests are never called.

@pontusn
Copy link
Author

pontusn commented Nov 20, 2020

Did some more experiements and when I added this to NavigationComplete it works...

if (--m_nDialogRetry >= 0)
{
    // Magic? Reset filters and try again and if works...
    webview->RemoveWebResourceRequestedFilter(L"http://*",
        COREWEBVIEW2_WEB_RESOURCE_CONTEXT::COREWEBVIEW2_WEB_RESOURCE_CONTEXT_ALL);
    webview->RemoveWebResourceRequestedFilter(L"https://*",
        COREWEBVIEW2_WEB_RESOURCE_CONTEXT::COREWEBVIEW2_WEB_RESOURCE_CONTEXT_ALL);
    RETURN_IF_FAILED(webview->AddWebResourceRequestedFilter(L"http://*",
        COREWEBVIEW2_WEB_RESOURCE_CONTEXT::COREWEBVIEW2_WEB_RESOURCE_CONTEXT_ALL));
    RETURN_IF_FAILED(webview->AddWebResourceRequestedFilter(L"https://*",
        COREWEBVIEW2_WEB_RESOURCE_CONTEXT::COREWEBVIEW2_WEB_RESOURCE_CONTEXT_ALL));
    return webview->Reload();
}

@pontusn
Copy link
Author

pontusn commented Nov 22, 2020

Something is call order dependant. By accident I discovered that half-way in some refactoring the callback started working when I manually reloaded after initial failure.

Now I've a workaround where the callback for NavicationComplete reinstalls the filters (see example above) and then reloads. This workaround is protected by an internal counter to avoid potentially infinite loop

Maybe this information can help you pinpoint the underlying problem...

@champnic champnic added tracked We are tracking this work internally. and removed tracked We are tracking this work internally. labels Dec 1, 2020
@pontusn
Copy link
Author

pontusn commented Dec 6, 2020

Same workaround us now also used for adding host objects for window open.

@champnic
Copy link
Member

champnic commented Jun 5, 2021

This should be fixed in runtimes version 92.0.886.0+. Thanks!

@champnic champnic closed this as completed Jun 5, 2021
@Kay-Burchardt
Copy link

This should be fixed in runtimes version 92.0.886.0+. Thanks!

This still doesn't work. I tested it with both runtime 92.0.902.62 + SDK 1.0.902.49 and Edge Dev 93.0.961.10 + SDK 1.0.955-prerelease.

There are 3 major issues with the first navigation in a child window:

  1. WebResourceRequested doesn't fire for GET requests. It actually fires for POST requests, though!
  2. A script error "Uncaught SyntaxError: Identifier 'globalParams' has already been declared" occurs, preventing window.chrome.webview to be initialized. This is a severe issue, since it prevents access to the list of host objects.
  3. The script set with AddScriptToExecuteOnDocumentCreated is not executed.

Note that all 3 issues only occur during the initial navigation and are gone in subsequent documents (either through page reload or navigations to new locations.

Our scenarios depend on working around issues 2) and 3). If you fixed them, it would make my life a lot easier. However, if you finally fix issue 1), I could at least implement an elegant workaround for issues 2) and 3) by intercepting the web request for the first navigation and providing an artificial response containing a script which reloads the page.

@mukeshmishra996
Copy link

@Kay-Burchardt - can you please share details how you have fixed the 2nd issue. I am also facing same challenge with child window window.chrome.webview is not initialized. So, I am unable to communicate from java script to c#. Can you please help me on this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working tracked We are tracking this work internally.
Projects
None yet
Development

No branches or pull requests

5 participants