npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

red-snapper

v1.4.1

Published

Take a screenshot of a webpage using headless Chrome

Downloads

23

Readme

Red Snapper

NPM Version NPM Downloads

Red Snapper is a simple module that takes a screenshot of a webpage using headless Chrome and the Chrome Debugging Protocol.

Important: The latest version has been tested on macOS Catalina and Ubuntu 16.04 LTS

Usage

In the following example, Red Snapper will take a 300px by 600px screenshot of github.com. If content is outside of that area it is cropped.

const fs = require('fs');
const snap = require('red-snapper');

snap({
    url: 'https://github.com/',
    width: 300,
    height: 600,
    delay: 500,
    format: 'png'
}).then((data) => {
    fs.writeFileSync('screenshot.png', Buffer.from(data, 'base64'));
}).catch((error) => {
    console.error(error);
});

In some cases you may need to pass a custom header to bypass bot detection or to get the correct cache version on a page. Add the headers object and populate with the values you need. Warning headers and their values must be strings otherwise you will get a ProtocolError.

To set the user agent string use the separate userAgent config parameter.

const fs = require('fs');
const snap = require('red-snapper');

snap({
    url: 'https://github.com/',
    width: 300,
    height: 600,
    delay: 500,
    format: 'png',
    headers: {
        'x-custom-header': 'header value'
    },
    userAgent: 'Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)'
}).then((data) => {
    fs.writeFileSync('screenshot.png', Buffer.from(data, 'base64'));
}).catch((error) => {
    console.error(error);
});

To take multiple screenshots specify an array of delays. The delays happen sequentially. So for example if you want screenshots at 1 second, 5 seconds, and 8 seconds from a page load use an array with values of [1000,4000,3000]; The return object then becomes an array of buffers.

snap({
    url: 'https://github.com/',
    width: 300,
    height: 600,
    delay: [1000,4000,3000],
    format: 'png'
}).then((data) => {
    for(let i = 0; i < data.length; i++) {
        fs.writeFileSync('screenshot'+i+'.png', Buffer.from(data[i], 'base64'));
    }
}).catch((error) => {
    console.error(error);
});

Parameters

  • url (string) - Path to website to screenshot. Use file:/// to load a local file.
  • width (integer) - Width of the browser. Defaults to 1024px. (optional)
  • height (integer) - Height of the browser. Defaults to 768px. (optional)
  • delay (integer|array) - Number of milliseconds to wait after page load before taking a screenshot. If an array is specified several screenshots will be taken with delays taking place sequentially. (optional)
  • format (string) - File format of the screenshot. Acceptable values are "png" or "jpeg". Defaults to PNGs. (optional)
  • quality (integer) - Value between [0..100] and only used when format is jpeg. Defaults to 80. (optional)
  • fullPage (boolean) - When set to true the height of the image will grow to expand the content of the page. Defaults to false. (optional)
  • headers (object) - An object containing key-value pairs. (optional)
  • userAgent (string) - A string representing the user agent to send to a website. (optional)