Coder Social home page Coder Social logo

phantomjs-node's Introduction

PhantomJS bridge for NodeJS Build Status

"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.

Installation

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. It's only been tested with PhantomJS 1.3, and almost certainly doesn't work with anything older.

Install it like this:

npm install phantom

For a brief introduction continue reading, otherwise go to the Wiki page for more information!

How do I use it?

Use it like this in Coffeescript:

phantom = require 'phantom'

phantom.create (ph) ->
  ph.createPage (page) ->
    page.open "http://www.google.com", (status) ->
      console.log "opened google? ", status
      page.evaluate (-> document.title), (result) ->
        console.log 'Page title is ' + result
        ph.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:

  • Return values (ie, of page.evaluate) are returned in a callback instead
  • page.render() takes a callback so you can tell when it's done writing the file
  • Properties can't be get/set directly, instead use p.get('version', callback) or p.page.set('viewportSize', {width:640,height:480}), etc. Nested objects can be accessed by including dots in keys, such as p.page.set('settings.loadImages', false)
  • Callbacks can't be set directly, instead use page.set('callbackName', callback), e.g. page.set('onLoadFinished', function(success) {})

ph.createPage() makes new PhantomJS WebPage objects, so use that if you want to open lots of webpages. You can also make multiple phantomjs processes by calling phantom.create('flags', { port: someDiffNumber}) multiple times, so if you need that for some crazy reason, knock yourself out!

You can also pass command line switches to the phantomjs process by specifying additional args to phantom.create(), eg:

phantom.create '--load-images=no', '--local-to-remote-url-access=yes', (page) ->

##Note for Mac users

Phantom requires you to have the XCode Command Line Tools installed on your box, or else you will get some nasty errors (xcode not found or make not found). If you haven't already, simply install XCode through the App Store, then install the command line tools.

How does it work?

Don't ask. The things these eyes have seen.

No really, how does it work?

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's pidgin Javascript environment.

If you'd like to hack on phantom, please do! You can run the tests with cake test or npm test, and rebuild the coffeescript/browserified code with cake build. You might need to npm install -g coffee-script for cake to work.

phantomjs-node's People

Contributors

amir20 avatar baudehlo avatar brettjonesdev avatar calamari avatar digitalcraft avatar dotnetwise avatar endel avatar enki avatar ggoodale avatar jhurliman avatar juriejan avatar marcellodisimone avatar markdalgleish avatar meelash avatar obeattie avatar pilsy avatar pongells avatar rauchg avatar scien avatar sgentle avatar shaunxcode avatar stygeo avatar trevordixon avatar ysimonson avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.