node.js: problems with jsdom - jQuery object retur

2019-08-11 02:42发布

I am interested in doing some server-side DOM manipulations for one of projects, so I decided to take a look at node.js, and at jsdom in particular. I'm completely new to node.js, so probably my mistakes are obvious, but never the less i couldn't find solution for my problem anywhere...

First of all i'm running node on Windows locally, and also use online IDE cloud9

It was worth mentioning as i got troubles installing latest jsdom package for node (which is 0.2.15), because a required dependency contextify would not compile under Windows (C++ and Python required, and as I discovered npm does not support building C++ packages). So i took two alternative approaches:

  1. I've installed an older version that does not depend on contextify, that is jsdom@0.2.1. Here is npm list of what i currently have

    └─┬ jsdom@0.2.1
      ├── cssom@0.2.5
      ├── htmlparser@1.7.6
      └── request@2.10.0
    

    When i tried to run simple test script taken from jsdom github repo cloud9 returned this warning

    The "sys" module is now called "util". It should have a similar
    

    I tried to fix this replacing all the mentions of sys to util, error disappeared... (still not so sure it was right thing to do though...). And so, finally when I tried to run this snippet

    var jsdom = require("jsdom");
    
    jsdom.env("http://nodejs.org/dist/", [
      'http://code.jquery.com/jquery-1.5.min.js'
    ],
    function(errors, window) {
      console.log("there have been", window.$("a").length, "nodejs releases!");
    });
    

    I found out that window.$ === undefined. Tried to run with node v0.6.x and node v0.4.x on cloud9 and with node v0.8.7 locally.

  2. Another thing i tried to do was to install latest jsdom@0.2.15, get a compiled contextify from over here, install bindings and add it to contextify/node_modules as contextify is dependent on it. I was not able to make it work at all =( Didn't waste much time, as it seems to be a false approach, and contextify is not up to date.

    Here's npm list

    └─┬ jsdom@0.2.15
      ├─┬ contextify@0.1.2
      │ └── bindings@1.0.0
      ├── cssom@0.2.5
      ├── htmlparser@1.7.6
      └── request@2.10.0
    

Also, here's my cloud9 workspace, don't be shy to play around.

Any help appreciated. Maybe there are other better to accomplish what I wanted.

Thanks.

1条回答
孤傲高冷的网名
2楼-- · 2019-08-11 03:39

Cheerio is a new module that came out not a long time ago that is a lot more simple to use than jsdom and access many of the issue you can run with jsdom.

jsdom is awesome but it can be really hard to use if you need to parse non valid HTML you have to play with the html5 parser module, which is super slow and the all thing is not dev friendly.

There is also PhantomJS to consider:
http://phantomjs.org my bootstrap to get started with jQuery.
https://github.com/sgentle/phantomjs-node (this is such a hack, I'm speechless)

查看更多
登录 后发表回答