Created
February 14, 2013 21:51
-
-
Save oloynet/4956732 to your computer and use it in GitHub Desktop.
Just my two cents for screenshot with a size parameter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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(); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
casper.start() needs an URL as first argument:
https://gist.github.com/nhoizey/4959525