Last active
July 9, 2020 13:43
-
-
Save mhsattarian/c420dddc9390d641ed962ce4ec2d7c36 to your computer and use it in GitHub Desktop.
Using puppeteer-core and chrome-aws-lamba to screenshot a web element
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
// 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