Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # TEMPROARY ITEMS FOR DEBUGGING USE ONLY
- New-Variable -Name 'sky_api_user_data_path' -Value "$([Environment]::GetEnvironmentVariable('LOCALAPPDATA'))\SKYAPI PowerShell" -Scope Global -Force
- $Url = "https://www.google.com"
- # ACTUAL CODE BELOW
- # Set EdgeWebView2 Control Version to Use
- $EdgeWebView2Control_VersionNumber = '1.0.1210.39'
- switch ($PSVersionTable.PSEdition)
- {
- Desktop {$EdgeWebView2Control_DotNETVersion = "net45"}
- Core {$EdgeWebView2Control_DotNETVersion = "netcoreapp3.0"}
- Default {$EdgeWebView2Control_DotNETVersion = "netcoreapp3.0"}
- }
- switch ([System.Environment]::Is64BitProcess)
- {
- $true {$EdgeWebView2Control_OSArchitecture = "win-x64"}
- $false {$EdgeWebView2Control_OSArchitecture = "win-x86"}
- Default {$EdgeWebView2Control_OSArchitecture = "win-x64"}
- }
- # Update $AuthenticationMethod Variable (not currently needed but is useful to have in a variable)
- $AuthenticationMethod = "EdgeWebView2"
- # Load Assemblies
- Add-Type -AssemblyName System.Windows.Forms
- # Note, you also need the following two files in the same folder as "Microsoft.Web.WebView2.WinForms.dll":
- # - Microsoft.Web.WebView2.Core.dll
- # - WebView2Loader.dll
- Add-Type -Path "$PSScriptRoot\Dependencies\Microsoft.Web.WebView2\$EdgeWebView2Control_VersionNumber\$EdgeWebView2Control_DotNETVersion\$EdgeWebView2Control_OSArchitecture\Microsoft.Web.WebView2.WinForms.dll"
- $form = New-Object -TypeName System.Windows.Forms.Form -Property @{Width=600;Height=800}
- $WebView2 = New-Object -TypeName Microsoft.Web.WebView2.WinForms.WebView2
- $WebView2.CreationProperties = New-Object -TypeName 'Microsoft.Web.WebView2.WinForms.CoreWebView2CreationProperties'
- $WebView2.CreationProperties.UserDataFolder = $sky_api_user_data_path
- # Clear WebView2 cache in the previously specified UserDataFolder
- # 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
- if ($ClearBrowserControlCache)
- {
- Remove-Item "$($WebView2.CreationProperties.UserDataFolder)\EBWebView\Default" -Force -Recurse
- $ClearBrowserControlCache = $false
- }
- $WebView2.Source = $Url
- $WebView2.Size = New-Object System.Drawing.Size(584, 760)
- # Set Event Handlers. See APIs here: https://github.com/MicrosoftEdge/WebView2Browser#webview2-apis
- $WebView2_NavigationCompleted = {
- # Write-Host $($WebView2.Source.AbsoluteUri) # DEBUG LINE
- if ($WebView2.Source.AbsoluteUri -match "error=[^&]*|$([regex]::escape($redirect_uri))*")
- {
- $form.Close()
- }
- }
- $WebView2.add_NavigationCompleted($WebView2_NavigationCompleted)
- # Add WebView2 Control to the Form and Show It
- $form.Controls.Add($WebView2)
- $form.Add_Shown({$form.Activate()})
- $form.ShowDialog() | Out-Null
- # Parse Return URL
- $queryOutput = [System.Web.HttpUtility]::ParseQueryString($WebView2.Source.Query)
- $output = @{}
- foreach($key in $queryOutput.Keys){
- $output["$key"] = $queryOutput[$key]
- }
- # Dispose Form & Webview2 Control
- $WebView2.Dispose()
- $form.Dispose()
Advertisement
Add Comment
Please, Sign In to add comment