简短故事:
我想要解析一个有无限滚动的网站,但我不想实现无限滚动。所以我想创建一个脚本,在浏览器控制台中自动滚动页面,等待数据出现,然后重复执行直到结束。
长篇故事:
我试图使用javascript控制台在我的浏览器中向下滚动无限滚动。当我们滚动到底部时,会进行Ajax调用并填充一个<div>
元素,因此我们必须等待它发生然后恢复我们的过程。
我主要的问题在于所有这些都太快了,在恢复过程之前它没有等待ajax完成。
为了举例说明,当我转到AngelList作业页面(需要登录),并在浏览器控制台中键入以下内容:
function sleep(µs) {
var end = performance.now() + µs/1000;
while (end > performance.now()) ; // do nothing
}
var loading = true;
$(window).load(function () {
loading = false;
}).ajaxStart(function () {
loading = true;
}).ajaxComplete(function () {
loading = false;
});
function waitAfterScroll() {
if(loading === true) {
console.log("Wait the Ajax to finish loading");
setTimeout(waitAfterScroll, 1000);
return;
}
console.log("Ajax DONE");
}
var X= 0;
while(X < 100){
sleep(1000000);
X = X + 10;
console.log(X);
window.scrollTo(0,document.body.scrollHeight);
waitAfterScroll();
}
我拿到了这个结果:
10
Wait the Ajax to finish loading
20
Wait the Ajax to finish loading
30
Wait the Ajax to finish loading
40
Wait the Ajax to finish loading
50
Wait the Ajax to finish loading
60
Wait the Ajax to finish loading
70
Wait the Ajax to finish loading
80
Wait the Ajax to finish loading
90
Wait the Ajax to finish loading
100
Wait the Ajax to finish loading
Wait the Ajax to finish loading
Ajax DONE
What I would like is :
10
Wait the Ajax to finish loading // one or multiple times.....
Ajax DONE
20
Wait the Ajax to finish loading // one or multiple times.....
Ajax DONE
30
Wait the Ajax to finish loading // one or multiple times.....
Ajax DONE
40
Wait the Ajax to finish loading // one or multiple times.....
Ajax DONE
50
Wait the Ajax to finish loading // one or multiple times.....
Ajax DONE
60
Wait the Ajax to finish loading // one or multiple times.....
Ajax DONE
70
Wait the Ajax to finish loading // one or multiple times.....
Ajax DONE
80
Wait the Ajax to finish loading // one or multiple times.....
Ajax DONE
90
Wait the Ajax to finish loading // one or multiple times.....
Ajax DONE
100
Wait the Ajax to finish loading // one or multiple times.....
Ajax DONE
我希望这很清楚。
换句话说,我希望能够向下滚动,停止javascript的执行或至少等待ajax加载完成,然后重复执行。