我正在尝试从示例运行简单的代码:

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();


但它在此行返回很多错误:

var window = jsdom.jsdom(body, {},{}).createWindow();


错误:

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)


我有带有稳定的node.js 0.2.5的Ubuntu 10.10。 WTF?

最佳答案

对于jsom而言,这是正常的工作,正在进行中,DOM API庞大,重新实现所有浏览器怪癖需要花费大量时间。

但是首先,您调用jsdom.jsdom(body, {},{}).createWindow();是错误的,第二个参数应该是一个完全配置的DOM对象,如果您不提供该参数,则只需传递null使其回到默认值即可。

但是在那之后,它一直都是乌龟。

例如,Twitter执行此window.top.location,结果表明jsdom没有定义window.top和BOOM!

解决这个问题,是的,乌龟。错误不会停止,如果您有兴趣彻底解决它,请打开<npm folder>/jsdom/0.1.21/package/lib/jsdom/level2/languages/javascript.js在那里,您可以记录代码并修复window对象,直到它起作用为止。

关于jquery - jsdom的奇怪错误-node.js,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4378176/

10-11 08:20