mirror of
https://github.com/melvincarvalho/nostrefresh.git
synced 2025-12-17 08:04:19 +01:00
34 lines
876 B
JavaScript
34 lines
876 B
JavaScript
; (() => {
|
|
globalThis.qs = Object.fromEntries(new URLSearchParams(document.location.search))
|
|
|
|
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)
|
|
let subscribe = `["REQ", "tail", {"since": ${now} }]`
|
|
if (qs.pubkey) {
|
|
subscribe = `["REQ", "tail", { "authors": [${qs.pubkey}], "since": ${now} }]`
|
|
}
|
|
console.log(subscribe)
|
|
socket.send(subscribe)
|
|
}
|
|
socket.onmessage = function (event) {
|
|
console.log('refreshing in', t)
|
|
const json = JSON.parse(event?.data)
|
|
if (json[0] === 'EOSE') {
|
|
console.log('EOSE')
|
|
return
|
|
}
|
|
setTimeout(() => {
|
|
document.location.reload()
|
|
}, t * 1000)
|
|
}
|
|
}
|
|
}
|
|
)()
|