Skip to content

High-performance browser automation bridge and multi-instance orchestrator with advanced stealth injection and real-time dashboard.

fix: performance issue with clicks

Luigi Agosti0f96f93f22df
diff --git a/internal/bridge/action_form.go b/internal/bridge/action_form.go
index 5bbd486a..e6570435 100644
--- a/internal/bridge/action_form.go
+++ b/internal/bridge/action_form.go
@@ -156,14 +156,19 @@ func checkUncheck(ctx context.Context, req ActionRequest, wantChecked bool) (map
return nil, err
}
- // Click only if state needs to change.
+ // Click only if state needs to change. Use the JS-dispatch path so the
+ // toggle isn't gated on the headless=new CDP renderer ack chain.
if isChecked != wantChecked {
if req.NodeID > 0 {
- if err := ClickByNodeID(ctx, req.NodeID); err != nil {
+ if err := JSClickByBackendNode(ctx, req.NodeID); err != nil {
return nil, err
}
} else {
- if err := chromedp.Run(ctx, chromedp.Click(req.Selector, chromedp.ByQuery)); err != nil {
+ node, err := firstNodeBySelector(ctx, req.Selector)
+ if err != nil {
+ return nil, err
+ }
+ if err := JSClickByBackendNode(ctx, int64(node.BackendNodeID)); err != nil {
return nil, err
}
}
diff --git a/internal/bridge/action_pointer.go b/internal/bridge/action_pointer.go
index 65bf8da9..bdab4a1e 100644
--- a/internal/bridge/action_pointer.go
+++ b/internal/bridge/action_pointer.go
@@ -145,7 +145,7 @@ func (b *Bridge) actionClick(ctx context.Context, req ActionRequest) (map[string
// JS submit handlers, and actual submission in one shot (issue #411).
submitted, subErr := submitFormIfButton(clickCtx, req.Selector)
if subErr != nil {
- slog.Debug("submitFormIfButton failed, falling back to CDP click",
+ slog.Debug("submitFormIfButton failed, falling back to JS click",
"selector", req.Selector, "error", subErr)
} else if submitted {
resultCh <- clickResult{err: nil}
@@ -156,9 +156,9 @@ func (b *Bridge) actionClick(ctx context.Context, req ActionRequest) (map[string
resultCh <- clickResult{err: nodeErr}
return
}
- err = ClickByNodeID(clickCtx, int64(node.BackendNodeID))
+ err = JSClickByBackendNode(clickCtx, int64(node.BackendNodeID))
} else if req.NodeID > 0 {
- err = ClickByNodeID(clickCtx, req.NodeID)
+ err = JSClickByBackendNode(clickCtx, req.NodeID)
} else if req.HasXY {
err = ClickByCoordinate(clickCtx, req.X, req.Y)
} else {
@@ -240,9 +240,9 @@ func (b *Bridge) actionDoubleClick(ctx context.Context, req ActionRequest) (map[
if nodeErr != nil {
return nil, nodeErr
}
- err = DoubleClickByNodeID(ctx, int64(node.BackendNodeID))
+ err = JSDoubleClickByBackendNode(ctx, int64(node.BackendNodeID))
} else if req.NodeID > 0 {
- err = DoubleClickByNodeID(ctx, req.NodeID)
+ err = JSDoubleClickByBackendNode(ctx, req.NodeID)
} else if req.HasXY {
err = DoubleClickByCoordinate(ctx, req.X, req.Y)
} else {
diff --git a/internal/bridge/cdpops/pointer.go b/internal/bridge/cdpops/pointer.go
index 2d013dea..c39a0533 100644
--- a/internal/bridge/cdpops/pointer.go
+++ b/internal/bridge/cdpops/pointer.go
@@ -424,3 +424,119 @@ func HoverByNodeID(ctx context.Context, nodeID int64) error {
return dispatchMouseMoveToNode(ctx, nodeID, x, y, input.None, 0)
}
+
+// jsClickFn is invoked via Runtime.callFunctionOn against the resolved
+// backend node. It dispatches synthetic mousedown/mouseup MouseEvents and
+// then calls el.click() so the browser runs default actions (link nav,
+// checkbox toggle, button form submit) AND fires a 'click' event that
+// bubbles to listeners. Walks up to find an anchor ancestor so clicking an
+// inner span inside an <a> still follows the href.
+const jsClickFn = `function() {
+ var el = this;
+ var r = el.getBoundingClientRect();
+ var cx = r.left + r.width / 2;
+ var cy = r.top + r.height / 2;
+ var init = {
+ clientX: cx, clientY: cy, screenX: cx, screenY: cy,
+ button: 0, buttons: 1,
+ bubbles: true, cancelable: true, view: window
+ };
+ if (typeof el.focus === 'function') {
+ try { el.focus({preventScroll: true}); } catch (e) {}
+ }
+ el.dispatchEvent(new MouseEvent('mousedown', init));
+ el.dispatchEvent(new MouseEvent('mouseup', Object.assign({}, init, {buttons: 0})));
+ var target = el;
+ while (target && target.nodeType === 1) {
+ if (target.tagName === 'A' && target.hasAttribute('href')) break;
+ target = target.parentElement;
+ }
+ if (!target) target = el;
+ if (typeof target.click === 'function') {
+ target.click();
+ } else {
+ target.dispatchEvent(new MouseEvent('click', init));
+ }
+}`
+
+const jsDoubleClickFn = `function() {
+ var el = this;
+ var r = el.getBoundingClientRect();
+ var cx = r.left + r.width / 2;
+ var cy = r.top + r.height / 2;
+ var base = {
+ clientX: cx, clientY: cy, screenX: cx, screenY: cy,
+ button: 0, buttons: 1,
+ bubbles: true, cancelable: true, view: window
+ };
+ if (typeof el.focus === 'function') {
+ try { el.focus({preventScroll: true}); } catch (e) {}
+ }
+ for (var i = 1; i <= 2; i++) {
+ el.dispatchEvent(new MouseEvent('mousedown', Object.assign({}, base, {detail: i})));
+ el.dispatchEvent(new MouseEvent('mouseup', Object.assign({}, base, {detail: i, buttons: 0})));
+ el.dispatchEvent(new MouseEvent('click', Object.assign({}, base, {detail: i})));
+ }
+ el.dispatchEvent(new MouseEvent('dblclick', Object.assign({}, base, {detail: 2})));
+}`
+
+func resolveBackendNodeObjectID(ctx context.Context, backendNodeID int64) (string, error) {
+ var resolved json.RawMessage
+ if err := chromedp.Run(ctx, chromedp.ActionFunc(func(ctx context.Context) error {
+ return chromedp.FromContext(ctx).Target.Execute(ctx, "DOM.resolveNode", map[string]any{
+ "backendNodeId": backendNodeID,
+ }, &resolved)
+ })); err != nil {
+ return "", fmt.Errorf("DOM.resolveNode: %w", err)
+ }
+ var out struct {
+ Object struct {
+ ObjectID string `json:"objectId"`
+ } `json:"object"`
+ }
+ if err := json.Unmarshal(resolved, &out); err != nil {
+ return "", err
+ }
+ if strings.TrimSpace(out.Object.ObjectID) == "" {
+ return "", fmt.Errorf("element not found in DOM (backendNodeId=%d)", backendNodeID)
+ }
+ return out.Object.ObjectID, nil
+}
+
+// JSClickByBackendNode performs a click via Runtime.callFunctionOn rather
+// than synthesized CDP Input.dispatchMouseEvent. Headless Chromium's CDP
+// path can stall ~5s waiting on the renderer ack chain for press/release;
+// the JS path runs the same handler chain (mousedown, mouseup, click) plus
+// the browser's default action (el.click()) without the ack tax.
+func JSClickByBackendNode(ctx context.Context, backendNodeID int64) error {
+ if _, _, err := PointerPointForNode(ctx, backendNodeID, true); err != nil {
+ return err
+ }
+ objectID, err := resolveBackendNodeObjectID(ctx, backendNodeID)
+ if err != nil {
+ return err
+ }
+ return chromedp.Run(ctx, chromedp.ActionFunc(func(ctx context.Context) error {
+ return chromedp.FromContext(ctx).Target.Execute(ctx, "Runtime.callFunctionOn", map[string]any{
+ "functionDeclaration": jsClickFn,
+ "objectId": objectID,
+ }, nil)
+ }))
+}
+
+// JSDoubleClickByBackendNode is the dblclick counterpart of JSClickByBackendNode.
+func JSDoubleClickByBackendNode(ctx context.Context, backendNodeID int64) error {
+ if _, _, err := PointerPointForNode(ctx, backendNodeID, true); err != nil {
+ return err
+ }
+ objectID, err := resolveBackendNodeObjectID(ctx, backendNodeID)
+ if err != nil {
+ return err
+ }
+ return chromedp.Run(ctx, chromedp.ActionFunc(func(ctx context.Context) error {
+ return chromedp.FromContext(ctx).Target.Execute(ctx, "Runtime.callFunctionOn", map[string]any{
+ "functionDeclaration": jsDoubleClickFn,
+ "objectId": objectID,
+ }, nil)
+ }))
+}
diff --git a/internal/bridge/cdpops_facade.go b/internal/bridge/cdpops_facade.go
index a8d3dd04..7491bf62 100644
--- a/internal/bridge/cdpops_facade.go
+++ b/internal/bridge/cdpops_facade.go
@@ -59,6 +59,10 @@ func ClickByNodeID(ctx context.Context, nodeID int64) error {
return bridgecdpops.ClickByNodeID(ctx, nodeID)
}
+func JSClickByBackendNode(ctx context.Context, nodeID int64) error {
+ return bridgecdpops.JSClickByBackendNode(ctx, nodeID)
+}
+
func DoubleClickByCoordinate(ctx context.Context, x, y float64) error {
return bridgecdpops.DoubleClickByCoordinate(ctx, x, y)
}
@@ -67,6 +71,10 @@ func DoubleClickByNodeID(ctx context.Context, nodeID int64) error {
return bridgecdpops.DoubleClickByNodeID(ctx, nodeID)
}
+func JSDoubleClickByBackendNode(ctx context.Context, nodeID int64) error {
+ return bridgecdpops.JSDoubleClickByBackendNode(ctx, nodeID)
+}
+
func DragByNodeID(ctx context.Context, nodeID int64, dx, dy int) error {
return bridgecdpops.DragByNodeID(ctx, nodeID, dx, dy)
}