phantomstream
Write and automate PhantomJS scripts inside node, using
a standard Stream interface.
Getting Started
First off, install the phantomstream
module.
npm install phantomstream
Next, create a simple script named "myscript.js".
phantomstream = require("../phantomstream")
var conf = {logger: console}
var ps = phantomstream.open(conf, function(nodestream, phantom, require, global) {
var page = require("webpage").create()
nodestream.on("data", function(data) {
var matches = /TITLE\s*(.+)/.exec(data)
if (matches) {
var url = matches[1]
console.info("opening", url)
page.open(url, function() {
var title = page.evaluate(function() {
return document.title
})
console.info("sending title =", title)
nodestream.write(title)
})
}
})
})
ps.write("TITLE http://www.google.com")
ps.on("data", function(data) {
console.info("node received:", data.toString())
process.exit()
})
Run your script with node.
node myscript.js
...you should see output like this:
[phantom-stdout] opening http://www.google.com
[phantom-stdout] sending title = Google
node received: Google
Enjoy! For more ideas on what is possible inside the callback for
phantomstream.open()
, read the
PhantomJS documentation.
Don't forget try out some demos from the examples/
directory :)
Alternatives
If you are looking for higher-level implementations that wrap the PhantomJS API,
check these out: