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.

1.1 KB
Raw Blame
export class RefMap {
  map: Map<string, any>;
  readonly allowFallback: boolean;

  constructor({ allowFallback = true } = {}) {
    this.map = new Map();
    this.allowFallback = allowFallback;
  }

  add(refId, backendNodeId, role, name, nth = undefined) {
    this.addWithFrame(refId, backendNodeId, role, name, nth, undefined);
  }

  addWithFrame(
    refId,
    backendNodeId,
    role,
    name,
    nth = undefined,
    frameId = undefined,
    frameProvenance = undefined,
  ) {
    this.map.set(refId, {
      backendNodeId,
      role,
      name,
      nth,
      frameId,
      ...(frameProvenance ? { frameProvenance } : {}),
    });
  }

  get(refId) {
    return this.map.get(refId);
  }

  clear() {
    this.map.clear();
  }
}

export function parseRef(input) {
  const trimmed = String(input || "").trim();
  for (const candidate of [
    trimmed.startsWith("@") ? trimmed.slice(1) : null,
    trimmed.startsWith("ref=") ? trimmed.slice(4) : null,
    trimmed,
  ]) {
    if (candidate && /^\d+$/.test(candidate)) {
      return candidate;
    }
  }
  return null;
}