Created
January 26, 2014 06:49
-
-
Save khilnani/8629452 to your computer and use it in GitHub Desktop.
Phantom.js script to take a screenshot of a URL
This file contains 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
#!/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