artoo.js bundles some sniffers to help you grab some of the circulating data of the web page you need to scrape.
For instance, the ajax sniffer enables you to fire callbacks whenever ajax requests are made or received. This way you can understand how the page retrieves data and use it to achieve your goal.
Example
You need to retrieve the subtitles of a video on a website and know that the site uses an ajax request to get them back. However, the request might be really hard to forge and could need tokens and a lot of silly parameters.
But you, as a user, know that clicking a button will trigger the ajax request because the UI works so.
You can therefore tap on the ajax response’s data with artoo.ajaxSniffer.after
to retrieve the needed subtitles.
Ajax Sniffer
Gives you a chance to register a callback to be fired any time an ajax request is made by the page.
artoo.ajaxSniffer.before(callback);
Callback arguments
Note that within the callback, this
refer to the current xhr
object.
Example
// Counting number of times the GET article url is hit
var count = 0;
artoo.ajaxSniffer.before(function(req) {
if (~req.url.search(/article/))
count++;
});
Gives you a chance to register a callback to be fired any time an ajax request is completed by the page.
artoo.ajaxSniffer.after(callback);
Callback arguments
Note that within the callback, this
refer to the current xhr
object.
Example
// Downloading json data retrieved through complex ajax request
artoo.ajaxSniffer.after(function(req, res) {
artoo.savePrettyJson(res.data);
});
Remove the given callback from the ajax sniffing stack.
artoo.ajaxSniffer.off(callback);