-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.js
45 lines (40 loc) · 937 Bytes
/
utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
const cheerio = require("cheerio");
function info(message, type = "info") {
if (type == "info") {
console.info(message);
} else if (type == "error") {
console.error(message);
} else {
console.log(message);
}
}
async function autoScroll(page) {
await page.evaluate(() => {
return new Promise((resolve, reject) => {
var totalHeight = 0;
var distance = 500;
var timer = setInterval(() => {
var scrollHeight = document.body.scrollHeight;
window.scrollBy(0, distance);
totalHeight += distance;
if (totalHeight >= scrollHeight) {
clearInterval(timer);
resolve();
}
}, 100);
});
});
}
function chunk(array, chunkSize) {
return [].concat.apply(
[],
array.map(function (elem, i) {
return i % chunkSize ? [] : [array.slice(i, i + chunkSize)];
})
);
}
module.exports = {
info,
autoScroll,
chunk,
};