Last active
May 24, 2023 01:41
-
-
Save huaxlin/c8bd3df89ac7bd7c5a9297aacb74cf1b to your computer and use it in GitHub Desktop.
puppeteer use headless browser to capture screenshot
This file contains hidden or 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
/* | |
Usage: | |
``` | |
$ mkdir myscreenshot | |
$ cd myscreenshot | |
$ npm i puppeteer --save | |
$ | |
$ vim myScreenshot.js | |
const puppeteer = require('puppeteer'); | |
... | |
~ | |
:wq | |
$ node myScreenshot.js | |
... | |
$ catimg myscreenshot.png # https://github.com/posva/catimg.git | |
``` | |
*/ | |
const puppeteer = require('puppeteer'); | |
function timeout(ms) { | |
return new Promise(resolve => setTimeout(resolve, ms)); | |
}; | |
(async() => { | |
const browser = await puppeteer.launch({ | |
args: ['--no-sandbox'], // https://stackoverflow.com/a/51038064/8991693 | |
}); | |
const page = await browser.newPage(); | |
await page.setViewport({width: 800, height: 600}) | |
await page.goto('http://localhost:5173'); | |
await timeout(1000) | |
await page.screenshot({path: 'myscreenshot.png'}); | |
browser.close(); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment