Starting to play with webView2 in Winforms Net 6. Is there a way to inject the jQuery library in the current page (which I don't control), so that I can later use jQuery when passing scripts to the page through ExecuteScriptAsync or add event listeners.
The AddScriptToExecuteOnDocumentCreatedAsync
event is what you need. You can find the documentation here: CoreWebView2.AddScriptToExecuteOnDocumentCreatedAsync(String)
First: Download jQuery and save it to disk.
Next, subscribe to the CoreWebView2InitializationCompleted
event:
private async void WebView21_CoreWebView2InitializationCompleted(object sender, Microsoft.Web.WebView2.Core.CoreWebView2InitializationCompletedEventArgs e)
{
string script = await File.ReadAllTextAsync(@"C:\path\jquery.js");
await webView21.CoreWebView2.AddScriptToExecuteOnDocumentCreatedAsync(script);
}
Now you will be able to call jquery functions using await ExecuteScriptAsync()
.