Skip to content

Instantly share code, notes, and snippets.

@BinaryMuse
Created April 13, 2012 18:33
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save BinaryMuse/2378993 to your computer and use it in GitHub Desktop.
Save BinaryMuse/2378993 to your computer and use it in GitHub Desktop.
Take screenshots with Phantom.js from Node.js
#!/usr/bin/env coffee
# Call the program with: coffee phantom.coffee http://url.to/screengrab
phantom = require 'phantom' # npm install phantom
child_process = require 'child_process'
url = process.argv[2]
createScreenshot = (page, filename) ->
page.render filename, ->
child_process.exec "open #{filename}"
process.exit()
phantom.create (ph) ->
ph.createPage (page) ->
page.set 'viewportSize', width: 1000, height: 1000
page.set 'clipRect', top: 0, left: 0, width: 1000, height: 1000
console.log "Opening #{url} ..."
page.open url, (status) ->
console.log "Rendering screenshot ..."
setTimeout (-> createScreenshot(page, 'output.png')), 1000
@matbee-eth
Copy link

Seriously, CoffeeScript? Fffffffffffffffff.

@BinaryMuse
Copy link
Author

Yep, I'm a pretty big fan overall.

@greenlieber
Copy link

Yeah would be nice to add the Gist with the regular js (readable) :)

@sh4n3d4v15
Copy link

You can convert the above code to javascript here: http://js2coffee.org/ :)

@qrpike
Copy link

qrpike commented Mar 20, 2013

+1 for coffeescript - I used to complain about it too, then i learned it, now I love it....

@underbluewaters
Copy link

Ah, I'll just paste this coffeescript example directly into my coffeescript app. Muahaha :P

@Rayjax
Copy link

Rayjax commented Jun 2, 2014

Nice. I had the same code, I added your sets for viewPortSize and a few other things.(sharing of a single Phantom instance)
https://gist.github.com/Rayjax/b793c561a54518bff845
Both JavaScript and Coffee versions

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment