-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbrowser.ps1
More file actions
111 lines (90 loc) · 3.25 KB
/
Copy pathbrowser.ps1
File metadata and controls
111 lines (90 loc) · 3.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
param(
[switch]$Headless
)
Set-StrictMode -Version Latest
$ErrorActionPreference = 'Stop'
Set-Location -Path $PSScriptRoot
$port = 9222
$windowSize = '1920,1080'
$profileRoot = [System.IO.Path]::GetTempPath()
$profileDir = Join-Path -Path $profileRoot -ChildPath ("chrome-profile-" + [System.Guid]::NewGuid().ToString('N'))
$logPath = Join-Path -Path $profileRoot -ChildPath 'chrome.log'
$errorLogPath = Join-Path -Path $profileRoot -ChildPath 'chrome-error.log'
if (Test-Path -Path $logPath) {
Remove-Item -Path $logPath -Force
}
if (Test-Path -Path $errorLogPath) {
Remove-Item -Path $errorLogPath -Force
}
New-Item -ItemType Directory -Path $profileDir -Force | Out-Null
function Get-ChromePath {
$candidatePaths = @(
(Join-Path -Path $env:ProgramFiles -ChildPath 'Google\Chrome\Application\chrome.exe'),
(Join-Path -Path ${env:ProgramFiles(x86)} -ChildPath 'Google\Chrome\Application\chrome.exe'),
'chrome.exe'
)
foreach ($path in $candidatePaths) {
if ($path -and (Test-Path -Path $path)) {
return (Resolve-Path -Path $path).Path
}
}
throw 'Chrome executable not found. Install Google Chrome or add it to PATH.'
}
$chromePath = Get-ChromePath
Write-Host 'Updating Chrome instances...'
Get-Process -Name 'chrome' -ErrorAction SilentlyContinue | Stop-Process -Force -ErrorAction SilentlyContinue
if ($Headless) {
Write-Host "Starting Chrome in headless mode with profile $profileDir..."
} else {
Write-Host "Starting Chrome in full screen with profile $profileDir..."
}
$chromeArgs = @(
"--remote-debugging-port=$port",
'--remote-debugging-address=0.0.0.0',
"--user-data-dir=$profileDir",
'--no-first-run',
'--no-default-browser-check',
'--disable-extensions',
'--disable-translate',
'--disable-features=TranslateUI,Translate',
'--force-device-scale-factor=1',
'--disable-geolocation',
'--use-fake-ui-for-media-stream',
"--window-size=$windowSize",
'--enable-logging',
'--v=1'
)
if ($Headless) {
$chromeArgs += '--headless=new'
} else {
$chromeArgs += '--start-fullscreen'
}
$chromeProcess = Start-Process -FilePath $chromePath -ArgumentList $chromeArgs -PassThru -RedirectStandardOutput $logPath -RedirectStandardError $errorLogPath
Write-Host "Chrome PID: $($chromeProcess.Id)"
Write-Host 'Waiting for Chrome DevTools endpoint...'
$endpoint = $null
$pattern = [regex]'ws://[^\s]+'
$timeout = [TimeSpan]::FromSeconds(30)
$stopwatch = [System.Diagnostics.Stopwatch]::StartNew()
while (-not $endpoint -and $stopwatch.Elapsed -lt $timeout) {
$combinedContent = ''
if (Test-Path -Path $logPath) {
$combinedContent += (Get-Content -Path $logPath -Raw -ErrorAction SilentlyContinue)
}
if (Test-Path -Path $errorLogPath) {
$combinedContent += (Get-Content -Path $errorLogPath -Raw -ErrorAction SilentlyContinue)
}
if ($combinedContent) {
$match = $pattern.Match($combinedContent)
if ($match.Success) {
$endpoint = $match.Value
break
}
}
Start-Sleep -Milliseconds 500
}
if ($endpoint) {
Write-Host "DevTools endpoint: $endpoint"
} else {
Write-Warning "DevTools endpoint not found within $($timeout.TotalSeconds) seconds. Inspect $logPath for details."
}