aboutsummaryrefslogtreecommitdiffstats
path: root/extension/src/popup.js
blob: 8d9f8cfab97f3ae18fb6eff4b4134fa0b55855b6 (plain) (blame)
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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();