Files
viraladblitz/public/assets/common.js
T

135 lines
12 KiB
JavaScript

// Shared page helpers (VAB namespace, modelled on VBB / Downline Banners common.js): nav, the royal ticker, verified visits,
// ad cards, the shared SVG bits of the daylight look (mark, network lines, green checks), footer (legal, company line, the network line from the hub), network ad zones and toasts of real events.
window.VAB = (function () {
let cfgP = null, vt = 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; } })();
const n = x => Number(x || 0).toLocaleString('en-US');
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 + ')'; j._status = r.status; return j; }
const post = (url, body) => getJSON(url, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(body || {}) });
// the daylight look's shared SVG bits
const MARK = '<svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="#3D8BFF" stroke-width="2.4" aria-hidden="true"><path d="M4 18 L12 4 L20 18"/><path d="M8 12 H16" stroke="#FF3B30"/></svg>';
const NETLINES = '<svg class="netlines" viewBox="0 0 600 400" preserveAspectRatio="none" aria-hidden="true"><g stroke="#9DBEFF" stroke-width="1" fill="none"><path d="M0 300 Q150 180 300 240 T600 120"/><path d="M0 220 Q200 90 360 160 T600 40"/><path d="M40 400 L220 120 L420 260 L600 60"/></g><g fill="#FFC21A"><circle cx="220" cy="120" r="4"/><circle cx="420" cy="260" r="4"/><circle cx="360" cy="160" r="3"/><circle cx="300" cy="240" r="3"/><circle cx="520" cy="90" r="4"/></g></svg>';
const CHECK = '<svg width="24" height="24" viewBox="0 0 26 26" aria-hidden="true"><circle cx="13" cy="13" r="12" fill="#16B85A" stroke="#FFFFFF" stroke-width="2"/><path d="M7.5 13.5 L11.5 17 L18.5 9" fill="none" stroke="#FFFFFF" stroke-width="3"/></svg>';
function renderNav(active) {
const nav = document.createElement('nav'); nav.className = 'top';
const links = [['/#board', 'Top ads'], ['/my', 'My page'], ['/boost', 'Boost'], ['/ledger', 'Ledger'], ['/contract', 'Contract'], ['/promote', 'Promote']];
nav.innerHTML = '<div class="wrap"><a class="brand" href="/" aria-label="ViralAdBlitz, front page"><span class="mk">' + MARK + '</span><span class="wm">VIRAL<b>AD</b>BLITZ</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>'
+ '<a class="cta" href="/my">Post my free text ad</a></div>';
document.body.prepend(nav);
const tk = document.createElement('div'); tk.className = 'ticker'; tk.id = 'ticker'; tk.setAttribute('aria-label', 'Live ticker of the newest text ads');
tk.innerHTML = '<span class="empty">Loading the wire...</span>'; nav.after(tk);
if (framed) document.querySelectorAll('a[href="/my"]').forEach(a => { a.target = '_blank'; a.rel = 'noopener'; });
}
// a link through the click relay: placement + the visitor's verified token once their visit has verified
const goHref = (id, p) => '/go/' + id + '?p=' + encodeURIComponent(p || 'site') + (vt ? '&v=' + vt : '');
function decorate() { document.querySelectorAll('a[data-go]').forEach(a => { a.href = goHref(a.dataset.go, a.dataset.p); }); }
function renderTicker(items) {
const el = $('ticker'); if (!el) return;
if (!items || !items.length) { el.innerHTML = '<span class="empty">The wire is quiet. Post the first text ad.</span>'; return; }
const one = items.map(t => '<a data-go="' + t.id + '" data-p="ticker" target="_blank" rel="noopener sponsored" class="' + (t.boosted ? 'star' : '') + '" href="' + goHref(t.id, 'ticker') + '">'
+ (t.boosted ? '<b>★ BOOSTED</b> ' : '<i>▲ LIVE</i> ') + esc(t.headline) + ' · ' + esc(t.owner) + '</a>').join('');
const reps = Math.max(1, Math.ceil(8 / items.length));
el.innerHTML = '<div class="run" style="--tick-s:' + Math.max(30, items.length * reps * 6) + 's">' + one.repeat(reps) + one.repeat(reps) + '</div>';
}
// a white clipped card for a text ad (front board, member page Boosted places); right = a small figure (verified clicks)
function paper(t, tag, place, right) {
const lab = (t.boosted ? '★ BOOSTED' + (tag ? ' · ' : '') : '') + (tag || '');
return '<article class="paper' + (t.boosted ? ' star' : '') + '">' + (lab || right ? '<div class="top"><span class="tag">' + esc(lab) + '</span>' + (right ? '<span class="cl">' + esc(right) + '</span>' : '') + '</div>' : '')
+ '<h3>' + esc(t.headline) + '</h3><p>' + esc(t.line1) + (t.line2 ? ' ' + esc(t.line2) : '') + '</p>'
+ '<div class="foot"><a class="visit" data-go="' + t.id + '" data-p="' + esc(place || 'card') + '" target="_blank" rel="noopener sponsored" href="' + goHref(t.id, place) + '">Visit ' + esc(t.host) + '</a><span class="host">' + esc(t.owner) + '</span></div></article>';
}
// the page's data and its verified visit: the ads come with a ticket; after visitSeconds of the page being visible the
// visit is sent once, and from then on clicks carry the verified token
async function wire(page, u) {
const src = document.referrer || '';
const d = await getJSON('/api/wire?page=' + encodeURIComponent(page) + (u ? '&u=' + encodeURIComponent(u) : '') + (src ? '&src=' + encodeURIComponent(src) : ''));
renderTicker(d.ticker || []);
if (d.ticket) {
const need = (d.seconds || 6) * 1000; let seen = 0, last = Date.now(), sent = false;
const step = async () => {
const now = Date.now(); if (document.visibilityState === 'visible') seen += now - last; last = now;
if (!sent && seen >= need) {
sent = true; clearInterval(iv);
const r = await post('/api/visit/seen', { ticket: d.ticket, visibleMs: seen }).catch(() => ({}));
if (r.vt) { vt = r.vt; decorate(); }
document.dispatchEvent(new CustomEvent('vab:verified', { detail: r }));
}
};
const iv = setInterval(step, 500);
document.addEventListener('visibilitychange', () => { last = Date.now(); });
}
return d;
}
// network ad zones (LinkSpin zones on Marty's account for viraladblitz.com, the VBB pattern; made 2026-11-15 through the admin
// zone route): footer wide 728x90, footer phone 320x50, ledger 468x60, Boost page 300x250, My page 468x60; 404 in server.js.
const ZONES = { footWide: 'ls-766b4a', footPhone: 'ls-66e157', ledger: 'ls-a5c68c', boost: 'ls-754307', my: 'ls-f880d2' };
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 pageZones() {
const p = location.pathname.replace(/\/$/, '') || '/';
if (p === '/admin') return;
const after = (el, key) => { if (!el || !el.parentNode) return; const d = document.createElement('div'); el.parentNode.insertBefore(d, el.nextSibling); zone(d, key); };
if (p === '/ledger') after($('rows') && $('rows').closest('section'), 'ledger');
if (p === '/boost') after(document.querySelector('main section'), 'boost');
if (p === '/my') after($('dash'), 'my');
const za = document.createElement('div'); za.className = 'wrap'; document.body.appendChild(za);
zone(za, window.innerWidth >= 760 ? 'footWide' : 'footPhone');
}
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'], ['viraladblitz', 'ViralAdBlitz', 'https://viraladblitz.com'], ['stackpol', 'Stack POL', 'https://stackpol.com'], ['earnerscorner', 'Earners Corner', 'https://earnerscorner.com']].map(a => ({ id: a[0], name: a[1], url: a[2] }));
function netLine(el) {
if (!el) return;
const paint = list => { el.innerHTML = '<b>Part of the network</b> &middot; One account across the network: ' + list.map(s => (s.id === 'viraladblitz' || /viraladblitz\.com/.test(s.url))
? '<span class="here" aria-current="page">' + esc(s.name) + '</span>' : '<a href="' + esc(s.go || s.url) + '" target="_blank" rel="noopener nofollow">' + esc(s.name) + '</a>').join(' &middot; '); };
paint(NET);
fetch('/api/network-sites').then(r => r.json()).then(l => { if (Array.isArray(l) && l.length) paint(l.some(s => s.id === 'viraladblitz') ? l : l.concat([NET[4]])); }).catch(() => {});
}
function renderFooter() {
pageZones();
const f = document.createElement('footer'); f.className = 'foot';
f.innerHTML = '<div class="wrap"><p><b>ViralAdBlitz</b>, the network\'s text ad wire</p>'
+ '<p>A Boost buys advertising, not income. Nothing is promised.</p>'
+ '<p><a href="/">Front page</a> &middot; <a href="/my">My page</a> &middot; <a href="/boost">Boost</a> &middot; <a href="/promote">Promote</a></p>'
+ '<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">TrustClickz &middot; 615 Green Valley Drive, Moody, AL 35004</p><p class="netline"></p></div>';
document.body.appendChild(f);
netLine(f.querySelector('.netline'));
toasts();
}
// live toasts: real new ads and Boosts 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' + (c.kind === 'boost' ? ' gold' : '');
if (c.kind === 'boost') { el.href = '/ledger'; el.innerHTML = '<b>' + (c.name ? '@' + esc(c.name) : 'A member') + ' just boosted an ad</b><br><span>' + (c.levels ? (c.levels === 1 ? '1 sponsor' : c.levels + ' sponsors') + ' paid in the same transaction' : 'paid on-chain, split by the contract') + '</span>'; }
else { el.href = '/'; el.innerHTML = '<b>' + (c.name ? '@' + esc(c.name) : 'A member') + ' posted a new text ad</b><br><span>' + esc(c.headline) + '</span>'; }
box.appendChild(el); requestAnimationFrame(() => el.classList.add('in'));
setTimeout(() => { el.classList.remove('in'); setTimeout(() => el.remove(), 400); }, 7000);
};
const tick = async () => {
const d = await getJSON('/api/recent').catch(() => null); const all = (d && d.items) || [];
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);
}
const polOf = (w, d) => { try { const k = BigInt(w || 0); const v = Number(k / 10n ** 12n) / 1e6; return v.toFixed(d != null ? d : (v >= 100 ? 2 : 4)); } catch (e) { return '0'; } };
const when = t => t ? new Date(t).toLocaleString('en-US', { timeZone: 'America/Chicago', month: 'short', day: 'numeric', hour: 'numeric', minute: '2-digit' }) + ' CT' : '';
return { MARK, NETLINES, CHECK, $, esc, getConfig, short, framed, status, getJSON, post, n, renderNav, renderFooter, renderTicker, paper, wire, decorate, goHref, polOf, when };
})();