feat: cdp_wait_load y mejoras en CDP connect/launch
Nueva función cdp_wait_load para esperar carga completa de página. CdpConnect ahora soporta host remoto via CdpConnectHost (útil para WSL2 donde Chrome Windows escucha en IP distinta). Mejoras en chrome_launch para configuración más flexible. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -16,6 +16,8 @@ type ChromeLaunchOpts struct {
|
||||
UserDataDir string
|
||||
// Headless activa el modo headless (--headless=new). Por defecto false.
|
||||
Headless bool
|
||||
// ChromePath es la ruta al ejecutable de Chrome. Si esta vacio, se busca automaticamente.
|
||||
ChromePath string
|
||||
// ExtraArgs permite pasar flags adicionales a Chrome.
|
||||
ExtraArgs []string
|
||||
}
|
||||
@@ -45,9 +47,13 @@ func findChrome() (string, error) {
|
||||
}
|
||||
|
||||
// waitCDPReady espera hasta que el puerto CDP responda conexiones TCP.
|
||||
func waitCDPReady(port int, timeout time.Duration) error {
|
||||
// host puede estar vacio (usa "localhost").
|
||||
func waitCDPReady(host string, port int, timeout time.Duration) error {
|
||||
if host == "" {
|
||||
host = "localhost"
|
||||
}
|
||||
deadline := time.Now().Add(timeout)
|
||||
addr := fmt.Sprintf("localhost:%d", port)
|
||||
addr := fmt.Sprintf("%s:%d", host, port)
|
||||
for time.Now().Before(deadline) {
|
||||
conn, err := net.DialTimeout("tcp", addr, 200*time.Millisecond)
|
||||
if err == nil {
|
||||
@@ -56,7 +62,7 @@ func waitCDPReady(port int, timeout time.Duration) error {
|
||||
}
|
||||
time.Sleep(200 * time.Millisecond)
|
||||
}
|
||||
return fmt.Errorf("chrome: puerto CDP %d no disponible despues de %s", port, timeout)
|
||||
return fmt.Errorf("chrome: puerto CDP %s:%d no disponible despues de %s", host, port, timeout)
|
||||
}
|
||||
|
||||
// ChromeLaunch lanza Google Chrome con remote debugging habilitado en el puerto indicado.
|
||||
@@ -70,9 +76,13 @@ func ChromeLaunch(opts ChromeLaunchOpts) (int, error) {
|
||||
opts.UserDataDir = "/tmp/chrome-cdp-profile"
|
||||
}
|
||||
|
||||
chromePath, err := findChrome()
|
||||
if err != nil {
|
||||
return 0, err
|
||||
chromePath := opts.ChromePath
|
||||
if chromePath == "" {
|
||||
var err error
|
||||
chromePath, err = findChrome()
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
}
|
||||
|
||||
args := []string{
|
||||
@@ -111,10 +121,19 @@ func ChromeLaunch(opts ChromeLaunchOpts) (int, error) {
|
||||
pid := cmd.Process.Pid
|
||||
|
||||
// Esperar a que el puerto CDP este listo
|
||||
if err := waitCDPReady(opts.Port, 15*time.Second); err != nil {
|
||||
// Matar proceso si no arranco correctamente
|
||||
cmd.Process.Kill()
|
||||
return 0, err
|
||||
// Si Chrome escucha en 0.0.0.0 (ej: WSL2 -> Windows), el caller se encarga del wait
|
||||
skipWait := false
|
||||
for _, a := range opts.ExtraArgs {
|
||||
if a == "--remote-debugging-address=0.0.0.0" {
|
||||
skipWait = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !skipWait {
|
||||
if err := waitCDPReady("localhost", opts.Port, 15*time.Second); err != nil {
|
||||
cmd.Process.Kill()
|
||||
return 0, err
|
||||
}
|
||||
}
|
||||
|
||||
return pid, nil
|
||||
|
||||
Reference in New Issue
Block a user