feat: funciones Chrome CDP para automatización de navegador
10 funciones Go en infra/ para controlar Chrome via Chrome DevTools Protocol: chrome_launch, cdp_connect, cdp_navigate, cdp_evaluate, cdp_screenshot, cdp_click, cdp_type_text, cdp_wait_element, cdp_get_html, cdp_close. WebSocket RFC 6455 implementado sin dependencias externas. Incluye tests de integración con Chrome real. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,105 @@
|
||||
package infra
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// cdpTarget representa un target CDP del endpoint /json.
|
||||
type cdpTarget struct {
|
||||
ID string `json:"id"`
|
||||
Type string `json:"type"`
|
||||
Title string `json:"title"`
|
||||
URL string `json:"url"`
|
||||
WebSocketDebuggerURL string `json:"webSocketDebuggerUrl"`
|
||||
}
|
||||
|
||||
// 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))
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("cdp targets: %w", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
var targets []cdpTarget
|
||||
if err := json.NewDecoder(resp.Body).Decode(&targets); err != nil {
|
||||
return "", fmt.Errorf("cdp targets: decode: %w", err)
|
||||
}
|
||||
|
||||
// Buscar la primera tab de tipo "page"
|
||||
for _, t := range targets {
|
||||
if t.Type == "page" && t.WebSocketDebuggerURL != "" {
|
||||
return t.WebSocketDebuggerURL, nil
|
||||
}
|
||||
}
|
||||
|
||||
// No hay tabs — crear una nueva via /json/new
|
||||
newResp, err := http.Get(fmt.Sprintf("http://localhost:%d/json/new", port))
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("cdp new tab: %w", err)
|
||||
}
|
||||
defer newResp.Body.Close()
|
||||
|
||||
var newTarget cdpTarget
|
||||
if err := json.NewDecoder(newResp.Body).Decode(&newTarget); err != nil {
|
||||
return "", fmt.Errorf("cdp new tab: decode: %w", err)
|
||||
}
|
||||
if newTarget.WebSocketDebuggerURL == "" {
|
||||
return "", fmt.Errorf("cdp new tab: webSocketDebuggerUrl vacio")
|
||||
}
|
||||
return newTarget.WebSocketDebuggerURL, nil
|
||||
}
|
||||
|
||||
// CdpConnect se conecta al endpoint CDP en localhost:{port} y retorna una CDPConn lista para usar.
|
||||
// Conecta a la primera tab de tipo "page" (que soporta Page.*, Runtime.*, Input.*).
|
||||
// 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)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cdp connect: %w", err)
|
||||
}
|
||||
|
||||
// Parsear la URL del WebSocket para extraer host y path
|
||||
u, err := url.Parse(wsURL)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cdp connect: parsear ws url %q: %w", wsURL, err)
|
||||
}
|
||||
|
||||
host := u.Host
|
||||
if !strings.Contains(host, ":") {
|
||||
host = host + ":80"
|
||||
}
|
||||
|
||||
// Abrir conexion TCP
|
||||
tcpConn, err := net.Dial("tcp", host)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cdp connect: tcp dial %s: %w", host, err)
|
||||
}
|
||||
|
||||
// Realizar handshake WebSocket
|
||||
path := u.RequestURI()
|
||||
reader, err := wsHandshake(tcpConn, host, path)
|
||||
if err != nil {
|
||||
tcpConn.Close()
|
||||
return nil, fmt.Errorf("cdp connect: ws handshake: %w", err)
|
||||
}
|
||||
|
||||
c := &CDPConn{
|
||||
conn: tcpConn,
|
||||
reader: reader,
|
||||
port: port,
|
||||
pending: make(map[int64]chan cdpResponse),
|
||||
}
|
||||
|
||||
// Iniciar goroutine de lectura
|
||||
go c.readLoop()
|
||||
|
||||
return c, nil
|
||||
}
|
||||
Reference in New Issue
Block a user