I am trying to run simple code from examples:
require.paths.unshift('/etc/npm'); // path to modules
var httpAgent = require('http-agent'),
jsdom = require('jsdom'),
sys = require('sys');
var agent = httpAgent.create('www.twitter.com', ['', 'about']);
agent.addListener('next', function (e, agent) {
var body = agent.body;
var window = jsdom.jsdom(body, {},{}).createWindow();
jsdom.jQueryify(window, 'http://code.jquery.com/jquery-1.4.2.js', function (window, jquery) {
agent.next();
});
});
agent.addListener('stop', function (agent) {
sys.puts('the agent has stopped');
});
agent.start();
But it returns a lot of errors on this line:
var window = jsdom.jsdom(body, {},{}).createWindow();
Erros:
kir@nas:~/node$ node test.js
/usr/local/lib/node/.npm/jsdom/0.1.21/package/lib/jsdom/browser/index.js:197
if (!dom.HTMLDocument.write) {
^
TypeError: Cannot read property 'write' of undefined
at Object.browserAugmentation (/usr/local/lib/node/.npm/jsdom/0.1.21/package/lib/jsdom/browser/index.js:197:24)
at Object.jsdom (/usr/local/lib/node/.npm/jsdom/0.1.21/package/lib/jsdom.js:15:25)
at EventEmitter.<anonymous> (/home/kir/node/test.js:12:22)
at EventEmitter.emit (events:27:15)
at Object.emit (/usr/local/lib/node/.npm/http-agent/0.1.0/package/lib/http-agent.js:180:41)
at /usr/local/lib/node/.npm/http-agent/0.1.0/package/lib/http-agent.js:145:14
at IncomingMessage.<anonymous> (/usr/local/lib/node/.npm/request/0.10.0/package/lib/main.js:89:7)
at IncomingMessage.emit (events:41:20)
at HTTPParser.onMessageComplete (http:107:23)
at Client.onData [as ondata] (http:848:27)
I have Ubuntu 10.10 with stable node.js 0.2.5. WTF?
Normal for jsom, it’s work in progress, the DOM API is gigantic and re-implementing all the Browser quirks takes a lot of time.
But first of all your call
jsdom.jsdom(body, {},{}).createWindow();is wrong, second parameter should be a fully configure DOM object, if you don’t provide one just passnullto make it fall back to a default one.But after that it’s turtles all the way down.
Twitter for example does this
window.top.location, turns out that jsdom does not definewindow.topand BOOM!Fixing that… yeah, turtles. The errors don’t stop, if you’re interested in fixing it all the way down, open
<npm folder>/jsdom/0.1.21/package/lib/jsdom/level2/languages/javascript.jsthere you can log the code and do fixes to thewindowobject till it works.