Skip to content

Instantly share code, notes, and snippets.

@ox
Created December 10, 2018 18:57
Show Gist options
  • Save ox/0099eccb30451d567c9cfd7ba2f0bb2b to your computer and use it in GitHub Desktop.
Save ox/0099eccb30451d567c9cfd7ba2f0bb2b to your computer and use it in GitHub Desktop.
Tool using `puppeteer-core` to take full screenshots of a given url.
const Promise = require('bluebird');
const puppeteer = require('puppeteer-core');
const step = 3000;
async function Screenshot(page, y = 0) {
const toRemove = ['header', '.back-to-top-button', '.ad', '.bxc'];
await Promise.mapSeries(toRemove, (cls) => {
return page.$$eval(cls, elements => elements.forEach(elem => elem.remove()))
.catch(() => {});
});
await page.waitFor(2000);
await page.screenshot({path: `out/${y}.png`});
// get current scroll position
const scrollY = await page.evaluate(() => window.scrollY);
// move the viewport
console.log('scrolling to', y);
await page.evaluate(() => window.scrollBy(0, window.innerHeight));
const newScrollY = await page.evaluate(() => window.scrollY);
if (scrollY != newScrollY) {
return Screenshot(page, y + step);
}
return;
}
(async () => {
if (process.argv.length != 3) {
console.error(`Usage: $ node ${__filename} <url>`);
return;
}
const url = process.argv[2];
const browser = await puppeteer.launch({headless: true, executablePath: '/usr/bin/google-chrome'});
const page = await browser.newPage();
page.setViewport({width: 1240, height: step, deviceScaleFactor: 3});
await page.goto(url);
await page.waitFor(1000);
await Screenshot(page);
console.log('done');
await browser.close();
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment