<# .SYNOPSIS Launch Chrome with remote debugging enabled for CDP capture. .DESCRIPTION Starts Chrome with --remote-debugging-port so the CDP capture tool can connect and observe all browser activity. .PARAMETER Port Remote debugging port (default: 9222) .PARAMETER UseRealProfile Use your real Chrome profile (with logins, cookies, etc.) WARNING: Chrome must be fully closed first. .PARAMETER ProfileDir Custom profile directory path. Defaults to a separate profile if not specified. .PARAMETER Url Starting URL (default: about:blank) .EXAMPLE .\launch-chrome.ps1 Launches Chrome with a fresh profile on port 9222 .EXAMPLE .\launch-chrome.ps1 -UseRealProfile Launches Chrome with your real profile (closes existing Chrome first) .EXAMPLE .\launch-chrome.ps1 -Port 9223 -Url "https://example.com" Custom port and starting URL #> param( [int]$Port = 9222, [switch]$UseRealProfile, [string]$ProfileDir = "", [string]$Url = "about:blank" ) $chromePath = "C:\Program Files\Google\Chrome\Application\chrome.exe" if (-not (Test-Path $chromePath)) { # Try other common locations $altPaths = @( "$env:ProgramFiles\Google\Chrome\Application\chrome.exe", "$env:ProgramFiles(x86)\Google\Chrome\Application\chrome.exe", "$env:LOCALAPPDATA\Google\Chrome\Application\chrome.exe" ) foreach ($p in $altPaths) { if (Test-Path $p) { $chromePath = $p; break } } if (-not (Test-Path $chromePath)) { Write-Host "❌ Chrome not found!" -ForegroundColor Red exit 1 } } # Determine user data directory if ($UseRealProfile) { # Check if Chrome is running $chromeProcs = Get-Process chrome -ErrorAction SilentlyContinue if ($chromeProcs) { Write-Host "⚠️ Chrome is running. Closing it to use your real profile..." -ForegroundColor Yellow Stop-Process -Name chrome -Force -ErrorAction SilentlyContinue Start-Sleep -Seconds 2 } $userDataDir = "$env:LOCALAPPDATA\Google\Chrome\User Data" Write-Host "Using real profile: $userDataDir" -ForegroundColor Green } elseif ($ProfileDir -ne "") { $userDataDir = $ProfileDir } else { # Separate profile for capture sessions $userDataDir = "$env:LOCALAPPDATA\Google\Chrome\Capture-Profile" Write-Host "Using capture profile: $userDataDir" -ForegroundColor Cyan Write-Host "(Use -UseRealProfile for your real logins/cookies)" -ForegroundColor DarkGray } # Launch Chrome $chromeArgs = @( "--remote-debugging-port=$Port", "--user-data-dir=`"$userDataDir`"", "--no-first-run", "--no-default-browser-check", $Url ) Write-Host "" Write-Host "╔══════════════════════════════════════════════════╗" -ForegroundColor Cyan Write-Host "║ Chrome — Remote Debugging Enabled ║" -ForegroundColor Cyan Write-Host "╚══════════════════════════════════════════════════╝" -ForegroundColor Cyan Write-Host "" Write-Host " Port: $Port" -ForegroundColor White Write-Host " Profile: $userDataDir" -ForegroundColor White Write-Host " CDP URL: http://localhost:$Port" -ForegroundColor Green Write-Host "" Start-Process -FilePath $chromePath -ArgumentList $chromeArgs # Wait for CDP to be ready Write-Host "Waiting for CDP endpoint..." -NoNewline $ready = $false for ($i = 0; $i -lt 30; $i++) { Start-Sleep -Milliseconds 500 try { $response = Invoke-RestMethod -Uri "http://localhost:$Port/json/version" -ErrorAction Stop $ready = $true break } catch { Write-Host "." -NoNewline -ForegroundColor DarkGray } } if ($ready) { Write-Host " Ready!" -ForegroundColor Green Write-Host "" Write-Host "Chrome is running with CDP on port $Port" -ForegroundColor Green Write-Host "Now run the capture tool:" -ForegroundColor Cyan Write-Host " cd tools\scripts\cdp-capture" -ForegroundColor White Write-Host " node capture.js" -ForegroundColor White Write-Host "" Write-Host "Or with options:" -ForegroundColor Cyan Write-Host " node capture.js --screenshot --dom --tab=-1" -ForegroundColor White Write-Host "" } else { Write-Host " Timeout!" -ForegroundColor Red Write-Host "Chrome may not have started correctly." -ForegroundColor Red }