screenshotmachine-nodejs
This demo shows how to call online screenshot machine API using Node.js - JavaScript run-time environment.
Installation
Install Node.js package:
npm install screenshotmachine --save
First, you need to create a free/premium account at www.screenshotmachine.com website. After registration, you will see your customer key in your user profile. Also secret phrase is maintained here. Please, use secret phrase always, when your API calls are called from publicly available websites.
Set-up your customer key and secret phrase (if needed) in the script:
var customerKey = 'PUT_YOUR_CUSTOMER_KEY_HERE';
secretPhrase = '';
Set other options to fulfill your needs:
options = {
url : 'https://www.google.com',
dimension : '1366xfull',
device : 'desktop',
format: 'png',
cacheLimit: '0',
delay: '200'
}
More info about options can be found in our API guide.
Sample code
var screenshotmachine = require('screenshotmachine');
var customerKey = 'PUT_YOUR_CUSTOMER_KEY_HERE';
secretPhrase = '';
options = {
url : 'https://www.google.com',
dimension : '1366xfull',
device : 'desktop',
format: 'png',
cacheLimit: '0',
delay: '200'
}
var apiUrl = screenshotmachine.generateApiUrl(customerKey, secretPhrase, options);
console.log('<img src="' + apiUrl + '">');
Generated apiUrl
link can be placed in <img>
tag or used in your business logic later.
If you need to store captured screenshot as an image, just call:
var screenshotmachine = require('screenshotmachine');
var customerKey = 'PUT_YOUR_CUSTOMER_KEY_HERE';
secretPhrase = '';
options = {
url : 'https://www.google.com',
dimension : '1366xfull',
device : 'desktop',
format: 'png',
cacheLimit: '0',
delay: '200'
}
var apiUrl = screenshotmachine.generateApiUrl(customerKey, secretPhrase, options);
var fs = require('fs');
var output = 'output.png';
screenshotmachine.readScreenshot(apiUrl).pipe(fs.createWriteStream(output).on('close', function() {
console.log('Screenshot saved as ' + output);
}));
Captured screenshot will be saved as output.png
file in current directory.
License
The MIT License (MIT)