-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathputility.js
More file actions
executable file
·87 lines (74 loc) · 2.91 KB
/
putility.js
File metadata and controls
executable file
·87 lines (74 loc) · 2.91 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
#!/usr/bin/env node
const program = require('commander');
const puppeteer = require('puppeteer-core');
program
.name('./putility.js')
.usage('<url> [-u <user_agent>] [-w <millisecond>] [-p <path>] [-c <cmd1,cmd2...>] [-s]')
.option('-u, --agent <user_agent>', 'optional, browser user agent')
.option('-w, --wait <millisecond>', 'optional, wait for n milliseconds')
.option('-p, --path <binary_path>', 'optional, path to chrome/chromium binary\ndefault "/usr/bin/chromium"')
.option('-c, --cmd <cmd1,cmd2...>', 'optional, one or multiple commands:\n["html", "screenshot", "cookie", "header", "matchrequest"]\ndefault "html"')
.option('-m, --match <pattern>', 'optional, define pattern used with "matchrequest" command')
.option('-s, --show', 'optional, show browser\ndefault not show')
.arguments('<url>')
.action(function (url) {
_URL = url;
});
program.parse(process.argv);
const options = program.opts();
const cPath = (options.path === undefined) ? '/usr/bin/chromium' : options.path;
const hMode = (options.show === undefined) ? true : false;
const wMsec = (options.wait === undefined) ? '' : parseInt(options.wait);
const cExec = (options.cmd === undefined) ? 'html' : options.cmd;
const mPatt = (options.match === undefined) ? '' : options.match;
const tStamp = Math.floor(Date.now() / 1000);
(async() => {
const browser = await puppeteer.launch({executablePath: cPath, headless: hMode});
const page = await browser.newPage();
var uAgent = (options.agent === undefined) ? await browser.userAgent() : options.agent;
uAgent = uAgent.replace('Headless', '');
/* fetch URL and headers of matched request URL */
if (cExec.indexOf('matchrequest') !== -1) {
await page.setRequestInterception(true);
page.on('request', request => {
if (request.url().indexOf(mPatt) > -1) {
var jsonObj = request.headers();
jsonObj['requestURL'] = request.url();
console.log(JSON.stringify(jsonObj));
return;
} else {
request.continue();
}});
}
/* fetch response header*/
if (cExec.indexOf('header') !== -1) {
page.on('response', r => {
if (r.url() == _URL)
console.log(r.headers());
});
}
await page.setUserAgent(uAgent);
await page.goto(_URL, {timeout: 30000, waitUntil: 'domcontentloaded'});
if (wMsec !== '') {
await page.waitForTimeout(wMsec);
} else {
await page.waitForNavigation();
}
/* take screenshot */
if (cExec.indexOf('screenshot') !== -1) {
await page.screenshot({path:'./' + tStamp + '.jpg', type: 'jpeg', fullPage: true});
}
/* fetch html */
if (cExec.indexOf('html') !== -1) {
const html = await page.evaluate(function () {
return document.getElementsByTagName('html')[0].innerHTML;
});
console.log(html);
}
/* fetch cookie */
if (cExec.indexOf('cookie') !== -1) {
const cookie = await page.cookies();
console.log(JSON.stringify(cookie));
}
await browser.close();
})();