This repository has been archived by the owner on Mar 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils.js
45 lines (39 loc) · 1.5 KB
/
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 puppeteer = require('puppeteer');
// スクロールする便利なやつ
async function autoScroll(page){
await page.evaluate(async () => {
await new Promise((resolve, reject) => {
var totalHeight = 0;
var distance = 100;
var count = 0;
var timer = setInterval(() => {
var scrollHeight = document.body.scrollHeight;
window.scrollBy(0, distance);
totalHeight += distance;
count++;
if(totalHeight >= scrollHeight){
clearInterval(timer);
resolve();
}
}, 100);
});
});
}
exports.autoScroll = autoScroll;
async function getInnerText(elementHandle, querySelector){
return await elementHandle.$eval(querySelector, e => e.innerText).catch(()=>null);
}
exports.getInnerText = getInnerText;
async function getNestedInnerText(elementHandle, querySelector1, querySelector2){
const parentElement = await elementHandle.$(querySelector1);
return await parentElement.$eval(querySelector2, e => e.innerText).catch(()=>null);
}
exports.getNestedInnerText = getNestedInnerText;
async function getAttribute(elementHandle, querySelector, attr){
return await elementHandle.$eval(querySelector, (e, attr) => e.getAttribute(attr), attr).catch(()=>null);
}
exports.getAttribute = getAttribute;
async function getAttributes(elementHandle, querySelector, attr){
return await elementHandle.$$eval(querySelector, (es, attr) => es.map(e => e.getAttribute(attr)), attr).catch(()=>null);
}
exports.getAttributes = getAttributes;