-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.mjs
51 lines (43 loc) · 1.55 KB
/
common.mjs
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
import {stdin as input, stdout as output} from 'node:process';
import * as readline from 'node:readline/promises';
import puppeteer from 'puppeteer';
export const browser = await puppeteer.launch({
// No need to use unstable once the PWA implementations roll to prod.
executablePath:
'/usr/bin/google-chrome-unstable',
headless: false,
args: ['--window-size=700,700', '--window-position=10,10'],
// Use pipe to allow executing high priviledge commands.
pipe: true,
});
const browserSession = await browser.target().createCDPSession();
const rl = readline.createInterface({input, output});
export async function waitfor_enter(msg) {
console.log('============================================================');
console.log('**** ', msg);
await rl.question('', ans => {});
}
export async function trim(text) {
const len = 200;
if (!text || !text.length || text.length < len) {
return text;
}
return text.substring(0, len) + '...';
}
export async function send(session, msg, param) {
if (session == null) {
session = browserSession;
}
console.log('\x1b[32m', ' >>> Sending: ', msg, ': ', JSON.stringify(param), '\x1b[0m');
console.log('\x1b[32m', ' <<< Response: ', trim(JSON.stringify(
(await session.send(msg, param)
.catch(error => console.log('\x1b[32m', ' <<< Error: ', error, '\x1b[0m'))))),
'\x1b[0m');
}
export async function current_page_session() {
return (await browser.pages()).pop().createCDPSession();
}
export function shutdown() {
rl.close();
process.exit();
}