From 9a354d5c8f70213910dc7780081a8d3021ce895a Mon Sep 17 00:00:00 2001 From: Ardis Lu Date: Sat, 15 Jun 2024 21:29:03 -0700 Subject: [PATCH] Add Open-TemporaryBrowser --- ArdisUtilities/ArdisUtilities.psd1 | 2 +- ArdisUtilities/ArdisUtilities.psm1 | 73 ++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+), 1 deletion(-) diff --git a/ArdisUtilities/ArdisUtilities.psd1 b/ArdisUtilities/ArdisUtilities.psd1 index 65f192d..9108994 100644 --- a/ArdisUtilities/ArdisUtilities.psd1 +++ b/ArdisUtilities/ArdisUtilities.psd1 @@ -4,7 +4,7 @@ GUID = '57a6bea3-0ea5-44b3-95d2-71d122772f1c' Author = 'Ardis Lu' Description = 'My personal utility functions.' - FunctionsToExport = @('Invoke-RandomFile', 'ConvertTo-Base64String', 'Get-StringHash', 'Get-SizeDurationRatio', 'Get-ExtendedFileProperty', 'Get-DocumentationIP', 'Get-DocumentationPhoneNumber', 'Test-TcpPort', 'Get-HelpAsMarkdown', 'Open-ParentFolder', 'Get-SubresourceIntegrity') + FunctionsToExport = @('Invoke-RandomFile', 'ConvertTo-Base64String', 'Get-StringHash', 'Get-SizeDurationRatio', 'Get-ExtendedFileProperty', 'Get-DocumentationIP', 'Get-DocumentationPhoneNumber', 'Test-TcpPort', 'Get-HelpAsMarkdown', 'Open-ParentFolder', 'Get-SubresourceIntegrity', 'Open-TemporaryBrowser') CmdletsToExport = @() VariablesToExport = @() AliasesToExport = @() diff --git a/ArdisUtilities/ArdisUtilities.psm1 b/ArdisUtilities/ArdisUtilities.psm1 index d384764..13ac635 100644 --- a/ArdisUtilities/ArdisUtilities.psm1 +++ b/ArdisUtilities/ArdisUtilities.psm1 @@ -952,3 +952,76 @@ function Get-SubresourceIntegrity { } } } + +function Open-TemporaryBrowser { + <# + .SYNOPSIS + Opens a new web browser instance with a temporary profile. Optionally passes flags to the temporary instance. + + .DESCRIPTION + Finds the full path to the web browser, then passes a temporary profile to open a new isolated instance of the application. + + .PARAMETER Browser + The web browser to open. Must be 'Brave', 'Chrome', or 'Edge'. Defaults to 'Chrome'. + + .PARAMETER CORS + Pass the --disable-web-security flag to disable the same-origin policy, which disables CORS restrictions. + + .EXAMPLE + PS> Open-TemporaryBrowser -CORS + + Opens a new isolated instance of Chrome with the same-origin policy disabled. + #> + + [OutputType([System.Void])] + [CmdletBinding()] + param( + [ValidateSet('Brave', 'Chrome', 'Edge')] + [String]$Browser = 'Chrome', + + [Switch]$CORS + ) + + begin { + # Retrieve full paths to Chromium browsers + # Check the registry key for the Start Menu executable, then the key for the Win + R executable, then fallback to hardcoded default path + # Wrapping in try/catch is required because -ErrorAction is ignored for terminating errors + foreach ($key in @('Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Clients\StartMenuInternet\Brave\shell\open\command', 'Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\brave.exe')) { + try { + $bravePath ??= Get-ItemPropertyValue -LiteralPath $key -Name '(default)' + } + catch {} + } + $bravePath ??= 'C:\Program Files\BraveSoftware\Brave-Browser\Application\brave.exe' + + foreach ($key in @('Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Clients\StartMenuInternet\Google Chrome\shell\open\command', 'Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\chrome.exe')) { + try { + $chromePath ??= Get-ItemPropertyValue -LiteralPath $key -Name '(default)' + } + catch {} + } + $chromePath ??= 'C:\Program Files (x86)\Google\Chrome Dev\Application\chrome.exe' + + foreach ($key in @('Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Clients\StartMenuInternet\Microsoft Edge\shell\open\command', 'Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\msedge.exe')) { + try { + $edgePath ??= Get-ItemPropertyValue -LiteralPath $key -Name '(default)' + } + catch {} + } + $edgePath ??= 'C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe' + } + + process { + switch ($Browser) { + 'Brave' { $execPath = $bravePath } + 'Chrome' { $execPath = $chromePath } + 'Edge' { $execPath = $edgePath } + } + + $temp = Join-Path $Env:Temp $(New-Guid) + + # Directly using the call expression (&) will not work, must either wrap it inside Invoke-Expression or use System.Diagnostics.Process::Start + # Invoke-Expression "& $execPath --user-data-dir=`"$temp`" $(if ($CORS) {'--disable-web-security'})" + [System.Diagnostics.Process]::Start($execPath, "--user-data-dir=`"$temp`" $(if ($CORS) {'--disable-web-security'})") | Out-Null + } +} -- 2.47.3