Skip to content

Instantly share code, notes, and snippets.

@perfectacle
Last active July 17, 2017 07:34
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 perfectacle/df9c38a80bb80df7fd25456731af1382 to your computer and use it in GitHub Desktop.
Save perfectacle/df9c38a80bb80df7fd25456731af1382 to your computer and use it in GitHub Desktop.
phantomjs-prebuilt v2.1.14 for infinite scroll
// this code must to transpile with babel
const page = require('webpage').create();
const URI = ''; // infinite URI
page.open(URI, () => {
// previous height
let base = 0;
// current height
let tmp = 0;
// timer id for stop the interval
const timerID = setInterval(() => {
// evaluate is sandboxed, so evaluate can't access outer context (our app code),
// only can access in page context (in browser code).
// and evaluate is sync.
// first, scroll to bottom.
// second, return document.body.scrollHeight.
// third, assign return value to tmp.
tmp = page.evaluate(() => document.body.scrollTop = document.body.scrollHeight);
// if previous height and current height isn't same
if(base !== tmp) {
console.log(`previous height: ${base}px, current height: ${tmp}px`);
return base = tmp;
}
// if previous height and current height is same
console.log(`page height is ${base}px`);
page.render('screen.png'); // capture
clearInterval(timerID); // stop interval function
phantom.exit(); // phantom close.
// if network is unstable, increase time.
}, 2000);
});
'use strict';
var page = require('webpage').create();
var URI = ''; // infinite URI
page.open(URI, function () {
// previous height
var base = 0;
// current height
var tmp = 0;
// timer id for stop interval
var timerID = setInterval(function () {
// evaluate is sandboxed, so evaluate can't access outer context, only can access in page context
// and evaluate is sync.
// first, scroll to bottom
// second, return document.body.scrollHeight
// third, assign return value
tmp = page.evaluate(function () {
return document.body.scrollTop = document.body.scrollHeight;
});
// if previous height and current height isn't same
if (base !== tmp) {
console.log('previous height: ' + base + 'px, current height: ' + tmp + 'px');
return base = tmp;
}
console.log('page height is ' + base + 'px');
// if previous height and current height is same
page.render('screen.png'); // capture
clearInterval(timerID); // stop interval function
phantom.exit(); // phantom close.
// if network is unstable, increase time.
}, 2000);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment