Skip to content

Instantly share code, notes, and snippets.

@stekhn
Last active August 29, 2015 14:14
Show Gist options
  • Save stekhn/601e35a71f1055887cb3 to your computer and use it in GitHub Desktop.
Save stekhn/601e35a71f1055887cb3 to your computer and use it in GitHub Desktop.
Take a screenshot from a website using PhantomJS
var page = require('webpage').create(),
system = require('system'),
args = system.args,
siteName;
function takeScreenshot() {
if (args.length === 1) {
console.log('Please specify a URL to screenshot.');
phantom.exit();
} else {
siteName = args[1].match(/^https?\:\/\/([^\/:?#]+)(?:[\/:?#]|$)/i)[1];
timestamp = getTimestamp();
page.open('http://sueddeutsche.de', function(status) {
console.log('Trying to take screenshot from ' + siteName);
if(status === 'success') {
page.render(sitename + '.png');
console.log('Screenshot taken.');
phantom.exit();
} else {
console.log('Could not open page. Status: ' + status);
phantom.exit();
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment