Open-source AI browser agent for Chrome and Firefox (monorepo) ðŸ§
JavaScript ai-agentai-agentsai-toolsbrowser-aibrowser-automationbrowser-extensionchrome-aichrome-devtoolschrome-extensionchrome-extensionsfirefox-addonfirefox-addonsfirefox-extensionfirefox-extensionsgemmagemma4minimaxoffline-aioffline-llmqwen
Keep a Mastodon post's menu bound when it opens late or sits beside the post
Clauded25c39fe9386
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex f04c109a1..b63a81355 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -7,6 +7,7 @@ This changelog was generated from the repository Git history and release tags. V## [Unreleased]### Fixed+- Deleting your own Mastodon post through its "…" menu works again on current Mastodon (4.4): the menu fades in a moment after the click, so WebBrain lost track of which post it belonged to and refused "Delete" ("Sil" on a Turkish server) as not belonging to any post. The menu the agent's own click opens is now kept for up to three seconds while it appears, and the menu on a post's own page counts as that post's, since Mastodon draws it beside the post rather than inside it.- Preserve exact editor identity and private values through owned focus and scroll preparation, while permitting the resulting geometry changes.- Bind native input to the certified receiving control, allowing passive descendants while rejecting unrelated or nested activators.- Preserve acknowledged click delivery through page rendering and focus restoration, with separate pending effect verification and repeat protection.diff --git a/src/chrome/src/agent/agent.js b/src/chrome/src/agent/agent.jsindex c4b715652..e594f894a 100644--- a/src/chrome/src/agent/agent.js+++ b/src/chrome/src/agent/agent.js@@ -28293,6 +28293,27 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d}return true;};+ // As nativeResourceVisible, but an element fading in from transparent+ // counts: an app's opening animation starts at opacity 0.+ const nativeResourceRendered = element => {+ if (!element?.isConnected || !isVisible(element)) return false;+ for (let node = element; node; node = node.parentElement) {+ const style = getComputedStyle(node);+ if (node.matches('[hidden],[inert],[aria-hidden="true"]') || style.display === 'none'+ || ['hidden', 'collapse'].includes(style.visibility) || style.contentVisibility === 'hidden') return false;+ }+ return true;+ };+ // The native record an element belongs to: its own status, or for the+ // action bar Mastodon renders beside a detailed status (not inside it),+ // the one detailed status in the same wrapper.+ const nativeRecordOf = element => {+ const own = element?.closest?.('.status,.detailed-status');+ if (own) return own;+ const bar = element?.closest?.('.detailed-status__action-bar');+ const records = [...(bar?.closest?.('.detailed-status__wrapper')?.querySelectorAll('.detailed-status') || [])];+ return records.length === 1 ? records[0] : null;+ };const nativeResourcePopups = () => {const native = '.dropdown-menu,.modal-root__modal.safety-action-modal';const nodes = [...doc.querySelectorAll(`${native},dialog,[role="dialog"],[role="alertdialog"],[role="menu"]`)];@@ -28364,7 +28385,7 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout dconst records = resourceProofs();const ownerSelector = 'article,[role="article"],[role="listitem"],[itemid]'+ (args.__wbNativeResourceAdapter === 'mastodon' ? ',.status,.detailed-status' : '');- const owner = target?.closest?.(ownerSelector);+ const owner = (args.__wbNativeResourceAdapter === 'mastodon' && nativeRecordOf(target)) || target?.closest?.(ownerSelector);let matches = owner ? records.filter(record => record._record === owner) : [];const scope = args.__wbOwnedResourceScope;// An existing owner is authoritative even when its identity is unknown.@@ -28433,21 +28454,30 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout ddoc.removeEventListener('click', listener, true);clearTimeout(binding.listenerTimer);if (binding.until < Date.now() || !nativeResourceVisible(control)- || (nativeKind === 'menu' && control.closest('.status,.detailed-status') !== record)+ || (nativeKind === 'menu' && nativeRecordOf(control) !== record)|| (sourcePopup && control.closest('.dropdown-menu') !== sourcePopup)) return;binding.clicked = true;- // React's native dropdown/menu commits synchronously in this- // click. Capture that exact new node after the handler returns,- // never a later unrelated popup or a last-resource fallback.- setTimeout(() => {- const visible = popups().filter(nativeResourceVisible);- const selector = nativeKind === 'menu' ? '.dropdown-menu' : '.modal-root__modal.safety-action-modal';- if (visible.length === 1 && !before.has(visible[0]) && visible[0].matches(selector)- && (!sourcePopup || !nativeResourceVisible(sourcePopup))) binding.popup = visible[0];- }, 0);+ // The app opens its menu or modal in reply to this click: a menu+ // can mount a frame later and fades in from transparent, a modal+ // can load a moment later. Capture the one new popup that appears+ // within three seconds, judged by structure, while it is the only+ // one shown and the menu it came from has closed; never an+ // unrelated popup that was already there, or a last-resource+ // fallback.+ const selector = nativeKind === 'menu' ? '.dropdown-menu' : '.modal-root__modal.safety-action-modal';+ const deadline = Date.now() + 3000;+ const capture = () => {+ if (binding.popup || binding.until < Date.now()) return;+ const shown = popups().filter(nativeResourceRendered);+ if (shown.length === 1 && !before.has(shown[0]) && shown[0].matches(selector)+ && (!sourcePopup || !nativeResourceRendered(sourcePopup))) { binding.popup = shown[0]; return; }+ if (Date.now() < deadline) binding.captureTimer = setTimeout(capture, 50);+ };+ binding.captureTimer = setTimeout(capture, 0);};doc.addEventListener('click', listener, true);- binding.cleanup = () => { doc.removeEventListener('click', listener, true); clearTimeout(binding.listenerTimer); };+ binding.cleanup = () => { doc.removeEventListener('click', listener, true); clearTimeout(binding.listenerTimer);+ clearTimeout(binding.captureTimer); };binding.listenerTimer = setTimeout(() => { binding.cleanup(); scopeRegistry.launchers.delete(token); }, 120000);while (scopeRegistry.launchers.size > 256) {const oldest = scopeRegistry.launchers.keys().next().value;diff --git a/src/firefox/src/agent/agent.js b/src/firefox/src/agent/agent.jsindex 33798a376..fec9d4a1f 100644--- a/src/firefox/src/agent/agent.js+++ b/src/firefox/src/agent/agent.js@@ -25613,6 +25613,27 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d}return true;};+ // As nativeResourceVisible, but an element fading in from transparent+ // counts: an app's opening animation starts at opacity 0.+ const nativeResourceRendered = element => {+ if (!element?.isConnected || !isVisible(element)) return false;+ for (let node = element; node; node = node.parentElement) {+ const style = getComputedStyle(node);+ if (node.matches('[hidden],[inert],[aria-hidden="true"]') || style.display === 'none'+ || ['hidden', 'collapse'].includes(style.visibility) || style.contentVisibility === 'hidden') return false;+ }+ return true;+ };+ // The native record an element belongs to: its own status, or for the+ // action bar Mastodon renders beside a detailed status (not inside it),+ // the one detailed status in the same wrapper.+ const nativeRecordOf = element => {+ const own = element?.closest?.('.status,.detailed-status');+ if (own) return own;+ const bar = element?.closest?.('.detailed-status__action-bar');+ const records = [...(bar?.closest?.('.detailed-status__wrapper')?.querySelectorAll('.detailed-status') || [])];+ return records.length === 1 ? records[0] : null;+ };const nativeResourcePopups = () => {const native = '.dropdown-menu,.modal-root__modal.safety-action-modal';const nodes = [...doc.querySelectorAll(`${native},dialog,[role="dialog"],[role="alertdialog"],[role="menu"]`)];@@ -25684,7 +25705,7 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout dconst records = resourceProofs();const ownerSelector = 'article,[role="article"],[role="listitem"],[itemid]'+ (args.__wbNativeResourceAdapter === 'mastodon' ? ',.status,.detailed-status' : '');- const owner = target?.closest?.(ownerSelector);+ const owner = (args.__wbNativeResourceAdapter === 'mastodon' && nativeRecordOf(target)) || target?.closest?.(ownerSelector);let matches = owner ? records.filter(record => record._record === owner) : [];const scope = args.__wbOwnedResourceScope;// An existing owner is authoritative even when its identity is unknown.@@ -25753,21 +25774,30 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout ddoc.removeEventListener('click', listener, true);clearTimeout(binding.listenerTimer);if (binding.until < Date.now() || !nativeResourceVisible(control)- || (nativeKind === 'menu' && control.closest('.status,.detailed-status') !== record)+ || (nativeKind === 'menu' && nativeRecordOf(control) !== record)|| (sourcePopup && control.closest('.dropdown-menu') !== sourcePopup)) return;binding.clicked = true;- // React's native dropdown/menu commits synchronously in this- // click. Capture that exact new node after the handler returns,- // never a later unrelated popup or a last-resource fallback.- setTimeout(() => {- const visible = popups().filter(nativeResourceVisible);- const selector = nativeKind === 'menu' ? '.dropdown-menu' : '.modal-root__modal.safety-action-modal';- if (visible.length === 1 && !before.has(visible[0]) && visible[0].matches(selector)- && (!sourcePopup || !nativeResourceVisible(sourcePopup))) binding.popup = visible[0];- }, 0);+ // The app opens its menu or modal in reply to this click: a menu+ // can mount a frame later and fades in from transparent, a modal+ // can load a moment later. Capture the one new popup that appears+ // within three seconds, judged by structure, while it is the only+ // one shown and the menu it came from has closed; never an+ // unrelated popup that was already there, or a last-resource+ // fallback.+ const selector = nativeKind === 'menu' ? '.dropdown-menu' : '.modal-root__modal.safety-action-modal';+ const deadline = Date.now() + 3000;+ const capture = () => {+ if (binding.popup || binding.until < Date.now()) return;+ const shown = popups().filter(nativeResourceRendered);+ if (shown.length === 1 && !before.has(shown[0]) && shown[0].matches(selector)+ && (!sourcePopup || !nativeResourceRendered(sourcePopup))) { binding.popup = shown[0]; return; }+ if (Date.now() < deadline) binding.captureTimer = setTimeout(capture, 50);+ };+ binding.captureTimer = setTimeout(capture, 0);};doc.addEventListener('click', listener, true);- binding.cleanup = () => { doc.removeEventListener('click', listener, true); clearTimeout(binding.listenerTimer); };+ binding.cleanup = () => { doc.removeEventListener('click', listener, true); clearTimeout(binding.listenerTimer);+ clearTimeout(binding.captureTimer); };binding.listenerTimer = setTimeout(() => { binding.cleanup(); scopeRegistry.launchers.delete(token); }, 120000);while (scopeRegistry.launchers.size > 256) {const oldest = scopeRegistry.launchers.keys().next().value;diff --git a/test/native-resource-deletion-binding.mjs b/test/native-resource-deletion-binding.mjsindex f9fe7280a..de0cb38be 100644--- a/test/native-resource-deletion-binding.mjs+++ b/test/native-resource-deletion-binding.mjs@@ -58,6 +58,68 @@ for (const build of ['chrome', 'firefox']) {} finally { await f.page.close(); }});+ // Mastodon mounts its menu a moment after the click and fades it in from+ // transparent (.dropdown-animation, 250 ms); its confirmation modal can+ // load a moment later still. The menu the agent's own click opened is+ // still that post's menu.+ await t.test('a menu and confirmation that open late and fade in keep the exact resource', async () => {+ const f = await fixture();+ try {+ await f.page.evaluate(() => {+ const style = document.createElement('style');+ style.textContent = '.dropdown-animation{animation:qa-dropdown 250ms cubic-bezier(.1,.7,.1,1)}'+ + '@keyframes qa-dropdown{from{opacity:0}to{opacity:1}}';+ document.head.append(style);+ const open = window.qaOpenMenu;+ window.qaOpenMenu = () => setTimeout(() => {+ open();+ for (const menu of document.querySelectorAll('#portal .dropdown-menu')) menu.classList.add('dropdown-animation');+ const remove = document.querySelector('#delete');+ const confirm = remove.onclick;+ remove.onclick = () => setTimeout(confirm, 120);+ }, 40);+ document.querySelector('#more-200').onclick = qaOpenMenu; document.querySelector('#more-201').onclick = qaOpenMenu;+ });+ const more = await f.open();+ assert.equal(more.actionResource.url, resource);+ await f.page.waitForTimeout(400);+ const next = await f.probe('#delete');+ assert.equal(next.actionResource?.url, resource, 'The late, fading menu is the one the agent opened');+ assert.equal(next.mutationKind, 'delete'); assert.ok(next.popupLauncherToken);+ f.adopt(next); await f.click('#delete'); await f.page.waitForTimeout(400);+ const final = await f.probe('#confirm');+ assert.equal(final.mutationKind, 'delete'); assert.equal(final.semanticCommit, true);+ assert.equal(final.actionResource?.url, resource, 'The late confirmation is the one the agent opened');+ } finally { await f.page.close(); }+ });++ // On a post's own page Mastodon renders the action bar beside the+ // detailed status, inside their shared wrapper, not inside the status.+ await t.test('the detailed status page menu keeps the exact resource', async () => {+ const f = await fixture();+ try {+ await f.page.evaluate(() => {+ document.querySelector('main').innerHTML = '<div class="focusable detailed-status__wrapper" tabindex="0">'+ + '<div class="detailed-status" id="detailed"><a class="detailed-status__display-name" href="/@ownedqa">@ownedqa</a>'+ + '<div class="status__content__text--visible"><p>Exact owned post 200</p></div><div class="detailed-status__meta">'+ + '<a class="detailed-status__datetime" href="/@ownedqa/200">Now</a></div></div><div class="detailed-status__action-bar">'+ + '<div class="detailed-status__action-bar-dropdown"><button type="button" id="detailed-more" title="More">'+ + '<svg class="icon icon-ellipsis-h" aria-hidden="true"></svg></button></div></div></div>';+ document.querySelector('#detailed-more').onclick = qaOpenMenu;+ });+ const more = await f.probe('#detailed-more');+ assert.equal(more.actionResource?.url, resource, 'The action bar beside the detailed status belongs to it');+ assert.ok(more.popupLauncherToken);+ f.adopt(more); await f.click('#detailed-more');+ const next = await f.probe('#delete');+ assert.equal(next.actionResource?.url, resource); assert.equal(next.mutationKind, 'delete');+ // Two detailed statuses in one wrapper leave the bar's owner unknown.+ await f.page.evaluate(() => document.querySelector('.detailed-status__wrapper')+ .prepend(document.querySelector('#detailed').cloneNode(true)));+ assert.equal((await f.probe('#detailed-more'))?.actionResource, null);+ } finally { await f.page.close(); }+ });+for (const preference of ['disabled', 'missing']) await t.test(`${preference} confirmation preference retains direct deletion`, async () => {const f = await fixture();try {