summaryrefslogtreecommitdiff
path: root/src/clicks.ts
blob: 100e5b699963850ed13d64783f275a5dec621ba0 (plain)
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
class Session {
    private wss: WebSocketStream
    private queue: string[] = []
    private writer?: WritableStreamDefaultWriter

    constructor() {
        this.wss = new WebSocketStream("ws://127.0.0.1:8001/")
    }

    async connect() {
        const openInfo = await this.wss.opened
        this.writer = openInfo.writable.getWriter()
        for (const event of this.queue) {
            this.writer.write(event)
        }
    }

    reportEvent(event: string) {
        if (this.writer === undefined) {
            this.queue.push(event)
        } else {
            this.writer.write(event)
        }
    }
}

function startSession(data: string): Session {
    const session = new Session()
    session.connect()
    session.reportEvent(data)
    return session
}

document.addEventListener("DOMContentLoaded", () => {
    const session = startSession(
        `${document.documentElement.clientWidth}x${document.documentElement.clientHeight}:${navigator.userAgentData?.brands}`
    )
    document.documentElement.addEventListener(
        "click",
        (e: MouseEvent) => {
            const target =
                e.target instanceof HTMLElement ? e.target.tagName : ""
            const x = e.pageX / document.documentElement.clientWidth
            const y = e.pageY / document.documentElement.clientHeight
            session.reportEvent(`${x}x${y}:${target}`)
        },
        {
            capture: true,
        }
    )
})