Skip to content

Instantly share code, notes, and snippets.

@oloynet
Created February 14, 2013 21:51
Show Gist options
  • Save oloynet/4956732 to your computer and use it in GitHub Desktop.
Save oloynet/4956732 to your computer and use it in GitHub Desktop.
Just my two cents for screenshot with a size parameter
/**
* This script will capture a screenshot of a webpage page
* Usage: $ casperjs screenshot.js <url> <filename.[jpg|png|pdf]> [--size=800x600] [--rename]
*
* QVGA (320x240)
* HVGA (480x320)
* VGA (640x480)
* SVGA (800x600)
* XGA (1024x768)
* HD (1280x720)
* SXGA (1280x1024)
* SXGA+ (1400x1050)
* UXGA (1600x1200)
* WUXGA (1920x1200)
*/
var casper = require('casper').create(),
utils = require('utils'),
url = casper.cli.get(0),
filename = casper.cli.get(1),
size = casper.cli.raw.get('size') || '640x480',
rename = casper.cli.get('rename') || false;
if (!url || !filename || !/\.(png|jpg|pdf)$/i.test(filename)) {
casper
.echo("Usage: $ casperjs screenshot.js <url> <filename.[jpg|png|pdf]> [--size=800x600] [--rename]")
.exit(1)
;
}
casper.start(size, function() {
if ( size.indexOf('x') < 0 ) {
this.echo("Incorrect format '" + size + "', need a 'x' to separate size values");
this.exit(1);
}
var size_img = size.split('x');
var width = Math.max(~~size_img[0], 240) || 320; // minimum = 320px
var height = ~~size_img[1] || 240;
size = width.toString() + 'x' + height;
this.page.viewportSize = {
width: width,
height: height,
};
});
casper.thenOpen(url, function() {
if ( rename ) {
filename = filename.replace(/(.*)\.(png|jpg|pdf)$/i, '$1-' + size + '.$2');
}
this.captureSelector(filename, "html");
var result = {
'currentUrl': this.getCurrentUrl(),
'filename': filename
}
this.echo(JSON.stringify(result)).exit();
});
casper.run();
@nhoizey
Copy link

nhoizey commented Feb 15, 2013

casper.start() needs an URL as first argument:
https://gist.github.com/nhoizey/4959525

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment