forked from tungs/timesnap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
397 lines (380 loc) · 13 KB
/
index.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
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
/**
* BSD 3-Clause License
*
* Copyright (c) 2018, Steve Tung
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* * Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
const puppeteer = require('puppeteer');
const fs = require('fs');
const path = require('path');
const sprintf = require('sprintf-js').sprintf;
const defaultDuration = 5;
const defaultFPS = 60;
const overwriteTime = function (page, animationFrameDuration) {
return page.evaluateOnNewDocument(function (animationFrameDuration) {
(function (exports) {
var _virtualTime = (new Date()).getTime();
var _startTime = _virtualTime;
var _oldDate = Date;
// a block is a segment of blocking code, wrapped in a function
// to be run at a certain virtual time. They're created by
// window.requestAnimationFrame, window.setTimeout, and window.setInterval
var _pendingBlocks = [];
var _intervals = {};
var _idCount = 1;
var _sortPendingBlocks = function () {
_pendingBlocks = _pendingBlocks.sort(function (a, b) {
if (a.time !== b.time) {
return a.time - b.time;
}
return a.id - b.id;
});
};
var _processNextBlock = function () {
if (!_pendingBlocks.length) {
return null;
}
_sortPendingBlocks();
var block = _pendingBlocks.shift();
_virtualTime = block.time;
block.fn.apply(exports, block.args);
};
var _processUntilTime = function (ms) {
_sortPendingBlocks();
while (_pendingBlocks.length && _pendingBlocks[0].time <= _startTime + ms) {
_processNextBlock();
_sortPendingBlocks();
}
// TODO: maybe wait a little while for possible promises to resolve?
_virtualTime = _startTime + ms;
};
var _setTimeout = function (fn, timeout, ...args) {
var id = _idCount;
if (!timeout || isNaN(timeout)) {
// If timeout is 0, there may be an infinite loop
// Changing it to 1 shouldn't disrupt code, because
// setTimeout doesn't usually execute code immediately
timeout = 1;
}
_pendingBlocks.push({
time: timeout + _virtualTime,
id: id,
fn: fn,
args: args
});
_idCount++;
return id;
};
var _clearTimeout = function (id) {
// according to https://developer.mozilla.org/en-US-docs/Web/API/WindowOrWorkerGlobalScope/setInterval,
// setInterval and setTimeout share the same pool of IDs, and clearInterval and clearTimeout
// can technically be used interchangeably
var i = 0;
if (_intervals[id]) {
_intervals[id].clear();
}
while (i < _pendingBlocks.length) {
if (_pendingBlocks[i].id === id) {
_pendingBlocks.splice(i, 1);
} else {
i++;
}
}
};
// overwriting built-in functions...
exports.Date = class Date extends _oldDate {
constructor() {
if (!arguments.length) {
super(_virtualTime);
} else {
super(...arguments);
}
}
};
exports.Date.now = exports.performance.now = function () {
return _virtualTime;
};
exports.setTimeout = _setTimeout;
exports.requestAnimationFrame = function (fn) {
return _setTimeout(fn, animationFrameDuration);
};
exports.setInterval = function (fn, interval, ...args) {
var lastCallId;
var id = _idCount;
var running = true;
var intervalFn = function () {
fn.apply(exports, args);
if (running) {
lastCallId = _setTimeout(intervalFn, interval);
}
};
_intervals[id] = {
clear: function () {
_clearTimeout(lastCallId);
running = false;
}
};
_idCount++;
lastCallId = _setTimeout(intervalFn, interval);
// according to https://developer.mozilla.org/en-US-docs/Web/API/WindowOrWorkerGlobalScope/setInterval,
// setInterval and setTimeout share the same pool of IDs, and clearInterval and clearTimeout
// can technically be used interchangeably
return id;
};
exports.cancelAnimationFrame = _clearTimeout;
exports.clearTimeout = _clearTimeout;
exports.clearInterval = _clearTimeout;
// exported custom functions
exports._processNextBlock = _processNextBlock;
exports._processUntilTime = _processUntilTime;
})(this);
}, animationFrameDuration);
};
const promiseLoop = function (condition, body) {
var loop = function () {
if (condition()) {
return body().then(loop);
}
};
return Promise.resolve().then(loop);
};
const getBrowserFrames = function (frame) {
return [frame].concat(...frame.childFrames().map(getBrowserFrames));
};
const goToTime = function (browserFrames, time) {
// Goes to a certain time. Can't go backwards
return Promise.all(browserFrames.map(function (frame) {
return frame.evaluate(function (ms) {
window._processUntilTime(ms);
}, time);
}));
};
const getSelectorDimensions = function (page, selector) {
return page.evaluate(function (selector) {
var el = document.querySelector(selector);
var dim = el.getBoundingClientRect();
if (el) {
return {
left: dim.left,
top: dim.top,
right: dim.right,
bottom: dim.bottom,
scrollX: window.scrollX,
scrollY: window.scrollY,
x: dim.x,
y: dim.y,
width: dim.width,
height: dim.height
};
}
}, selector);
};
const makeFileDirectoryIfNeeded = function (filepath) {
var dir = path.parse(filepath).dir, ind, currDir;
var directories = dir.split(path.sep);
for (ind = 1; ind <= directories.length; ind++) {
currDir = directories.slice(0, ind).join(path.sep);
if (currDir && !fs.existsSync(currDir)) {
fs.mkdirSync(currDir);
}
}
};
module.exports = function (config) {
config = Object.assign({}, config || {});
var url = config.url || 'index.html';
var delayMs = 1000 * (config.start || 0);
var startWaitMs = 1000 * (config.startDelay || 0);
var frameProcessor = config.frameProcessor;
var frameNumToTime = config.frameNumToTime;
var fps = config.fps, frameDuration;
var framesToCapture;
var outputPath = path.resolve(process.cwd(), (config.outputDirectory || './'));
var animationFrameDuration;
if (url.indexOf('://') === -1) {
// assume it is a file path
url = 'file://' + path.resolve(process.cwd(), url);
}
if (config.frames) {
framesToCapture = config.frames;
if (!fps) {
if (config.duration) {
fps = framesToCapture / config.duration;
}
else {
fps = defaultFPS;
}
}
} else {
if (!fps) {
fps = defaultFPS;
}
if (config.duration) {
framesToCapture = config.duration * fps;
} else {
framesToCapture = defaultDuration * fps;
}
}
frameDuration = 1000/fps;
if (fps > 60) {
animationFrameDuration = frameDuration;
} else {
animationFrameDuration = 1000/60;
}
if (!frameNumToTime) {
frameNumToTime = function (frameCount) {
return (frameCount-1) * frameDuration;
};
}
var fileNameConverter = config.fileNameConverter;
if (!fileNameConverter) {
if (config.outputPattern) {
fileNameConverter = function (num) {
return sprintf(config.outputPattern, num);
};
} else if (frameProcessor && !config.outputDirectory) {
fileNameConverter = function () {
return undefined;
};
} else {
fileNameConverter = function (num, maxNum) {
var outputPattern = '%0' + maxNum.toString().length + 'd.png';
return sprintf(outputPattern, num);
};
}
}
const log = function () {
if (!config.quiet) {
if (config.logToStdErr) {
// eslint-disable-next-line no-console
console.error.apply(this, arguments);
} else {
// eslint-disable-next-line no-console
console.log.apply(this, arguments);
}
}
};
const launchOptions = {
dumpio: !config.quiet && !config.logToStdErr
};
return puppeteer.launch(launchOptions).then(function (browser) {
return browser.newPage().then(function (page) {
return Promise.resolve().then(function () {
if (config.viewport) {
if (!config.viewport.width) {
config.viewport.width = page.viewport().width;
}
if (!config.viewport.height) {
config.viewport.height = page.viewport().height;
}
return page.setViewport(config.viewport);
}
}).then(function () {
return overwriteTime(page, animationFrameDuration);
}).then(function () {
log('Going to ' + url + '...');
return page.goto(url, { waitUntil: 'networkidle0' });
}).then(function () {
log('Page loaded');
return new Promise(function (resolve) {
setTimeout(resolve, startWaitMs);
});
}).then(function () {
if (config.selector) {
return getSelectorDimensions(page, config.selector).then(function (dimensions) {
if (!dimensions) {
log('Warning: no element found for ' + config.selector);
return;
}
return dimensions;
});
}
}).then(function (dimensions) {
var browserFrames = getBrowserFrames(page.mainFrame());
var frameCount = 0;
var viewport = page.viewport();
var x = config.xOffset || config.left || 0;
var y = config.yOffset || config.top || 0;
var right = config.right || 0;
var bottom = config.bottom || 0;
var width;
var height;
if (dimensions) {
width = config.width || (dimensions.width - x - right);
height = config.height || (dimensions.height - y - bottom);
x += dimensions.scrollX + dimensions.left;
y += dimensions.scrollY + dimensions.top;
} else {
width = config.width || (viewport.width - x - right);
height = config.height || (viewport.height - y - bottom);
}
width = Math.ceil(width);
if (config.roundToEvenWidth && (width % 2 === 1)) {
width++;
}
height = Math.ceil(height);
if (config.roundToEvenHeight && (height % 2 === 1)) {
height++;
}
var screenshotClip = {
x: x,
y: y,
width: width,
height: height
};
return promiseLoop(function () {
return frameCount++ < framesToCapture;
}, function () {
return goToTime(browserFrames, delayMs + frameNumToTime(frameCount, framesToCapture)).then(function () {
var fileName = fileNameConverter(frameCount, framesToCapture);
var filePath;
if (fileName) {
filePath = path.resolve(outputPath, fileName);
makeFileDirectoryIfNeeded(filePath);
} else {
filePath = undefined;
}
log('Capturing Frame ' + frameCount + (filePath ? ' to ' + filePath : '') + '...');
return page.screenshot({
path: filePath,
clip: screenshotClip,
omitBackground: config.transparentBackground ? true : false
}).then(function (buffer) {
if (frameProcessor) {
return frameProcessor(buffer, frameCount, framesToCapture);
}
});
});
});
});
}).then(function () {
return browser.close();
}).catch(function (err) {
log(err);
});
});
};