summaryrefslogtreecommitdiff
path: root/src/clicks.ts
diff options
context:
space:
mode:
authorJoel Klinghed <the_jk@spawned.biz>2024-12-17 20:46:15 +0100
committerJoel Klinghed <the_jk@spawned.biz>2024-12-17 20:46:15 +0100
commit4134750ef6921c8f0fcac898594101a6ca5f4f4f (patch)
tree88928056cd4bfe14d3d78f82256fd78cf5672620 /src/clicks.ts
Initial commit
Diffstat (limited to 'src/clicks.ts')
-rw-r--r--src/clicks.ts44
1 files changed, 44 insertions, 0 deletions
diff --git a/src/clicks.ts b/src/clicks.ts
new file mode 100644
index 0000000..a40cb66
--- /dev/null
+++ b/src/clicks.ts
@@ -0,0 +1,44 @@
+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
+ })
+}) \ No newline at end of file