Skip to content

Instantly share code, notes, and snippets.

@bkeating
Last active September 15, 2021 16:07
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 bkeating/92fa4822846ff7d0d40aa83331034fa4 to your computer and use it in GitHub Desktop.
Save bkeating/92fa4822846ff7d0d40aa83331034fa4 to your computer and use it in GitHub Desktop.
Capture a full-page screeenshot of a URL. Taken from Abstractly's dja project.
/**
* Capture a screenshot of a given URL
*
* USAGE: $ node ./capture_screenshot.js my-page-slug 34sdg-252gsg-23dsg-2234 https://mysite.com/my-page-slug/
*
*/
const puppeteer = require("puppeteer");
let pageSlug = process.argv[2];
let clientGuid = process.argv[3];
let url = process.argv[4];
(async () => {
// 1. Launch the browser
const browser = await puppeteer.launch({
args: ["--no-sandbox"],
defaultViewport: {
width: 1200,
height: 1440,
// isLandscape: true,
},
});
// 2. Open a new page
const page = await browser.newPage();
// 3. Navigate to URL
await page.goto(url, { waitUntil: "networkidle2" });
// 4. Take screenshot
// const randomHash = Math.random().toString(36).substr(2, 10);
const filename = `${pageSlug}.jpg`;
const path = `media/${clientGuid}/screenshots/${filename}`;
await page.screenshot({
type: "jpeg",
path,
// omitBackground: true
});
await browser.close();
// Return the relative path to the image
console.log(path);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment