Last active
January 2, 2016 13:59
-
-
Save kozy4324/8313491 to your computer and use it in GitHub Desktop.
capture script for phantomjs
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
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