Skip to content

Instantly share code, notes, and snippets.

@kozy4324
Last active January 2, 2016 13:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kozy4324/8313491 to your computer and use it in GitHub Desktop.
Save kozy4324/8313491 to your computer and use it in GitHub Desktop.
capture script for phantomjs
if (phantom.args.length == 0) {
phantom.exit();
}
var i = 0, ua, url, clipRect = null, file, wait_ms;
if (!phantom.args[i].match(/^https?:\/\//)) {
ua = phantom.args[i++];
}
url = phantom.args[i++];
file = phantom.args[i++];
wait_ms = +phantom.args[i++];
if (url.match(/(.+)#(\d+)x(\d+)/)) {
url = RegExp.$1;
clipRect = {left: 0, top: 0, width: +RegExp.$2, height: +RegExp.$3};
}
if (url.match(/(.+)#(\d+),(\d+)-(\d+)x(\d+)/)) {
url = RegExp.$1;
clipRect = {left: +RegExp.$2, top: +RegExp.$3, width: +RegExp.$4, height: +RegExp.$5};
}
if (!file) {
file = url.replace(/^https?:\/\//, '').replace(/\/+$/, '').replace(/\//g, '_');
}
if (!file.match(/\.png$/)) {
file = file + '.png';
}
if (!wait_ms) {
wait_ms = 1000;
}
var page = require('webpage').create();
if (ua) {
page.settings.userAgent = ua;
if (ua.match(/(?:iPhone|Android)/)) {
page.viewportSize = {width: 320, height: 480};
}
}
page.open(url, function () {
window.setTimeout(function () {
if (clipRect) {
page.clipRect = clipRect;
}
page.render(file);
console.log('create ' + file);
phantom.exit();
}, +wait_ms);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment