惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

宝玉的分享
宝玉的分享
小众软件
小众软件
J
Java Code Geeks
I
InfoQ
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
腾讯CDC
L
LangChain Blog
博客园 - 司徒正美
量子位
Y
Y Combinator Blog
C
Check Point Blog
T
Tailwind CSS Blog
D
DataBreaches.Net
Blog — PlanetScale
Blog — PlanetScale
N
Netflix TechBlog - Medium
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
F
Fortinet All Blogs
云风的 BLOG
云风的 BLOG
A
About on SuperTechFans
B
Blog RSS Feed
酷 壳 – CoolShell
酷 壳 – CoolShell
大猫的无限游戏
大猫的无限游戏
V
V2EX
阮一峰的网络日志
阮一峰的网络日志

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
The scrollHeight Lie: How I Finally Got Full-Page Screens...
KNALLHART.DEV · 2026-06-22 · via DEV Community

KNALLHART.DEV

If you've ever tried to take a full-page screenshot of a modern website
programmatically, you've probably run into the same wall I did:
document.body.scrollHeight lies. Constantly.

The problem

Lazy-loaded images, infinite scroll sections, sticky headers, scroll-triggered
animations — all of these mess with the page's reported height in ways that
make scrollHeight an unreliable signal for "have I reached the bottom of
this page yet?"

I was building knallhart.dev, a tool that takes a
full-page screenshot of any website and sends an AI-generated critique via
email. The screenshot step turned out to be the hardest part of the entire
build — harder than the AI integration, harder than the payment flow.

What didn't work

My first approach was the obvious one: read scrollHeight, calculate how
many scroll steps are needed, scroll that many times, done.

const pageHeight = await page.evaluate(() => document.body.scrollHeight);
const steps = Math.ceil(pageHeight / 600);

for (let i = 0; i < steps; i++) {
  await page.evaluate((y) => window.scrollBy(0, y), 600);
  await page.waitForTimeout(200);
}

This worked on simple static pages and broke immediately on anything modern.
Pages with lazy-loaded content report a short initial scrollHeight, then
grow as you scroll — so the calculated step count is wrong before you've
even started. Pages with infinite scroll never stop growing at all.

What actually works

Instead of trying to calculate the destination upfront, I scroll step by
step and watch whether window.scrollY is still changing. If it stops
changing for a few consecutive checks, I've actually hit the bottom —
regardless of what scrollHeight claims.

let lastScrollY = -1;
let noChangeCount = 0;
const maxAttempts = 40; // safety cap, ~8 seconds

for (let i = 0; i < maxAttempts; i++) {
  await page.evaluate(() => window.scrollBy(0, 600));
  await page.waitForTimeout(200);

  const currentScrollY = await page.evaluate(() => window.scrollY);

  if (currentScrollY === lastScrollY) {
    noChangeCount++;
    if (noChangeCount >= 3) break; // confirmed: no more progress
  } else {
    noChangeCount = 0;
    lastScrollY = currentScrollY;
  }
}

await page.evaluate(() => window.scrollTo(0, 0));

This is more robust because it doesn't trust any single height value — it
trusts observed behavior over time. A page that's still loading content
will keep moving scrollY; a page that's truly done won't, no matter how
confusing its scrollHeight is.

The bonus problem: images

Triggering the scroll isn't enough on its own — lazy-loaded images often
need a moment to actually finish loading after they enter the viewport.
I added an explicit wait for image load events before taking the final
screenshot:

\\javascript
await page.evaluate(async () => {
const images = Array.from(document.querySelectorAll("img"));
await Promise.all(
images.map((img) => {
if (img.complete) return Promise.resolve();
return new Promise((resolve) => {
img.addEventListener("load", resolve);
img.addEventListener("error", resolve);
setTimeout(resolve, 5000); // don't hang forever on one broken image
});
})
);
});
\\

What I'd tell past me

Don't trust any single DOM measurement as an endpoint signal on a page you
don't control. Modern websites are dynamic enough that almost any static
value can lie to you at some point. Behavior over time — does this keep
changing or not — is a much sturdier signal than asking the DOM "are we
there yet?"

If you're curious what this screenshot pipeline turned into:
knallhart.dev — it roasts your website with AI
and emails you three things that are actually wrong with it.

Happy to go deeper on any part of this if useful.