Skip to content

Instantly share code, notes, and snippets.

@xDimGG
Last active June 5, 2018 19:30
Show Gist options
  • Save xDimGG/9813e36bbcb79fdb3737a2ab7fdd4c35 to your computer and use it in GitHub Desktop.
Save xDimGG/9813e36bbcb79fdb3737a2ab7fdd4c35 to your computer and use it in GitHub Desktop.
Freely grab screenshots of web pages via Google's hidden PageSpeed API.
const { get } = require('snekfetch');
/**
* @typedef {Object} Query
* @prop {'mobile'|'desktop'} strategy The strategy to use when taking the screenshot
*/
/**
* Screenshot a web page using its URL
* @param {string} url Web page URL to screenshot
* @param {?Query} query Extra querystring parameters
* @returns {Promise<String>} Base64 encoded image
*/
module.exports = (url, query = {}) =>
get('https://www.googleapis.com/pagespeedonline/v1/runPagespeed')
.query({
screenshot: true,
strategy: 'desktop',
...query,
url,
})
.then(res => res.body.screenshot.data.replace(/_/g, '/').replace(/-/g, '+'));
{
"author": "lloti <[email protected]> (https://dim.codes)",
"name": "page-screenshotter",
"version": "1.0.0",
"description": "Freely grab screenshots of web pages via Google's hidden PageSpeed API.",
"private": true,
"dependencies": {
"snekfetch": "^4.0.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment