Sanitizing UI screenshots with Playwright: text is easy, images are the trap

From
Joe Hahn
Date
Read
5 min
Animated share card for Sanitizing UI screenshots with Playwright: text is easy, images are the trap

Every screenshot that ends up in a blog post, a ticket, or a design doc is a little data leak waiting to happen. A customer's name in a dashboard mockup, a real email in a "here's the bug" screenshot, a chart with actual revenue numbers on it — none of it was meant to go public, and all of it does, because sanitizing a screenshot by hand is tedious enough that nobody does it consistently.

I've been building lorem-gibson-generator to make that free: a small toolkit of Cyberpunk (William Gibson-style) placeholder text tools, and — the part I want to show off here — a Playwright plugin that walks a live page's DOM right before you screenshot it and replaces everything real with something that isn't, without touching layout.

Why not page.screenshot({ mask: [...] })

Playwright already ships an answer to "hide sensitive stuff before a screenshot": mask. It paints a solid box over whatever locators you give it. That's the right tool when you want something gone entirely. It's the wrong tool when you want the screenshot to still look like a working product — which is most of the time a screenshot exists at all. Nobody wants a blog post illustrated with black rectangles.

@lorem-gibson/playwright-sanitizer takes the opposite approach: don't hide content, replace it. Walk the DOM, swap visible text (and alt/title/ aria-label/placeholder attributes, and input values) for dictionary words that are roughly the same length, so the layout doesn't reflow and the page still reads as populated and real.

import { chromium } from 'playwright'
import { sanitizePage } from '@lorem-gibson/playwright-sanitizer'

const browser = await chromium.launch()
const page = await browser.newPage()
await page.goto('https://example.com/dashboard')

await sanitizePage(page, { seed: 'dashboard-fixture' })
await page.screenshot({ path: 'dashboard.png', fullPage: true })

Here's a fixture dashboard — fake data standing in for the real customer records this is built to protect — before anything's touched:

Customer support dashboard before sanitizing: shows a named customer, their email, phone number, billing zip, and order stats

And after sanitizePage:

The same dashboard after text sanitization: names, email, phone number, and stats are all replaced with placeholder text, but the layout is identical

Same layout, same card sizes, same table structure — just nothing real left in it. Numbers become same-shaped random digits (so 1,204 stays a four-digit-looking number instead of turning into a word), and anything that looks like an email or URL gets swapped for an obvious fake (user@example.test) rather than word salad that might still read as a real address at a glance.

seed is what makes this usable for visual-regression testing, not just one-off blog screenshots: pass the same seed and the same elements get the same placeholder text on every run, so an unrelated screenshot diff doesn't show up as noise just because the sanitizer picked different words.

The gap text sanitization can't close

Look at the "after" screenshot again — specifically the activity chart. The card title got sanitized fine, because that's real DOM text. The chart image itself didn't change at all, because it's baked into the <img> — a canvas render, a screenshot embed, whatever generated it. The customer's actual usage numbers are still sitting right there in pixels. Text sanitization did its job perfectly and the screenshot is still not safe to publish.

This is exactly the case images: true exists for:

await sanitizePage(page, { seed: 'dashboard-fixture', images: true })

Every <img> at least 32px in both directions (configurable via imageMinDimension — small icons and logos are usually fine to leave alone) gets its content replaced with a canvas-drawn CRT dead-channel texture, sized to the image's existing rendered box:

Four sanitized image styles side by side, labeled snow, scanlines, scrambled, and phosphor: black-and-white static, a dim glowing idle-tube pattern, blue and purple tracking-error bands, and green monochrome noise

'snow', 'scanlines', 'scrambled', 'phosphor', or 'random' (the default) to pick one per image, deterministically, from the seed. And the full pass — text and images together:

The dashboard fully sanitized: text replaced with placeholder content and the activity chart replaced with a dead-channel scanlines texture, same layout throughout

Images are opt-in rather than on by default, on purpose. Unlike text, there's no universal safe replacement — a dictionary word reads fine in any UI copy slot, but there's no reliable signal telling a user's uploaded photo apart from a decorative logo or a brand icon. sanitizePage assumes "everything is potentially real data unless you say otherwise" for text, which is the safer default; for images, opt-in is the safer default, since nuking every <img> on the page by default would just as often ruin a legitimate branding asset as protect real data.

The rest of the surface area

A few other options that matter in practice, not just the demo:

  • exclude / include — CSS selectors to skip, or an allowlist to scope sanitizing to one container (#main-content), instead of the whole page.
  • watch: true — keeps a MutationObserver running so content added after the initial pass (a modal you open, an SPA route change) gets sanitized too; call stopWatching(page) when you're done.
  • includeProperNouns — mixes in the Neuromancer glossary tier (Chatsubo, Freeside, black ice) alongside general vocabulary, which is where the project's name comes from.

It's built on a shared word dictionary — merged and deduped from three existing "Lorem Gibson" projects — that a separate CLI (npx lorem-gibson) also draws from, for anywhere you just want placeholder text without a browser in the loop.

An honest aside

Getting the screenshots for this post onto joehahn.dev turned into its own small yak-shave: the content API here could only keep one uploaded image permanent per article, which doesn't work for a post with four. Fixing it meant a new unlimited-cardinality field on the article content type, plus a role permission that — it turns out — had been silently broken since the draft workflow was built: api_editor could create a draft article but couldn't see it again afterward, so publish/update on a genuine draft had never actually worked. Both fixes are live on prod now. Sometimes writing the post about the tool finds the bug in the thing publishing the post.


Still actively being built — an MCP server package landed in the repo this week, ahead of its own writeup. If you're anonymizing screenshots by hand right now, or worse, not anonymizing them at all, this is the fix.