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,48 @@
|
||||
package infra
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// CdpEvaluate ejecuta una expresion JavaScript en la pagina actual via Runtime.evaluate.
|
||||
// Retorna el resultado como string. Soporta expresiones simples y complejas.
|
||||
// Para valores no-string, Chrome los serializa a JSON.
|
||||
func CdpEvaluate(c *CDPConn, expression string) (string, error) {
|
||||
if c == nil {
|
||||
return "", fmt.Errorf("cdp evaluate: conexion nula")
|
||||
}
|
||||
|
||||
params := map[string]any{
|
||||
"expression": expression,
|
||||
"returnByValue": true,
|
||||
"awaitPromise": true,
|
||||
"userGesture": true,
|
||||
}
|
||||
|
||||
result, err := c.sendCDP("Runtime.evaluate", params)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("cdp evaluate: %w", err)
|
||||
}
|
||||
|
||||
// Verificar excepcion JS
|
||||
if exc, ok := result["exceptionDetails"]; ok && exc != nil {
|
||||
excMap, _ := exc.(map[string]any)
|
||||
text, _ := excMap["text"].(string)
|
||||
return "", fmt.Errorf("cdp evaluate: excepcion JS: %s", text)
|
||||
}
|
||||
|
||||
// Extraer valor del resultado
|
||||
resVal, ok := result["result"].(map[string]any)
|
||||
if !ok {
|
||||
return "", fmt.Errorf("cdp evaluate: resultado inesperado: %v", result)
|
||||
}
|
||||
|
||||
value, ok := resVal["value"]
|
||||
if !ok {
|
||||
// Puede ser undefined o un tipo no serializable
|
||||
typ, _ := resVal["type"].(string)
|
||||
return typ, nil
|
||||
}
|
||||
|
||||
return fmt.Sprintf("%v", value), nil
|
||||
}
|
||||
Reference in New Issue
Block a user