// LinkSpin home (2026-10-07): the working demo (paste pages -> live spin link -> email to keep it), the live // strip from /api/stats, the package credits from /api/catalog. Plain script, no libraries, CSP-clean. (function () { const $ = id => document.getElementById(id); const fmt = n => Number(n || 0).toLocaleString(); const q = new URLSearchParams(location.search); const ref = q.get('ref') || ''; // ?ref= on the home page rides along to /join so the sponsor cookie is set there (viral-link rule) if (ref) document.querySelectorAll('[data-join]').forEach(a => { try { const u = new URL(a.getAttribute('href'), location.origin); u.searchParams.set('ref', ref); a.setAttribute('href', u.pathname + u.search); } catch (e) {} }); // framed (traffic exchanges): open sign-in and the spin link in a new tab const framed = (() => { try { return window.top !== window.self; } catch (e) { return true; } })(); if (framed) document.querySelectorAll('a[data-join], a[href^="/contract"], a[href^="/ledger"]').forEach(a => { a.target = '_blank'; a.rel = 'noopener'; }); let CONFIG = null; fetch('/api/config').then(r => r.json()).then(c => { CONFIG = c; if (c && c.contract && $('ftContract')) { $('ftContract').textContent = c.contract.slice(0, 6) + '…' + c.contract.slice(-4); } }).catch(() => {}); // live strip fetch('/api/stats').then(r => r.json()).then(s => { $('stServed').textContent = fmt(s.servedToday); $('stMembers').textContent = fmt(s.onchainMembers); $('stPackages').textContent = fmt(s.purchases); const pol = Number(BigInt(String(s.paidInWei || '0')) / 10000000000000000n) / 100; // two decimals of POL $('stPol').textContent = pol.toLocaleString(undefined, { maximumFractionDigits: 2 }); $('stHealth').textContent = 'healthy'; $('stDot').className = 'dot'; }).catch(() => { $('stHealth').textContent = 'offline'; $('stDot').className = 'dot off'; }); // package credits from the catalog (flat credits = cents, Marty 2026-10-07) and the example POL figure fetch('/api/catalog').then(r => r.json()).then(c => { const by = {}; (c.products || []).forEach(p => { by[p.priceCents] = p; }); document.querySelectorAll('#pkgGrid .cr').forEach(el => { const p = by[Number(el.dataset.cr)]; el.textContent = p ? fmt(p.creditAmount) + ' credits' : fmt(Number(el.dataset.cr)) + ' credits'; }); const hundred = by[10000]; if (hundred && hundred.costWei) { const pol = Number(BigInt(String(hundred.costWei)) / 10000000000000000n) / 100; $('exPol').textContent = pol.toFixed(2); } else $('exPol').textContent = '[live]'; }).catch(() => {}); // the demo const form = $('demoForm'), err = $('demoErr'), out = $('demoOut'); const showErr = (el, m) => { el.textContent = m; el.hidden = !m; }; let demoUrl = ''; form.addEventListener('submit', async ev => { ev.preventDefault(); showErr(err, ''); const urls = ['d1', 'd2', 'd3'].map(id => $(id).value.trim()).filter(Boolean); const bad = urls.find(u => !/^https?:\/\/[^\s]+\.[^\s]+$/i.test(u)); if (!urls.length) return showErr(err, 'Paste at least one page address.'); if (bad) return showErr(err, 'Addresses need to start with https:// (check: ' + bad.slice(0, 40) + ')'); $('spinBtn').disabled = true; try { const r = await (await fetch('/api/demo/spin', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ urls }) })).json(); if (r.error) throw new Error(r.error); demoUrl = r.url; $('demoLink').textContent = r.url.replace(/^https:\/\//, ''); $('openBtn').href = r.url; $('demoMeta').textContent = r.destinations + (r.destinations === 1 ? ' page' : ' pages') + ' · equal shares · live'; form.hidden = true; out.hidden = false; out.style.display = 'flex'; $('em').focus(); } catch (e) { showErr(err, e.message); } finally { $('spinBtn').disabled = false; } }); $('copyBtn').addEventListener('click', () => { const done = () => { $('copyBtn').textContent = 'Copied'; setTimeout(() => { $('copyBtn').textContent = 'Copy'; }, 1800); }; try { navigator.clipboard.writeText(demoUrl).then(done, () => { window.prompt('Copy your link', demoUrl); }); } catch (e) { window.prompt('Copy your link', demoUrl); } }); // keep it: the engine's email-code door; the ls.demo cookie set by /api/demo/spin claims the spin on verify const keep = $('keepForm'), kerr = $('keepErr'); const fts = Date.now(); keep.addEventListener('submit', async ev => { ev.preventDefault(); showErr(kerr, ''); const email = $('em').value.trim(); if (!/^[^\s@]+@[^\s@]+\.[^\s@]{2,}$/.test(email)) return showErr(kerr, 'That email address does not look right.'); $('keepBtn').disabled = true; try { const r = await (await fetch('/api/auth/email/start', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ email, fts, hp_field_x9: $('hp_field_x9').value, pick: null }) })).json(); if (r.error) throw new Error(r.error); $('codeRow').hidden = false; $('codeRow').style.display = 'flex'; $('keepBtn').textContent = 'Send again'; if (r.devCode) $('code').value = r.devCode; $('code').focus(); } catch (e) { showErr(kerr, e.message); } finally { $('keepBtn').disabled = false; } }); $('verifyBtn').addEventListener('click', async () => { showErr(kerr, ''); const email = $('em').value.trim(), code = $('code').value.trim(); if (code.length < 6) return showErr(kerr, 'Enter the six digits from the email.'); $('verifyBtn').disabled = true; try { const r = await (await fetch('/api/auth/email/verify', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ email, code, followups: true }) })).json(); if (r.error) throw new Error(r.error); const dest = '/links'; if (framed) window.open(dest, '_blank', 'noopener'); else location.href = dest; } catch (e) { showErr(kerr, e.message); } finally { $('verifyBtn').disabled = false; } }); })();