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
Match a timestamp in one representation and bound the timestamps scanned
Claude59e605ad8862
diff --git a/src/chrome/src/agent/agent.js b/src/chrome/src/agent/agent.jsindex b52a65763..f3b877802 100644--- a/src/chrome/src/agent/agent.js+++ b/src/chrome/src/agent/agent.js@@ -33938,11 +33938,17 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout dlet cursor = 0;const times = [...node.querySelectorAll?.('time[datetime]') || []].filter(time => !time.parentElement?.closest('time[datetime]'));+ // Beyond this many timestamps the text is left as it is rendered: it+ // still binds the post, and a ticking clock only makes it refuse.+ if (times.length > 16) return text;let previous = null;for (const time of times) {- const shown = rendered(time);- // The occurrence that belongs to this element: as many equal strings- // as the text between the previous timestamp and this one holds come first.+ const shown = rendered(time).trim();+ // One representation for counting and for replacing: the rendered text+ // of the timestamp, in any case and white-space.+ const pattern = () => new RegExp(shown.split(/\s+/).map(word => word.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')).join('\\s+'), 'gi');+ // The occurrence that belongs to this element: as many matches as the+ // rendered text between the previous timestamp and this one holds come first.let skip = 0;if (shown) {try {@@ -33956,23 +33962,20 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout dconst owner = part.parentElement;if (owner && (typeof owner.checkVisibility !== 'function' || owner.checkVisibility({ visibilityProperty: true }))) between += part.data;}- // Compared as rendered: case and white-space as styled are folded on both sides.- const fold = value => value.replace(/\s+/g, ' ').toLowerCase();- const needle = fold(shown), haystack = fold(between);- for (let from = needle ? haystack.indexOf(needle) : -1; from >= 0; from = haystack.indexOf(needle, from + needle.length)) skip += 1;+ skip = (between.match(pattern()) || []).length;} catch { skip = 0; }}previous = time;- let at = -1;- for (let n = 0, from = cursor; shown && n <= skip; n += 1) {- at = text.indexOf(shown, from);- if (at < 0) break;- from = at + shown.length;+ let found = null;+ if (shown) {+ const search = pattern();+ search.lastIndex = cursor;+ for (let n = 0; n <= skip; n += 1) { found = search.exec(text); if (!found) break; }}- if (at < 0) continue;+ if (!found) continue;const replacement = marker(time);- text = text.slice(0, at) + replacement + text.slice(at + shown.length);- cursor = at + replacement.length;+ text = text.slice(0, found.index) + replacement + text.slice(found.index + found[0].length);+ cursor = found.index + replacement.length;}return text;};diff --git a/src/firefox/src/agent/agent.js b/src/firefox/src/agent/agent.jsindex b762ebf8b..d2c91e8dd 100644--- a/src/firefox/src/agent/agent.js+++ b/src/firefox/src/agent/agent.js@@ -30806,11 +30806,17 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout dlet cursor = 0;const times = [...node.querySelectorAll?.('time[datetime]') || []].filter(time => !time.parentElement?.closest('time[datetime]'));+ // Beyond this many timestamps the text is left as it is rendered: it+ // still binds the post, and a ticking clock only makes it refuse.+ if (times.length > 16) return text;let previous = null;for (const time of times) {- const shown = rendered(time);- // The occurrence that belongs to this element: as many equal strings- // as the text between the previous timestamp and this one holds come first.+ const shown = rendered(time).trim();+ // One representation for counting and for replacing: the rendered text+ // of the timestamp, in any case and white-space.+ const pattern = () => new RegExp(shown.split(/\s+/).map(word => word.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')).join('\\s+'), 'gi');+ // The occurrence that belongs to this element: as many matches as the+ // rendered text between the previous timestamp and this one holds come first.let skip = 0;if (shown) {try {@@ -30824,23 +30830,20 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout dconst owner = part.parentElement;if (owner && (typeof owner.checkVisibility !== 'function' || owner.checkVisibility({ visibilityProperty: true }))) between += part.data;}- // Compared as rendered: case and white-space as styled are folded on both sides.- const fold = value => value.replace(/\s+/g, ' ').toLowerCase();- const needle = fold(shown), haystack = fold(between);- for (let from = needle ? haystack.indexOf(needle) : -1; from >= 0; from = haystack.indexOf(needle, from + needle.length)) skip += 1;+ skip = (between.match(pattern()) || []).length;} catch { skip = 0; }}previous = time;- let at = -1;- for (let n = 0, from = cursor; shown && n <= skip; n += 1) {- at = text.indexOf(shown, from);- if (at < 0) break;- from = at + shown.length;+ let found = null;+ if (shown) {+ const search = pattern();+ search.lastIndex = cursor;+ for (let n = 0; n <= skip; n += 1) { found = search.exec(text); if (!found) break; }}- if (at < 0) continue;+ if (!found) continue;const replacement = marker(time);- text = text.slice(0, at) + replacement + text.slice(at + shown.length);- cursor = at + replacement.length;+ text = text.slice(0, found.index) + replacement + text.slice(found.index + found[0].length);+ cursor = found.index + replacement.length;}return text;};diff --git a/test/native-resource-deletion-binding.mjs b/test/native-resource-deletion-binding.mjsindex 17bc7dafc..36055341a 100644--- a/test/native-resource-deletion-binding.mjs+++ b/test/native-resource-deletion-binding.mjs@@ -407,6 +407,25 @@ for (const build of ['chrome', 'firefox']) {} finally { await f.page.close(); }});+ await t.test('preformatted text that renders like the relative time does not take the timestamp\'s place', async () => {+ const f = await fixture();+ try {+ const request = `Delete only ${resource}.`;+ const provider = { chat() {} }, agent = new Agent({ getActive: () => provider });+ agent.conversations.set(1, [{ role: 'system', content: 'system' }, { role: 'user', content: request }]);+ agent._startPlanExecutionGuard(1, 'act', { requestKind: 'execute', requiresStateChange: true, requiresSubmission: true });+ await f.page.evaluate(() => {+ document.querySelector('#record-200 .status__relative-time').innerHTML+ = '<span style="white-space:pre">8 dk</span> <time datetime="2026-10-18T19:22:00.000Z" title="18 Eki 2026 22:22">8 dk</time>';+ });+ await f.showConfirmation();+ const detected = await f.probe('#confirm');+ const key = found => agent._resourceMutationActionKey(1, 'click', { selector: '#confirm' }, found);+ await f.page.evaluate(() => { document.querySelector('#record-200 .status__relative-time time').textContent = '9 dk'; });+ assert.equal(key(await f.probe('#confirm')), key(detected), 'Only the timestamp ticked');+ } finally { await f.page.close(); }+ });+await t.test('hidden text equal to the relative time does not hide the timestamp from the key', async () => {const f = await fixture();try {