Blocking ads in a screenshot without breaking the page

Blocking ad requests leaves grey holes, and blocking the wrong host strips every image from the page. Whole-token matching, empty-slot collapse, and the measurement that showed a 50x content loss from blocking a tag manager.

"Block ads before the screenshot" sounds like a one-line feature. Pull an ad-blocker list, refuse those hosts, capture. I shipped roughly that, ran it against 202 real sites, and found two separate ways it makes captures worse.

Blocking an ad does not reclaim its space

The first is visual. Blocking the request stops the ad from arriving, but the page already reserved the slot's height. You get a grey rectangle where the ad would be, pushing the real content below the fold. On a news front page that can mean the entire top of the capture is empty boxes.

So after the page settles, the renderer finds ad containers that are still empty and collapses them. On france24.com that took a capture from 122 KB to 234 KB of actual content, because real content moved up into the frame.

The timing matters. The collapse runs after the settle, so a slot that did fill with a house ad or a publisher promo is left alone. Collapsing early would delete content that was about to arrive.

Match whole tokens, or you will eat the page

Finding "an ad container" in someone else's markup is where this gets dangerous. The obvious test is whether the class name contains "ad", and that test eats header, shadow, download and gradient.

So class names and ids are split into tokens and compared exactly:

const AD_TOKENS = new Set([
  'ad', 'ads', 'advert', 'adverts', 'advertisement', 'advertising',
  'adsense', 'adslot', 'adunit', 'adbox', 'adcontainer', 'adwrapper',
  'adspace', 'adregion', 'adzone', 'dfp', 'gpt', 'googlead', 'googleads',
  'sponsored', 'leaderboard', 'skyscraper', 'mpu', 'billboard',
]);

Exact tokens alone are not enough, because build tools run the words together. The BBC ships AdSlotContainerStyled-sc-4b576bed-0, which no exact list will ever contain, so a short prefix list covers hashed class names: adslot, adunit, adcontainer, adwrapper, adbox, adspace, advert, googlead, dfpad.

An element with no class or id at all gets one more chance through the attributes ad tech leaves behind, data-ad-slot and data-google-query-id.

And a slot is only collapsed if it is genuinely empty: no text, nothing painted inside it, and not large enough to be the page itself.

The hosts I deliberately do not block

The second failure is worse, because it is invisible in the DOM and obvious in the image.

Every ad-blocker list includes googletagmanager, google-analytics, hotjar, mixpanel, segment, fullstory, mouseflow, clarity.ms and facebook.net. None of them render anything. Blocking them improves no screenshot.

But many sites bootstrap their lazy-loading and image pipelines through a tag manager. Block it and the images never load.

The measurement that settled it: timesofindia.indiatimes.com returned 23 KB with those hosts blocked and 1,219 KB without, with identical DOM text. A 50x content loss in exchange for hiding nothing.

Analytics hosts are not ad hosts. A list built for a human reading a page in their own browser is not a list built for a machine photographing it.

Consent scripts: block the source, not the symptom

Related, and the same principle in reverse. For consent management platforms the renderer blocks the script rather than removing the banner it injects, because a banner that never gets built cannot lose a race with the capture. The sweep that removes injected banners still exists for everything else, and it is a separate post.

What is still wrong

apnews.com leaves about 330 pixels of dead space at the top when ad blocking is on. Their leaderboard slot renders a visible "ADVERTISEMENT" label, so the empty-slot collapser correctly leaves it alone: it has text, and text means content. Fixing it means special-casing a label, which is exactly the kind of rule that eats a real heading on some other site, so it stays on the open list rather than in the code.

That trade is the whole job here. Every rule that removes something from someone else's page can remove the wrong thing, and the failure is silent: you get a clean 200 with a hole in it.

What to copy

  1. Collapse after settling, not before. A slot that fills late is content.
  2. Compare whole tokens. Substring matching on short words destroys unrelated elements.
  3. Allow for hashed class names with a small prefix list, then stop.
  4. Require emptiness before deleting anything: no text, nothing painted, not page-sized.
  5. Do not block analytics hosts in a renderer. They render nothing, and sites load their images through them.
  6. Measure both directions. Smaller can be better, when a consent modal goes. Bigger can be better, when ad holes collapse. Neither is proof.

The blocking rules are in src/blocking.js, with the sites that forced each one named in the comments. block_ads=true is the default on the hosted API, which has 500 free renders a month.

Get a free key — 500 renders a month Try it without signing up

More from the blog