Product
Introducing License Enforcement in Socket
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
"It sure would be neat if PhantomJS was a NodeJS module", I hear you say. Well, wait no longer! This node module implements a nauseously clever bridge between Phantom and Node, so that you can use all your favourite PhantomJS functions without leaving NPM behind and living in a cave.
First, make sure PhantomJS is installed. This module expects the phantomjs
binary to be in PATH somewhere. In other words, type this:
$ phantomjs
If that works, so will phantomjs-node.
Install it like this:
npm install phantom
Use it like this in Coffeescript:
phantom = require 'phantom'
phantom.create (p) ->
p.page.open "http://www.google.com", (status) ->
console.log "opened google? ", status
p.page.evaluate (-> document.title), (result) ->
console.log 'Page title is ' + result
p.exit()
In Javascript, do the same but add parentheses and curly braces everywhere.
You can use all the methods listed on the PhantomJS API page
Due to the async nature of the bridge, some things have changed, though:
page.evaluate
) are returned in a callback insteadp.get('version', callback)
or p.page.set('viewportSize', {width:640,height:480})
, etcOnly one page object per phantom is currently supported. Each phantom.create()
makes a new phantom process, so that kinda sucks. Patches welcome!
Don't ask.
I will answer that question with a question. How do you communicate with a process that doesn't support shared memory, sockets, FIFOs, or standard input?
Well, there's one thing PhantomJS does support, and that's opening webpages. In fact, it's really good at opening web pages. So we communicate with PhantomJS by spinning up an instance of ExpressJS, opening Phantom in a subprocess, and pointing it at a special webpage that turns socket.io messages into alert()
calls. Those alert()
calls are picked up by Phantom and there you go!
The communication itself happens via James Halliday's fantastic dnode library, which fortunately works well enough when combined with browserify to run straight out of PhantomJS.
FAQs
PhantomJS integration module for NodeJS
The npm package phantom receives a total of 13,046 weekly downloads. As such, phantom popularity was classified as popular.
We found that phantom demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
Product
We're launching a new set of license analysis and compliance features for analyzing, managing, and complying with licenses across a range of supported languages and ecosystems.
Product
We're excited to introduce Socket Optimize, a powerful CLI command to secure open source dependencies with tested, optimized package overrides.