9d0dde4b57
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
62 lines
4.7 KiB
JavaScript
62 lines
4.7 KiB
JavaScript
// Shared page helpers (VBB namespace, modelled on Downline Banners' common.js): nav, footer, toasts, fetch helpers.
|
|
window.VBB = (function () {
|
|
let cfgP = null;
|
|
const $ = id => document.getElementById(id);
|
|
const esc = s => String(s == null ? '' : s).replace(/[&<>"']/g, c => ({ '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' }[c]));
|
|
const getConfig = () => cfgP || (cfgP = fetch('/api/config').then(r => r.json()));
|
|
const short = a => a ? a.slice(0, 6) + '...' + a.slice(-4) : '';
|
|
const framed = (() => { try { return window.top !== window.self; } catch (e) { return true; } })();
|
|
function status(msg, cls) { const el = $('status'); if (!el) return; el.hidden = !msg; el.className = 'note ' + (cls || ''); el.textContent = msg || ''; }
|
|
async function getJSON(url, opts) { const r = await fetch(url, opts); let j = {}; try { j = await r.json(); } catch (e) {} if (!r.ok && !j.error) j.error = 'Request failed (' + r.status + ')'; return j; }
|
|
const post = (url, body) => getJSON(url, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(body || {}) });
|
|
const n = x => Number(x || 0).toLocaleString('en-US');
|
|
|
|
function renderNav(active) {
|
|
const nav = document.createElement('nav'); nav.className = 'top';
|
|
const links = [['/', 'The loop'], ['/leaders', 'Leaderboards'], ['/how', 'How it works'], ['/packages', 'Packages'], ['/my', 'My Banners']];
|
|
nav.innerHTML = '<div class="wrap"><a class="brand" href="/"><img src="/art/mark.svg" alt="" width="36" height="36"><span><span class="wm">Viral Banner <b>Blitz</b></span>'
|
|
+ '<span class="by">Part of the Crypto Team Build Network</span></span></a><div class="links">'
|
|
+ links.map(l => '<a href="' + l[0] + '"' + (l[0] === active ? ' class="on" aria-current="page"' : '') + '>' + l[1] + '</a>').join('') + '</div></div>';
|
|
document.body.prepend(nav);
|
|
// inside a traffic-exchange frame, the member page opens in a new tab
|
|
if (framed) document.querySelectorAll('nav a[href="/my"]').forEach(a => { a.target = '_blank'; a.rel = 'noopener'; });
|
|
}
|
|
function renderFooter() {
|
|
const f = document.createElement('footer'); f.className = 'foot';
|
|
f.innerHTML = '<div class="wrap"><p><b>Viral Banner Blitz</b>, part of the Crypto Team Build Network</p>'
|
|
+ '<p>A banner that cycles earns advertising across the network, not cash. Nothing here is an income opportunity.</p>'
|
|
+ '<p><a href="/">The loop</a> · <a href="/leaders">Leaderboards</a> · <a href="/how">How it works</a> · <a href="/packages">Packages</a> · <a href="/my">My Banners</a></p></div>';
|
|
document.body.appendChild(f);
|
|
toasts();
|
|
}
|
|
// live cycle toasts: real cycles from the ledger only, bottom-left, one at a time, never older than 30 minutes
|
|
let toastsOn = false;
|
|
function toasts() {
|
|
if (toastsOn) return; toastsOn = true;
|
|
const seen = new Set(); let first = true;
|
|
const box = document.createElement('div'); box.className = 'toasts'; box.setAttribute('aria-live', 'polite'); document.body.appendChild(box);
|
|
const show = c => {
|
|
const el = document.createElement('a'); el.className = 'toast'; el.href = '/how';
|
|
el.innerHTML = '<b>' + (c.name ? esc(c.name) + '\'s banner' : 'A banner') + ' just cycled</b><br><span>' + n(c.views) + ' network ad views earned for its owner</span>';
|
|
box.appendChild(el); requestAnimationFrame(() => el.classList.add('in'));
|
|
setTimeout(() => { el.classList.remove('in'); setTimeout(() => el.remove(), 400); }, 7000);
|
|
};
|
|
const tick = async () => {
|
|
let d; try { d = await getJSON('/api/recent'); } catch (e) { return; } if (!d || !d.cycles) return;
|
|
const fresh = d.cycles.filter(c => !seen.has(c.id)); d.cycles.forEach(c => seen.add(c.id));
|
|
const list = first ? fresh.slice(0, 1) : fresh.reverse(); first = false;
|
|
list.slice(0, 3).forEach((c, i) => setTimeout(() => show(c), 1500 + i * 8000));
|
|
};
|
|
tick(); setInterval(tick, 25000);
|
|
}
|
|
function tile(t, extra) {
|
|
const a = document.createElement('a'); a.className = 'tile' + (extra && extra.cls ? ' ' + extra.cls : '');
|
|
a.href = t.href; a.target = '_blank'; a.rel = 'noopener';
|
|
a.innerHTML = '<img alt="' + esc(t.owner ? 'Banner by ' + t.owner : 'Member banner #' + t.id) + '" width="125" height="125" loading="lazy" referrerpolicy="no-referrer">'
|
|
+ (extra && extra.pos ? '<span class="pos">' + esc(extra.pos) + '</span>' : '') + (t.owner ? '<span class="who">' + esc(t.owner) + '</span>' : '');
|
|
const img = a.querySelector('img'); img.src = t.img; img.onerror = () => { img.onerror = null; img.src = '/art/tile-fallback.svg'; };
|
|
return a;
|
|
}
|
|
return { $, esc, getConfig, short, framed, status, getJSON, post, n, renderNav, renderFooter, tile };
|
|
})();
|