CronShot
Node module that allows you to schedule, take, alter, and store web page screenshots
npm install cronshot
How
CronShot uses:
Setup
Examples
Take and Save An Image To The Local Filesystem One Time
npm install cronshot-local
var cronshot = require('cronshot'),
middleware = {
'local': require('cronshot-local')
};
cronshot.startCapturing({
'url': 'http://sports.yahoo.com',
'path': __dirname,
'cronPattern': false,
'saveMiddleware': [middleware.local]
}, function(err) {
if (err) {
console.error(err);
}
});
Take and Save a Transparent Screenshot
npm install cronshot-imagemagick
brew update && brew install imagemagick
var cronshot = require('cronshot'),
middleware = {
'imagemagick': require('cronshot-imagemagick')
};
cronshot.startCapturing({
'url': 'http://sports.yahoo.com',
'path': __dirname,
'cronPattern': false,
'saveMiddleware': [{
'middleware': middleware.imagemagick,
'options': {
'gmCommands': [{
'method': 'trim',
'args': []
}, {
'method': 'transparent',
'args': ['#FFFFFF']
}]
}
}]
}, function(err) {
});
Take and Save A Screenshot To The Local Filesystem Every 10 seconds
npm install cronshot-local
var cronshot = require('cronshot'),
middleware = {
'local': require('cronshot-local')
};
cronshot.startCapturing({
'url': 'http://sports.yahoo.com',
'path': __dirname,
'cronPattern': '*/10 * * * * *',
'saveMiddleware': [middleware.local],
}, function(err) {
if (err) {
console.error(err);
}
});
Run One Or More Cron Jobs In Parallel
npm install cronshot-local
var cronshot = require('cronshot'),
middleware = {
'local': require('cronshot-local')
};
cronshot.startCapturing({
'parallelLimit': 2,
'screenshots': [{
'url': 'http://sports.yahoo.com',
'path': __dirname,
'imageName': 'screenshot.png',
'saveMiddleware': [middleware.local]
}, {
'url': 'http://yahoo.com',
'path': __dirname,
'imageName': 'screenshot1.png',
'saveMiddleware': [middleware.local]
}]
}, function(err) {
if (err) {
console.error(err);
}
});
Passing Options via Command Line
node cronshot-runner.js --customCSS 'body { background: blue !important; }' --url 'http://yahoo.com'
Note: You can pass BOTH code options AND command line options. If you pass the same option via both methods, the command line option takes precedence.
Save Middleware
The saveMiddleware
option accepts one or more functions, that are run serially (in order), to manipulate/save a screenshot image.
Available Save Middleware
Below are the current middleware functions available:
cronshot-local - Cronshot middleware to save images locally
cronshot-imagemagick - Cronshot middleware to manipulate and save images with ImageMagick
Writing Your Own Save Middleware
Best Practices
-
Should be small (~100 or less of code)
-
Should do one thing (eg. save to filesystem, manipulate image, save to cdn)
-
Should call a callback function when completed
Example
module.exports = function(obj, callback) {
var options = obj.options,
content = obj.readStream,
info = {
'name': 'example-middleware'
},
error = false;
callback(error, info);
}
Review
Save middleware functions are only provided three things; the user options, the screenshot read stream, and the callback function to call when all actions are completed. Please make sure to pass an info
object, with the name of the middleware, to the callback function.
Options
'cronPattern': '*/10 * * * * *',
'url': '',
'imageName': 'screenshot.png',
'path': '',
'userAgent': '',
'screenSize': {
'width': 1024,
'height': 768
},
'shotSize': {
'width': 'window',
'height': 'window'
},
'shotOffset': {
'left': 0,
'right': 0,
'top': 0,
'bottom': 0
},
'phantomPath': 'phantomjs',
'phantomConfig': {},
'customHeaders': null,
'defaultWhiteBackground': true,
'customCSS': '',
'quality': 75,
'streamType': 'png',
'siteType': 'url',
'renderDelay': 0,
'timeout': 0,
'takeShotOnCallback': false,
'errorIfStatusIsNot200': false,
'silent': false,
'max': false,
'parallelLimit': 5
Contributing
Please send all PR's to the dev
branch.
If your PR is a code change:
- Install all node.js dev dependencies:
npm install
- Update the appropriate module inside of the
src/modules
directory. - Add a unit test inside of
tests/unit/cronshot.js
. - Verify that all tests are passing by running
npm test
. - Send the PR!
Credits
cronshot.js would not have been possible without the help/inspiration of the following libraries/awesome people:
- Schedules cron jobs
- Copyright (c) Brenden Kokoszka, 2010 - MIT License
- Async utilities for node and the browser
- Copyright (c) Caolan McMahon, 2010-2014 - MIT License
Contributors