MicrosoftEdge / WebView2Feedback

Feedback and discussions about Microsoft Edge WebView2
https://aka.ms/webview2
425 stars 51 forks source link

[Feature]: Control script injection for individual frames #4375

Open BalassaMarton opened 5 months ago

BalassaMarton commented 5 months ago

Describe the feature/enhancement you need

I'd like to be able to inject different scripts into individual frames. An AddScriptToExecuteOnDocumentCreatedAsync method for CoreWebView2Frame might be sufficient. I'm using WebView2 in a .NET WPF app via the Microsoft.Web.WebView2 package.

The scenario/use case where you would use this feature

I'm injecting scripts that contain a subscriber ID and access token for the page, as well as JS code that uses this information for connecting to a backend service. However, I want the scripts to be different for each iframe because I want to treat them as unique subscribers. Some scripts I want to inject to the top level frame only, to avoid information leakage in case that top level frame loads some external page. The behavior and API for injecting scripts should be the same for windows created with window.open() and iframes.

How important is this request to you?

Impactful. My app's user experience would be significantly compromised without it.

Suggested implementation

No response

What does your app do? Is there a pending deadline for this request?

I'm currently experimenting with various WV2 methods and events, my working theory is this:

vbryh-msft commented 5 months ago

Have you checked icorewebview2frame2::ExecuteScript ?

BalassaMarton commented 5 months ago

Sorry I forgot to add this information: I'm on .NET, ExecuteScript only has an asynchronous wrapper.

BalassaMarton commented 5 months ago

I did a super hacky workaround where the injected script fetches the actual scripts from a host object and runs them with eval... But I'm not sure about eval's limitations.