Created
June 6, 2011 02:11
-
-
Save indexzero/1009644 to your computer and use it in GitHub Desktop.
Code samples from the "jsdom and jquery" article on the Nodejitsu blog
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | |
}); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "jsdom-jquery-example", | |
"description": "Code samples from the 'jsdom and jquery' article on the Nodejitsu blog", | |
"version": "0.2.0", | |
"author": "Charlie Robbins <[email protected]>", | |
"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" } | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment