]> git.ardis.lu - ArdisUtilities/commitdiff
Add Open-TemporaryBrowser
authorArdis Lu <ardisglu@gmail.com>
Sun, 16 Jun 2024 04:29:03 +0000 (21:29 -0700)
committerArdis Lu <ardisglu@gmail.com>
Sun, 16 Jun 2024 04:29:03 +0000 (21:29 -0700)
ArdisUtilities/ArdisUtilities.psd1
ArdisUtilities/ArdisUtilities.psm1

index 65f192d6809b4780a57fa17226ba10ad2765824a..9108994b32caa02f566bf15bf61a6de230222a56 100644 (file)
@@ -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   = @()
index d3847641c3515fe0803b3d81a0b236dbce33d4e6..13ac6355c8b2fb5551050dcaf45cf20579da4d53 100644 (file)
@@ -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
+  }
+}
This page took 0.04295 seconds and 4 git commands to generate.