Sekers

PowerShell Microsoft Edge WebView2 Example

Jun 10th, 2022 (edited)
2,134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # TEMPROARY ITEMS FOR DEBUGGING USE ONLY
  2. New-Variable -Name 'sky_api_user_data_path' -Value "$([Environment]::GetEnvironmentVariable('LOCALAPPDATA'))\SKYAPI PowerShell" -Scope Global -Force
  3. $Url = "https://www.google.com"
  4.  
  5. # ACTUAL CODE BELOW
  6.  
  7. # Set EdgeWebView2 Control Version to Use
  8. $EdgeWebView2Control_VersionNumber = '1.0.1210.39'
  9. switch ($PSVersionTable.PSEdition)
  10. {
  11.     Desktop {$EdgeWebView2Control_DotNETVersion = "net45"}
  12.     Core {$EdgeWebView2Control_DotNETVersion = "netcoreapp3.0"}
  13.     Default {$EdgeWebView2Control_DotNETVersion = "netcoreapp3.0"}
  14. }
  15. switch ([System.Environment]::Is64BitProcess)
  16. {
  17.     $true {$EdgeWebView2Control_OSArchitecture = "win-x64"}
  18.     $false {$EdgeWebView2Control_OSArchitecture = "win-x86"}
  19.     Default {$EdgeWebView2Control_OSArchitecture = "win-x64"}
  20. }
  21.  
  22. # Update $AuthenticationMethod Variable (not currently needed but is useful to have in a variable)
  23. $AuthenticationMethod = "EdgeWebView2"
  24.  
  25. # Load Assemblies
  26. Add-Type -AssemblyName System.Windows.Forms
  27.  
  28. # Note, you also need the following two files in the same folder as "Microsoft.Web.WebView2.WinForms.dll":
  29. # - Microsoft.Web.WebView2.Core.dll
  30. # - WebView2Loader.dll
  31. Add-Type -Path "$PSScriptRoot\Dependencies\Microsoft.Web.WebView2\$EdgeWebView2Control_VersionNumber\$EdgeWebView2Control_DotNETVersion\$EdgeWebView2Control_OSArchitecture\Microsoft.Web.WebView2.WinForms.dll"
  32.  
  33. $form = New-Object -TypeName System.Windows.Forms.Form -Property @{Width=600;Height=800}
  34. $WebView2 = New-Object -TypeName Microsoft.Web.WebView2.WinForms.WebView2
  35.  
  36. $WebView2.CreationProperties = New-Object -TypeName 'Microsoft.Web.WebView2.WinForms.CoreWebView2CreationProperties'
  37. $WebView2.CreationProperties.UserDataFolder = $sky_api_user_data_path
  38.  
  39. # Clear WebView2 cache in the previously specified UserDataFolder
  40. # TODO For now this is just hardcoded as deleting the folder... Need to figure out how to clear the user data folder using the WebView2 Control
  41. if ($ClearBrowserControlCache)
  42. {
  43.     Remove-Item "$($WebView2.CreationProperties.UserDataFolder)\EBWebView\Default" -Force -Recurse
  44.     $ClearBrowserControlCache = $false
  45. }
  46.  
  47. $WebView2.Source = $Url
  48. $WebView2.Size = New-Object System.Drawing.Size(584, 760)
  49.  
  50. # Set Event Handlers. See APIs here: https://github.com/MicrosoftEdge/WebView2Browser#webview2-apis
  51. $WebView2_NavigationCompleted = {
  52.     # Write-Host $($WebView2.Source.AbsoluteUri) # DEBUG LINE
  53.     if ($WebView2.Source.AbsoluteUri -match "error=[^&]*|$([regex]::escape($redirect_uri))*")
  54.     {
  55.         $form.Close()
  56.     }
  57. }
  58. $WebView2.add_NavigationCompleted($WebView2_NavigationCompleted)
  59.  
  60. # Add WebView2 Control to the Form and Show It
  61. $form.Controls.Add($WebView2)
  62. $form.Add_Shown({$form.Activate()})
  63. $form.ShowDialog() | Out-Null
  64.  
  65. # Parse Return URL
  66. $queryOutput = [System.Web.HttpUtility]::ParseQueryString($WebView2.Source.Query)
  67. $output = @{}
  68. foreach($key in $queryOutput.Keys){
  69.     $output["$key"] = $queryOutput[$key]
  70. }
  71.  
  72. # Dispose Form & Webview2 Control
  73. $WebView2.Dispose()
  74. $form.Dispose()
Advertisement
Add Comment
Please, Sign In to add comment