Skip to content

Instantly share code, notes, and snippets.

@khilnani
Created January 26, 2014 06:49
Show Gist options
  • Save khilnani/8629452 to your computer and use it in GitHub Desktop.
Save khilnani/8629452 to your computer and use it in GitHub Desktop.
Phantom.js script to take a screenshot of a URL
#!/usr/bin/env phantomjs
//--------------------------------------------------------
var page = require('webpage').create(),
system = require('system'),
file = null,
url = null;
//--------------------------------------------------------
if (system.args.length != 3) {
console.log('Usage: screenshot.js <some Url> <image name>');
phantom.exit(1);
} else {
url = system.args[1];
file = system.args[2];
}
//--------------------------------------------------------
page.onResourceReceived = function (response) {
if(response.stage == "end")
console.log('Loaded Asset (#' + response.id + ', status "' + response.status + '"): ' + response.url);
}
//--------------------------------------------------------
page.onLoadFinished = function(status) {
// console.log('Status: ' + status);
if(status == 'success') {
console.log( "URL Loaded.");
page.render( file + '.png');
console.log( "Saved screenshot to " + file + '.png');
phantom.exit();
} else {
console.log('Connection failed.');
phantom.exit();
}
}
//--------------------------------------------------------
page.open(url);
console.log( "Loading URL: " + url);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment