Files
viraladblitz/public/assets/my.js
T

156 lines
18 KiB
JavaScript

// My page (the member dashboard): sign in (network or wallet), post or edit the one free text ad, the member's page link, their
// line, Boost delivery, recent activity and what the contract has paid them. Every figure comes from /api/my; nothing is estimated.
(async function () {
VAB.renderNav('/my'); VAB.renderFooter(); VAB.wire('other').catch(() => {});
const $ = VAB.$, esc = VAB.esc;
const c = await VAB.getConfig().catch(() => ({}));
const me = await VAB.getJSON('/api/me');
if (!me.signedIn) {
// signed in on InstantAdPay already? one quick hop there and back signs you in here too
try { const ss = await VAB.getJSON('/api/sso/status'); if (ss && ss.check) { location.replace('/sso/start?mode=check&via=iap&to=' + encodeURIComponent('/my')); return; } } catch (e) {}
$('signin').hidden = false;
if (!c.sso) { $('viaIap').hidden = true; $('viaLs').hidden = true; }
if (c.joinUrl) $('joinLink').href = c.joinUrl;
$('viaWallet').onclick = async () => {
try { VAB.status('Check your wallet for the sign-in message.'); await VABWallet.signIn(); location.reload(); }
catch (e) { VAB.status(String(e && e.message || e).slice(0, 300), 'err'); }
};
return;
}
$('signout').onclick = async () => { await VAB.post('/api/auth/logout'); location.reload(); };
$('signoutAll').onclick = async () => { $('signoutAll').disabled = true; await VAB.post('/api/auth/logout-everywhere'); location.reload(); };
const counter = (inp, out, max) => { const f = () => { out.textContent = inp.value.length + ' / ' + max; }; inp.addEventListener('input', f); f(); };
const copyText = (t, b) => { const o = b.textContent; try { navigator.clipboard.writeText(t); b.textContent = 'Copied'; setTimeout(() => { b.textContent = o; }, 1400); } catch (e) {} };
const ex = c.explorer || 'https://polygonscan.com';
const vlink = tx => '<a href="' + esc(ex) + '/tx/' + esc(tx) + '" target="_blank" rel="noopener">verify</a>';
const stat = (label, val, note, cls) => '<div class="stat' + (cls ? ' ' + cls : '') + '"><span>' + label + '</span><b>' + val + '</b>' + (note ? '<em>' + note + '</em>' : '') + '</div>';
let counted = false;
// LinkSpin tracking of the ad (server lstrack.js): the tracked link and its clicks as LinkSpin counts them, or why there is none
const lsLine = ls => {
if (!ls || !ls.on) return '';
const why = { off: 'Tracking is off for this ad: visitors go straight to your link.', macros: 'Your link has {clickid} or {placement} codes, so it goes out as it is (not through LinkSpin).',
pending: 'Your LinkSpin tracked link is being set up. Until then visitors go straight to your link.', 'no-username': 'Sign in through InstantAdPay or LinkSpin once to link your LinkSpin account.',
'no-account': 'No LinkSpin account was found for your network username yet. Visitors go straight to your link.',
spin: 'Your ad link is your own LinkSpin spin link, so its clicks already show in your LinkSpin Reports.' }[ls.state];
if (ls.state !== 'tracked') return '<p class="lsline small">' + esc(why || '') + '</p>';
const c = ls.clicks;
return '<p class="lsline small">Tracked with LinkSpin: <b>' + esc(ls.link.replace(/^https?:\/\//, '').replace(/\?.*$/, '')) + '</b>'
+ (c ? ' · ' + VAB.n(c.hits) + ' clicks in all, ' + VAB.n(c.hits30) + ' in 30 days (' + VAB.n(c.unique30) + ' unique)' : ' · clicks show in your LinkSpin Reports')
+ ' · <a href="https://linkspin.app/ads" target="_blank" rel="noopener">Open LinkSpin</a></p>';
};
// "Use one of my spin links" (2026-11-15): the member's own LinkSpin spin links as the ad link. A spin link is already tracked on
// LinkSpin, so "Track with LinkSpin" is switched off (and greyed) while the link is one. The server checks the link is the member's own.
let spins = null, spinAd = null;
const isSpin = v => { v = String(v || '').trim().toLowerCase(); if (spinAd && v === String(spinAd).toLowerCase()) return true; return !!(spins && spins.some(x => x.url.toLowerCase() === v)); };
const spinMark = () => { const on = isSpin($('fLink').value); $('fTrack').disabled = on; $('trackNote').hidden = !on; $('fTrack').closest('label').style.opacity = on ? '0.6' : ''; };
$('fLink').addEventListener('input', spinMark);
// the AI ad writer (Marty 2026-11-17): 3 suggestions the hub writes only from the member's own page; picking one fills the form
if (c.aiWriter) {
$('aiBox').hidden = false;
let opts = [];
$('aiWrite').onclick = async () => {
const box = $('aiList'), url = $('fLink').value.trim(); box.hidden = false;
if (!/^https?:\/\/\S+\.\S+/i.test(url)) { box.innerHTML = '<p class="small" style="margin:0">Put your link in the Your link box first (starting with https://), then tap the button again.</p>'; $('fLink').focus(); return; }
$('aiWrite').disabled = true; box.innerHTML = '<p class="small muted" style="margin:0">Reading your page and writing 3 ads. This takes a few seconds...</p>';
const r = await VAB.post('/api/my/adwriter', { url }).catch(() => ({ error: 'The ad writer did not answer. Try again in a minute.' }));
$('aiWrite').disabled = false;
if (r.error) { box.innerHTML = '<p class="small" style="margin:0">' + esc(r.error) + '</p>'; return; }
opts = r.options || [];
box.innerHTML = '<p class="small muted" style="margin:0 0 8px">Tap one to use it. Check every word is true for your offer before you post.' + (typeof r.left === 'number' ? ' Ad writes left today: ' + r.left + '.' : '') + '</p>'
+ opts.map((o, i) => '<button type="button" class="btn-soft" data-ai="' + i + '" style="display:block;width:100%;text-align:left;margin:0 0 6px"><b>' + esc(o.headline) + '</b><br><span class="small">' + esc(o.line || '') + '</span></button>').join('');
box.querySelectorAll('button[data-ai]').forEach(b => { b.onclick = () => { const o = opts[Number(b.dataset.ai)]; $('fHead').value = o.headline; $('fL1').value = o.line || ''; ['fHead', 'fL1'].forEach(id => $(id).dispatchEvent(new Event('input'))); box.hidden = true; VAB.status('Filled in. Edit it as you like, then save your ad.', 'ok'); }; });
};
}
$('spinBtn').onclick = async () => {
const box = $('spinList');
if (!box.hidden) { box.hidden = true; return; }
box.hidden = false; box.innerHTML = '<p class="small muted" style="margin:0">Asking LinkSpin for your spin links...</p>';
const r = await VAB.getJSON('/api/my/spins').catch(() => ({ error: 'LinkSpin did not answer. Try again in a minute.' }));
if (r.error) { box.innerHTML = '<p class="small" style="margin:0">' + esc(r.error) + '</p>'; return; }
spins = r.spins || [];
if (!r.account) { box.innerHTML = '<p class="small" style="margin:0">No LinkSpin account was found for your network username yet. <a href="https://linkspin.app" target="_blank" rel="noopener">Open LinkSpin</a></p>'; return; }
if (!spins.length) { box.innerHTML = '<p class="small" style="margin:0">You have no spin links on LinkSpin yet. Make one there (add every page you promote), then come back and pick it. <a href="https://linkspin.app/links" target="_blank" rel="noopener">Open LinkSpin</a></p>'; return; }
box.innerHTML = '<p class="small muted" style="margin:0 0 8px">Pick one. Your ad sends each click to the next page on that spin link.</p>'
+ spins.map((x, i) => '<button type="button" class="btn-soft" data-i="' + i + '" style="display:block;width:100%;text-align:left;margin:0 0 6px"' + (x.paused || !x.destinations ? ' disabled' : '') + '><b>' + esc(x.name || x.code) + '</b> <span class="small muted mono">' + esc(x.url.replace(/^https?:\/\//, '')) + '</span> <span class="small">'
+ (x.paused ? '(paused on LinkSpin)' : !x.destinations ? '(no active pages yet)' : VAB.n(x.destinations) + (x.destinations === 1 ? ' page' : ' pages')) + '</span></button>').join('');
box.querySelectorAll('button[data-i]').forEach(b => { b.onclick = () => { const x = spins[Number(b.dataset.i)]; $('fLink').value = x.url; spinMark(); box.hidden = true; VAB.status('Spin link picked. Save your ad to use it.', 'ok'); }; });
};
async function load() {
const d = await VAB.getJSON('/api/my'); if (d.error) { VAB.status(d.error, 'err'); return; }
$('dash').hidden = false;
const a = d.ad; const lim = c.ad || { headline: 60, line: 90 };
const name = d.username ? '@' + d.username : VAB.short(d.address);
const boosts = d.boosts || [];
const active = boosts.filter(b => b.status === 'delivering');
$('dHello').textContent = 'Welcome back, ' + name;
$('whoVia').textContent = d.netVerified ? 'Signed in through the network' : 'Signed in with your wallet';
$('whoName').textContent = name; $('whoAddr').textContent = VAB.short(d.address);
// stat cards (lifetime figures of the member's own ad, Boost delivery, contract payouts)
const bv = boosts.reduce((s, b) => s + (b.views || 0), 0), bd = boosts.reduce((s, b) => s + Math.min(b.delivered || 0, b.views || 0), 0);
$('stats').innerHTML = stat('Verified views of your ad', VAB.n(a ? a.views : 0), 'real visitors, once a day each')
+ stat('Verified clicks', VAB.n(a ? a.vclicks : 0), 'never your own', 'red')
+ stat('Boost views delivered', VAB.n(bd), boosts.length ? 'of ' + VAB.n(bv) + ' guaranteed' : 'no Boost yet', 'blue')
+ stat('Paid to your wallet', VAB.polOf(d.earned && d.earned.wei) + ' POL', 'straight from the contract', 'dark');
// the page link
$('pageCard').innerHTML = d.page
? '<div class="hd"><h2>Your page</h2></div><p>Share it anywhere. It shows your ad and the five people above you, and everyone who joins through it shows your ad on their page.</p><p class="mono" style="overflow-wrap:anywhere;margin:0"><a id="pageLink" href="' + esc(d.page) + '" target="_blank" rel="noopener">' + esc(d.page) + '</a></p><div class="row" style="margin:0"><button class="btn-line" type="button" id="copyPage">Copy my link</button><a class="btn-soft" href="/promote">Promo kit</a></div>'
: '<div class="hd"><h2>Your page</h2></div><p>Your page address uses your network username. Sign in through InstantAdPay or LinkSpin once and it is set.</p>';
if ($('copyPage')) $('copyPage').onclick = () => copyText(d.page, $('copyPage'));
$('copyTop').hidden = !d.page; $('copyTop').onclick = () => copyText(d.page, $('copyTop'));
// my text ad: badges, preview, the form (folded behind "Edit ad" once the ad exists)
$('adBadges').innerHTML = a ? (a.status === 'live' ? '<span class="chip live">LIVE</span>' : '<span class="chip idle">' + esc(a.status.toUpperCase()) + '</span>') + (active.length ? ' <span class="chip gold">★ BOOSTED</span>' : '') : '';
$('adPrev').innerHTML = a ? '<div class="adprev' + (active.length ? ' star' : '') + '">' + (active.length ? '<span class="tag">★ BOOSTED</span>' : '') + '<b>' + esc(a.headline) + '</b><span>' + esc(a.line1) + '</span>' + (a.line2 ? '<span>' + esc(a.line2) + '</span>' : '') + '<a href="' + esc(a.link) + '" target="_blank" rel="noopener nofollow">' + esc(a.link.replace(/^https?:\/\//, '').slice(0, 60)) + '</a></div>'
+ lsLine(d.linkspin)
+ '<div class="row" style="margin-top:14px"><button type="button" class="btn-line" id="editAd">Edit ad</button>' + (d.page ? '<a class="btn-soft" href="' + esc(d.page) + '" target="_blank" rel="noopener">Preview my page</a>' : '') + '</div>' : '';
if (!d.netVerified) {
$('adNote').innerHTML = 'Text ads are free for network members. This wallet is not in the network yet: <a href="' + esc(d.joinUrl) + '" target="_blank" rel="noopener">join InstantAdPay free</a>, link this wallet there, then sign in here through InstantAdPay.';
$('adForm').hidden = true; if ($('editAd')) $('editAd').hidden = true;
} else {
$('adForm').hidden = !!a; $('adNote').hidden = !!a;
if ($('editAd')) $('editAd').onclick = () => { const h = !$('adForm').hidden; $('adForm').hidden = h; $('adNote').hidden = h; $('editAd').textContent = h ? 'Edit ad' : 'Close the editor'; if (!h) $('fHead').focus(); };
if (a) { $('adHead').textContent = 'My text ad'; $('fHead').value = a.headline; $('fL1').value = a.line1; $('fL2').value = a.line2 || ''; $('fLink').value = a.link; $('fTrack').checked = a.track !== false; $('adSave').textContent = 'Save changes'; spinAd = a.spin ? a.link : null; }
spinMark();
if (!counted) { counter($('fHead'), $('cHead'), lim.headline); counter($('fL1'), $('cL1'), lim.line); counter($('fL2'), $('cL2'), lim.line); counted = true; }
else ['fHead', 'fL1', 'fL2'].forEach(id => $(id).dispatchEvent(new Event('input')));
}
// the line: the people above, nearest first (their ads show on this member's page)
$('line').innerHTML = (d.line || []).length ? '<p class="small" style="margin:0 0 14px">The people above you in the network, nearest first. Your page shows the first five who have an ad here, and your ad shows on the page of everyone you bring in.</p><div class="lbars">'
+ d.line.map(x => '<div class="lbar' + (x.hasAd ? ' has' : '') + '"><span class="k">PLACE ' + x.depth + '</span><span class="who2"><b>' + (x.username ? '@' + esc(x.username) : 'a member') + '</b><span class="bar"><i style="width:' + (x.hasAd ? 100 : 0) + '%"></i></span></span><span class="pill' + (x.hasAd ? ' on' : '') + '">' + (x.hasAd ? 'ad live' : 'no ad yet') + '</span></div>').join('') + '</div>'
: '<p class="small">' + (d.lineOk === false ? 'The network line could not be read just now. Try again in a minute.' : 'No sponsor line is known for you in the network yet.') + '</p>';
// Boosts: the ring for the Boost being delivered now, then every Boost with its delivery
const ring = b => { const pct = Math.min(100, Math.round((b.delivered || 0) / b.views * 100)); return '<div class="ring"><svg width="96" height="96" viewBox="0 0 36 36" role="img" aria-label="' + VAB.n(b.delivered) + ' of ' + VAB.n(b.views) + ' views delivered"><circle cx="18" cy="18" r="15.9" fill="none" stroke="#23409F" stroke-width="3.5"/><circle cx="18" cy="18" r="15.9" fill="none" stroke="#3D8BFF" stroke-width="3.5" stroke-dasharray="' + pct + ' 100" pathLength="100" transform="rotate(-90 18 18)"/></svg><div><b>' + VAB.n(b.delivered) + ' / ' + VAB.n(b.views) + '</b><span>views delivered on your active Boost</span></div></div>'; };
$('boosts').innerHTML = (boosts.length ? (active.length ? ring(active[active.length - 1]) : '')
+ '<div class="bl">' + boosts.map(b => '<div style="flex-direction:column;align-items:stretch"><div style="display:flex;justify-content:space-between;gap:10px;flex-wrap:wrap"><span>' + VAB.when(b.at) + ' · ' + VAB.n(b.views) + ' guaranteed views</span><span class="pill' + (b.status === 'delivered' ? ' done' : ' on') + '">' + esc(b.status.replace(/-/g, ' ')) + '</span></div>'
+ '<div class="meter"><i style="width:' + Math.min(100, Math.round(b.delivered / b.views * 100)) + '%"></i></div><span>' + VAB.n(b.delivered) + ' of ' + VAB.n(b.views) + ' delivered' + (b.nas ? ' (' + VAB.n(b.nas) + ' through Network Ad Space)' : '') + (b.dueAt && b.status === 'delivering' ? ' · on-site until ' + VAB.when(b.dueAt) : '') + '</span></div>').join('') + '</div>'
+ '<p class="small">Anything not delivered on ViralAdBlitz in ' + (c.deliverDays || 30) + ' days is finished through Network Ad Space.</p>'
: '<h2>No Boost yet</h2><p class="small">A Boost is $' + (c.boost ? c.boost.usd : 5) + ' once and buys ' + VAB.n(c.boost ? c.boost.views : 500) + ' guaranteed network views for your ad, plus a starred spot on the ticker.</p>');
$('boostMore').textContent = boosts.length ? 'Add another Boost, $' + (c.boost ? c.boost.usd : 5) : 'Boost my ad, $' + (c.boost ? c.boost.usd : 5);
// recent activity: real events only (payouts to this wallet, this member's Boosts, the ad going live)
const ev = [];
for (const x of (d.earned && d.earned.items) || []) ev.push({ at: x.at, cls: 'red', h: 'Level ' + x.level + ' paid you ' + VAB.polOf(x.wei) + ' POL', s: (x.buyerName ? '@' + esc(x.buyerName) : 'Someone in your team') + ' bought a Boost' + (x.failed ? ' (waiting in the contract for you to claim)' : '') + ' · ' + vlink(x.tx) });
for (const b of boosts) ev.push({ at: b.at, cls: 'gold', h: 'You bought a Boost', s: VAB.n(b.views) + ' guaranteed views · ' + esc(b.status.replace(/-/g, ' ')) });
if (a && a.createdAt) ev.push({ at: a.createdAt, cls: '', h: 'Your text ad went live', s: 'On your page and your team\'s pages' });
ev.sort((x, y) => (y.at || 0) - (x.at || 0));
$('acts').innerHTML = ev.length ? ev.slice(0, 6).map(e => '<div class="act ' + e.cls + '"><i></i><div><b>' + e.h + '</b><span>' + VAB.when(e.at) + ' · ' + e.s + '</span></div></div>').join('') : '<p class="small">Nothing yet. Post your free text ad to get started.</p>';
// what the contract paid
$('earned').innerHTML = (d.earned && d.earned.items.length) ? d.earned.items.map(x => '<div class="lrow"><span class="small muted">' + VAB.when(x.at) + '</span><span style="flex:1;min-width:140px">Level ' + x.level + ' share from ' + (x.buyerName ? '@' + esc(x.buyerName) : 'a Boost') + (x.failed ? ' (waiting in the contract for you to claim)' : '') + '</span><b class="mono">' + VAB.polOf(x.wei) + ' POL</b><span class="small">' + vlink(x.tx) + '</span></div>').join('')
: '<p class="small">Nothing yet. The contract pays a level share in the same transaction when someone in your team buys a Boost, if you have bought one yourself. Advertising, not income. Nothing is promised.</p>';
}
$('adForm').addEventListener('submit', async e => {
e.preventDefault(); $('adSave').disabled = true; VAB.status('Checking your link...');
const r = await VAB.post('/api/my/ad', { headline: $('fHead').value, line1: $('fL1').value, line2: $('fL2').value, link: $('fLink').value, track: $('fTrack').checked });
$('adSave').disabled = false;
if (r.error) return VAB.status(r.error, 'err');
VAB.status(r.created ? 'Posted. Your ad is live on your page and on your team\'s pages.' : 'Saved. Your ad shows the new text everywhere.', 'ok');
load();
});
await load();
})();