How to Use JQuery on an External Web Page With NodeJS

• 1 min read

I recently started a new project that scrapes certain web pages looking for content changes. There are a few Node modules designed for this purpose, and for my first attempt I chose to use jsdom. Jsdom worked great, but it needed a certain version of Python to be installed, which caused problems when running on Nodejitsu. I ended up switching to a combination of domino (which got rid of the Python dependancy) paired with zepto-node (JQuery-like library). Here is a quick snippet to get you started:

var request = require('request');
var domino = require('domino');
var Zepto = require('zepto-node');

var params = {
  url: 'https://google.com'
};

request(params, function(err, response, body) {
  if(err || response.statusCode !== 200) {
    console.warn('Failed to fetch url '+url);
  }

  var window = domino.createWindow(body);
  var $ = Zepto(window);
  
  var pageTitle = $('title').text();
  console.log('Page Title: '+pageTitle);
});

/**
 * OUTPUT
 *
 * Page Title: Google
 *
 */

Pretty easy right? Since domino is implemented in 100% Javascript, the app can safely be run inside of any Node environment.

If you enjoyed this tutorial, please consider sponsoring my work on GitHub 🤗

Be the first to cheers
Now look what you've done 🌋
Stop clicking and run for your life! 😱
Uh oh, I don't think the system can't handle it! 🔥
Stop it, you're too kind 😄
Thanks for the love! ❤️
Thanks, glad you enjoyed it! Care to share?
Hacker News Reddit

×

Recommended Posts ✍🏻

See All »
• 3 min read
✨ HTML Share Buttons
Read Post »
• 3 min read
🚅 Next Stop, Yaak
Read Post »
• 4 min read
💻 Wait for User to Stop Typing, in JavaScript
Read Post »