aboutsummaryrefslogtreecommitdiffstats
path: root/extension/src/popup.js
diff options
context:
space:
mode:
Diffstat (limited to 'extension/src/popup.js')
-rw-r--r--extension/src/popup.js81
1 files changed, 81 insertions, 0 deletions
diff --git a/extension/src/popup.js b/extension/src/popup.js
new file mode 100644
index 0000000..8d9f8cf
--- /dev/null
+++ b/extension/src/popup.js
@@ -0,0 +1,81 @@
+// The extension's popup (and, opened in a tab, its settings page): the key,
+// the connected wallet's balance and recent payments, and the sites that
+// were allowed or blocked.
+const $ = (id) => document.getElementById(id);
+const send = async (m) => {
+ const r = await chrome.runtime.sendMessage(m);
+ if (!r || r.error) throw new Error((r && r.error) || 'No answer');
+ return r.result;
+};
+if (new URLSearchParams(location.search).has('tab') || window.innerWidth > 500) document.body.classList.add('tab');
+const sats = (n) => Number(n).toLocaleString('en-US');
+const el = (tag, cls, text) => { const e = document.createElement(tag); if (cls) e.className = cls; if (text != null) e.textContent = text; return e; };
+
+async function render() {
+ const s = await send({ type: 'state' });
+ $('setup').hidden = !!s.pubkey;
+ $('main').hidden = !s.pubkey;
+ if (!s.pubkey) { $('key-input').focus(); return; }
+
+ $('npub').textContent = s.npub.slice(0, 14) + '…' + s.npub.slice(-6);
+ $('name').textContent = 'Your key';
+ $('copy').onclick = async () => { await navigator.clipboard.writeText(s.npub); $('copy').textContent = 'Copied'; setTimeout(() => { $('copy').textContent = 'Copy'; }, 1200); };
+ send({ type: 'profile' }).then((p) => {
+ if (!p) return;
+ if (p.name) $('name').textContent = p.name;
+ if (p.picture) $('pic').src = p.picture;
+ }).catch(() => {});
+
+ $('wallet-on').hidden = !s.wallet;
+ $('wallet-off').hidden = !!s.wallet;
+ if (s.wallet) {
+ $('wallet-err').textContent = '';
+ send({ type: 'balance' }).then((b) => {
+ $('balance').textContent = sats(b);
+ $('balance').append(el('span', '', 'sats'));
+ }).catch((e) => { $('balance').textContent = '—'; $('wallet-err').textContent = e.message; });
+ send({ type: 'transactions' }).then((txs) => {
+ $('txs').replaceChildren(...txs.map((t) => {
+ const li = el('li');
+ li.append(el('span', 'grow', t.memo || (t.type === 'incoming' ? 'Received' : 'Sent')));
+ li.append(el('span', t.type === 'incoming' ? 'in' : '', (t.type === 'incoming' ? '+' : '−') + sats(t.sats)));
+ return li;
+ }));
+ }).catch(() => {});
+ }
+
+ const sites = Object.entries(s.sites).sort((a, b) => (b[1].at || 0) - (a[1].at || 0));
+ $('no-sites').hidden = sites.length > 0;
+ $('sites').replaceChildren(...sites.map(([origin, p]) => {
+ const li = el('li');
+ li.append(el('span', 'grow', origin.replace(/^https:\/\//, '')));
+ if (p.sign === true) li.append(el('span', 'tag', 'signs'));
+ if (p.pay === true) li.append(el('span', 'tag', 'pays'));
+ if (p.sign === false || p.pay === false) li.append(el('span', 'tag', 'blocked'));
+ const rm = el('button', 'link', 'Remove');
+ rm.setAttribute('aria-label', 'Remove ' + origin);
+ rm.onclick = async () => { await send({ type: 'removeSite', origin }); render(); };
+ li.append(rm);
+ return li;
+ }));
+}
+
+$('key-save').onclick = async () => {
+ $('key-err').textContent = '';
+ try { await send({ type: 'setKey', input: $('key-input').value }); $('key-input').value = ''; render(); }
+ catch (e) { $('key-err').textContent = e.message; }
+};
+$('nwc-save').onclick = async () => {
+ $('nwc-err').textContent = '';
+ try { await send({ type: 'setWallet', uri: $('nwc-input').value }); $('nwc-input').value = ''; render(); }
+ catch (e) { $('nwc-err').textContent = e.message; }
+};
+$('wallet-remove').onclick = async () => { await send({ type: 'removeWallet' }); render(); };
+$('key-remove').onclick = async () => {
+ const b = $('key-remove');
+ if (b.dataset.sure !== '1') { b.dataset.sure = '1'; b.textContent = 'Click again to remove the key'; return; }
+ await send({ type: 'removeKey' });
+ b.dataset.sure = ''; b.textContent = 'Remove key from this browser';
+ render();
+};
+render();