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:
2026-03-30 14:24:21 +02:00
parent 90693fb32f
commit 9d3bfd2cd2
4 changed files with 127 additions and 20 deletions
+22 -10
View File
@@ -20,8 +20,11 @@ type cdpTarget struct {
// cdpGetPageWSURL obtiene el webSocketDebuggerUrl de la primera tab de tipo "page"
// via el endpoint /json. Si no hay ninguna, crea una nueva con /json/new.
func cdpGetPageWSURL(port int) (string, error) {
resp, err := http.Get(fmt.Sprintf("http://localhost:%d/json", port))
func cdpGetPageWSURL(host string, port int) (string, error) {
if host == "" {
host = "localhost"
}
resp, err := http.Get(fmt.Sprintf("http://%s:%d/json", host, port))
if err != nil {
return "", fmt.Errorf("cdp targets: %w", err)
}
@@ -40,7 +43,7 @@ func cdpGetPageWSURL(port int) (string, error) {
}
// No hay tabs — crear una nueva via /json/new
newResp, err := http.Get(fmt.Sprintf("http://localhost:%d/json/new", port))
newResp, err := http.Get(fmt.Sprintf("http://%s:%d/json/new", host, port))
if err != nil {
return "", fmt.Errorf("cdp new tab: %w", err)
}
@@ -61,7 +64,16 @@ func cdpGetPageWSURL(port int) (string, error) {
// Si no hay tabs disponibles, crea una nueva via /json/new.
// Realiza el handshake WebSocket RFC 6455 sobre TCP puro (sin dependencias externas).
func CdpConnect(port int) (*CDPConn, error) {
wsURL, err := cdpGetPageWSURL(port)
return CdpConnectHost("localhost", port)
}
// CdpConnectHost es como CdpConnect pero permite especificar el host.
// Util para WSL2 donde Chrome Windows escucha en una IP distinta a localhost.
func CdpConnectHost(host string, port int) (*CDPConn, error) {
if host == "" {
host = "localhost"
}
wsURL, err := cdpGetPageWSURL(host, port)
if err != nil {
return nil, fmt.Errorf("cdp connect: %w", err)
}
@@ -72,20 +84,20 @@ func CdpConnect(port int) (*CDPConn, error) {
return nil, fmt.Errorf("cdp connect: parsear ws url %q: %w", wsURL, err)
}
host := u.Host
if !strings.Contains(host, ":") {
host = host + ":80"
wsHost := u.Host
if !strings.Contains(wsHost, ":") {
wsHost = wsHost + ":80"
}
// Abrir conexion TCP
tcpConn, err := net.Dial("tcp", host)
tcpConn, err := net.Dial("tcp", wsHost)
if err != nil {
return nil, fmt.Errorf("cdp connect: tcp dial %s: %w", host, err)
return nil, fmt.Errorf("cdp connect: tcp dial %s: %w", wsHost, err)
}
// Realizar handshake WebSocket
path := u.RequestURI()
reader, err := wsHandshake(tcpConn, host, path)
reader, err := wsHandshake(tcpConn, wsHost, path)
if err != nil {
tcpConn.Close()
return nil, fmt.Errorf("cdp connect: ws handshake: %w", err)