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.
JavaScript agent-skillsai-agentautomationbrowserbrowser-automationclaude-codecodexhermes-agentskillsskills-sh
stabilize managed page creation
Auted5f6d613a3c4
diff --git a/package/ego-browser/src/page-model.test.mjs b/package/ego-browser/src/page-model.test.mjsindex 6c17d94..5717134 100644--- a/package/ego-browser/src/page-model.test.mjs+++ b/package/ego-browser/src/page-model.test.mjs@@ -96,6 +96,18 @@ function createFixture(rootDir) {if (params.expression === "document.title") {return { result: { type: "string", value: tab.title } };}+ if (params.expression.includes("performance.timeOrigin")) {+ return {+ result: {+ type: "object",+ value: {+ readyState: "complete",+ url: tab.url,+ timeOrigin: Date.now() + 1_000,+ },+ },+ };+ }if (params.expression.includes("innerWidth")) {return {result: {@@ -294,6 +306,58 @@ test("a page label restores in a new round and goto reuses its target", async ()});});+test("newPage waits for the document created by this call, not an already-complete placeholder", async () => {+ await withFixture(async (fixture) => {+ const requestedUrl = "https://example.test/created-document";+ const baseCreateTab = fixture.services.createTab;+ const baseCdp = fixture.services.cdp;+ let now = 10_000;+ let targetId;+ let sleepCount = 0;++ const task = taskForRound(fixture, "round-a", {+ async createTab(url) {+ targetId = await baseCreateTab(url);+ fixture.tabs.get(targetId).url = "chrome://newtab/";+ return targetId;+ },+ async cdp(method, params, sessionId, timeoutMs) {+ if (method === "Runtime.evaluate") {+ if (params.expression === "document.readyState") {+ return { result: { type: "string", value: "complete" } };+ }+ if (params.expression.includes("performance.timeOrigin")) {+ const tab = fixture.tabs.get(targetId);+ return {+ result: {+ type: "object",+ value: {+ readyState: "complete",+ url: tab.url,+ timeOrigin: tab.url === requestedUrl ? now : now - 5_000,+ },+ },+ };+ }+ }+ return baseCdp(method, params, sessionId, timeoutMs);+ },+ now: () => now,+ async sleep(ms) {+ sleepCount += 1;+ now += ms;+ fixture.tabs.get(targetId).url = requestedUrl;+ },+ });++ const page = await task.newPage(requestedUrl);++ assert.equal(page.targetId, targetId);+ assert.equal(sleepCount, 1);+ assert.equal(fixture.tabs.get(targetId).url, requestedUrl);+ });+});+test("snapshot activates the addressed page, not whichever tab was current", async () => {await withFixture(async (fixture) => {const task = taskForRound(fixture, "round-a");@@ -587,8 +651,14 @@ test("Page actions resolve snapshot refs inside the addressed page", async () =>params.backendNodeId === 21 &&sessionId === "session:target-2",);- assert(firstResolveCall, "click must resolve through the first Page session");- assert(secondResolveCall, "fill must resolve through the second Page session");+ assert(+ firstResolveCall,+ "click must resolve through the first Page session",+ );+ assert(+ secondResolveCall,+ "fill must resolve through the second Page session",+ );assert.equal(fixture.activeTarget(), second.targetId);});});@@ -624,10 +694,7 @@ test("an unknown Page ref fails after one target-scoped refresh", async () => {const task = taskForRound(fixture, "round-a");const page = await task.newPage("https://example.test/first");- await assert.rejects(- () => page.click("@99"),- /Unknown ref: 99/,- );+ await assert.rejects(() => page.click("@99"), /Unknown ref: 99/);assert.deepEqual(fixture.calls.filter(([kind]) => kind === "snapshot"),[["snapshot", page.targetId]],@@ -664,7 +731,8 @@ test("Page click does not dispatch input when scrolling cannot make the element);assert.equal(fixture.calls.some(- ([kind, method]) => kind === "cdp" && method === "Input.dispatchMouseEvent",+ ([kind, method]) =>+ kind === "cdp" && method === "Input.dispatchMouseEvent",),false,);diff --git a/package/ego-browser/src/page-model.ts b/package/ego-browser/src/page-model.tsindex 7fecdd6..12d068c 100644--- a/package/ego-browser/src/page-model.ts+++ b/package/ego-browser/src/page-model.ts@@ -362,6 +362,7 @@ export class TaskSpace {if (managedCount >= this.#services.pageBudget) {throw pageBudgetError(this, this.#services.pageBudget, ledger, tabs);}+ const creationStartedAtMs = this.#services.now();const targetId = await this.#services.createTab(url);this.#services.setPreferredTarget(targetId);const existingManaged = Object.entries(ledger.pages).find(@@ -395,7 +396,13 @@ export class TaskSpace {const page = new Page(this, entry.label, this.#services, entry);try {const sessionId = await this.#services.ensureSession(targetId);- await waitForReadyState(this.#services, sessionId, timeoutMs);+ await waitForCreatedDocument(+ this.#services,+ sessionId,+ url,+ creationStartedAtMs,+ timeoutMs,+ );} catch (error) {throw new Error(`page ${entry.label} was created but did not finish loading; retrieve it with task.page('${entry.label}'): ${error?.message || error}`,@@ -715,10 +722,7 @@ export class Page {});}- async #refMapForAction(- page: PageTarget,- selector: string,- ): Promise<RefMap> {+ async #refMapForAction(page: PageTarget, selector: string): Promise<RefMap> {let refs = this.#services.pageRefs.forTarget(page.targetId);const refId = parseRef(selector);if (!refId || refs.get(refId)) return refs;@@ -907,6 +911,71 @@ async function fetchInPage({}}+async function waitForCreatedDocument(+ services: PageModelServices,+ sessionId: string,+ requestedUrl: string,+ creationStartedAtMs: number,+ timeoutMs: number,+): Promise<void> {+ const deadline = services.now() + timeoutMs;+ while (services.now() <= deadline) {+ const remaining = Math.max(1, deadline - services.now());+ const response = await services.cdp(+ "Runtime.evaluate",+ {+ // createTab() can return while the target still exposes an already+ // complete Chrome placeholder document. Read all three values in one+ // evaluation so newPage resolves only after the requested navigation+ // has committed a document created during this call.+ expression:+ "({readyState:document.readyState,url:location.href,timeOrigin:performance.timeOrigin})",+ returnByValue: true,+ },+ sessionId,+ Math.min(1_000, remaining),+ );+ const observation = response?.result?.value;+ if (+ isCreatedDocumentReady(observation, requestedUrl, creationStartedAtMs)+ ) {+ return;+ }+ await services.sleep(Math.min(100, remaining));+ }+ throw new Error(`task.newPage timed out after ${timeoutMs}ms`);+}++function isCreatedDocumentReady(+ observation: any,+ requestedUrl: string,+ creationStartedAtMs: number,+): boolean {+ if (+ observation?.readyState !== "complete" ||+ typeof observation?.url !== "string" ||+ typeof observation?.timeOrigin !== "number" ||+ observation.timeOrigin < creationStartedAtMs+ ) {+ return false;+ }++ if (isBrowserPlaceholderUrl(requestedUrl)) {+ return observation.url === requestedUrl;+ }+ return !isBrowserPlaceholderUrl(observation.url);+}++function isBrowserPlaceholderUrl(url: string): boolean {+ return (+ url === "" ||+ url === ":" ||+ url === "about:blank" ||+ url === "chrome://newtab/" ||+ url === "chrome://new-tab-page/"+ );+}+async function waitForReadyState(services: PageModelServices,sessionId: string,