Created
February 26, 2012 08:40
-
-
Save meso/1915298 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var http = require('http'), | |
URL = require('url'), | |
jsdom = require('jsdom'); | |
module.exports = function sqrape(url, callback) { | |
if (!callback) throw new Error('Callback is not specified'); | |
if (!url) return callback(new Error('URL is not specified')); | |
var parsedUrl = URL.parse(url); | |
var options = { | |
host: parsedUrl.hostname, | |
port: parsedUrl.port, | |
path: parsedUrl.pathname + (parsedUrl.search || '') | |
}; | |
http.get(options, function(res) { | |
if ([301, 302, 303, 307].indexOf(res.statusCode) !== -1) { | |
if (res.headers["location"]) { | |
var redirect = URL.resolve(URL.format(parsedUrl), res.headers["location"]); | |
process.nextTick(function() { | |
sqrape(redirect, callback); | |
}); | |
return; | |
} | |
} | |
if (res.statusCode !== 200) return callback(new Error('URL: ' + url + ', StatusCode: ' + res.statusCode), null); | |
var buf = ''; | |
res.setEncoding('utf8'); | |
res.on('data', function(chunk) { | |
buf += chunk; | |
}); | |
res.on('end', function() { | |
jsdom.env(buf, ['http://code.jquery.com/jquery.min.js'], function(err, window) { | |
if (err) return callback(err, null); | |
callback(null, window.jQuery); | |
}); | |
}); | |
res.on('error', function(err) { | |
callback(err, null); | |
}); | |
}).on('error', function(err) { | |
callback(err, null); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment