// 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 = '
';
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 = '';
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 = '' + (c.name ? esc(c.name) + '\'s banner' : 'A banner') + ' just cycled
' + n(c.views) + ' network ad views earned for its owner';
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 = '
'
+ (extra && extra.pos ? '' + esc(extra.pos) + '' : '') + (t.owner ? '' + esc(t.owner) + '' : '');
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 };
})();