Recursive functions inside evaluate
chiefsmurph opened this issue · comments
Perhaps this issue should be titled more generally "Function declarations within evaluate()", however more specifically, I am trying to make a recursive call within evaluate(). Use case: One of those "endless scrolling divs". So I want to scroll to bottom, see if more has been added to the div, and then continue...
horseman
.open(url)
.wait(6000)
.click('button#openEndlessDiv')
.wait(2000)
.evaluate(function(done) {
var scrollDiv = document.querySelector('#scrollableDiv');
var scrolls = [];
var scrollIt = function() {
var beforeHeight = scrollDiv.scrollHeight;
scrolls.push(beforeHeight);
setTimeout(function() {
var nowHeight = scrollDiv.scrollHeight;
if (nowHeight === beforeHeight) {
done(null, scrolls);
} else {
scrollIt();
}
}, 3000);
$(scrollDiv).scrollTop(currentHeight);
};
scrollIt();
})
.log()
.evaluate(function() {
var allLis = [].slice.call(document.querySelectorAll('#scrollableDiv li'));
return allLis.map(function(li) {
return $(li).text();
});
})
.then(list => {
horseman.close();
resolve(list);
});
Get this error: Unhandled rejection fn evaluate
I tried injectJs('[external and relative url with that function]') and that did not work. Does this mean there can't be function declarations whatsoever within evaluate?
It does work (loads 1 more collection of results) when I remove the scrollIt
function declaration and only scroll to the bottom once.
Well apparently I made the silly mistake of referencing currentHeight
when the var I was referencing was beforeHeight
.