-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
65 lines (58 loc) · 1.8 KB
/
index.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
const puppeteer = require('puppeteer-core');
const EventEmiter = require('events');
const path = require('path');
class Client extends EventEmiter {
/**
* @param {Object} obj
* @param {String} obj.chromePath
* @param {Boolean} obj.continuous
*/
constructor({ chromePath, continuous }) {
super();
if (!Client.instance) {
this.chromePath = chromePath;
this.continuous = continuous || false;
this.init();
Client.instance = this;
}
return Client.instance;
}
record() {
if (this.page) this.page.evaluate((continuous) => startRecording(continuous), this.continuous);
}
async init() {
try {
this.browser = await puppeteer.launch({
headless: false,
args: [
'--window-size=0,0',
'--enable-speech-input',
'--window-position=0,0',
'--enable-speech-dispatcher', // Needed for Linux?
'--use-fake-ui-for-media-stream', // dissable mic popup
'--no-first-run',
'--no-default-browser-check'
],
executablePath: this.chromePath,
ignoreDefaultArgs: '--mute-audio',
});
const [page] = await this.browser.pages();
this.page = page;
await page.exposeFunction('newTranscript', (e) => {
/**
* @event Client#data
* @type {String}
*/
this.emit('data', e)
});
await page.exposeFunction('newError', (e) => this.emit('error', e));
await page.exposeFunction('newEnd', () => this.emit('end'));
await page.exposeFunction('newStart', () => this.emit('start'));
await page.exposeFunction('newReady', () => this.emit('ready'));
await page.goto(`file:${path.join(__dirname, '/html/index.html')}`);
} catch (err) {
this.emit('error', err);
}
}
}
module.exports = Client;