-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetCapture.js
More file actions
176 lines (149 loc) · 4.05 KB
/
getCapture.js
File metadata and controls
176 lines (149 loc) · 4.05 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
const puppeteer = require('puppeteer');
let page, browser;
const viewportHeight = 1200;
const viewportWidth = 1200;
async function setup(data) {
browser = await puppeteer.launch({
headless: true,
args: [
'--disable-gpu',
'--disable-dev-shm-usage',
'--disable-setuid-sandbox',
'--no-first-run',
'--no-sandbox',
'--no-zygote',
'--single-process',
'--incognito'
]
});
page = await browser.newPage();
// TODO: あとでデバイス振り分けができるようにする
//let pc = true;
let pc = false;
if (pc) {
await page.setUserAgent(
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36'
);
await page.setViewport({
width: viewportWidth,
height: viewportHeight
});
} else {
// 選択するパターン
const devices = require('puppeteer/DeviceDescriptors');
await page.emulate(devices['iPhone X']);
}
}
async function capture(data) {
const threadNumber = data.threadNumber;
if (data.query === undefined) {
process.send({
message: 'exit',
threadNumber: threadNumber
});
process.exit();
return;
}
const { url, output } = data.query;
async function scrollToBottom(page, viewportHeight) {
const getScrollHeight = () => {
return Promise.resolve(document.documentElement.scrollHeight);
};
let scrollHeight = await page.evaluate(getScrollHeight);
let currentPosition = 0;
let scrollNumber = 0;
while (currentPosition < scrollHeight) {
scrollNumber += 1;
const nextPosition = scrollNumber * viewportHeight;
await page.evaluate(function(scrollTo) {
return Promise.resolve(window.scrollTo(0, scrollTo));
}, nextPosition);
await page
.waitForNavigation({
waitUntil: 'networkidle2',
timeout: 50
})
.catch(e => {
//console.log('timeout exceed. proceed to next operation')
});
currentPosition = nextPosition;
//console.log(`scrollNumber: ${scrollNumber}`);
//console.log(`currentPosition: ${currentPosition}`);
// 2
scrollHeight = await page.evaluate(getScrollHeight);
//console.log(`ScrollHeight ${scrollHeight}`);
}
}
async function getCapture() {
await page
.goto(url, {
waitUntil: 'networkidle2',
timeout: 30000
//waitUntil: 'load',
//timeout: 0
})
.catch(e => {
// console.log('timeout exceed. proceed to next operation'));
console.log('error goto', url);
//console.log(e);
});
try {
await scrollToBottom(page, viewportHeight);
} catch (e) {
console.log('error', url);
}
await page.waitFor(1000); // ミリ秒
//await page
// .waitForNavigation({
// waitUntil: 'networkidle2',
// timeout: 3000
// })
// .catch(e => {
// // console.log('timeout exceed. proceed to next operation');
// });
await page.screenshot({
path: output,
fullPage: true
});
console.log('save screenshot: ' + url);
}
console.log('start screenshot: ' + url);
await getCapture();
}
process.on('exit', function() {
console.log('child exit');
});
//process.on('message', async function({ url, output }, processExit = false) {
process.on('message', async function(data) {
console.log(data);
const threadNumber = data.threadNumber;
if (data.query === 'setup') {
await setup(data);
process.send({
message: 'setup-fix',
threadNumber: threadNumber
});
} else if (data.query === 'close') {
await browser.close();
process.send({
message: 'close',
threadNumber: threadNumber
});
process.exit();
} else {
await capture(data);
if (!data.processExit) {
process.send({
message: 'fix',
threadNumber: threadNumber
});
} else {
await browser.close();
process.send({
message: 'exit',
threadNumber: threadNumber
});
process.exit();
}
}
});