Skip to content

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

Derive the stabilized resource text from the rendered text

Claudeb136d5c5398e
diff --git a/src/chrome/src/agent/agent.js b/src/chrome/src/agent/agent.js
index 90404a3c8..75fe01954 100644
--- a/src/chrome/src/agent/agent.js
+++ b/src/chrome/src/agent/agent.js
@@ -33925,43 +33925,33 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
// datetime, not as the relative time it renders ("8 min" turns into
// "9 min" by itself while a deletion waits on WebBrain's card), so a
// ticking timestamp does not make the same post another one.
- // Where the page breaks a line (a block box, a <br>) is part of what the
- // text reads as: it is kept as a separator, so an edit that only changes
- // the layout of the same text nodes is still another version of the post.
- const stableText = (node, breaks = new WeakMap()) => {
- const breaksLine = element => {
- if (element.tagName === 'BR') return true;
- if (!breaks.has(element)) {
- let display = 'inline';
- try { display = doc.defaultView.getComputedStyle(element).display || 'inline'; } catch { /* keep inline */ }
- breaks.set(element, display !== 'inline' && display !== 'contents' && display !== 'none');
- }
- return breaks.get(element);
- };
- let text = '';
- const pending = [node];
- while (pending.length) {
- const current = pending.pop();
- if (typeof current === 'string') text += current;
- else if (current.nodeType === 3) text += current.data;
- else if (current.nodeType === 1 && current.matches('time[datetime]')) text += `\u0000${current.getAttribute('datetime')}\u0000`;
- else if (current.nodeType === 1) {
- const separator = breaksLine(current) ? '\n' : '';
- if (separator) pending.push(separator);
- for (let index = current.childNodes.length - 1; index >= 0; index -= 1) pending.push(current.childNodes[index]);
- if (separator) text += separator;
- }
+ // The text is what the page renders (innerText: line breaks, case and
+ // white-space as styled), with each <time datetime> in it standing for its
+ // datetime, so an edit that only changes the presentation of the same text
+ // is still another version of the post.
+ const stableText = node => {
+ const marker = time => `\u0000${time.getAttribute('datetime')}\u0000`;
+ if (node.matches?.('time[datetime]')) return marker(node);
+ const rendered = element => (typeof element.innerText === 'string' ? element.innerText : element.textContent) || '';
+ let text = rendered(node);
+ let cursor = 0;
+ const times = [...node.querySelectorAll?.('time[datetime]') || []]
+ .filter(time => !time.parentElement?.closest('time[datetime]'));
+ for (const time of times) {
+ const shown = rendered(time);
+ const at = shown ? text.indexOf(shown, cursor) : -1;
+ if (at < 0) continue;
+ const replacement = marker(time);
+ text = text.slice(0, at) + replacement + text.slice(at + shown.length);
+ cursor = at + replacement.length;
}
return text;
};
// A node inside a <time datetime> is part of the timestamp that node already
// stands for, so it adds no text of its own (<time><span>8 min</span></time>).
- const stableTexts = nodes => {
- const breaks = new WeakMap();
- return [...new Set(nodes
- .filter(node => !node.parentElement?.closest('time[datetime]'))
- .map(node => stableText(node, breaks)).filter(value => value.length <= 25000))];
- };
+ const stableTexts = nodes => [...new Set(nodes
+ .filter(node => !node.parentElement?.closest('time[datetime]'))
+ .map(stableText).filter(value => value.length <= 25000))];
const nativeResourceProofs = () => {
if (args.__wbNativeResourceAdapter !== 'mastodon') return [];
const signedIn = nativeSignedInAccount();
diff --git a/src/firefox/src/agent/agent.js b/src/firefox/src/agent/agent.js
index 7d65a1ad8..a07795bd5 100644
--- a/src/firefox/src/agent/agent.js
+++ b/src/firefox/src/agent/agent.js
@@ -30793,43 +30793,33 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
// datetime, not as the relative time it renders ("8 min" turns into
// "9 min" by itself while a deletion waits on WebBrain's card), so a
// ticking timestamp does not make the same post another one.
- // Where the page breaks a line (a block box, a <br>) is part of what the
- // text reads as: it is kept as a separator, so an edit that only changes
- // the layout of the same text nodes is still another version of the post.
- const stableText = (node, breaks = new WeakMap()) => {
- const breaksLine = element => {
- if (element.tagName === 'BR') return true;
- if (!breaks.has(element)) {
- let display = 'inline';
- try { display = doc.defaultView.getComputedStyle(element).display || 'inline'; } catch { /* keep inline */ }
- breaks.set(element, display !== 'inline' && display !== 'contents' && display !== 'none');
- }
- return breaks.get(element);
- };
- let text = '';
- const pending = [node];
- while (pending.length) {
- const current = pending.pop();
- if (typeof current === 'string') text += current;
- else if (current.nodeType === 3) text += current.data;
- else if (current.nodeType === 1 && current.matches('time[datetime]')) text += `\u0000${current.getAttribute('datetime')}\u0000`;
- else if (current.nodeType === 1) {
- const separator = breaksLine(current) ? '\n' : '';
- if (separator) pending.push(separator);
- for (let index = current.childNodes.length - 1; index >= 0; index -= 1) pending.push(current.childNodes[index]);
- if (separator) text += separator;
- }
+ // The text is what the page renders (innerText: line breaks, case and
+ // white-space as styled), with each <time datetime> in it standing for its
+ // datetime, so an edit that only changes the presentation of the same text
+ // is still another version of the post.
+ const stableText = node => {
+ const marker = time => `\u0000${time.getAttribute('datetime')}\u0000`;
+ if (node.matches?.('time[datetime]')) return marker(node);
+ const rendered = element => (typeof element.innerText === 'string' ? element.innerText : element.textContent) || '';
+ let text = rendered(node);
+ let cursor = 0;
+ const times = [...node.querySelectorAll?.('time[datetime]') || []]
+ .filter(time => !time.parentElement?.closest('time[datetime]'));
+ for (const time of times) {
+ const shown = rendered(time);
+ const at = shown ? text.indexOf(shown, cursor) : -1;
+ if (at < 0) continue;
+ const replacement = marker(time);
+ text = text.slice(0, at) + replacement + text.slice(at + shown.length);
+ cursor = at + replacement.length;
}
return text;
};
// A node inside a <time datetime> is part of the timestamp that node already
// stands for, so it adds no text of its own (<time><span>8 min</span></time>).
- const stableTexts = nodes => {
- const breaks = new WeakMap();
- return [...new Set(nodes
- .filter(node => !node.parentElement?.closest('time[datetime]'))
- .map(node => stableText(node, breaks)).filter(value => value.length <= 25000))];
- };
+ const stableTexts = nodes => [...new Set(nodes
+ .filter(node => !node.parentElement?.closest('time[datetime]'))
+ .map(stableText).filter(value => value.length <= 25000))];
const nativeResourceProofs = () => {
if (args.__wbNativeResourceAdapter !== 'mastodon') return [];
const signedIn = nativeSignedInAccount();
diff --git a/test/native-resource-deletion-binding.mjs b/test/native-resource-deletion-binding.mjs
index c6d515c69..647f720f2 100644
--- a/test/native-resource-deletion-binding.mjs
+++ b/test/native-resource-deletion-binding.mjs
@@ -343,7 +343,7 @@ for (const build of ['chrome', 'firefox']) {
// The same text nodes, laid out differently (a line break appears), read
// as another version of the post.
- await t.test('an edit that only changes where the post breaks lines is another post', async () => {
+ await t.test('an edit that only changes how the text of the post renders is another post', async () => {
const f = await fixture();
try {
const request = `Delete only ${resource}.`;
@@ -361,6 +361,10 @@ for (const build of ['chrome', 'firefox']) {
assert.equal(key(await f.probe('#confirm')), key(detected), 'Unchanged layout is the same post');
await f.page.evaluate(() => { document.getElementById('second-part').style.display = 'block'; });
assert.notEqual(key(await f.probe('#confirm')), key(detected), 'A new line break is another version');
+ await f.page.evaluate(() => { document.getElementById('second-part').style.display = ''; });
+ assert.equal(key(await f.probe('#confirm')), key(detected), 'Restoring the layout restores the post');
+ await f.page.evaluate(() => { document.getElementById('first-part').style.textTransform = 'uppercase'; });
+ assert.notEqual(key(await f.probe('#confirm')), key(detected), 'Rendered case is part of the post');
} finally { await f.page.close(); }
});