Skip to content

Instantly share code, notes, and snippets.

@khajavi
Created March 7, 2015 21:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save khajavi/a571e899cb79825c1180 to your computer and use it in GitHub Desktop.
Save khajavi/a571e899cb79825c1180 to your computer and use it in GitHub Desktop.
Phatomjs Infinite Scroll
/*jshint devel:true, phantom:true*/
/**
* PhantomJS Cookbook Chapter 3 | Working with webpage Objects Recipe 9 |
* Simulating scrolling in PhantomJS
*/
var webpage = require('webpage').create();
var jquery = "jquery.min.js";
webpage.viewportSize = {
width : 1280,
height : 800
};
webpage.scrollPosition = {
top : 0,
left : 0
};
webpage.open('http://fa-ir.facebook.com/abdolkarimi.org', function(status) {
if (status === 'fail') {
console.error('webpage did not open successfully');
phantom.exit(1);
}
webpage.injectJs(jquery);
var i = 0, top, queryFn = function() {
return document.body.scrollHeight;
};
setInterval(function() {
var filename = 'twitter-' + (++i) + '.png';
console.log('Writing ' + filename + '...');
top = webpage.evaluate(queryFn);
console.log('[' + i + '] top = ' + top);
webpage.scrollPosition = {
top : top + 1,
left : 0
};
if (i >= 10) {
var res = webpage.evaluate(function() {
var links = [];
var result = $('.userContent').each(function(i, v) {
links.push($(v).html().trim());
});
return links;
});
webpage.render('b.png');
console.log(res.length);
phantom.exit();
}
}, 700);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment