diff --git a/src/index.ts b/src/index.ts index 22e9b03..bea54f0 100644 --- a/src/index.ts +++ b/src/index.ts @@ -70,7 +70,7 @@ class CdpSession { async captureScreenshot(format: string = 'png'): Promise { const res = await this.send('Page.captureScreenshot', { format, - captureBeyondViewport: true, + captureBeyondViewport: false, }); return Buffer.from(res.data, 'base64'); } @@ -99,19 +99,21 @@ async function scrollAndCapture( ) || 0; const viewportHeight: number = await cdp.evaluate('window.innerHeight') || 900; - // Scroll through the page and capture screenshots + // Scroll through the page and capture viewport-sized screenshots + // Use 80% step to overlap slightly and avoid missing content at boundaries + const step = Math.floor(viewportHeight * 0.8); let scrollY = 0; let idx = 1; while (scrollY < pageHeight) { await cdp.evaluate(`window.scrollTo(0, ${scrollY})`); - await new Promise(r => setTimeout(r, 500)); // wait for render + await new Promise(r => setTimeout(r, 800)); // wait for lazy-load render const buf = await cdp.captureScreenshot('png'); const filePath = path.join(outputDir, `page_${String(idx).padStart(3, '0')}.png`); fs.writeFileSync(filePath, buf); saved.push(filePath); - scrollY += viewportHeight; + scrollY += step; idx++; }