Skip to content

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

fix(content): preserve native Tieba controls

Barack Sokullu06ed5c360e54
diff --git a/src/chrome/src/cdp/cdp-client.js b/src/chrome/src/cdp/cdp-client.js
index 51597fd7f..eadfd34f6 100644
--- a/src/chrome/src/cdp/cdp-client.js
+++ b/src/chrome/src/cdp/cdp-client.js
@@ -2933,14 +2933,14 @@ export class CDPClient {
['.pc-pb-reply-box', '回复'],
['.pc-pb-reply-box .publish-btn', '发布'],
] : [];
- const SELECTORS = [
+ const NATIVE_SELECTORS = [
'a[href]', 'button', 'input:not([type="hidden"])', 'textarea', 'select',
'[role="button"]', '[role="link"]', '[role="tab"]', '[role="menuitem"]',
'[role="textbox"]', '[role="combobox"]', '[role="searchbox"]',
'[contenteditable=""]', '[contenteditable="true"]', '[contenteditable="plaintext-only"]',
- '[onclick]', '[data-action]', 'summary', 'label',
- ...SITE_RULES.map(([selector]) => selector)
+ '[onclick]', '[data-action]', 'summary', 'label'
];
+ const SELECTORS = [...NATIVE_SELECTORS, ...SITE_RULES.map(([selector]) => selector)];
function matchesSiteRule(el, [selector, , iconHref]) {
try {
@@ -2965,6 +2965,16 @@ export class CDPClient {
});
}
+ function matchesNativeInteractive(el) {
+ return NATIVE_SELECTORS.some((selector) => {
+ try {
+ return el.matches(selector);
+ } catch (e) {
+ return false;
+ }
+ });
+ }
+
function interactiveText(el) {
for (const rule of SITE_RULES) {
const [selector, label] = rule;
@@ -3013,7 +3023,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;
+ if (!matchesNativeInteractive(el) && 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/content.js b/src/chrome/src/content/content.js
index 823190c1d..8a53fe287 100644
--- a/src/chrome/src/content/content.js
+++ b/src/chrome/src/content/content.js
@@ -266,8 +266,16 @@
}
}
+ function _isStandardInteractive(el) {
+ try {
+ return INTERACTIVE_SELECTORS.some(selector => el.matches(selector));
+ } catch {
+ return false;
+ }
+ }
+
function _isUsableSiteInteractive(el) {
- return !_isSiteSelectorCandidate(el) || _isSiteInteractive(el);
+ return _isStandardInteractive(el) || !_isSiteSelectorCandidate(el) || _isSiteInteractive(el);
}
function _composedParent(node) {
diff --git a/src/firefox/src/content/content.js b/src/firefox/src/content/content.js
index 12f0c0bfc..b62dfb582 100644
--- a/src/firefox/src/content/content.js
+++ b/src/firefox/src/content/content.js
@@ -489,8 +489,16 @@
}
}
+ function _isStandardInteractive(el) {
+ try {
+ return INTERACTIVE_SELECTORS.some(selector => el.matches(selector));
+ } catch {
+ return false;
+ }
+ }
+
function _isUsableSiteInteractive(el) {
- return !_isSiteSelectorCandidate(el) || _isSiteInteractive(el);
+ return _isStandardInteractive(el) || !_isSiteSelectorCandidate(el) || _isSiteInteractive(el);
}
function _composedParent(node) {
diff --git a/test/run.js b/test/run.js
index 2bea49316..bd84ddba9 100644
--- a/test/run.js
+++ b/test/run.js
@@ -4695,6 +4695,26 @@ test('matches Baidu Tieba and exposes custom post and reply controls', () => {
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 filterStart = content.indexOf(' const INTERACTIVE_SELECTORS = [');
+ const filterEnd = content.indexOf('\n\n function _composedParent', filterStart);
+ assert.ok(filterStart >= 0 && filterEnd > filterStart, `${label}: site-interaction filter slice must have valid bounds`);
+ const filterContext = {
+ window: {
+ __wbSiteInteractions: {
+ selectors: () => [firstFloorSelector],
+ isInteractive: el => el.siteInteractive === true,
+ },
+ },
+ };
+ vm.runInNewContext(`${content.slice(filterStart, filterEnd)}\nthis.isUsableSiteInteractive = _isUsableSiteInteractive;`, filterContext);
+ const candidate = ({ nativeSelector = '', siteInteractive = false } = {}) => ({
+ siteInteractive,
+ matches: selector => selector === firstFloorSelector || selector === nativeSelector,
+ });
+ assert.equal(filterContext.isUsableSiteInteractive(candidate({ siteInteractive: true })), true, `${label}: valid custom actions must remain discoverable`);
+ assert.equal(filterContext.isUsableSiteInteractive(candidate()), false, `${label}: non-interactive broad-selector wrappers must remain filtered`);
+ assert.equal(filterContext.isUsableSiteInteractive(candidate({ nativeSelector: 'a[href]' })), true, `${label}: native links must survive broad site-rule filtering`);
+ assert.equal(filterContext.isUsableSiteInteractive(candidate({ nativeSelector: '[role="button"]' })), true, `${label}: ARIA controls must survive broad site-rule filtering`);
}
const cdp = fs.readFileSync(path.join(ROOT, 'src/chrome/src/cdp/cdp-client.js'), 'utf8');
@@ -4705,7 +4725,8 @@ test('matches Baidu Tieba and exposes custom post and reply controls', () => {
assert.match(cdp, /comment_comment/);
assert.match(cdp, /publish-btn.*发布/);
assert.match(cdp, /agree_comment/);
- assert.match(cdp, /matchesAnySiteSelector\(el\)[\s\S]*matchesSiteRule\(el, rule\)/);
+ assert.match(cdp, /NATIVE_SELECTORS[\s\S]*matchesNativeInteractive\(el\)/);
+ assert.match(cdp, /!matchesNativeInteractive\(el\)[\s\S]*matchesAnySiteSelector\(el\)[\s\S]*matchesSiteRule\(el, rule\)/);
});
test('matches twitter.com and x.com', () => {