Skip to content

Instantly share code, notes, and snippets.

@jzelenkov
Created July 8, 2014 18:13
Show Gist options
  • Save jzelenkov/47aa0e2430a37f642437 to your computer and use it in GitHub Desktop.
Save jzelenkov/47aa0e2430a37f642437 to your computer and use it in GitHub Desktop.
jar of cookies
var page = require('webpage').create()
var args = require('system').args
var argv = require('optimist').argv
var url = args[1]
var resolution = args[2]
var width = resolution.split('x')[0]
var height = resolution.split('x')[1]
var timeout = args[3]
var cut = args[4] == 'true'
var cookies = argv.cookie
page.viewportSize = {
width : width,
height : height
}
if (cut) page.clipRect = {
top: 0,
left: 0,
width: width,
height: height
}
if (cookies) {
// convert to array even if we have a single entry
if (!Array.isArray(cookies)) cookies = [cookies];
cookies.forEach(function(cookie) {
phantom.addCookie(JSON.parse(cookies))
})
}
// silence phantomjs
page.onConsoleMessage = function () {}
page.onConfirm = function () {}
page.onPrompt = function () {}
page.onError = function () {}
page.open(url, function (status) {
if (status !== 'success') throw 'Unable to load'
window.setTimeout(function () {
console.log(page.renderBase64('PNG'))
phantom.exit()
}, timeout)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment