Functions/GenXdev.Webbrowser/Get-ChromiumRemoteDebuggingPort.ps1

<##############################################################################
Part of PowerShell module : GenXdev.Webbrowser
Original cmdlet filename : Get-ChromiumRemoteDebuggingPort.ps1
Original author : René Vaessen / GenXdev
Version : 1.300.2025
################################################################################
Copyright (c) René Vaessen / GenXdev
 
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
 
    http://www.apache.org/licenses/LICENSE-2.0
 
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
################################################################################>

###############################################################################
<#
.SYNOPSIS
Returns the remote debugging port for the system's default Chromium browser.
 
.DESCRIPTION
Detects whether Microsoft Edge or Google Chrome is the default browser and
returns the appropriate debugging port number. If Chrome is the default browser,
returns the Chrome debugging port. Otherwise returns the Edge debugging port
(also used when no default browser is detected).
 
.OUTPUTS
[int] The remote debugging port number for the detected browser.
 
.EXAMPLE
Get debugging port using full command name
Get-ChromiumRemoteDebuggingPort
 
.EXAMPLE
Get debugging port using alias
Get-BrowserDebugPort
#>

function Get-ChromiumRemoteDebuggingPort {

    [CmdletBinding()]
    [OutputType([int])]
    param(

        [switch] $Chrome,
        [switch] $Edge
    )
    ############################################################################

    begin {
        # verbose output to indicate start of browser detection
        Microsoft.PowerShell.Utility\Write-Verbose 'Starting detection of default Chromium browser type'

        # get the system's default browser information
        $defaultBrowser = GenXdev.Webbrowser\Get-DefaultWebbrowser

        # log the detected default browser name
        Microsoft.PowerShell.Utility\Write-Verbose ('Default browser detected: {0}' -f `
            $(if ($null -eq $defaultBrowser) { 'None' }
                else { $defaultBrowser.Name }))
    }


    process {

        if ($Chrome) {
            # return chrome debugging port
            Microsoft.PowerShell.Utility\Write-Verbose 'Using Chrome debugging port'
            GenXdev.Webbrowser\Get-ChromeRemoteDebuggingPort
            return;
        }

        if ($Edge) {
            # return edge debugging port
            Microsoft.PowerShell.Utility\Write-Verbose 'Using Edge debugging port'
            GenXdev.Webbrowser\Get-EdgeRemoteDebuggingPort
            return;
        }

        # determine and return appropriate debugging port based on browser
        if (($null -ne $defaultBrowser) -and
            ($defaultBrowser.Name -like '*Chrome*')) {

            # chrome is default - return chrome debugging port
            Microsoft.PowerShell.Utility\Write-Verbose 'Using Chrome debugging port'
            GenXdev.Webbrowser\Get-ChromeRemoteDebuggingPort
        }
        else {

            # edge is default or no browser - return edge debugging port
            Microsoft.PowerShell.Utility\Write-Verbose 'Using Edge debugging port'
            GenXdev.Webbrowser\Get-EdgeRemoteDebuggingPort
        }
    }

    end {
    }
}