[Console]::OutputEncoding = [System.Text.Encoding]::UTF8
$ErrorActionPreference = 'Continue'

$userDataDir = "C:\ChromeDebug_BJH"

try {
    $null = [System.IO.Directory]::CreateDirectory($userDataDir)
    Write-Host "[DIR] Created/confirmed: $userDataDir"
} catch {
    Write-Host "[DIR] Using existing: $userDataDir"
}

$acl = Get-Acl $userDataDir -ErrorAction SilentlyContinue
if ($acl) {
    Write-Host "[DIR] ACL owner: $($acl.Owner)"
}

$chrome = "C:\Program Files\Google\Chrome\Application\chrome.exe"
$argList = @(
    "--remote-debugging-port=9223",
    "--user-data-dir=$userDataDir",
    "--no-first-run",
    "--no-default-browser-check",
    "--disable-crash-reporter",
    "--noerrdialogs",
    "https://baijiahao.baidu.com"
)

Write-Host "[LAUNCH] Chrome..."
$proc = Start-Process -FilePath $chrome -ArgumentList $argList -PassThru
Write-Host "[LAUNCH] PID: $($proc.Id)"
Start-Sleep 7

$check = Get-Process -Id $proc.Id -ErrorAction SilentlyContinue
if ($check) {
    Write-Host "[OK] Chrome running - Window: $($check.MainWindowTitle)"
} else {
    Write-Host "[FAIL] ExitCode: $($proc.ExitCode)"
}
