Files
fn_registry/functions/browser/cdp_click_ref.go
T
egutierrez 029dbf57bd feat(core): auto-commit con 10 cambios
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-06-06 13:20:36 +02:00

41 lines
1.6 KiB
Go

package browser
import "fmt"
// refBoxCenter resuelve el centro (x,y) en coords de página de un nodo DOM por su
// backendDOMNodeId, vía DOM.getBoxModel. El content quad son 8 floats (4 esquinas).
func refBoxCenter(c *CDPConn, backendNodeID int) (float64, float64, error) {
res, err := c.sendCDP("DOM.getBoxModel", map[string]any{"backendNodeId": backendNodeID})
if err != nil {
return 0, 0, fmt.Errorf("getBoxModel ref %d: %w", backendNodeID, err)
}
model, ok := res["model"].(map[string]any)
if !ok {
return 0, 0, fmt.Errorf("ref %d: sin boxModel (nodo no visible o inexistente)", backendNodeID)
}
content, ok := model["content"].([]any)
if !ok || len(content) < 8 {
return 0, 0, fmt.Errorf("ref %d: content quad invalido", backendNodeID)
}
num := func(i int) float64 { f, _ := content[i].(float64); return f }
cx := (num(0) + num(2) + num(4) + num(6)) / 4
cy := (num(1) + num(3) + num(5) + num(7)) / 4
return cx, cy, nil
}
// CdpClickRef hace click humanizado (Bézier + jitter) sobre el elemento del #ref.
// El #ref es un backendDOMNodeId extraído del AX outline por page_perceive.
// Hace scroll al elemento si es necesario antes de calcular las coordenadas.
func CdpClickRef(c *CDPConn, backendNodeID int, opts MouseHumanOpts) error {
if c == nil {
return fmt.Errorf("cdp click ref: conexión nil")
}
// scroll al elemento si no está visible; ignorar error (no fatal)
_, _ = c.sendCDP("DOM.scrollIntoViewIfNeeded", map[string]any{"backendNodeId": backendNodeID})
cx, cy, err := refBoxCenter(c, backendNodeID)
if err != nil {
return fmt.Errorf("cdp click ref: %w", err)
}
return CdpClickXYHuman(c, cx, cy, opts)
}