Skip to content

Extensions to allow easier coding and debugging of ExecuteScriptAsync in WebView2

License

Notifications You must be signed in to change notification settings

ukandrewc/WebView2.Winforms.JSHelper

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 

Repository files navigation

WebView2.Winforms.JSHelper

Extensions to allow easier coding and debugging of ExecuteScriptAsync in WebView2

The inspiration was to try and get as close as possible to inline editing of javascript for executing in WebView2.

The library allows you to execute scripts from file in debug, and as from resources in release. When in release it will execute a minfied version from resources, if there is one.

In debug you can then edit your scripts in a decent editor instead of strings or resources, and without having to recompile to update the script.

The library also includes a RunSync function and method, as an extension of Task to run tasks synchronously.

Usage

  • Add your .js scripts to a folder in your project.
  • Add the same file (or minified version) to your project as resource.

If you want to minify your script, then add the minified file to your resources with _min appended to the name.

image

Execute your script using the ExecuteScriptResourceAsync or ExecuteScriptResourceSync

e.g.

Res = Web.CoreWebView2.ExecuteScriptResourceSync(relative_path)

Task = Web.CoreWebView2.ExecuteScriptResourceAsync(relative_path)

Private Sub TestFrm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
	Web.EnsureCoreWebView2Async.RunSync
	Web.CoreWebView2.SetResourceManager(My.Resources.ResourceManager)
	Web.CoreWebView2.Navigate("https://w3.org/WAI/UA/2002/06/thead-test")
End Sub

Private Sub Web_NavigationCompleted(sender As Object, e As CoreWebView2NavigationCompletedEventArgs) Handles Web.NavigationCompleted
	Web.CoreWebView2.ExecuteScriptResourceSync("Script\test.js")
End Sub

When in debug, this will load the script from the named relative_path. In release, it will load the resource with the same name or minified version, if there is one.

Debugging

When the DevTools window is open, adding the command debugger, to you script will pause the script at that line. You can then inspect and set other breakpoints in your script.

About

Extensions to allow easier coding and debugging of ExecuteScriptAsync in WebView2

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published