Skip to content

Instantly share code, notes, and snippets.

@mhsattarian
Last active July 9, 2020 13:43
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 mhsattarian/c420dddc9390d641ed962ce4ec2d7c36 to your computer and use it in GitHub Desktop.
Save mhsattarian/c420dddc9390d641ed962ce4ec2d7c36 to your computer and use it in GitHub Desktop.
Using puppeteer-core and chrome-aws-lamba to screenshot a web element
// Code from https://github.com/leighhalliday/generate-og-image/blob/master/src/chromium.ts
// and https://github.com/wesbos/wesbos/blob/master/functions/ogimage/ogimage.js
// and thanks wes.bos for the video on this at https://youtu.be/A0Ww-SU7K5E
const chrome = require("chrome-aws-lambda");
const puppeteer = require("puppeteer-core");
const exePath = "/usr/bin/google-chrome";
async function getOptions(isDev) {
let options;
if (isDev) {
options = {
args: ["--incognito"],
executablePath: exePath,
headless: true,
};
} else {
options = {
args: chrome.args,
executablePath: await chrome.executablePath,
headless: chrome.headless,
};
}
return options;
}
async function getScreenshot(url, isDev) {
const options = await getOptions(isDev);
console.log("📸");
const browser = await puppeteer.launch(options);
const page = await browser.newPage();
await page.setViewport({ width: 720, height: 1920, deviceScaleFactor: 1.5 });
await page.goto(url);
return await page.screenshot({ type: "jpeg", quality: 100 });
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment