Skip to content

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

Cookie banners: register autoconsent's styles as page decorations

Barack Sokullua1064f5158b8
diff --git a/docs/cookie-banners.md b/docs/cookie-banners.md
index ac9dc5ebb..fbe753bd2 100644
--- a/docs/cookie-banners.md
+++ b/docs/cookie-banners.md
@@ -39,6 +39,12 @@ Settings → Advanced → **Cookie banners** (storage key `cookieBanners`):
- On `init`, the host replies with the compact rules for that frame, WebBrain's
own rules for that URL, and a fixed config: opt out only, the heuristic in
reject-only mode, hide-only rules on, banner pre-hiding for at most 2 s.
+- The wrapper creates the `<style>` elements autoconsent hides with
+ (`autoconsent-prehide`, `autoconsent-css-rules`) and registers them with the
+ page monitor as WebBrain's own decorations, so adding them, filling them and
+ removing the pre-hide style 2 s later are not reported as page changes that
+ would refuse the agent's next action. A page's own element with the same id
+ is not claimed.
- Some rules call the consent platform's own JavaScript. The host runs only
autoconsent's fixed `evalSnippets`, by name, with
`scripting.executeScript({ world: 'MAIN' })` in the frame that asked, once per
diff --git a/src/chrome/src/content/autoconsent-runtime.js b/src/chrome/src/content/autoconsent-runtime.js
index 986684cde..9918d3f1d 100644
--- a/src/chrome/src/content/autoconsent-runtime.js
+++ b/src/chrome/src/content/autoconsent-runtime.js
@@ -2,6 +2,29 @@
// clicks in waitForThenClick and click-all rules, recheck background coverage.
import AutoConsent from '../../vendor/autoconsent/autoconsent.esm.js';
+// The page monitor reports any stylesheet it does not own as page churn.
+// Pre-hiding adds autoconsent's <style> and removes it 2 s later, after the
+// turn was released, so that removal refused the agent's next action as if
+// the page had changed. WebBrain creates the style elements upstream writes
+// into and registers them by reference; a page's element with the same id
+// stays the page's own.
+const PREHIDE_STYLE_ID = 'autoconsent-prehide';
+const RULES_STYLE_ID = 'autoconsent-css-rules';
+
+function existingStyle(id) {
+ const style = globalThis.document?.querySelector(`style#${id}`);
+ return style instanceof HTMLStyleElement ? style : null;
+}
+
+function ownStyle(id) {
+ const doc = globalThis.document;
+ if (!doc || existingStyle(id)) return;
+ const style = doc.createElement('style');
+ style.id = id;
+ (window.__wbPageMonitorDecorations ??= new WeakSet()).add(style);
+ (doc.head || doc.getElementsByTagName('head')[0] || doc.documentElement).appendChild(style);
+}
+
export function guardCookieBannerInstance(consent, hasCoverage) {
let stopped = false;
const cancelled = new Error('Cookie-banner coverage revoked');
@@ -33,6 +56,9 @@ export function guardCookieBannerInstance(consent, hasCoverage) {
consent[name] = async (...args) => {
try {
await requireCoverage();
+ // Some upstream rules (TrustArc, Evidon) hide through the shared
+ // rules style directly rather than through domActions.hide.
+ if (name !== '_start') ownStyle(RULES_STYLE_ID);
return await original(...args);
} catch (error) {
if (error !== cancelled) throw error;
@@ -81,7 +107,21 @@ export function guardCookieBannerInstance(consent, hasCoverage) {
// changes and pre-hiding while an earlier rule is waiting for an element.
for (const name of ['hide', 'stylesheet', 'removeClass', 'setStyle', 'addStyle', 'prehide']) {
const original = dom[name].bind(dom);
- dom[name] = (...args) => stopped ? false : original(...args);
+ const styleId = name === 'prehide' ? PREHIDE_STYLE_ID
+ : name === 'hide' || name === 'stylesheet' ? RULES_STYLE_ID : null;
+ dom[name] = (...args) => {
+ if (stopped) return false;
+ if (styleId) ownStyle(styleId);
+ return original(...args);
+ };
+ }
+ // Upstream creates the pre-hide style in order to remove it. With nothing
+ // pre-hidden, that insert-and-remove pair was itself reported as churn.
+ if (typeof dom.undoPrehide === 'function') {
+ const undoPrehide = dom.undoPrehide.bind(dom);
+ dom.undoPrehide = () => {
+ if (!globalThis.document || existingStyle(PREHIDE_STYLE_ID)) undoPrehide();
+ };
}
const restoreFocus = consent.restoreFocus.bind(consent);
consent.restoreFocus = () => { if (!stopped) restoreFocus(); };
diff --git a/src/firefox/src/content/autoconsent-runtime.js b/src/firefox/src/content/autoconsent-runtime.js
index 986684cde..9918d3f1d 100644
--- a/src/firefox/src/content/autoconsent-runtime.js
+++ b/src/firefox/src/content/autoconsent-runtime.js
@@ -2,6 +2,29 @@
// clicks in waitForThenClick and click-all rules, recheck background coverage.
import AutoConsent from '../../vendor/autoconsent/autoconsent.esm.js';
+// The page monitor reports any stylesheet it does not own as page churn.
+// Pre-hiding adds autoconsent's <style> and removes it 2 s later, after the
+// turn was released, so that removal refused the agent's next action as if
+// the page had changed. WebBrain creates the style elements upstream writes
+// into and registers them by reference; a page's element with the same id
+// stays the page's own.
+const PREHIDE_STYLE_ID = 'autoconsent-prehide';
+const RULES_STYLE_ID = 'autoconsent-css-rules';
+
+function existingStyle(id) {
+ const style = globalThis.document?.querySelector(`style#${id}`);
+ return style instanceof HTMLStyleElement ? style : null;
+}
+
+function ownStyle(id) {
+ const doc = globalThis.document;
+ if (!doc || existingStyle(id)) return;
+ const style = doc.createElement('style');
+ style.id = id;
+ (window.__wbPageMonitorDecorations ??= new WeakSet()).add(style);
+ (doc.head || doc.getElementsByTagName('head')[0] || doc.documentElement).appendChild(style);
+}
+
export function guardCookieBannerInstance(consent, hasCoverage) {
let stopped = false;
const cancelled = new Error('Cookie-banner coverage revoked');
@@ -33,6 +56,9 @@ export function guardCookieBannerInstance(consent, hasCoverage) {
consent[name] = async (...args) => {
try {
await requireCoverage();
+ // Some upstream rules (TrustArc, Evidon) hide through the shared
+ // rules style directly rather than through domActions.hide.
+ if (name !== '_start') ownStyle(RULES_STYLE_ID);
return await original(...args);
} catch (error) {
if (error !== cancelled) throw error;
@@ -81,7 +107,21 @@ export function guardCookieBannerInstance(consent, hasCoverage) {
// changes and pre-hiding while an earlier rule is waiting for an element.
for (const name of ['hide', 'stylesheet', 'removeClass', 'setStyle', 'addStyle', 'prehide']) {
const original = dom[name].bind(dom);
- dom[name] = (...args) => stopped ? false : original(...args);
+ const styleId = name === 'prehide' ? PREHIDE_STYLE_ID
+ : name === 'hide' || name === 'stylesheet' ? RULES_STYLE_ID : null;
+ dom[name] = (...args) => {
+ if (stopped) return false;
+ if (styleId) ownStyle(styleId);
+ return original(...args);
+ };
+ }
+ // Upstream creates the pre-hide style in order to remove it. With nothing
+ // pre-hidden, that insert-and-remove pair was itself reported as churn.
+ if (typeof dom.undoPrehide === 'function') {
+ const undoPrehide = dom.undoPrehide.bind(dom);
+ dom.undoPrehide = () => {
+ if (!globalThis.document || existingStyle(PREHIDE_STYLE_ID)) undoPrehide();
+ };
}
const restoreFocus = consent.restoreFocus.bind(consent);
consent.restoreFocus = () => { if (!stopped) restoreFocus(); };
diff --git a/test/cookie-banner-host.mjs b/test/cookie-banner-host.mjs
index 8de98ae36..9dbdb52d6 100644
--- a/test/cookie-banner-host.mjs
+++ b/test/cookie-banner-host.mjs
@@ -174,6 +174,66 @@ for (const build of BUILDS) {
assert.equal(clicks, 1, 'cancellation unwinds a rule before its direct DOM mutations');
});
+ test(`${build}: autoconsent's own style elements are page-monitor decorations`, async () => {
+ // A minimal document: upstream's getStyleElement looks the style up by id
+ // and creates it when missing; undoPrehide removes it.
+ class FakeStyle { constructor() { this.id = ''; this.parent = null; } remove() { this.parent?.delete(this); this.parent = null; } }
+ const head = new Set();
+ let inserts = 0;
+ const doc = {
+ head: { appendChild(el) { inserts += 1; el.parent = head; head.add(el); return el; } },
+ createElement: () => new FakeStyle(),
+ querySelector: selector => [...head].find(el => `style#${el.id}` === selector) || null,
+ getElementsByTagName: () => [],
+ };
+ const getStyleElement = id => doc.querySelector(`style#${id}`) || doc.head.appendChild(Object.assign(doc.createElement('style'), { id }));
+ const saved = { document: globalThis.document, window: globalThis.window, HTMLStyleElement: globalThis.HTMLStyleElement };
+ Object.assign(globalThis, { document: doc, window: {}, HTMLStyleElement: FakeStyle });
+ try {
+ const consent = {
+ config: { enabled: true, autoAction: 'optOut' },
+ doOptOut() { getStyleElement('autoconsent-css-rules'); return true; }, doOptIn() {}, handlePopup() {},
+ findCmp() {}, waitForPopup() {}, restoreFocus() {},
+ undoPrehide() { this.domActions.undoPrehide(); },
+ domActions: {
+ elementSelector: () => [], clickElement: async () => true,
+ prehide() { getStyleElement('autoconsent-prehide'); return true; },
+ undoPrehide() { getStyleElement('autoconsent-prehide').remove(); },
+ hide() { getStyleElement('autoconsent-css-rules'); return true; },
+ stylesheet() { getStyleElement('autoconsent-css-rules'); return true; },
+ removeClass() {}, setStyle() {}, addStyle() {},
+ },
+ };
+ const guard = guardCookieBannerInstance(consent, async () => true);
+ const decorations = () => globalThis.window.__wbPageMonitorDecorations;
+
+ consent.domActions.undoPrehide();
+ assert.equal(inserts, 0, 'undoing with nothing pre-hidden inserts no style');
+
+ consent.domActions.prehide('#banner');
+ const prehide = doc.querySelector('style#autoconsent-prehide');
+ assert.ok(prehide && decorations().has(prehide), 'the pre-hide style is registered before it reaches the page');
+ consent.domActions.hide('#banner');
+ await consent.doOptOut();
+ const rules = doc.querySelector('style#autoconsent-css-rules');
+ assert.ok(rules && decorations().has(rules), 'the shared rules style is registered too');
+ assert.equal(inserts, 2, 'each style is inserted once');
+
+ guard.stop();
+ assert.equal(doc.querySelector('style#autoconsent-prehide'), null, 'stopping still removes pre-hiding');
+
+ const pageStyle = Object.assign(new FakeStyle(), { id: 'autoconsent-prehide' });
+ doc.head.appendChild(pageStyle);
+ const second = { ...consent, config: { enabled: true }, domActions: { ...consent.domActions } };
+ guardCookieBannerInstance(second, async () => true);
+ second.domActions.prehide('#banner');
+ assert.equal(decorations().has(pageStyle), false, 'a page element with the same id is not claimed');
+ } finally {
+ Object.assign(globalThis, saved);
+ for (const key of Object.keys(saved)) if (saved[key] === undefined) delete globalThis[key];
+ }
+ });
+
test(`${build}: 'agent' mode injects only into tabs an Act or Dev run is driving`, async () => {
const { fake } = setup({ runs: new Map([[1, 'act'], [2, 'ask'], [4, 'dev']]) });
fake.commit(1, 0, 'https://shop.example/');