indexzero
6/6/2011 - 2:11 AM

Code samples from the "jsdom and jquery" article on the Nodejitsu blog

Code samples from the "jsdom and jquery" article on the Nodejitsu blog

curl -o jsdom-jquery.tar.gz https://gist.github.com/gists/1009644/download
tar -xvf jsdom-jquery.tar.gz
cd gist1009644*
npm install
node request.js
node jsdom-jquery.js
node jquery-request.js
node jquery-http-agent.js
node http-agent.js
var request = require('request'),
    sys = require('sys');

request({ uri:'http://www.google.com' }, function (error, response, body) {
  if (error && response.statusCode !== 200) {
    console.log('Error when contacting google.com')
  }
  
  // Print the google web page.
  sys.puts(body);
});
{
  "name": "jsdom-jquery-example",
  "description": "Code samples from the 'jsdom and jquery' article on the Nodejitsu blog",
  "version": "0.2.0",
  "author": "Charlie Robbins <charlie.robbins@gmail.com>",
  "repository": {
    "type": "git",
    "url": "git://gist.github.com/1009644.git" 
  },
  "keywords": ["jsdom", "jquery", "samples"],
  "dependencies": {
    "jsdom": "0.2.x",
    "http-agent": "0.1.x",
    "request": "1.9.x"
  },
  "engines": { "node": ">= 0.4.4" }
}
var jsdom = require('jsdom');

jsdom.env({
  html: "<html><body></body></html>",
  scripts: [
    'http://code.jquery.com/jquery-1.5.min.js'
  ]
}, function (err, window) {
  var $ = window.jQuery;

  $('body').append("<div class='testing'>Hello World</div>");
  console.log($(".testing").text()); // outputs Hello World
});
var request = require('request'),
    jsdom = require('jsdom');

request({ uri:'http://www.google.com' }, function (error, response, body) {
  if (error && response.statusCode !== 200) {
    console.log('Error when contacting google.com')
  }
  
  jsdom.env({
    html: body,
    scripts: [
      'http://code.jquery.com/jquery-1.5.min.js'
    ]
  }, function (err, window) {
    var $ = window.jQuery;

    // jQuery is now loaded on the jsdom window created from 'agent.body'
    console.log($('body').html());
  });
});
var httpAgent = require('http-agent'),
    jsdom = require('jsdom');

var agent = httpAgent.create('www.google.com', ['finance', 'news', 'images']);

agent.addListener('next', function (err, agent) {
  jsdom.env({
    html: agent.body,
    scripts: [
      'http://code.jquery.com/jquery-1.5.min.js'
    ]
  }, function (err, window) {
    var $ = window.jQuery;
    
    // jQuery is now loaded on the jsdom window created from 'agent.body'
    console.log($('body').html());
    
    agent.next();
  });
});

agent.addListener('stop', function (agent) {
  console.log('the agent has stopped');
});

agent.start();
var httpAgent = require('http-agent'),
    util = require('util');

var agent = httpAgent.create('www.google.com', ['finance', 'news', 'images']);

agent.addListener('next', function (err, agent) {
  console.log('Body of the current page: ' + agent.body);
  console.log('Response we saw for this page: ' + util.inspect(agent.response));

  // Go to the next page in the sequence
  agent.next();
});

agent.addListener('stop', function (err, agent) {
  console.log('the agent has stopped');
});

agent.start();