-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbash.js
83 lines (67 loc) · 2.65 KB
/
bash.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
#!/usr/bin/env node
var stdin = process.stdin,
stdout = process.stdout,
inputChunks = [];
// Exit if we have any unhandled rejections
process.on('unhandledRejection', (err) => {
console.error(err)
process.exit(1)
})
stdin.resume();
stdin.setEncoding('utf8');
stdin.on('data', function (chunk) {
inputChunks.push(chunk);
});
stdin.on('end', function () {
var options = JSON.parse(inputChunks.join());
var pdf_options = options['pdf_options'];
var current_path = options['current_path'];
var viewport_options = options['viewport_options'];
var media_type = options['emulate_media'];
var launch_options = options['launch_options'];
var wait_for_timeout = options['wait_for_timeout'];
var url = options['url'];
var url_options = options['url_options'];
var debug_mode = options['debug_mode'];
module.paths.push(current_path + '/node_modules');
const puppeteer = require('puppeteer');
const fs = require('fs');
if (typeof url === 'undefined') {
var html = fs.readFileSync(process.argv[2], 'utf8');
}
(async() => {
const browser = await puppeteer.launch(launch_options);
const page = await browser.newPage();
let wait_promise = null;
if (typeof media_type !== 'undefined' && media_type) {
if (typeof page.emulateMedia !== 'undefined') {
await page.emulateMedia(media_type);
} else {
await page.emulateMediaType(media_type);
}
}
if (typeof viewport_options !== 'undefined' && viewport_options) {
await page.setViewport(viewport_options);
}
if (typeof wait_for_timeout !== 'undefined' && wait_for_timeout) {
wait_promise = page.waitFor(wait_for_timeout);
}
if (typeof url !== 'undefined' && url) {
await page.goto(url, url_options);
} else {
await page.setContent(html, { waitUntil: ["networkidle0", "load"] });
}
// If we were given a wait timeout, we should wait for the rest of the timeout now. That way,
// the timeout will act as a minimum amount of time to wait for the page. If it's greater than
// how long the waitUntil idle took, it will wait for the extra time. If it is shorter than how
// long the page actually needs, then it will not add any time.
if (typeof wait_promise !== 'undefined' && wait_promise) {
await wait_promise;
}
if (typeof debug_mode === 'undefined') {
const pdf_output = await page.pdf(pdf_options);
stdout.write(pdf_output);
}
await browser.close();
})();
});