Last active
July 2, 2020 13:28
-
-
Save tankxu/2f776f7780edede1b18c96f500aa611a to your computer and use it in GitHub Desktop.
Take a full screenshot for long website
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") | |
// 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