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

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

Open
BalassaMarton opened this issue Feb 14, 2024 · 3 comments
Open

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

BalassaMarton opened this issue Feb 14, 2024 · 3 comments
Assignees
Labels
feature request feature request

Comments

@BalassaMarton
Copy link

BalassaMarton commented Feb 14, 2024

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:

  • Remove all injected scripts in FrameCreated event handler
  • Cancel navigation in FrameNavigationStarting event handler, and start a sequence when I inject scripts and then navigate the frame - unfortunately there's no reference to the frame in the event args, so this is probably a dead end.
@BalassaMarton BalassaMarton added the feature request feature request label Feb 14, 2024
@vbryh-msft
Copy link
Contributor

Have you checked icorewebview2frame2::ExecuteScript ?

@BalassaMarton
Copy link
Author

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

@BalassaMarton
Copy link
Author

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.

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

No branches or pull requests

3 participants