-
Notifications
You must be signed in to change notification settings - Fork 0
/
snapshot.js
68 lines (54 loc) · 1.87 KB
/
snapshot.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
/**
* small script which creates a screenshot of the annotation with the provided annotation id
*
* Usage:
* node snapshot.js <annotation id> <output path>
*
* e.g:
*
* node snapshot.js f2164814-507c-4760-9f30-d05250938506 /tmp/output/f2164814-507c-4760-9f30-d05250938506.png
*
**/
const puppeteer = require('puppeteer');
var annotationId = process.argv[2];
if(annotationId === undefined) {
console.log('Please specify a annotation id!');
return;
}
var outputPath = process.argv[3];
if(outputPath === undefined) {
console.log('Please specify a output path!');
return;
}
const waitTillHTMLRendered = async (page, timeout = 30000) => {
const checkDurationMsecs = 1000;
const maxChecks = timeout / checkDurationMsecs;
let lastHTMLSize = 0;
let checkCounts = 1;
let countStableSizeIterations = 0;
const minStableSizeIterations = 3;
while(checkCounts++ <= maxChecks){
let html = await page.content();
let currentHTMLSize = html.length;
let bodyHTMLSize = await page.evaluate(() => document.body.innerHTML.length);
console.log('last: ', lastHTMLSize, ' <> curr: ', currentHTMLSize, " body html size: ", bodyHTMLSize);
if(lastHTMLSize != 0 && currentHTMLSize == lastHTMLSize)
countStableSizeIterations++;
else
countStableSizeIterations = 0; //reset the counter
if(countStableSizeIterations >= minStableSizeIterations) {
console.log("Page rendered fully..");
break;
}
lastHTMLSize = currentHTMLSize;
await page.waitFor(checkDurationMsecs);
}
};
(async () => {
const browser = await puppeteer.launch({args: ['--no-sandbox', '--disable-setuid-sandbox']});
const page = await browser.newPage();
await page.goto('http://127.0.0.1:8080/annotate?view=unified&annotation_id='+annotationId);
await waitTillHTMLRendered(page);
await page.screenshot({path: outputPath});
await browser.close();
})();