moonlight-coding / casper-nodejs

Control CasperJS from your nodejs application.
MIT License
15 stars 5 forks source link

casper-nodejs

Control CasperJS from your nodejs application.

Objective

Allow you to write powerful scraping scripts in nodeJs, by using CasperJS from nodeJs.

Requirements

How to Use

Installation

Install the dependencies:

Api

casper.then(fn_in_current_context, null);
casper.then(fn_in_casperjs_context, fn_in_current_context);

Usage

Create a nodejs script.


var casper_nodejs = require('../../index.js');

var url = "http://google.com";
// load the page refered with 'url' with casper
var casper = casper_nodejs.create(url, {});

// once the page is loaded, execute that in our current nodejs context
casper.then(function executed_in_this_context() {
  console.log("page loaded");
});

// then, execute that in casperjs, and the second callback in the current nodejs context
casper.then(function executed_in_casperjs_context() {
  return 42;
}, function executed_in_this_context(ret) {
  console.log("it works: " + ret);

  // casper.exit() can be placed here too, instead of in the bottom :)
  // casper.exit();
});

// exit casper after executing the 2 previous 'then'
casper.exit();

How it works

casper.create() spawns a child (casper-child), which:

In the nodejs context:

Advantages

Disavantages

See Also

License

Project under MIT License (see the file LICENSE).