Created
May 23, 2012 21:21
-
-
Save radmen/2777914 to your computer and use it in GitHub Desktop.
PhantomJS Shotr - simple script to make webpage shot
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
var page = require('webpage').create(), | |
system = require('system'), | |
fs = require('fs'), | |
uri = document.createElement('a'), // from this https://gist.github.com/2428561 | |
viewport, | |
hash = function(string) { | |
var hash = 0, | |
char; | |
if (string.length == 0) return hash; | |
for (i = 0; i < string.length; i++) { | |
char = string.charCodeAt(i); | |
hash = ((hash << 5) - hash) + char; | |
hash = hash & hash; // Convert to 32bit integer | |
} | |
return hash; | |
}; | |
if(system.args.length < 2) { | |
console.log('Usage: phantomjs ' + system.args[0] + ' url [viewport]'); | |
phantom.exit(); | |
} | |
page.viewportSize = { | |
'width': 1024, | |
'height': 728 | |
}; | |
if(system.args[2]) { | |
viewport = system.args[2].split('x'); | |
if(!isNaN(viewport[0]) && !isNaN(viewport[1])) { | |
page.viewportSize = { | |
'width': viewport[0], | |
'height': viewport[1] | |
}; | |
} | |
} | |
uri.href = system.args[1]; | |
if('file:' == uri.protocol) { | |
console.log('Enter valid URL'); | |
phantom.exit(); | |
} | |
page.open(system.args[1], function(status) { | |
var dir = uri.hostname, | |
filename = hash(uri.pathname + uri.search) + '.jpg', | |
full_path = dir + fs.separator + filename; | |
if(!fs.exists(dir) || !fs.isDirectory(dir)) { | |
fs.makeDirectory(dir); | |
} | |
page.render(full_path); | |
console.log(fs.workingDirectory + fs.separator + full_path); | |
phantom.exit(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment