The browser already knows every image it has loaded, but an extension cannot ask it for that list. chrome.webRequest only shows requests while you listen, and it never sees an image that has not loaded yet: the lazy one 3,000 px below the fold, the one still sitting in data-src, the one a script will copy into place on scroll. So my extension, PixLasso, does it the hard way. It injects one content script into every frame and walks the DOM.
Below: what the walk looks at besides <img>, what it costs on a page with a third of a million elements, and how one off-screen iframe made every scan wait 8 seconds.
One script, every frame, the last expression is the result
The scan is a single file run with chrome.scripting.executeScript. It is an IIFE, and the value of the last expression comes back to the service worker, one result per frame:
const results = await chrome.scripting.executeScript({ target: { tabId, allFrames: true }, files: ['src/content/collect.js'], }); // [{ frameId: 0, result: { frameUrl, records: [...] } }, { frameId: 7, ... }] The script only gathers raw candidates. Deduping and picking originals happens in a plain module in the worker, which I can unit test in Node.
Open shadow roots are not in querySelectorAll
document.querySelectorAll('img') stops at shadow boundaries. Web components hide product galleries, carousels and avatars behind them, so the walk uses a TreeWalker and recurses into every open shadowRoot it meets:
const elements = []; const walk = (root) => { const tw = document.createTreeWalker(root, NodeFilter.SHOW_ELEMENT); let n = tw.currentNode; while (n) { if (n.nodeType === 1) { elements.push(n); if (n.shadowRoot) walk(n.shadowRoot); } n = tw.nextNode(); } }; walk(document.documentElement || document); Closed shadow roots stay invisible; el.shadowRoot is null for them and there is no way around that from a content script. The Allbirds collection page I test against has two shadow roots, both open.
What else counts as an image
Then one loop over elements, switching on tagName. <img> is the obvious case, but most of the work is the rest:
- Lazy attributes. The list in the extension has 24 names, from
data-srcanddata-originaltodata-zoom-image,data-old-hires(Amazon) anddata-ks-lazyload. Values starting with{are skipped, since some sites put JSON there. Three more names hold a lazysrcset. -
<picture>sources. A<source>that only offers another format of the same photo is covered by the<img>, which reports what the browser picked. A<source media="...">is a different crop and counts as its own image. - CSS backgrounds.
getComputedStyle(el).backgroundImage, plusborder-image-source,content, and the same for::beforeand::after. Lazy background loaders keep the URL indata-bguntil scroll, but so do slide frameworks, with values like#fffordark, so adata-bgvalue only counts if it containsurl(or looks like a URL. -
image-set(). A computedimage-set(url(a.jpg) 1x, url(b.jpg) 2x)is one picture, so only the densest file is kept. The computed style reports the density asdppx, notx, so the regex has to accept both. - Canvas.
toDataURL('image/png')for canvases at least 64 px a side and at most 4,000,000 px in area (maps and games are bigger and would put multi-megabyte strings into every scan). A canvas that drew a cross-origin image is tainted and throws, so the call sits in atry. - Inline SVG. Serialized with
XMLSerializerwhen it is at least 32×32 on screen, not nested in another SVG, and under 400,000 characters. - The rest.
<a href>pointing at an image file,<video poster>,<input type="image">,og:imageandtwitter:imagemeta tags.
One check on <img> saves a lot of confused bug reports: an image that finished loading and still has no width failed on the page itself, usually a blocked ad pixel.
const broken = el.complete && !el.naturalWidth && Boolean(el.currentSrc || el.src); A lazy image that has not started is not complete, so it is not flagged.
What the walk costs
getComputedStyle is the slow call, so only the first 25,000 elements get the CSS background pass. An earlier version capped the whole walk at 25,000 elements, and I removed that cap because images past that point were simply lost. Then I measured whether that made scans slower, with both builds loaded in the same Playwright Chromium and scanning the same tab, median of three rounds:
| Page | Elements | Old scan | New scan | Items found, old → new |
|---|---|---|---|---|
| Synthetic, 2,000 cards | 50,205 | 114 ms | 151 ms | 1,098 → 2,102 |
| vnexpress.net | 5,091, 26 frames | 100 ms | 111 ms | 132 to 150 → 150 |
| Wikipedia, list of largest cities | 6,551 | 49 ms | 51 ms | 61 → 61 |
| allbirds.com collection | 4,641, 8 frames | 35 ms | 36 ms | 51 → 51 |
On normal pages the cap never applied, so nothing changed. The stress case was the one-page HTML Standard, 337,200 elements. A top-frame collect there went from 95 to 178 ms, and the item count from 13 to 62.
I broke that down by running variants of the script directly in the page, median of five:
| Variant | ms |
|---|---|
| TreeWalker into an array, nothing else | 11 |
plus reading each tagName | 21 to 22 |
plus getAttribute('href') and the image-extension regex on each <a> | 31 to 32 |
| full script, walk capped at 25,000 | 72 |
| full script, uncapped | 120 to 124 |
So the whole difference is the 312,200 elements past the 25,000th, at about 0.15 µs each in the page and 0.27 µs through the extension. 61,997 of that page's 67,920 links come after element 25,000, and each one pays for a getAttribute and a regex. Swapping elements.entries() for an index loop saved 7 to 12 ms. I kept the uncapped version: about a tenth of a second on a page that size, against 49 images that would otherwise be missing.
The 8-second scan
The same benchmark turned up something worse. Every scan of the HTML Standard took about 8.1 seconds, in both builds.
executeScript with allFrames: true and without injectImmediately waits for every frame to reach document_idle. An <iframe loading="lazy"> far below the viewport never starts loading, so it never gets there. The HTML Standard has three of them. My call ran into its 8-second timeout, and the fallback scanned only the top frame, dropping every iframe's images on the way.
A minimal repro is one image, a 30,000 px spacer, and one iframe:
| iframe | injectImmediately: false | injectImmediately: true |
|---|---|---|
| eager | 12 ms, 2 frames | 1 ms, 2 frames |
loading="lazy" | 8,000 ms, timed out | 3 ms, 2 frames |
WordPress has added loading="lazy" to iframes by default since 5.7, so a blog post with a video embed below the fold is enough. A slow ad iframe that never settles does the same thing.
The fix keeps the patient call but gives it only 1.5 seconds. If that times out, it checks whether the top document itself is idle, because reading a top page that is still parsing would lose everything below the parse point. Once the top page is ready, it reads all frames immediately, as they are:
async function collectOnce(tabId, { allFrames = true, immediate = !allFrames } = {}) { const results = await withTimeout( chrome.scripting.executeScript({ target: { tabId, allFrames }, files: ['src/content/collect.js'], injectImmediately: immediate, }), immediate ? 4000 : 1500, null, ); if (!results) { if (!immediate) { const topIdle = await withTimeout( chrome.scripting.executeScript({ target: { tabId }, func: () => 0 }), 6500, null); if (topIdle) return collectOnce(tabId, { allFrames, immediate: true }); } if (allFrames) return collectOnce(tabId, { allFrames: false }); return []; } // ... } withTimeout is a Promise.race against a timer that resolves to the fallback value. A slow top page still gets the old 8 seconds in total (1.5 + 6.5), and an unloaded lazy iframe simply returns nothing.
Scrolling for the rest
Some images only exist after a scroll. The "deep scan" scrolls by max(400, 0.9 × innerHeight), waits 350 ms, and collects the top frame again, for at most 60 steps or 20 seconds. It stops once the page height stays the same twice at the bottom.
It collects at every step, not once at the end, because infinite feeds such as Pinterest remove the nodes that scrolled off. A single scan after scrolling would find fewer images than the first one. Each step re-reads the whole page, so records are deduped by frame URL, kind, source and srcset, and memory grows with distinct images, not steps. Animated canvases give a new data URL every step, so they are keyed by position, and scroll steps skip canvas and SVG encoding entirely; the first and last full scans cover those.
If you are building something similar
- Walk shadow roots yourself;
querySelectorAllwill not. - Cap
getComputedStyle, not the walk. - Never wait for
document_idleon all frames without a short timeout. Test with an off-screenloading="lazy"iframe. - On feeds, collect while you scroll.
If you only need the images off one page and not the code, I wrote up the manual ways (DevTools, Firefox's Page Info, wget and their limits) in how to download all images from a website. I make PixLasso, a Chrome and Edge extension that does the scan above and saves the result as a ZIP.

