-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathindex.js
62 lines (43 loc) · 1.28 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
const Koa = require('koa')
const SocketIO = require('koa-socket-2')
const serve = require('koa-static')
const puppeteer = require('puppeteer-core')
const PORT = 3000
const CHROMIUM_PATH = '/Applications/Chromium.app/Contents/MacOS/Chromium'
const app = new Koa()
const io = new SocketIO()
app.use(serve('.'))
io.attach(app)
io.on('connection', async () => {
console.log('a client connected')
})
io.on('disconnect', async () => {
console.log('a client disconnected')
})
io.on('ready', async (_, url) => {
console.log('Launching browser...')
const browser = await puppeteer.launch({
executablePath: CHROMIUM_PATH
})
const page = await browser.newPage()
console.log('Go to page: %s', url)
await page.goto(url)
const text = await page.evaluate(() => document.body.innerText)
const textList = text
.replace("'", '\x27')
.replace('"', '\x22')
.split('\n')
.filter(text => text !== '')
const createCommands = require('./mocks/drawTextCommands')
let y = 60
console.log('Sending draw commands to client...')
textList.forEach(text => {
const commands = createCommands({ text, x: 60, y })
y += 30
io.broadcast('draw', commands)
})
io.broadcast('done')
await browser.close()
})
console.log(`Listening on port: ${PORT}`)
app.listen(PORT)