forked from RemiixInc/screenshot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpuppeteer.ts
More file actions
29 lines (23 loc) · 796 Bytes
/
puppeteer.ts
File metadata and controls
29 lines (23 loc) · 796 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import { launch, Page } from "puppeteer-core";
const chromium = require('@sparticuz/chromium')
let _page: Page | null;
async function getPage() {
if (_page) return _page;
const options = {
args: chromium.args,
defaultViewport: chromium.defaultViewport,
executablePath: await chromium.executablePath(),
headless: chromium.headless,
};
const browser = await launch(options);
_page = await browser.newPage();
return _page;
}
export async function getScreenshot(url, width, height) {
const page = await getPage();
await page.setViewport({ width: Number(width) || 1280, height: Number(height) || 720, deviceScaleFactor: 2 });
await page.goto(url || './index.html');
await new Promise(r => setTimeout(r, 3000));
const file = await page.screenshot();
return file;
}