Auto-accept cookie banners without clicking
Cookie consent banners are the toll booth of the modern web. You pay the same toll on every site, every day. A short JustZix JavaScript rule can pay it for you — clicking the accept (or reject) button the instant the banner shows up.
How banners work
Most consent dialogs are a regular block of HTML injected into the page. They usually contain a button with predictable text — "Accept", "Accept all", "I agree", "Got it" — or a recognisable id/class. Because JustZix JS runs in the page context, you can find that button and click it programmatically.
The simplest version: click by button text
This rule scans every button on the page and clicks the first one whose label matches a list of accept phrases. It is case-insensitive and trims whitespace.
var ACCEPT = ['accept all', 'accept', 'i agree', 'agree', 'got it', 'allow all'];
function clickConsent() {
var buttons = document.querySelectorAll('button, a, [role="button"]');
for (var i = 0; i < buttons.length; i++) {
var label = (buttons[i].textContent || '').trim().toLowerCase();
if (ACCEPT.indexOf(label) !== -1) {
buttons[i].click();
return true;
}
}
return false;
}
clickConsent();
Handle banners that load late
Many consent managers (the page runs them through a tag manager) inject the banner a second or two after load. JustZix runs your JS once per page load, so a single call can miss a late banner. Use a MutationObserver to watch for new nodes and retry, then stop once you succeed.
var ACCEPT = ['accept all', 'accept', 'i agree', 'agree', 'got it', 'allow all'];
function clickConsent() {
var buttons = document.querySelectorAll('button, a, [role="button"]');
for (var i = 0; i < buttons.length; i++) {
var label = (buttons[i].textContent || '').trim().toLowerCase();
if (ACCEPT.indexOf(label) !== -1) { buttons[i].click(); return true; }
}
return false;
}
if (!clickConsent()) {
var observer = new MutationObserver(function () {
if (clickConsent()) observer.disconnect();
});
observer.observe(document.documentElement, { childList: true, subtree: true });
setTimeout(function () { observer.disconnect(); }, 10000);
}
The setTimeout guard disconnects the observer after ten seconds so it never runs forever on a page that has no banner.
Targeting a specific consent platform
If you only browse a handful of sites, target the exact button by selector — it is faster and avoids accidental clicks. Inspect the banner in DevTools, copy the id or class, and use it directly.
function clickById(selectors) {
for (var i = 0; i < selectors.length; i++) {
var el = document.querySelector(selectors[i]);
if (el) { el.click(); return true; }
}
return false;
}
clickById([
'#onetrust-accept-btn-handler',
'.cookie-consent__accept',
'button[data-testid="accept-all"]'
]);
Prefer to reject instead?
Privacy-minded users can flip the logic — just swap the phrase list. Many banners now require a "Reject all" button by law, so this works on a lot of sites.
var REJECT = ['reject all', 'reject', 'decline', 'necessary only', 'only necessary'];
var buttons = document.querySelectorAll('button, a, [role="button"]');
for (var i = 0; i < buttons.length; i++) {
var label = (buttons[i].textContent || '').trim().toLowerCase();
if (REJECT.indexOf(label) !== -1) { buttons[i].click(); break; }
}
Good practices
- Keep the rule scoped to
*only if you trust the phrase list — broad text matching can occasionally click an unrelated button. When in doubt, scope per site. - Always disconnect observers with a timeout so the rule cannot leak.
- This rule clicks buttons that already exist on the page; it sends no data and contacts no server.
- If a site hides content behind the banner, clicking accept also restores scrolling — no extra CSS needed.
Grab a tested copy from the ready-made examples, or download JustZix if you are starting fresh. Pair this with our guide on auto-expanding hidden content for a smoother read.
Rate this post
No ratings yet — be the first.