8742cb25be
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
72 lines
3.1 KiB
Go
72 lines
3.1 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 sobre el elemento del #ref (un backendDOMNodeId extraído
|
|
// del AX outline por page_perceive). Por defecto usa click humanizado (Bézier +
|
|
// jitter) sobre el centro del bbox. Dos casos caen al click via element.click() JS:
|
|
// - opts.Mode == "instant": sin eventos de ratón reales (rápido, tests).
|
|
// - el nodo no tiene box model (display:contents, área 0): degradado natural en
|
|
// vez de fallar con error duro — un elemento clicable sin geometría sí se clica.
|
|
// 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")
|
|
}
|
|
if opts.Mode == "instant" {
|
|
return clickRefViaJS(c, backendNodeID)
|
|
}
|
|
// 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 {
|
|
// Sin geometría: fallback a element.click() JS en vez de error duro.
|
|
return clickRefViaJS(c, backendNodeID)
|
|
}
|
|
return CdpClickXYHuman(c, cx, cy, opts)
|
|
}
|
|
|
|
// clickRefViaJS resuelve el nodo por backendDOMNodeId y llama element.click() en
|
|
// el contexto JS de la página. No dispara eventos de ratón reales (mousemove/
|
|
// mousedown), por lo que algunos listeners de hover no se activan; a cambio
|
|
// funciona sin geometría y al instante.
|
|
func clickRefViaJS(c *CDPConn, backendNodeID int) error {
|
|
res, err := c.sendCDP("DOM.resolveNode", map[string]any{"backendNodeId": backendNodeID})
|
|
if err != nil {
|
|
return fmt.Errorf("cdp click ref (js): resolveNode ref %d: %w", backendNodeID, err)
|
|
}
|
|
obj, _ := res["object"].(map[string]any)
|
|
objID, _ := obj["objectId"].(string)
|
|
if objID == "" {
|
|
return fmt.Errorf("cdp click ref (js): sin objectId para ref %d", backendNodeID)
|
|
}
|
|
if _, err := c.sendCDP("Runtime.callFunctionOn", map[string]any{
|
|
"objectId": objID,
|
|
"functionDeclaration": "function(){ this.click(); }",
|
|
}); err != nil {
|
|
return fmt.Errorf("cdp click ref (js): click ref %d: %w", backendNodeID, err)
|
|
}
|
|
return nil
|
|
}
|