Created
October 12, 2018 08:18
-
-
Save janumedia/11d5e1179891003e19873ca579a4da77 to your computer and use it in GitHub Desktop.
Get Lazy Images Screenshot Page with Puppeteer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const helper = require('./puppeteer-helper'); | |
(async() => { | |
const {browser, page} = await helper({deviceName:'iPhone 6'}); | |
const pageHeight = await page.evaluate(() => { | |
const body = document.body; | |
const html = document.documentElement; | |
return Math.max(body.scrollHeight, body.offsetHeight, html.clientHeight, html.scrollHeight, html.offsetHeight); | |
}); | |
const viewportHeight = page.viewport().height; | |
let screenHeight = 0; | |
while(screenHeight < pageHeight) { | |
await page.evaluate(vh => { | |
window.scrollBy(0, vh); | |
}, viewportHeight); | |
//give delay for load lazy image | |
await page.waitFor(10); | |
screenHeight += viewportHeight; | |
} | |
//give delay to complete images loaded | |
await page.waitFor(2000); | |
await page.screenshot({path: 'screenshot-lazyimages.jpg', type: 'jpeg', quality: 60, fullPage: true}); | |
await browser.close(); | |
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const puppeteer = require('puppeteer'); | |
const devices = require('puppeteer/DeviceDescriptors'); | |
const defaultOptions = { | |
headless: true, | |
pageURL: 'http://vue-web-components.netlify.com', | |
waitUntil: 'networkidle0' | |
}; | |
module.exports = async function(options) { | |
const {headless, pageURL, waitUntil, deviceName} = options || defaultOptions; | |
const browser = await puppeteer.launch({headless: headless || defaultOptions.headless}); | |
const page = await browser.newPage(); | |
if(deviceName) await page.emulate(devices[deviceName]); | |
await page.goto(pageURL || defaultOptions.pageURL, {waitUntil: waitUntil || defaultOptions.waitUntil }); | |
return { | |
browser, | |
page | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment