forked from SugarCoatJS/sugarcoat
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrecord.js
87 lines (73 loc) · 2.46 KB
/
record.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
// vim: set tw=99 ts=2 sw=2 et:
'use strict';
const crypto = require('crypto');
const path = require('path');
const am = require('am');
const sleep = require('await-sleep');
const { program } = require('commander');
const fs = require('fs-extra');
const puppeteer = require('puppeteer-core');
const tmp = require('tmp-promise');
tmp.setGracefulCleanup();
program
.version(require('./package.json').version)
.requiredOption('-b, --browser <path>', 'browser executable path')
.requiredOption('-o, --output <path>', 'Page Graph output directory')
.option('-p, --profile <path>', 'browser profile directory', null)
.option('-v, --verbose', 'enable verbose browser logging', false)
.parse(require('process').argv);
const {
browser: browserExePath,
output: outputDirPath,
profile: profileDirPath,
verbose: enableBrowserLogging,
} = program;
const computeHash = data => crypto.createHash('sha256').update(data).digest('hex');
const attachToTarget = async target => {
const client = await target.createCDPSession();
client.on('Page.finalPageGraph', async event => {
const graphFileName = `frame-${event.frameId.toLowerCase()}-${computeHash(
event.data
).substring(0, 32)}.graphml`;
const graphFilePath = path.join(outputDirPath, graphFileName);
await fs.writeFile(graphFilePath, event.data);
console.log(`Saved: ${graphFilePath}`);
});
};
am(async () => {
await fs.ensureDir(outputDirPath);
const profileDir = profileDirPath
? { path: profileDirPath, cleanup () {} }
: await tmp.dir({
unsafeCleanup: true,
});
const browser = await puppeteer.launch({
executablePath: browserExePath,
userDataDir: profileDir.path,
userDataDir: '/home/spinda/.config/BraveSoftware/Brave-Browser-Development',
args: [
...(enableBrowserLogging ? ['--enable-logging', '--v=0'] : []),
'--disable-brave-update',
...(enableBrowserLogging ? ['--enable-logging=stderr'] : []),
],
ignoreDefaultArgs: [
'--disable-sync',
'--enable-features=NetworkService,NetworkServiceInProcess',
// ^ breaks Brave Shields
'--disable-extensions',
],
dumpio: enableBrowserLogging,
headless: false,
});
browser.on('targetcreated', async target => {
if (target.type() === 'page') {
await attachToTarget(target);
}
});
for (const page of await browser.pages()) {
attachToTarget(page.target());
}
browser.on('disconnected', async () => {
profileDir.cleanup();
});
});