Skip to content

Instantly share code, notes, and snippets.

@huaxlin
Last active May 24, 2023 01:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save huaxlin/c8bd3df89ac7bd7c5a9297aacb74cf1b to your computer and use it in GitHub Desktop.
Save huaxlin/c8bd3df89ac7bd7c5a9297aacb74cf1b to your computer and use it in GitHub Desktop.
puppeteer use headless browser to capture screenshot
/*
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