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.

817 B
Raw Blame
import { cdp, js } from "../cdp-eval.js";
import { state } from "../state.js";

export type WaitForLoadOptions = {
  timeout?: number;
};

export async function waitForDocumentLoad(options: WaitForLoadOptions = {}) {
  const timeout = options.timeout ?? 15.0;
  const deadline = state.now() + timeout * 1000;
  while (state.now() < deadline) {
    let committed = true;
    try {
      const tree = await cdp("Page.getFrameTree");
      const url = tree.frameTree?.frame?.url || "";
      committed = url !== "" && url !== ":" && url !== "about:blank";
    } catch {
      // Page.getFrameTree may not be supported in some sessions; fall back to readyState only.
    }
    if (committed && (await js("document.readyState")) === "complete") {
      return true;
    }
    await state.sleep(300);
  }
  return false;
}