1

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?

2 Answers 2

7

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 pass null to 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 define window.top and 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.js there you can log the code and do fixes to the window object till it works.

Sign up to request clarification or add additional context in comments.

1 Comment

Oh, shit. I apologized, that jsdom works correctly and development was finished. It would be better to use PHP for parsing webpages.
3

as a clarification here, your real problem was here: jsdom(html, {}, {}). the second argument of jsdom() is a level (level1/core, level2/core, level2/html, etc). By passing an empty object you are essentially telling the jsdom method to operate without a corresponding W3c level.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.