-
Notifications
You must be signed in to change notification settings - Fork 4
/
app.js
79 lines (66 loc) · 1.87 KB
/
app.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
/** Pre requisites
1) Nodejs version 7+ installed
2) Use yarn to install dependencies:
`yarn`
or use npm if you do not have yarn
`npm install`
* USAGE:
* `node app.js --url=https://github.com/rupeshmore/k6-web-recorder`
* `node app.js` (setup config.json)
*/
/* Dependencies */
const chromeLauncher = require('chrome-launcher');
const CDP = require('chrome-remote-interface');
/** ARGUMENTS AND CONFIGUIRATION
* expects arguments to be
* --url string (url)
*/
const argv = require('minimist')(process.argv.slice(2));
const config = require('./config.json');
const checkConfig = require('./lib/checkConfig');
const url = argv.url || config.url;
if (!config.url) {
config.url = url;
}
if(!checkConfig(config)) return null;
const {logInit, logRequestObject} = require('./lib/logger');
let {ribbon, analytics, gaInit} = require('./lib/addons');
// to add additional chrome flags.
const launchConfig = {
logLevel: 'error',
chromeFlags: [
//add chrome config,
]
}
/**
* Launches a debugging instance of Chrome.
* False launches a full version of Chrome.
* @return {Promise<ChromeLauncher>}
*/
async function launchChrome(headless = true) {
return await chromeLauncher.launch(launchConfig);
}
/**
* Starts chrome.
* connects to the chrome instance.
* launches page with the url.
*/
(async function k6WebRecorder(url) {
const chrome = await launchChrome();
const protocol = await CDP({port: chrome.port});
const {Page, Network, Runtime} = protocol;
Network.requestWillBeSent((params) => {
logRequestObject(params.request, url);
});
await Promise.all([Page.enable(), Network.enable(), Runtime.enable()]);
logInit();
Page.navigate({url});
Page.domContentEventFired(async () => {
let expression = ribbon;
if (!gaInit) {
gaInit = true;
expression += analytics;
}
await Runtime.evaluate({expression});
});
})(url);