Skip to content

The fastest browser for AI agents to run browser automation, built for sharing your logged-in browser state with your AI agents, like Codex or Claude Code, without disturbing you. Zero cost, zero config.

fix(ego-browser): scroll into view through CDP, immune to page CSP

0x099b4141b1023
diff --git a/package/ego-browser/src/driver/pointer.test.mjs b/package/ego-browser/src/driver/pointer.test.mjs
index 36f6719..80228c5 100644
--- a/package/ego-browser/src/driver/pointer.test.mjs
+++ b/package/ego-browser/src/driver/pointer.test.mjs
@@ -6,6 +6,7 @@ import {
click,
down,
hover,
+ scrollIntoViewIfNeeded,
up,
wheel,
} from "../../dist/src/driver/pointer.js";
@@ -120,9 +121,7 @@ test("click scrolls a selector into view before resolving its click point", asyn
}
const scrollIndex = calls.findIndex(
- (call) =>
- call.method === "Runtime.callFunctionOn" &&
- call.params.functionDeclaration.includes("scrollIntoView"),
+ (call) => call.method === "DOM.scrollIntoViewIfNeeded",
);
const dispatchIndex = calls.findIndex(
(call) => call.method === "Input.dispatchMouseEvent",
@@ -131,6 +130,83 @@ test("click scrolls a selector into view before resolving its click point", asyn
assert.ok(scrollIndex < dispatchIndex, "scrolls before mouse dispatch");
});
+// The scroll must run browser-side: an in-page scroll is animated under CSS
+// scroll-behavior:smooth unless a style override is injected, and a strict
+// style-src CSP rejects that override. CDP scrolling has neither problem.
+test("scrollIntoViewIfNeeded scrolls through CDP, not page-side JavaScript", async () => {
+ const calls = [];
+ const restore = setOverrides({
+ cdpOverride(method, params) {
+ calls.push({ method, params });
+ if (
+ method === "Runtime.evaluate" &&
+ params.objectGroup === "ego-browser"
+ ) {
+ return { result: { objectId: "object-1" } };
+ }
+ return {};
+ },
+ });
+ try {
+ await scrollIntoViewIfNeeded("#target");
+ } finally {
+ restore();
+ }
+
+ const scroll = calls.find(
+ (call) => call.method === "DOM.scrollIntoViewIfNeeded",
+ );
+ assert.ok(scroll, "scrolls via DOM.scrollIntoViewIfNeeded");
+ assert.equal(scroll.params.objectId, "object-1");
+ assert.ok(
+ !calls.some((call) => call.method === "Runtime.callFunctionOn"),
+ "does not run page-side scroll code on the CDP path",
+ );
+ assert.ok(
+ calls.some(
+ (call) =>
+ call.method === "Runtime.releaseObject" &&
+ call.params.objectId === "object-1",
+ ),
+ "releases the resolved handle",
+ );
+});
+
+test("scrollIntoViewIfNeeded falls back to an in-page scroll when the CDP scroll fails", async () => {
+ const calls = [];
+ const restore = setOverrides({
+ cdpOverride(method, params) {
+ calls.push({ method, params });
+ if (
+ method === "Runtime.evaluate" &&
+ params.objectGroup === "ego-browser"
+ ) {
+ return { result: { objectId: "object-1" } };
+ }
+ if (method === "DOM.scrollIntoViewIfNeeded") {
+ throw new Error("'DOM.scrollIntoViewIfNeeded' wasn't found");
+ }
+ return {};
+ },
+ });
+ try {
+ await scrollIntoViewIfNeeded("#target");
+ } finally {
+ restore();
+ }
+
+ const fallback = calls.find(
+ (call) =>
+ call.method === "Runtime.callFunctionOn" &&
+ call.params.functionDeclaration.includes("scrollIntoView"),
+ );
+ assert.ok(fallback, "scrolls page-side when the CDP scroll is unavailable");
+ assert.match(
+ fallback.params.functionDeclaration,
+ /scroll-behavior:auto !important/,
+ );
+});
+
test("wheel defaults to scrolling down (positive deltaY) via CDP when visible and focused", async () => {
// CDP negates wheel deltas internally, so the DOM convention (positive = down)
// applies end to end — matching Playwright's mouse.wheel(deltaX, deltaY).
diff --git a/package/ego-browser/src/driver/pointer.ts b/package/ego-browser/src/driver/pointer.ts
index ba7fccf..da03d2a 100644
--- a/package/ego-browser/src/driver/pointer.ts
+++ b/package/ego-browser/src/driver/pointer.ts
@@ -1,7 +1,7 @@
import { cdp, evaluate } from "../cdp-eval.js";
import { browserCdp } from "../browser-runtime.js";
import { elementCenter } from "./observe.js";
-import { resolveAndCall } from "./element-ops.js";
+import { resolveAndCall, withHandle } from "./element-ops.js";
import { waitForSelector } from "./waits.js";
type MouseButton = "left" | "middle" | "right";
@@ -554,15 +554,32 @@ async function dispatchSyntheticWheel(
* Scroll an element into view only if it is not already fully visible,
* mirroring Playwright's locator.scrollIntoViewIfNeeded.
*
- * The scroll is forced to be instant: under CSS `scroll-behavior: smooth` it is
- * animated instead, and callers that read the element's position right after
- * this — click() resolving its point, for one — would aim at where the element
- * used to be. The override covers nested scroll containers too and is removed
- * before returning.
+ * Scrolls browser-side through CDP DOM.scrollIntoViewIfNeeded, as Playwright
+ * does: the scroll must be instant — under an animated scroll, callers that
+ * read the element's position right after this (click() resolving its point,
+ * for one) would aim at where the element used to be — and the browser-side
+ * scroll stays instant regardless of CSS `scroll-behavior: smooth`, is not
+ * subject to the page's CSP, and moves scroll containers in ancestor
+ * documents. When the CDP method fails (bridge without the DOM domain, no
+ * layout box), it falls back to an in-page scroll under a temporary
+ * scroll-behavior override; a strict `style-src` CSP can reject that override,
+ * which is why the CDP path comes first.
* @param {string} selector CSS selector or @ref of the element to reveal.
* @returns {Promise<void>}
*/
export async function scrollIntoViewIfNeeded(selector: string) {
+ const scrolled = await withHandle(
+ selector,
+ async ({ objectId, sessionId }) => {
+ try {
+ await cdp("DOM.scrollIntoViewIfNeeded", { objectId }, sessionId);
+ return true;
+ } catch {
+ return false;
+ }
+ },
+ );
+ if (scrolled) return;
await resolveAndCall(
selector,
`function(){