Is there a way to get jquery to work with node.io?

Node.js noob. I use node.io to clean sites, but I would like to use jquery in node.io. The $ object provided by node.io does not provide much flexibility.

var nodeio = require('node.io'), options = {timeout: 10}, jQuery = require('jquery'); exports.job = new nodeio.Job(options, { input: ['hello', 'foobar', 'weather'], run: function (keyword) { this.getHtml('http://www.google.com/search?q=' + encodeURIComponent(keyword), function (err, $) { // SOMEHOW CREATE THE JQUERY OBJECT USING $ var results = $('#resultStats').text.toLowerCase(); this.emit(keyword + ' has ' + results); }); } }); 

Can anyone help?

UPDATE

I don't notice that node.io was able to use jquery with the jsdom:true option. It does not work, even when I use this parameter, I always get a timeout error OR $ is an undefined error.

+4
source share
3 answers

Ok, got the problem.

node.io has the ability to use jquery, you need to pass this option to jsdom:true . The version of node.io (0.3.0) that I used had an error that always returned a timeout.

  // lib/node.io/dom.js window.onload = function() { callback.apply(self, [null, $, data, headers, response]); } 

Now this is fixed in node.io version 0.3.1

Thank you all for your reply!

+2
source

Take a look at jsdom - a module that mimics the DOM in JS, allowing you to use any JS library designed for the browser.

This blog post explains how to combine it with jQuery: http://blog.nodejitsu.com/jsdom-jquery-in-5-lines-on-nodejs .

+1
source

You can just look at using spider by mikeal - it supports this use case out of the box and is designed to be cleaned.

https://github.com/mikeal/spider

Example:

 var spider = require('../main'); spider() .route('www.nytimes.com', '/pages/dining/index.html', function (window, $) { $('a').spider(); }) .route('travel.nytimes.com', '*', function (window, $) { $('a').spider(); if (this.fromCache) return; var article = { title: $('nyt_headline').text(), articleBody: '', photos: [] } article.body = '' $('div.articleBody').each(function () { article.body += this.outerHTML; }) $('div#abColumn img').each(function () { var p = $(this).attr('src'); if (p.indexOf('ADS') === -1) { article.photos.push(p); } }) // console.log(article); }) .route('dinersjournal.blogs.nytimes.com', '*', function (window, $) { var article = {title: $('h1.entry-title').text()} // console.log($('div.entry-content').html()) }) .get('http://www.nytimes.com/pages/dining/index.html') .log('info') ; 

Also, if you intend to use node.io - I think node.io passes data as an optional parameter:

 io.getHTML('someurl', function(err, junk, data){ jsdom.env({ html: data, scripts : [ 'http://code.jquery.com/jquery-1.5.min.js' ] }, function(err, window) { var $ = window.jQuery; // use jquery here }); }); 
+1
source

All Articles