Last active
December 12, 2015 08:39
-
-
Save pgeraghty/4745896 to your computer and use it in GitHub Desktop.
Captures matching element of page with 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
page = require('webpage').create() | |
system = require 'system' | |
if system.args.length is 1 | |
console.log 'Usage: capture-element.coffee <URL> (filename) (#selector)' | |
phantom.exit 1 | |
else | |
address = system.args[1] | |
filename = system.args[2] || 'capture.png' | |
selector = system.args[3] | |
#console.log ([k,v] for k,v of page.viewportSize) | |
page.viewportSize = { width: 600, height: 800 } | |
page.open address, (status) -> | |
if status isnt 'success' | |
console.log 'ERROR: Failed to open URL' | |
else | |
clipRect = page.evaluate (s) -> | |
return document.querySelector(s)?.getBoundingClientRect() | |
, selector if selector? | |
if !selector? || (page.clipRect = clipRect) | |
page.render filename | |
console.log "Rendered to #{filename}" | |
else console.log "ERROR: Couldn't find a match for that selector" | |
phantom.exit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment