Skip to content

Instantly share code, notes, and snippets.

@Grassboy
Created August 23, 2016 17:29
Show Gist options
  • Save Grassboy/847ad8d2e0782b285c4065eb4eb28648 to your computer and use it in GitHub Desktop.
Save Grassboy/847ad8d2e0782b285c4065eb4eb28648 to your computer and use it in GitHub Desktop.
Node.js 下的 jQuery 起手式範例(ajax已可跨網域)
var jsdom = require('jsdom');
var $ = require('jquery')(jsdom.jsdom('<html></html>').defaultView);
var XMLHttpRequest = require('xmlhttprequest').XMLHttpRequest;
parseHTML = function(html){
//this extract body function may have a bug
var html = '<body'+html.split('<body')[1].split('</body>')[0]+'</body>'
return $(html);
};
$.ajaxSettings.xhr = function() {
return new XMLHttpRequest();
};
$.get('https://www.google.com.tw/search?q=facebook+javascript.tw&ie=utf-8&oe=utf-8&gws_rd=cr').then(parseHTML).then(function($page){
$page.find('h3 a').map(function(){
var $a = $(this);
console.log($a.text());
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment