Skip to content

Instantly share code, notes, and snippets.

@tankxu
Last active July 2, 2020 13:28
Show Gist options
  • Save tankxu/2f776f7780edede1b18c96f500aa611a to your computer and use it in GitHub Desktop.
Save tankxu/2f776f7780edede1b18c96f500aa611a to your computer and use it in GitHub Desktop.
Take a full screenshot for long website
const puppeteer = require("puppeteer")
// Set parameter
const urls = [
'https://github.com/enterprise',
'https://github.com/team'
]
const pageWidth = 1600
const delayTime = 5000
// Functions
async function shotFullPage(urls, pageWidth, delayTime) {
const browser = await puppeteer.launch({ headless: true })
console.log(`Total number of websites: ${urls.length}`)
await Promise.all(urls.map(async (url, index) => {
let page = await browser.newPage()
// Set a higher higher higher height value more than the page's height
await page.setViewport({width: pageWidth, height: 30000})
console.log(`====> Opening page ${index + 1}...`)
await page.goto(url, {waitUntil: 'networkidle2'})
let title = await page.title()
console.log(`Page ${index + 1} : Page's title is \"${title}\"`)
console.log(`Page ${index + 1} : Delaying ${delayTime} for page load...`)
await page.waitFor(delayTime)
// Get the page's height
let pageHeight = await page.evaluate(() => {
let body = document.querySelector('body')
let {x, y, width, height} = body.getBoundingClientRect()
return height
})
console.log(`Page ${index + 1} : The page's height is ${pageHeight}px`)
// Take a screeshot
await page.screenshot({
path: `${title}.jpg`,
clip: {
x: 0,
y: 0,
width: pageWidth,
height: pageHeight
}
})
console.log(`Page ${index + 1} : Screenshot has been saved to the \"${title}.png\"`)
await page.close();
}))
await browser.close();
}
shotFullPage(urls, pageWidth, delayTime)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment