-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
44 lines (37 loc) · 1.12 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
(() => {
globalThis.qs = Object.fromEntries(new URLSearchParams(document.location.search).entries())
const t = qs.t || 5
if (qs.relay) {
const socket = new WebSocket(qs.relay)
socket.onopen = function (event) {
console.log('wss open', qs.relay)
let now = new Date().getTime()
now = Math.floor(now / 1000.0)
const filter = { since: now }
if (qs.pubkey) {
filter.authors = [qs.pubkey]
}
if (qs.kind) {
const kinds = Array.isArray(qs.kind) ? qs.kind : [qs.kind]
filter.kinds = kinds.map(kind => parseInt(kind))
}
let subscribe = JSON.stringify(['REQ', 'tail', filter])
if (qs.pubkey) {
subscribe = `["REQ", "tail", { "authors": ["${qs.pubkey}"], "since": ${now} }]`
}
console.log(subscribe)
socket.send(subscribe)
};
socket.onmessage = function (event) {
const json = JSON.parse(event?.data)
if (json[0] === 'EOSE') {
console.log('EOSE')
return;
}
console.log('refreshing in', t)
setTimeout(() => {
document.location.reload()
}, t * 1000)
};
}
})()