From 78493a7702f8d440f23f790b315025b9e8df6c7b Mon Sep 17 00:00:00 2001 From: tohrxyz Date: Wed, 25 Sep 2024 18:30:50 +0200 Subject: [PATCH] fix unable to load npub from input --- scripts/nostr.js | 39 +++++++++++++++++++++++++++------------ 1 file changed, 27 insertions(+), 12 deletions(-) diff --git a/scripts/nostr.js b/scripts/nostr.js index acc5bad..4bef75d 100644 --- a/scripts/nostr.js +++ b/scripts/nostr.js @@ -17,15 +17,12 @@ const newNoteEvent = new CustomEvent("newNoteEvent", { detail: { message: "New e document.addEventListener('DOMContentLoaded', async () => { nostr = window.NostrTools - console.log({nostr}) - relay = await nostr.Relay.connect(relayUrl) console.log(`Connected to ${relay.url}`, {relay}) const hash = window.location.hash - console.log(hash) - if (hash.startsWith("#npub")) { - submitNpub(hash.slice(1)) + if (hash !== "" && hash.startsWith("#npub")) { + loadNpubFromHash(hash.slice(1)) } const btn = document.getElementById("npub-submit-input") @@ -76,21 +73,39 @@ window.addEventListener('newNoteEvent', (event) => { } }) -function submitNpub(npubIn) { +function submitNpub() { events = [] if (notesContainer) { while(notesContainer.firstChild) { notesContainer.removeChild(notesContainer.lastChild) } } - let npub; const input = document.getElementById("npub-input") - if (npubIn) { - npub = npubIn - input.textContent = npubIn - } else { - npub = input.value + const npub = input.value + const hexPubKey = nostr.nip19.decode(npub).data + relay.subscribe([ + { + kinds: [0, 1], + authors: [hexPubKey], + } + ], { + onevent(event) { + events.push(event) + window.dispatchEvent(newNoteEvent) + } + }) +} + +function loadNpubFromHash(hash) { + events = [] + if (notesContainer) { + while(notesContainer.firstChild) { + notesContainer.removeChild(notesContainer.lastChild) + } } + const input = document.getElementById("npub-input") + const npub = hash; + input.textContent = npub; const hexPubKey = nostr.nip19.decode(npub).data relay.subscribe([ {