0.2.5 • Published 11 years ago

node-phantom v0.2.5

Weekly downloads
404
License
-
Repository
github
Last release
11 years ago

Node-phantom

This is a bridge between PhantomJs and Node.js.

It is very much similar to the other bridge available, PhantomJS-Node, but is different in a few ways:

  • Way fewer dependencies.
  • API has the idiomatic error indicator as first parameter to callbacks.
  • Uses plain Javascript instead of Coffeescript.

Requirements

You will need to install PhantomJS first. The bridge assumes that the "phantomjs" binary is available in the PATH.

The only other dependency for using it is socket.io.

For running the tests you will need Expresso.

Installing

For now you will need to manually install socket.io and node-phantom. Npm package to be arriving shortly.

Usage

You can use it pretty much like you would use PhantomJS-Node, for example this is an adaptation of a web scraping example :

var phantom=require('node-phantom');
phantom.create(function(err,ph) {
  return ph.createPage(function(err,page) {
    return page.open("http://tilomitra.com/repository/screenscrape/ajax.html", function(err,status) {
      console.log("opened site? ", status);
      page.injectJs('http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js', function(err) {
        //jQuery Loaded.
        //Wait for a bit for AJAX content to load on the page. Here, we are waiting 5 seconds.
        setTimeout(function() {
          return page.evaluate(function() {
            //Get what you want from the page using jQuery. A good way is to populate an object with all the jQuery commands that you need and then return the object.
            var h2Arr = [],
            pArr = [];
            $('h2').each(function() {
              h2Arr.push($(this).html());
            });
            $('p').each(function() {
              pArr.push($(this).html());
            });

            return {
              h2: h2Arr,
              p: pArr
            };
          }, function(err,result) {
            console.log(result);
            ph.exit();
          });
        }, 5000);
      });
	});
  });
});

You can also have a look at the test folder to see some examples of using the API.

Other

Made by Alex Scheel Meyer. Released to the public domain.