High-performance browser automation bridge and multi-instance orchestrator with advanced stealth injection and real-time dashboard.
fix(cdpops): reveal hover and fade-in panels before refusing a transparent ancestor, and walk through shadow hosts
Luigi Agosti14b554bdf726
diff --git a/internal/bridge/action_pointer_opacity_test.go b/internal/bridge/action_pointer_opacity_test.goindex 8dd002e8..b817be71 100644--- a/internal/bridge/action_pointer_opacity_test.go+++ b/internal/bridge/action_pointer_opacity_test.go@@ -7,6 +7,8 @@ import ("testing""time"+ "github.com/chromedp/cdproto/dom"+ "github.com/chromedp/cdproto/runtime""github.com/chromedp/chromedp"bridgecdpops "github.com/pinchtab/pinchtab/internal/bridge/cdpops""github.com/pinchtab/pinchtab/internal/testbrowser"@@ -30,6 +32,11 @@ const opacityFixtureHTML = `<!doctype html><html><body><div style="opacity:0"><button id="ancestorzero">AncestorZero</button></div><div style="opacity:0"><div style="opacity:1"><button id="nested">NestedReset</button></div></div><div style="visibility:hidden"><button id="ancestorhidden">AncestorHidden</button></div>+<style>.row .actions{opacity:0} .row:hover .actions{opacity:1}</style>+<div class="row" style="padding:8px">Invoice 42 <span class="actions"><button id="rowaction">Edit</button></span></div>+<div id="fadepanel" style="opacity:0;transition:opacity 300ms linear"><button id="fadein">FadeIn</button></div>+<div id="shadowhost" style="opacity:0"></div>+<script>document.getElementById('shadowhost').attachShadow({mode:'open'}).innerHTML = '<button id="inshadow">InShadow</button>';</script></body></html>`func TestPointerRefusesAnElementHiddenByAnAncestorsOpacity(t *testing.T) {@@ -54,6 +61,7 @@ func TestPointerRefusesAnElementHiddenByAnAncestorsOpacity(t *testing.T) {for _, tc := range []struct {selector stringwantHidden bool+ setup stringwhy string}{{selector: "#normal", wantHidden: false, why: "nothing hides it"},@@ -62,13 +70,21 @@ func TestPointerRefusesAnElementHiddenByAnAncestorsOpacity(t *testing.T) {{selector: "#ancestorzero", wantHidden: true, why: "an opacity:0 panel hides what is inside it"},{selector: "#nested", wantHidden: true, why: "opacity multiplies down the tree, so an inner opacity:1 cannot undo an outer 0"},{selector: "#ancestorhidden", wantHidden: true, why: "visibility inherits, and was already refused"},+ {selector: "#rowaction", wantHidden: false, why: "a row action revealed by :hover is visible to anyone pointing at it"},+ {selector: "#fadein", wantHidden: false, setup: `document.getElementById('fadepanel').style.opacity = '1'`, why: "a panel fading in from opacity:0 is on its way to the screen, not off it"},+ {selector: "#inshadow", wantHidden: true, why: "an opacity:0 shadow host hides its shadow tree like any other ancestor"},} {t.Run(tc.selector, func(t *testing.T) {- node, err := firstNodeBySelector(ctx, tc.selector)+ if tc.setup != "" {+ if err := chromedp.Run(ctx, chromedp.Evaluate(tc.setup, nil)); err != nil {+ t.Fatal(err)+ }+ }+ backendNodeID, err := opacityFixtureNode(ctx, tc.selector)if err != nil {t.Fatal(err)}- _, _, err = bridgecdpops.PointerPointForNode(ctx, int64(node.BackendNodeID), false)+ _, _, err = bridgecdpops.PointerPointForNode(ctx, backendNodeID, false)gotHidden := errors.Is(err, bridgecdpops.ErrElementHidden)switch {@@ -82,3 +98,25 @@ func TestPointerRefusesAnElementHiddenByAnAncestorsOpacity(t *testing.T) {})}}++// opacityFixtureNode resolves a fixture node by selector, reaching into+// #shadowhost's open shadow root, which a document query cannot.+func opacityFixtureNode(ctx context.Context, selector string) (int64, error) {+ var backendNodeID int64+ err := chromedp.Run(ctx, chromedp.ActionFunc(func(ctx context.Context) error {+ obj, exception, err := runtime.Evaluate(`document.querySelector(` + "`" + selector + "`" + `) || document.getElementById('shadowhost').shadowRoot.querySelector(` + "`" + selector + "`" + `)`).Do(ctx)+ if err != nil {+ return err+ }+ if exception != nil {+ return exception+ }+ node, err := dom.DescribeNode().WithObjectID(obj.ObjectID).Do(ctx)+ if err != nil {+ return err+ }+ backendNodeID = int64(node.BackendNodeID)+ return nil+ }))+ return backendNodeID, err+}diff --git a/internal/bridge/cdpops/validation.go b/internal/bridge/cdpops/validation.goindex 21d0a953..76848ca6 100644--- a/internal/bridge/cdpops/validation.go+++ b/internal/bridge/cdpops/validation.go@@ -6,6 +6,7 @@ import ("errors""fmt""strings"+ "time""github.com/chromedp/chromedp")@@ -27,8 +28,17 @@ type pointerProbe struct {PointerEvent string `json:"pointerEvent"`Occluded bool `json:"occluded"`TopTag string `json:"topTag"`+ // TransparentAncestor is kept apart from Visible because it is the one+ // hidden state the pointer itself can end: see revealTransparentAncestor.+ TransparentAncestor bool `json:"transparentAncestor"`}+// transparentRevealWindow bounds how long a node hidden only by an opacity:0+// ancestor is given to become visible once the pointer is over it.+const transparentRevealWindow = 500 * time.Millisecond++const transparentRevealPoll = 50 * time.Millisecond+// PointerPointForNode validates clickability assumptions and returns a// stable pointer coordinate for the backend node.func PointerPointForNode(ctx context.Context, backendNodeID int64, requireTopMost bool) (float64, float64, error) {@@ -88,12 +98,17 @@ func PointerPointForNode(ctx context.Context, backendNodeID int64, requireTopMos// was clicked. An element nobody can see is the same hazard as the wrong// element: the caller is told OK about something it cannot perceive.//- // Walks this document only. An opacity:0 IFRAME hiding its contents is- // not covered here, because reaching the frame owner can throw on a- // cross-origin ancestor and the coordinate walk above already treats that- // as a case higher layers decide.+ // Walks this document only, crossing shadow roots to their hosts. An+ // opacity:0 IFRAME hiding its contents is not covered here, because+ // reaching the frame owner can throw on a cross-origin ancestor and the+ // coordinate walk above already treats that as a case higher layers decide.+ const parentAcrossShadow = (n) => {+ if (n.parentElement) return n.parentElement;+ const root = n.getRootNode && n.getRootNode();+ return root && root.host ? root.host : null;+ };const transparentAncestor = () => {- for (let n = this; n && n.nodeType === 1; n = n.parentElement) {+ for (let n = this; n && n.nodeType === 1; n = parentAcrossShadow(n)) {const s = view.getComputedStyle(n);if (s && !(Number(s.opacity === '' || s.opacity == null ? '1' : s.opacity) > 0)) {return true;@@ -101,7 +116,7 @@ func PointerPointForNode(ctx context.Context, backendNodeID int64, requireTopMos}return false;};- const visible = !!style && style.display !== 'none' && style.visibility !== 'hidden' && Number(style.opacity || '1') > 0 && !transparentAncestor();+ const visible = !!style && style.display !== 'none' && style.visibility !== 'hidden' && Number(style.opacity || '1') > 0;const pointerEvent = style ? String(style.pointerEvents || '') : '';let occluded = false;let topTag = '';@@ -122,32 +137,44 @@ func PointerPointForNode(ctx context.Context, backendNodeID int64, requireTopMosvisible,pointerEvent,occluded,- topTag+ topTag,+ transparentAncestor: transparentAncestor()};}`- var probeRaw json.RawMessage- if err := chromedp.Run(ctx, chromedp.ActionFunc(func(ctx context.Context) error {- return chromedp.FromContext(ctx).Target.Execute(ctx, "Runtime.callFunctionOn", map[string]any{- "functionDeclaration": probeJS,- "objectId": objectID,- "returnByValue": true,- }, &probeRaw)- })); err != nil {- return 0, 0, fmt.Errorf("pointer probe: %w", err)+ runProbe := func() (pointerProbe, error) {+ var probeRaw json.RawMessage+ if err := chromedp.Run(ctx, chromedp.ActionFunc(func(ctx context.Context) error {+ return chromedp.FromContext(ctx).Target.Execute(ctx, "Runtime.callFunctionOn", map[string]any{+ "functionDeclaration": probeJS,+ "objectId": objectID,+ "returnByValue": true,+ }, &probeRaw)+ })); err != nil {+ return pointerProbe{}, fmt.Errorf("pointer probe: %w", err)+ }+ var callRes struct {+ Result struct {+ Value pointerProbe `json:"value"`+ } `json:"result"`+ }+ if err := json.Unmarshal(probeRaw, &callRes); err != nil {+ return pointerProbe{}, err+ }+ return callRes.Result.Value, nil}- var callRes struct {- Result struct {- Value pointerProbe `json:"value"`- } `json:"result"`- }- if err := json.Unmarshal(probeRaw, &callRes); err != nil {+ probe, err := runProbe()+ if err != nil {return 0, 0, err}- probe := callRes.Result.Value+ if probe.Visible && probe.TransparentAncestor && probe.Width > 0 && probe.Height > 0 {+ if probe, err = revealTransparentAncestor(ctx, probe, runProbe); err != nil {+ return 0, 0, err+ }+ }- if probe.Width <= 0 || probe.Height <= 0 || !probe.Visible {+ if probe.Width <= 0 || probe.Height <= 0 || !probe.Visible || probe.TransparentAncestor {return 0, 0, fmt.Errorf("%w: width=%.2f height=%.2f", ErrElementHidden, probe.Width, probe.Height)}if !probe.InViewport {@@ -162,3 +189,32 @@ func PointerPointForNode(ctx context.Context, backendNodeID int64, requireTopMosreturn probe.X, probe.Y, nil}++// revealTransparentAncestor gives a node hidden only by an opacity:0 ancestor the+// chance a user's pointer would give it. Row actions revealed by+// .row:hover .actions{opacity:1}, and panels fading in from opacity:0, are both+// transparent at the instant of the first probe yet visible to anyone pointing+// at them. The pointer is moved to the point the action targets anyway, then the+// probe is repeated until the ancestor turns opaque or the window closes. Only a+// node that stays transparent under the pointer is refused.+func revealTransparentAncestor(ctx context.Context, probe pointerProbe, runProbe func() (pointerProbe, error)) (pointerProbe, error) {+ moveCtx, cancel := context.WithTimeout(ctx, transparentRevealWindow)+ _ = chromedp.Run(moveCtx, mouseEventAction(map[string]any{"type": "mouseMoved", "x": probe.X, "y": probe.Y}))+ cancel()++ deadline := time.Now().Add(transparentRevealWindow)+ for {+ next, err := runProbe()+ if err != nil {+ return probe, err+ }+ if !next.TransparentAncestor || time.Now().After(deadline) {+ return next, nil+ }+ select {+ case <-ctx.Done():+ return next, ctx.Err()+ case <-time.After(transparentRevealPoll):+ }+ }+}