Skip to content

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

审查:加固百度贴吧点赞控件识别

16Miku8a2305c3fe3d
diff --git a/src/chrome/src/agent/adapters.js b/src/chrome/src/agent/adapters.js
index 138d35602..e7507a8a3 100644
--- a/src/chrome/src/agent/adapters.js
+++ b/src/chrome/src/agent/adapters.js
@@ -16153,7 +16153,7 @@ const ADAPTERS = [
notes: `
- Tieba thread pages use a custom Vue action bar. On the first floor, the share, reply, like, and collect actions are icon-plus-count elements rather than native buttons; use the accessibility tree or interactive-element list and prefer the named "点赞" control over screenshot coordinates.
- Reply-level likes are separate "赞" controls in each comment row. Re-read the active post or comment container after scrolling, switching "只看楼主", or changing 热门/正序/倒序; indices and visible rows can change.
-- "点赞", "关注", "回复", and "发贴" change account or public state. Perform them only when explicitly requested, then verify the icon/count or resulting state; a successful mouse dispatch alone is not proof.
+- "点赞", "关注", "回复", and "发帖" change account or public state. Perform them only when explicitly requested, then verify the icon/count or resulting state; a successful mouse dispatch alone is not proof.
- The first-floor action bar may be below the initial viewport, while images open a viewer when clicked. Do not click nearby image coordinates or repeat a coordinate after an unexpected viewer opens; close or go back, then re-read the page.
- Public pages may require Baidu sign-in. If "登录", QR verification, or "百度安全验证" appears, stop for the user and do not bypass, retry, or claim that the requested action succeeded.`,
},
diff --git a/src/chrome/src/cdp/cdp-client.js b/src/chrome/src/cdp/cdp-client.js
index 70c2215df..19777eb55 100644
--- a/src/chrome/src/cdp/cdp-client.js
+++ b/src/chrome/src/cdp/cdp-client.js
@@ -2921,8 +2921,8 @@ export class CDPClient {
['.follow-button', '关注'], ['.send-btn', '发布评论'],
['.publish-btn', '发布'], ['.publish-button', '发布'],
] : onHost('tieba.baidu.com') ? [
- ['.pc-pb-first-floor-interactive .action-item:has(use[*|href="#agree_pb"])', '点赞'],
- ['.pc-pb-comments-desc .zan-container-dark:has(use[*|href="#agree_comment"])', '赞'],
+ ['.pc-pb-first-floor-interactive .action-item', '点赞', '#agree_pb'],
+ ['.pc-pb-comments-desc .zan-container-dark', '赞', '#agree_comment'],
] : [];
const SELECTORS = [
'a[href]', 'button', 'input:not([type="hidden"])', 'textarea', 'select',
@@ -2933,10 +2933,34 @@ export class CDPClient {
...SITE_RULES.map(([selector]) => selector)
];
+ function matchesSiteRule(el, [selector, , iconHref]) {
+ try {
+ if (!el.matches(selector)) return false;
+ } catch (e) {
+ return false;
+ }
+ if (!iconHref) return true;
+ return Array.from(el.querySelectorAll('use')).some((use) => (
+ use.getAttribute('href') === iconHref
+ || use.getAttribute('xlink:href') === iconHref
+ ));
+ }
+
+ function matchesAnySiteSelector(el) {
+ return SITE_RULES.some(([selector]) => {
+ try {
+ return el.matches(selector);
+ } catch (e) {
+ return false;
+ }
+ });
+ }
+
function interactiveText(el) {
- for (const [selector, label] of SITE_RULES) {
+ for (const rule of SITE_RULES) {
+ const [selector, label] = rule;
try {
- if (!el.matches(selector)) continue;
+ if (!matchesSiteRule(el, rule)) continue;
} catch (e) {
continue;
}
@@ -2980,6 +3004,7 @@ export class CDPClient {
let index = 0;
all.forEach((el) => {
if (!isVisiblyInteractive(el)) return;
+ if (matchesAnySiteSelector(el) && !SITE_RULES.some(rule => matchesSiteRule(el, rule))) return;
const rect = el.getBoundingClientRect();
elements.push({
index: index++,
diff --git a/src/chrome/src/content/accessibility-tree.js b/src/chrome/src/content/accessibility-tree.js
index ca0dc8d41..5c94acce8 100644
--- a/src/chrome/src/content/accessibility-tree.js
+++ b/src/chrome/src/content/accessibility-tree.js
@@ -156,8 +156,10 @@
['.publish-button', '发布'],
],
baiduTieba: [
- ['.pc-pb-first-floor-interactive .action-item:has(use[*|href="#agree_pb"])', '点赞'],
- ['.pc-pb-comments-desc .zan-container-dark:has(use[*|href="#agree_comment"])', '赞'],
+ // Match the stable action wrapper, then inspect SVG href/xlink:href via
+ // the DOM API because namespaced attribute selectors vary by engine.
+ ['.pc-pb-first-floor-interactive .action-item', '点赞', '#agree_pb'],
+ ['.pc-pb-comments-desc .zan-container-dark', '赞', '#agree_comment'],
],
};
@@ -177,12 +179,19 @@
function getSiteInteractionDescriptor(el) {
if (!el || typeof el.matches !== 'function') return null;
- for (const [selector, label] of currentSiteInteractionConfig().rules) {
+ for (const [selector, label, iconHref] of currentSiteInteractionConfig().rules) {
try {
if (!el.matches(selector)) continue;
} catch {
continue;
}
+ if (iconHref) {
+ const hasIcon = Array.from(el.querySelectorAll?.('use') || []).some(use => (
+ use.getAttribute('href') === iconHref
+ || use.getAttribute('xlink:href') === iconHref
+ ));
+ if (!hasIcon) continue;
+ }
const explicit = String(
el.getAttribute('aria-label') || el.getAttribute('title') || ''
).replace(/\s+/g, ' ').trim();
diff --git a/src/chrome/src/content/content.js b/src/chrome/src/content/content.js
index 27f77f6fd..823190c1d 100644
--- a/src/chrome/src/content/content.js
+++ b/src/chrome/src/content/content.js
@@ -258,6 +258,18 @@
}
}
+ function _isSiteSelectorCandidate(el) {
+ try {
+ return _siteInteractiveSelectors().some(selector => el.matches(selector));
+ } catch {
+ return false;
+ }
+ }
+
+ function _isUsableSiteInteractive(el) {
+ return !_isSiteSelectorCandidate(el) || _isSiteInteractive(el);
+ }
+
function _composedParent(node) {
if (!node) return null;
if (node.assignedSlot) return node.assignedSlot;
@@ -516,6 +528,7 @@
const out = [];
for (const el of all) {
if (!isVisiblyInteractive(el)) continue;
+ if (!_isUsableSiteInteractive(el)) continue;
// If a modal is open, only include elements that are inside it.
// This prevents the agent from seeing (and accidentally clicking)
// elements behind the overlay — the #1 cause of "clicked Export
@@ -3064,6 +3077,7 @@
try {
root.querySelectorAll(sel).forEach(el => {
if (seen.has(el)) return;
+ if (!_isUsableSiteInteractive(el)) return;
let rect = el.getBoundingClientRect();
// Use wrapper rect for zero-dimension form inputs
if ((rect.width < 2 || rect.height < 2) && /^(INPUT|SELECT|TEXTAREA)$/i.test(el.tagName)) {
diff --git a/src/firefox/src/agent/adapters.js b/src/firefox/src/agent/adapters.js
index d1226bb4a..c4ac3ab58 100644
--- a/src/firefox/src/agent/adapters.js
+++ b/src/firefox/src/agent/adapters.js
@@ -16152,7 +16152,7 @@ const ADAPTERS = [
notes: `
- Tieba thread pages use a custom Vue action bar. On the first floor, the share, reply, like, and collect actions are icon-plus-count elements rather than native buttons; use the accessibility tree or interactive-element list and prefer the named "点赞" control over screenshot coordinates.
- Reply-level likes are separate "赞" controls in each comment row. Re-read the active post or comment container after scrolling, switching "只看楼主", or changing 热门/正序/倒序; indices and visible rows can change.
-- "点赞", "关注", "回复", and "发贴" change account or public state. Perform them only when explicitly requested, then verify the icon/count or resulting state; a successful mouse dispatch alone is not proof.
+- "点赞", "关注", "回复", and "发帖" change account or public state. Perform them only when explicitly requested, then verify the icon/count or resulting state; a successful mouse dispatch alone is not proof.
- The first-floor action bar may be below the initial viewport, while images open a viewer when clicked. Do not click nearby image coordinates or repeat a coordinate after an unexpected viewer opens; close or go back, then re-read the page.
- Public pages may require Baidu sign-in. If "登录", QR verification, or "百度安全验证" appears, stop for the user and do not bypass, retry, or claim that the requested action succeeded.`,
},
diff --git a/src/firefox/src/content/accessibility-tree.js b/src/firefox/src/content/accessibility-tree.js
index ca0dc8d41..5c94acce8 100644
--- a/src/firefox/src/content/accessibility-tree.js
+++ b/src/firefox/src/content/accessibility-tree.js
@@ -156,8 +156,10 @@
['.publish-button', '发布'],
],
baiduTieba: [
- ['.pc-pb-first-floor-interactive .action-item:has(use[*|href="#agree_pb"])', '点赞'],
- ['.pc-pb-comments-desc .zan-container-dark:has(use[*|href="#agree_comment"])', '赞'],
+ // Match the stable action wrapper, then inspect SVG href/xlink:href via
+ // the DOM API because namespaced attribute selectors vary by engine.
+ ['.pc-pb-first-floor-interactive .action-item', '点赞', '#agree_pb'],
+ ['.pc-pb-comments-desc .zan-container-dark', '赞', '#agree_comment'],
],
};
@@ -177,12 +179,19 @@
function getSiteInteractionDescriptor(el) {
if (!el || typeof el.matches !== 'function') return null;
- for (const [selector, label] of currentSiteInteractionConfig().rules) {
+ for (const [selector, label, iconHref] of currentSiteInteractionConfig().rules) {
try {
if (!el.matches(selector)) continue;
} catch {
continue;
}
+ if (iconHref) {
+ const hasIcon = Array.from(el.querySelectorAll?.('use') || []).some(use => (
+ use.getAttribute('href') === iconHref
+ || use.getAttribute('xlink:href') === iconHref
+ ));
+ if (!hasIcon) continue;
+ }
const explicit = String(
el.getAttribute('aria-label') || el.getAttribute('title') || ''
).replace(/\s+/g, ' ').trim();
diff --git a/src/firefox/src/content/content.js b/src/firefox/src/content/content.js
index b888206ba..12f0c0bfc 100644
--- a/src/firefox/src/content/content.js
+++ b/src/firefox/src/content/content.js
@@ -481,6 +481,18 @@
}
}
+ function _isSiteSelectorCandidate(el) {
+ try {
+ return _siteInteractiveSelectors().some(selector => el.matches(selector));
+ } catch {
+ return false;
+ }
+ }
+
+ function _isUsableSiteInteractive(el) {
+ return !_isSiteSelectorCandidate(el) || _isSiteInteractive(el);
+ }
+
function _composedParent(node) {
if (!node) return null;
if (node.assignedSlot) return node.assignedSlot;
@@ -718,6 +730,7 @@
const out = [];
for (const el of all) {
if (!isVisiblyInteractive(el)) continue;
+ if (!_isUsableSiteInteractive(el)) continue;
if (modal && !_isComposedAncestor(modal, el)) continue;
out.push(el);
}
@@ -843,6 +856,7 @@
try {
root.querySelectorAll(sel).forEach(el => {
if (seen.has(el)) return;
+ if (!_isUsableSiteInteractive(el)) return;
const rect = el.getBoundingClientRect();
if (!isUsable(el, rect)) return;
seen.add(el);
diff --git a/test/run.js b/test/run.js
index 459947a56..9468ba298 100644
--- a/test/run.js
+++ b/test/run.js
@@ -4633,22 +4633,28 @@ test('matches Baidu Tieba and exposes custom post like controls', () => {
assert.equal(axChrome, axFirefox, 'Tieba accessibility shims must remain byte-identical');
const start = axChrome.indexOf('const SITE_INTERACTION_RULES = {');
const end = axChrome.indexOf('\n\n function getRole(el) {', start);
+ assert.ok(start >= 0 && end > start, 'Tieba site interaction helper slice must have valid bounds');
+ assert.doesNotMatch(axChrome, /\*\|href/, 'Tieba selectors must not rely on namespace wildcard syntax');
const location = { hostname: 'tieba.baidu.com' };
const context = { window: {}, location };
vm.runInNewContext(axChrome.slice(start, end), context);
const api = context.window.__wbSiteInteractions;
- const likeSelector = '.pc-pb-first-floor-interactive .action-item:has(use[*|href="#agree_pb"])';
- const replyLikeSelector = '.pc-pb-comments-desc .zan-container-dark:has(use[*|href="#agree_comment"])';
- const fakeElement = (selector, text) => ({
+ const likeSelector = '.pc-pb-first-floor-interactive .action-item';
+ const replyLikeSelector = '.pc-pb-comments-desc .zan-container-dark';
+ const fakeElement = (selector, text, iconHref) => ({
innerText: text,
textContent: text,
matches: candidate => candidate === selector,
+ querySelectorAll: candidate => candidate === 'use'
+ ? [{ getAttribute: name => ['href', 'xlink:href'].includes(name) ? iconHref : null }]
+ : [],
getAttribute: () => null,
});
assert.ok(api.selectors().includes(likeSelector));
assert.ok(api.selectors().includes(replyLikeSelector));
- assert.equal(api.describe(fakeElement(likeSelector, '98')).name, '点赞 98');
- assert.equal(api.describe(fakeElement(replyLikeSelector, '21')).name, '赞 21');
+ assert.equal(api.describe(fakeElement(likeSelector, '98', '#agree_pb')).name, '点赞 98');
+ assert.equal(api.describe(fakeElement(replyLikeSelector, '21', '#agree_comment')).name, '赞 21');
+ assert.equal(api.describe(fakeElement(likeSelector, '37', '#comment_pb')), null);
assert.equal(api.shouldPierceShadowRoots(), false);
for (const [label, rel] of [
@@ -4658,12 +4664,14 @@ test('matches Baidu Tieba and exposes custom post like controls', () => {
const content = fs.readFileSync(path.join(ROOT, rel), 'utf8');
assert.match(content, /\.\.\._siteInteractiveSelectors\(\)/, `${label}: Tieba selectors must reach content discovery`);
assert.match(content, /if \(_isSiteInteractive\(node\)\) return true/, `${label}: Tieba controls must be interactive`);
+ assert.match(content, /_isUsableSiteInteractive\(el\)/, `${label}: broad Tieba selectors must filter non-like action wrappers`);
}
const cdp = fs.readFileSync(path.join(ROOT, 'src/chrome/src/cdp/cdp-client.js'), 'utf8');
assert.match(cdp, /onHost\('tieba\.baidu\.com'\)/);
assert.match(cdp, /agree_pb/);
assert.match(cdp, /agree_comment/);
+ assert.match(cdp, /matchesAnySiteSelector\(el\)[\s\S]*matchesSiteRule\(el, rule\)/);
});
test('matches twitter.com and x.com', () => {