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.

refactor(ego-browser): rename dispatchKey to Playwright dispatchEvent

0x079eadf54db47
diff --git a/package/ego-browser/README.md b/package/ego-browser/README.md
index 87211a1..0108f85 100644
--- a/package/ego-browser/README.md
+++ b/package/ego-browser/README.md
@@ -67,7 +67,7 @@ src/
driver/
pointer.ts click, hover, drag, wheel, scrollIntoViewIfNeeded
observe.ts snapshot, screenshot, elementCenter
- keyboard.ts insertText, press, fill, dispatchKey
+ keyboard.ts insertText, press, fill, dispatchEvent
nav.ts tabs, goto, openOrReuseTab, closeTab
load.ts waitForDocumentLoad and load orchestration
waits.ts waitForTimeout, waitForLoadState, waitForSelector
diff --git a/package/ego-browser/scripts/real-browser-e2e/cases/helper-surface.mjs b/package/ego-browser/scripts/real-browser-e2e/cases/helper-surface.mjs
index eca9626..f1f998c 100644
--- a/package/ego-browser/scripts/real-browser-e2e/cases/helper-surface.mjs
+++ b/package/ego-browser/scripts/real-browser-e2e/cases/helper-surface.mjs
@@ -33,7 +33,7 @@ export function helperSurfaceCase() {
"press",
"insertText",
"fill",
- "dispatchKey",
+ "dispatchEvent",
"setInputFiles",
"waitForTimeout",
"waitForLoadState",
diff --git a/package/ego-browser/scripts/real-browser-e2e/cases/keyboard-regression.mjs b/package/ego-browser/scripts/real-browser-e2e/cases/keyboard-regression.mjs
index 338d91f..893dea7 100644
--- a/package/ego-browser/scripts/real-browser-e2e/cases/keyboard-regression.mjs
+++ b/package/ego-browser/scripts/real-browser-e2e/cases/keyboard-regression.mjs
@@ -27,14 +27,14 @@ export function keyboardRegressionCase() {
const xpathValue = await evaluate("return document.querySelector('#text-input').value");
assertEqual(xpathValue, "via-xpath", "fill resolves xpath= selector (Issue 2)");
- /* Issue 2: dispatchKey shares the same resolver path. */
+ /* Issue 2: dispatchEvent shares the same resolver path. */
await evaluate("window.__fixtureState.keys = []; return true;");
- await dispatchKey('xpath=//input[@id="text-input"]', "Enter", "keydown");
+ await dispatchEvent('xpath=//input[@id="text-input"]', "keydown", { key: "Enter" });
const keys = await evaluate("return window.__fixtureState.keys.join(',')");
- assertIncludes(keys, "Enter", "dispatchKey resolves xpath= selector (Issue 2)");
+ assertIncludes(keys, "Enter", "dispatchEvent resolves xpath= selector (Issue 2)");
/* Issue 2: setInputFiles shares the same resolver path (the pilot covered
- fill/dispatchKey but left setInputFiles out). */
+ fill/dispatchEvent but left setInputFiles out). */
await setInputFiles('xpath=//input[@id="file-input"]', uploadPath);
const xpathFile = await evaluate("return Array.from(document.querySelector('#file-input').files).map((file) => file.name).join(',')");
assertEqual(xpathFile, "fixture-upload.txt", "setInputFiles resolves xpath= selector (Issue 2)");
diff --git a/package/ego-browser/scripts/real-browser-e2e/cases/keyboard.mjs b/package/ego-browser/scripts/real-browser-e2e/cases/keyboard.mjs
index 1d3918f..b644b02 100644
--- a/package/ego-browser/scripts/real-browser-e2e/cases/keyboard.mjs
+++ b/package/ego-browser/scripts/real-browser-e2e/cases/keyboard.mjs
@@ -33,9 +33,9 @@ export function keyboardCase() {
const activeAfterPress = await evaluate("return document.activeElement?.id || ''");
assertEqual(activeAfterPress, "text-input", "press keeps the focused input active");
- await dispatchKey("#text-input", "Escape", "keydown");
+ await dispatchEvent("#text-input", "keydown", { key: "Escape" });
let keys = await evaluate("return window.__fixtureState.keys.join(',')");
- assertIncludes(keys, "Escape", "dispatchKey dispatches synthetic keyboard input");
+ assertIncludes(keys, "Escape", "dispatchEvent dispatches synthetic keyboard input");
await fill("#text-input", "select me", { timeout: 3000 });
await press("ControlOrMeta+a");
@@ -65,9 +65,9 @@ export function keyboardCase() {
"setInputFiles reports missing file inputs"
);
await assertRejects(
- () => dispatchKey("#missing-input", "Enter"),
+ () => dispatchEvent("#missing-input", "keypress", { key: "Enter" }),
"Element not found",
- "dispatchKey reports missing targets"
+ "dispatchEvent reports missing targets"
);
/* contentEditable typing */
diff --git a/package/ego-browser/src/driver/keyboard.ts b/package/ego-browser/src/driver/keyboard.ts
index ff92a3a..5e626cf 100644
--- a/package/ego-browser/src/driver/keyboard.ts
+++ b/package/ego-browser/src/driver/keyboard.ts
@@ -205,21 +205,59 @@ export async function fill(selector, value, options: FillOptions = {}) {
});
}
+// Page-side dispatcher, mirroring Playwright's injected dispatchEvent: the type
+// selects the event constructor and eventInit is spread onto the same defaults
+// Playwright uses. Types outside this table (input/change, touch*, custom, ...)
+// fall back to a generic Event. Kept as a string for Runtime.callFunctionOn.
+const DISPATCH_EVENT_SOURCE = `function(type, eventInit){
+ const init = { bubbles: true, cancelable: true, composed: true, ...(eventInit || {}) };
+ const category = {
+ auxclick: "mouse", click: "mouse", dblclick: "mouse", mousedown: "mouse",
+ mouseenter: "mouse", mouseleave: "mouse", mousemove: "mouse", mouseout: "mouse",
+ mouseover: "mouse", mouseup: "mouse", mousewheel: "mouse",
+ keydown: "keyboard", keyup: "keyboard", keypress: "keyboard", textInput: "keyboard",
+ pointerover: "pointer", pointerout: "pointer", pointerenter: "pointer",
+ pointerleave: "pointer", pointerdown: "pointer", pointerup: "pointer",
+ pointermove: "pointer", pointercancel: "pointer", gotpointercapture: "pointer",
+ lostpointercapture: "pointer",
+ focus: "focus", blur: "focus",
+ dragstart: "drag", drag: "drag", dragend: "drag", dragenter: "drag",
+ dragleave: "drag", dragover: "drag", dragexit: "drag", drop: "drag",
+ wheel: "wheel"
+ };
+ let event;
+ switch (category[type]) {
+ case "mouse": event = new MouseEvent(type, init); break;
+ case "keyboard": event = new KeyboardEvent(type, init); break;
+ case "pointer": event = new PointerEvent(type, init); break;
+ case "focus": event = new FocusEvent(type, init); break;
+ case "drag": event = new DragEvent(type, init); break;
+ case "wheel": event = new WheelEvent(type, init); break;
+ default: event = new Event(type, init); break;
+ }
+ this.dispatchEvent(event);
+}`;
+
/**
- * Focus an element and dispatch a DOM KeyboardEvent in page JavaScript.
- * Note: dispatched event has isTrusted=false; some frameworks ignore it (see docs/issues/dispatchKey-synthetic-keyboard-event.md).
+ * Dispatch a synthetic DOM event on an element, mirroring Playwright's
+ * locator.dispatchEvent. The event type picks the constructor — keydown/keyup/
+ * keypress -> KeyboardEvent, click/mousedown/... -> MouseEvent, and pointer* /
+ * focus / blur / drag* / wheel -> their typed events; any other type (input,
+ * change, touch*, custom events, ...) uses a generic Event. eventInit is spread
+ * verbatim onto { bubbles: true, cancelable: true, composed: true } and passed
+ * to the constructor.
+ * Note: the dispatched event has isTrusted=false; some frameworks ignore it. For
+ * real keyboard input prefer press().
* @param {string} selector CSS selector / @ref / loc= / xpath= for the target element.
- * @param {string} [key="Enter"] Event key.
- * @param {"keydown"|"keypress"|"keyup"|string} [event="keypress"] Event type.
+ * @param {string} type DOM event type, e.g. "keydown", "click", "input".
+ * @param {Record<string, unknown>} [eventInit={}] Event-specific init properties (key, code, clientX, ...).
* @returns {Promise<void>}
*/
-export async function dispatchKey(selector, key = "Enter", event = "keypress") {
- const { vk, code } = keyDefinition(key);
- await resolveAndCall(
- selector,
- "function(keyCode, key, code, event){this.focus(); this.dispatchEvent(new KeyboardEvent(event,{key,code,keyCode,which:keyCode,bubbles:true}));}",
- [vk, key, code, event],
- );
+export async function dispatchEvent(selector, type, eventInit = {}) {
+ if (typeof type !== "string" || type === "") {
+ throw new Error("dispatchEvent requires an event type string");
+ }
+ await resolveAndCall(selector, DISPATCH_EVENT_SOURCE, [type, eventInit]);
}
function inputEventDelay() {
diff --git a/package/ego-browser/src/helpers.ts b/package/ego-browser/src/helpers.ts
index 0247a72..8343cb6 100644
--- a/package/ego-browser/src/helpers.ts
+++ b/package/ego-browser/src/helpers.ts
@@ -31,7 +31,7 @@ export {
wheel,
scrollIntoViewIfNeeded,
} from "./driver/pointer.js";
-export { press, insertText, fill, dispatchKey } from "./driver/keyboard.js";
+export { press, insertText, fill, dispatchEvent } from "./driver/keyboard.js";
export {
INTERNAL_URL_PREFIXES,
pageInfo,
diff --git a/skills/ego-browser/SKILL.md b/skills/ego-browser/SKILL.md
index 747507e..fc032b5 100644
--- a/skills/ego-browser/SKILL.md
+++ b/skills/ego-browser/SKILL.md
@@ -37,7 +37,7 @@ The heredoc body runs as a Node.js script that controls the selected ego-browser
- Navigation / state: `listTabs`, `openOrReuseTab`, `closeTab`, `goto`, `currentTab`, `switchTab`, `pageInfo`, `ensureRealTab`
- Observation: `snapshot`, `screenshot`, `drainEvents`
- Scroll / mouse: `wheel`, `scrollIntoViewIfNeeded`, `click`, `dblclick`, `hover`, `drag`
-- Keyboard & input: `insertText`, `fill`, `press`, `dispatchKey`
+- Keyboard & input: `insertText`, `fill`, `press`, `dispatchEvent`
- File: `setInputFiles`
- Wait: `waitForTimeout`, `waitForLoadState`, `waitForSelector`
- Fetch: `serverFetch`, `browserFetch`