Files
vbb/public/assets/common.js
T

128 lines
12 KiB
JavaScript

// Shared page helpers (VBB namespace, modelled on Downline Banners' common.js): nav, footer, toasts, fetch helpers.
window.VBB = (function () {
// Network Weekend ribbon (Marty 2026-11-17): netweekend.js paints itself only while a network event is live (exact Central times)
if (!/^\/admin/.test(location.pathname)) { const nw = document.createElement('script'); nw.src = '/assets/netweekend.js?v=20261117a'; nw.defer = true; nw.setAttribute('data-site', 'vbb'); document.head.appendChild(nw); }
let cfgP = null;
const $ = id => document.getElementById(id);
const esc = s => String(s == null ? '' : s).replace(/[&<>"']/g, c => ({ '&': '&amp;', '<': '&lt;', '>': '&gt;', '"': '&quot;', "'": '&#39;' }[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 || ''; if (msg) el.scrollIntoView({ block: 'nearest', behavior: 'smooth' }); } // the note sits at the foot of the page: bring it into view (2026-11-15, a failed post looked like a dead button)
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'], ['/guides', 'Guides'], ['/packages', 'Packages'], ['/promote', 'Promote'], ['/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'; });
}
// network ad zones (Marty 2026-11-14, the Downline Banners pattern): LinkSpin zones on Marty's account, one per placement, each
// mounted once with a small 'Network' label. Footer (wide / phone), Leaderboards, My Banners, How it works; the 404 zone is in
// server.js. NEVER on the loop page (/) or admin. An empty id = that placement stays off.
const ZONES = { footWide: 'ls-b17eff', footPhone: 'ls-e8f989', leaders: 'ls-0be4df', my: 'ls-88b761', how: 'ls-9824e4' }; // Marty's LinkSpin zones for viralbannerblitz.com (2026-11-14)
function zone(el, key) {
if (!el || el.dataset.zoned || !ZONES[key]) return; el.dataset.zoned = '1'; el.classList.add('net-zone');
const lab = document.createElement('div'); lab.className = 'net-zone-label'; lab.textContent = 'Network';
const s = document.createElement('script'); s.async = true; s.src = 'https://linkspin.app/z.js'; s.setAttribute('data-zone', ZONES[key]);
el.appendChild(lab); el.appendChild(s);
}
function slot(after) { if (!after || !after.parentNode) return null; const d = document.createElement('div'); d.className = 'net-zone-slot'; after.parentNode.insertBefore(d, after.nextSibling); return d; }
function pageZones() {
const p = location.pathname.replace(/\/$/, '') || '/';
if (p === '/' || p === '/admin') return;
const card = id => { const el = $(id); return el ? el.closest('.card') : null; };
if (p === '/leaders') zone(slot(card('lbVisitors')), 'leaders');
if (p === '/my') zone(slot($('stats')), 'my');
if (p === '/how') zone(slot(document.querySelectorAll('main .card')[1]), 'how');
const za = document.createElement('div'); za.className = 'wrap net-zone-foot'; document.body.appendChild(za);
zone(za, window.innerWidth >= 760 ? 'footWide' : 'footPhone');
}
function renderFooter() {
pageZones();
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> &middot; <a href="/leaders">Leaderboards</a> &middot; <a href="/how">How it works</a> &middot; <a href="/guides">Guides</a> &middot; <a href="/packages">Packages</a> &middot; <a href="/promote">Promote</a> &middot; <a href="/my">My Banners</a></p>'
// trust pack (Marty 2026-11-14): legal + proof links, company line, and the network line served by the hub
+ '<p class="legal-links"><a href="/terms">Terms</a> &middot; <a href="/disclaimer">Earnings Disclaimer</a> &middot; <a href="/privacy">Privacy</a> &middot; <a href="/contract">Contract</a> &middot; <a href="/ledger">Ledger</a> &middot; <a href="https://instantadpay.com/support" target="_blank" rel="noopener">Support</a></p>'
+ '<p class="co">&copy; 2026 TrustClickz</p><p class="netline"></p></div>';
document.body.appendChild(f);
netLine(f.querySelector('.netline'));
toasts();
}
// "Part of the network": the list comes from the hub through /api/network-sites, the built-in copy if it does not answer
const NET = [['iap', 'InstantAdPay', 'https://instantadpay.com'], ['linkspin', 'LinkSpin', 'https://linkspin.app'], ['dlb', 'Downline Banners', 'https://downlinebanners.com'],
['vbb', 'Viral Banner Blitz', 'https://viralbannerblitz.com'], ['stackpol', 'Stack POL', 'https://stackpol.com'], ['earnerscorner', 'Earners Corner', 'https://earnerscorner.com']].map(a => ({ id: a[0], name: a[1], url: a[2] }));
// the footer links carry a referral username (Marty 2026-11-15): the signed-in member's own, else the ?ref= this visitor came in on
// (vbb.ref cookie, usernames only). Tracked links get it as s1 (LinkSpin sends it on as ?ref=), plain links as ?ref=. Earners Corner takes none.
const REF_RE = /^[A-Za-z0-9_.-]{1,40}$/;
const refCookie = () => { const m = document.cookie.match(/(?:^|;\s*)vbb\.ref=([^;]*)/); try { const v = m ? decodeURIComponent(m[1]) : ''; return REF_RE.test(v) && !/^0x[0-9a-f]{40}$/i.test(v) ? v : ''; } catch (e) { return ''; } };
function withRef(s, ref) {
const href = s.go || s.url; if (!ref || s.id === 'earnerscorner') return href;
try { const u = new URL(href); u.searchParams.set(s.go ? 's1' : 'ref', ref); return u.toString(); } catch (e) { return href; }
}
function netLine(el) {
if (!el) return;
let list = NET; let ref = refCookie();
const paint = () => { el.innerHTML = '<b>Part of the network</b> &middot; One account across the network: ' + list.map(s => (s.id === 'vbb' || /viralbannerblitz\.com/.test(s.url))
? '<span class="here" aria-current="page">' + esc(s.name) + '</span>' : '<a href="' + esc(withRef(s, ref)) + '" target="_blank" rel="noopener nofollow">' + esc(s.name) + '</a>').join(' &middot; '); };
paint();
fetch('/api/network-sites').then(r => r.json()).then(l => { if (Array.isArray(l) && l.length) { list = l; paint(); } }).catch(() => {});
fetch('/api/me', { credentials: 'same-origin' }).then(r => r.json()).then(m => { if (m && m.signedIn && m.username && REF_RE.test(m.username)) { ref = m.username; paint(); } }).catch(() => {});
}
// live toasts: real cycles, purchases and prize payouts 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 polOf = w => { try { const v = Number(BigInt(w || 0) / 10n ** 14n) / 1e4; return v.toFixed(v >= 100 ? 2 : 4); } catch (e) { return '0'; } };
const whoOf = (a, nm) => nm ? '@' + esc(nm) : esc(short(String(a || '')));
const show = c => {
const el = document.createElement('a'); el.className = 'toast';
if (c.kind === 'purchase') { // trust pack 2026-11-14: real purchases from the ledger, with the sponsor paid in the same transaction
const sp = (c.pays || []).find(p => p.role === 'sponsor' && !p.failed);
el.href = '/ledger'; el.className = 'toast buy';
el.innerHTML = '<b>' + whoOf(c.buyer, c.buyerName) + ' bought ' + esc(c.what) + '</b><br><span>' + (sp ? whoOf(sp.to, sp.name) + ' got ' + polOf(sp.wei) + ' POL, same transaction' : 'paid on-chain, split by the contract') + '</span>';
} else if (c.kind === 'prize') {
el.href = '/ledger'; el.className = 'toast prize';
el.innerHTML = '<b>' + esc(c.name) + ' won a weekly prize</b><br><span>' + esc(c.pol) + ' POL paid on-chain</span>';
} else {
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 () => {
const [d, l] = await Promise.all([getJSON('/api/recent').catch(() => null), getJSON('/api/ledger?n=6').catch(() => null)]);
const all = [];
((d && d.cycles) || []).forEach(c => all.push(Object.assign({ key: 'c' + c.id }, c)));
((l && l.items) || []).filter(x => x.at && Date.now() - x.at < 30 * 60000).forEach(x => all.push(Object.assign({ key: x.kind + x.tx + (x.place || '') + (x.board || '') }, x)));
all.sort((a, b) => (b.at || 0) - (a.at || 0));
const fresh = all.filter(c => !seen.has(c.key)); all.forEach(c => seen.add(c.key));
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' + (t.look ? ' look' : '') + (t.t === 'spin' ? ' fill' : '') + (extra && extra.cls ? ' ' + extra.cls : '');
if (t.t === 'spin') a.title = (t.owner ? t.owner + ': ' : '') + 'LinkSpin Banner Spin (fills an open place, not in the loop)';
a.href = t.href; a.target = '_blank'; a.rel = 'noopener';
a.innerHTML = '<img alt="' + esc(t.t === 'spin' ? 'LinkSpin Banner Spin' + (t.owner ? ' by ' + t.owner : '') : 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>' : '');
if (t.look && t.teaser) { const s = document.createElement('span'); s.className = 'teaser'; s.textContent = t.teaser; a.appendChild(s); a.title = t.teaser; }
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 };
})();