Skip to content

Open-source AI browser agent for Chrome and Firefox (monorepo) 🧠

fix: attribute contenteditable gate and synchronous native edits

Barack Sokullu05083abf3c35
diff --git a/docs/page-feedback-monitor.md b/docs/page-feedback-monitor.md
index db4293156..9ff279731 100644
--- a/docs/page-feedback-monitor.md
+++ b/docs/page-feedback-monitor.md
@@ -55,6 +55,10 @@ Steer or grant authorization. Enter and Alt+Enter retain their existing behavior
- Content and Firefox native preparation handshakes check the revision without
claiming input events. Local dispatch and native markers activate expectations
at the mutation boundary, preserving same-target intervention during preparation.
+- Contenteditable fallback marks its cancellable `beforeinput` before emission.
+ A native edit's repeated input phases are attributed only within the synchronous
+ command and its target; cancelled gates retain their undispatched outcome, and
+ user intervention during the settling wait prevents the remaining insertion.
- Both Chrome upload paths check feedback after preparation, immediately before
attaching files. A skipped attachment retains its undispatched outcome.
- Firefox companion uploads register as input and place their native marker on
diff --git a/src/chrome/src/content/content.js b/src/chrome/src/content/content.js
index e7de5e04d..910a6ec51 100644
--- a/src/chrome/src/content/content.js
+++ b/src/chrome/src/content/content.js
@@ -2348,6 +2348,7 @@
// execCommand is the native editing fallback: it preserves markup and
// the editor's undo stack. Its beforeinput behavior differs by browser,
// so offer the page a cancellable gate BEFORE invoking the command.
+ window.__wbPageMonitor?.beforeLocalDispatch();
const accepted = el.dispatchEvent(new InputEvent('beforeinput', {
bubbles: true, composed: true, cancelable: true, inputType, data,
}));
@@ -2369,7 +2370,9 @@
window.__wbPageMonitor?.beforeLocalDispatch();
dispatched = true;
try {
- if (!doc.execCommand(command, false, data)) {
+ const execute = () => doc.execCommand(command, false, data);
+ const inserted = window.__wbPageMonitor?.withLocalDispatch ? window.__wbPageMonitor.withLocalDispatch(execute) : execute();
+ if (!inserted) {
return failure('The editor rejected native text entry. No DOM replacement was attempted.');
}
} catch (error) {
diff --git a/src/chrome/src/content/page-monitor.js b/src/chrome/src/content/page-monitor.js
index 92b1b90be..e24812b5a 100644
--- a/src/chrome/src/content/page-monitor.js
+++ b/src/chrome/src/content/page-monitor.js
@@ -124,7 +124,7 @@
if (op.eventTypes && !op.eventTypes.has(event.type)) return null;
// One native dispatch owns one occurrence of each input phase. A later
// human action on the same target must not fit that expectation again.
- if (event.type !== 'pointermove' && op.seenEvents?.has(event.type)) return null;
+ if (!op.synchronous && event.type !== 'pointermove' && op.seenEvents?.has(event.type)) return null;
op.seenEvents?.add(event.type); matchedEvents.set(event, op);
}
return op;
@@ -446,6 +446,18 @@
dispatch({ ...localOperation, navigationCandidate: !preparation && localOperation.navigationCandidate,
fenceOnly: preparation, runToken });
}
+ function withLocalDispatch(callback) {
+ beforeLocalDispatch();
+ const op = operations.get(localOperation?.operationId);
+ if (!op) return callback();
+ // One synchronous native edit can emit repeated input phases (multiline
+ // execCommand). Attribute only its target until the command returns, never
+ // across an asynchronous preparation or settling wait.
+ const previous = op.synchronous;
+ op.synchronous = true;
+ try { return callback(); }
+ finally { op.synchronous = previous; }
+ }
const onMessage = (msg, _sender, respond) => {
if (disposed || msg?.target !== 'content') return;
if (msg.action === 'page_monitor_state') {
@@ -476,7 +488,7 @@
}
};
api.runtime.onMessage.addListener(onMessage);
- window.__wbPageMonitor = { beginContentAction, beforeLocalDispatch, dispatch, registerDecoration,
+ window.__wbPageMonitor = { beginContentAction, beforeLocalDispatch, withLocalDispatch, dispatch, registerDecoration,
dispose() { disposed = true; requestGeneration++; stop(); api.runtime.onMessage.removeListener?.(onMessage); } };
void requestState();
// A document restored from BFCache needs a fresh run/document handshake.
diff --git a/src/firefox/src/content/content.js b/src/firefox/src/content/content.js
index 9b77d0a85..c8baed163 100644
--- a/src/firefox/src/content/content.js
+++ b/src/firefox/src/content/content.js
@@ -2664,6 +2664,7 @@
// execCommand is the native editing fallback: it preserves markup and
// the editor's undo stack. Its beforeinput behavior differs by browser,
// so offer the page a cancellable gate BEFORE invoking the command.
+ window.__wbPageMonitor?.beforeLocalDispatch();
const accepted = el.dispatchEvent(new InputEvent('beforeinput', {
bubbles: true, composed: true, cancelable: true, inputType, data,
}));
@@ -2685,7 +2686,9 @@
window.__wbPageMonitor?.beforeLocalDispatch();
dispatched = true;
try {
- if (!doc.execCommand(command, false, data)) {
+ const execute = () => doc.execCommand(command, false, data);
+ const inserted = window.__wbPageMonitor?.withLocalDispatch ? window.__wbPageMonitor.withLocalDispatch(execute) : execute();
+ if (!inserted) {
return failure('The editor rejected native text entry. No DOM replacement was attempted.');
}
} catch (error) {
diff --git a/src/firefox/src/content/page-monitor.js b/src/firefox/src/content/page-monitor.js
index 92b1b90be..e24812b5a 100644
--- a/src/firefox/src/content/page-monitor.js
+++ b/src/firefox/src/content/page-monitor.js
@@ -124,7 +124,7 @@
if (op.eventTypes && !op.eventTypes.has(event.type)) return null;
// One native dispatch owns one occurrence of each input phase. A later
// human action on the same target must not fit that expectation again.
- if (event.type !== 'pointermove' && op.seenEvents?.has(event.type)) return null;
+ if (!op.synchronous && event.type !== 'pointermove' && op.seenEvents?.has(event.type)) return null;
op.seenEvents?.add(event.type); matchedEvents.set(event, op);
}
return op;
@@ -446,6 +446,18 @@
dispatch({ ...localOperation, navigationCandidate: !preparation && localOperation.navigationCandidate,
fenceOnly: preparation, runToken });
}
+ function withLocalDispatch(callback) {
+ beforeLocalDispatch();
+ const op = operations.get(localOperation?.operationId);
+ if (!op) return callback();
+ // One synchronous native edit can emit repeated input phases (multiline
+ // execCommand). Attribute only its target until the command returns, never
+ // across an asynchronous preparation or settling wait.
+ const previous = op.synchronous;
+ op.synchronous = true;
+ try { return callback(); }
+ finally { op.synchronous = previous; }
+ }
const onMessage = (msg, _sender, respond) => {
if (disposed || msg?.target !== 'content') return;
if (msg.action === 'page_monitor_state') {
@@ -476,7 +488,7 @@
}
};
api.runtime.onMessage.addListener(onMessage);
- window.__wbPageMonitor = { beginContentAction, beforeLocalDispatch, dispatch, registerDecoration,
+ window.__wbPageMonitor = { beginContentAction, beforeLocalDispatch, withLocalDispatch, dispatch, registerDecoration,
dispose() { disposed = true; requestGeneration++; stop(); api.runtime.onMessage.removeListener?.(onMessage); } };
void requestState();
// A document restored from BFCache needs a fresh run/document handshake.
diff --git a/test/page-monitor-ui.mjs b/test/page-monitor-ui.mjs
index b5b78ec88..d92a49c85 100644
--- a/test/page-monitor-ui.mjs
+++ b/test/page-monitor-ui.mjs
@@ -48,6 +48,23 @@ async function fixture(engine, build, { runToken = 'test-run', siteIsolation = f
return { browser, context, page };
}
+async function installContentEditableFallback(page, build) {
+ const insertion = read(build, 'content/content.js').match(/^ async function _insertContentEditableText\([\s\S]*?^ }/m)?.[0];
+ assert.ok(insertion, 'Exercise the real contenteditable fallback with the real monitor');
+ await page.addScriptTag({ content: `window._fieldMeta = () => ({ contentEditable: true }); window.richTextInsertion = ${insertion};` });
+ await page.evaluate(() => {
+ const editor = document.createElement('div'); editor.id = 'rich-editor'; editor.contentEditable = 'true';
+ document.body.prepend(editor);
+ window.typeRichText = async ({ text, clear }) => {
+ editor.focus();
+ const finish = __wbPageMonitor.beginContentAction('type', { selector: '#rich-editor' });
+ try { return await richTextInsertion(editor, text, clear); }
+ catch (error) { return { success: false, code: error.code, dispatched: error.dispatched }; }
+ finally { finish(); }
+ };
+ });
+}
+
for (const siteIsolation of [false, true]) {
test(`Chrome CDP input reaches the receiving iframe monitor (site isolation: ${siteIsolation})`, { timeout: 30000 }, async () => {
const savedChrome = globalThis.chrome, savedBrowser = globalThis.browser;
@@ -359,6 +376,81 @@ for (const [build, engine] of [['chrome', chromium], ['firefox', firefox]]) {
} finally { await browser.close(); }
});
+ test(`${build}: contenteditable fallback attributes its cancellable gate and native edits`, async () => {
+ const { browser, page } = await fixture(engine, build);
+ try {
+ await installContentEditableFallback(page, build);
+ for (const behavior of ['append', 'replace', 'cancel', 'editor-handles']) {
+ await page.evaluate(behavior => {
+ const editor = document.getElementById('rich-editor');
+ editor.innerHTML = '<p>Original <strong>format</strong></p>';
+ window.originalStrong = editor.querySelector('strong'); window.gates = [];
+ editor.onbeforeinput = event => {
+ gates.push(event.inputType);
+ if (behavior === 'cancel') event.preventDefault();
+ if (behavior === 'editor-handles') { event.preventDefault(); editor.append(' handled'); }
+ };
+ }, behavior);
+ await page.waitForTimeout(200);
+ await page.evaluate(() => { feedback = []; });
+ const result = await page.evaluate(params => typeRichText(params), {
+ text: behavior === 'replace' ? 'New\ntext' : ' added', clear: behavior === 'replace',
+ });
+ if (behavior === 'cancel') {
+ assert.equal(result.cancelled, true, JSON.stringify(result)); assert.equal(result.noDispatch, true);
+ assert.equal(await page.locator('#rich-editor').innerHTML(), '<p>Original <strong>format</strong></p>');
+ } else if (behavior === 'editor-handles') {
+ assert.equal(result.success, false); assert.equal(result.mutationMayHaveOccurred, true, JSON.stringify(result));
+ assert.equal(await page.locator('#rich-editor').textContent(), 'Original format handled');
+ } else {
+ assert.equal(result.success, true, JSON.stringify(result)); assert.equal(result.verified, true);
+ if (behavior === 'append') assert.equal(await page.evaluate(() => document.getElementById('rich-editor').contains(originalStrong)), true);
+ else assert.equal(result.value, 'New\ntext');
+ }
+ await page.waitForTimeout(200);
+ assert.deepEqual(await page.evaluate(() => feedback), [], 'Own beforeinput, input and DOM effects must not trigger replanning');
+ }
+ await page.evaluate(() => {
+ const editor = document.getElementById('rich-editor'); editor.onbeforeinput = null; editor.textContent = 'Ready';
+ });
+ await page.waitForTimeout(200);
+ await page.evaluate(() => { feedback = []; });
+ assert.equal((await page.evaluate(() => typeRichText({ text: ' agent' }))).success, true);
+ await page.keyboard.press('x');
+ await page.waitForFunction(() => feedback.some(event => event.kind === 'input' && event.source === 'user'), null, { timeout: 1000 });
+ assert.equal(await page.locator('#rich-editor').textContent(), 'Ready agentx', 'A later same-target human edit remains observable');
+ } finally { await browser.close(); }
+ });
+
+ test(`${build}: contenteditable fallback skips remaining insertion after user intervention`, async () => {
+ const { browser, page } = await fixture(engine, build);
+ try {
+ await installContentEditableFallback(page, build);
+ await page.evaluate(() => { document.getElementById('rich-editor').textContent = 'Original'; });
+ await page.waitForTimeout(200);
+ await page.evaluate(() => {
+ feedback = []; const schedule = window.setTimeout;
+ window.setTimeout = (callback, delay, ...args) => {
+ if (delay === 30) {
+ window.editPaused = true;
+ window.resumeEdit = () => { window.setTimeout = schedule; schedule(callback, 0, ...args); };
+ return 0;
+ }
+ return schedule(callback, delay, ...args);
+ };
+ window.editResult = typeRichText({ text: 'Never inserted', clear: true });
+ });
+ await page.waitForFunction(() => window.editPaused, null, { timeout: 1000 });
+ await page.locator('#field').fill('Human intervention');
+ await page.waitForFunction(() => feedback.some(event => event.source === 'user'));
+ const result = await page.evaluate(async () => { resumeEdit(); return await editResult; });
+ assert.equal(result.success, false, JSON.stringify(result));
+ assert.equal(result.dispatched, true, 'The completed deletion retains its dispatched outcome');
+ assert.equal(await page.locator('#rich-editor').textContent(), '');
+ assert.equal(await page.locator('#field').inputValue(), 'Human intervention');
+ } finally { await browser.close(); }
+ });
+
test(`${build}: animation and extension decoration do not produce page feedback`, async () => {
const { browser, page } = await fixture(engine, build);
try {