blind-monkeys / static /alerts.js
deanpowers's picture
fix(alerts): constrain banner width to 1100px, center horizontally
4788f5b
Raw History Blame Contribute Delete
3.65 kB
(function(){
function esc(s){return String(s==null?'':s).replace(/[&<>"']/g,function(c){return{'&':'&amp;','<':'&lt;','>':'&gt;','"':'&quot;',"'":'&#39;'}[c];});}
function injectStyle(){
if (document.getElementById('hsAlertStyle')) return;
var s = document.createElement('style');
s.id = 'hsAlertStyle';
s.textContent = '#hsAlertArea { background:#3d1f00; border:1px solid #f0b90b; border-radius:6px; padding:4px 10px; margin:6px auto; max-width:1100px; box-sizing:border-box; } #hsAlertArea .hs-alert-item { display:flex; justify-content:space-between; align-items:center; color:#f0b90b; font-size:13px; padding:2px 0; line-height:1.2; } #hsAlertArea .hs-alert-item .hs-time { color:#8b949e; font-size:11px; margin-left:8px; } #hsAlertArea .hs-dismiss { background:transparent; border:1px solid #f0b90b; color:#f0b90b; border-radius:4px; width:20px; height:20px; cursor:pointer; font-size:11px; line-height:1; padding:0; } #hsAlertArea .hs-dismiss:hover { background:#f0b90b; color:#0b0e11; }';
document.head.appendChild(s);
}
function findAnchor(){
var sels = ['#market-panel','.market-panel','[id*="market-panel"]','[class*="market-panel"]','.cards-row','#marketData','.market-data-panel','[class*="market-data"]'];
for (var i=0;i<sels.length;i++){
try { var el = document.querySelector(sels[i]); if (el) return el; } catch(_){}
}
return null;
}
function ensureContainer(){
var el = document.getElementById('hsAlertArea');
if (el) return el;
el = document.createElement('div');
el.id = 'hsAlertArea';
el.style.display = 'none';
var anchor = findAnchor();
if (anchor && anchor.parentNode) anchor.parentNode.insertBefore(el, anchor.nextSibling);
else document.body.insertBefore(el, document.body.firstChild);
return el;
}
function fmtTime(ts){
if (!ts) return '';
var t = Number(ts) * 1000;
if (!t) return '';
var diff = Math.floor((Date.now() - t) / 1000);
if (diff < 60) return diff + 's ago';
if (diff < 3600) return Math.floor(diff/60) + 'm ago';
if (diff < 86400) return Math.floor(diff/3600) + 'h ago';
return Math.floor(diff/86400) + 'd ago';
}
function poll(){
injectStyle();
var el = ensureContainer();
if (!el) return;
fetch('/api/alerts', {cache:'no-store'}).then(function(r){return r.json();}).then(function(d){
if (d && d.banner_enabled === false){ el.style.display='none'; el.innerHTML=''; return; }
var list = (d && d.alerts) || [];
if (!list.length){ el.style.display='none'; el.innerHTML=''; return; }
// Sort most recent first, cap at 10
list = list.slice().sort(function(a,b){ return (b.ts||0) - (a.ts||0); }).slice(0, 10);
el.style.display = 'block';
el.innerHTML = list.map(function(a){
var s = esc(a.symbol||''); var sc = esc(a.score||''); var m = esc(a.mode||'');
var tm = esc(fmtTime(a.ts));
return '<div class="hs-alert-item"><span>\uD83D\uDEA8 <strong>'+s+'</strong> scored '+sc+' <span style="opacity:.6">('+m+')</span><span class="hs-time">'+tm+'</span></span><button class="hs-dismiss" data-sym="'+s+'">\u00d7</button></div>';
}).join('');
el.querySelectorAll('.hs-dismiss').forEach(function(b){
b.onclick = function(){
fetch('/api/alerts/dismiss',{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({symbol:b.dataset.sym})}).then(function(){poll();});
};
});
}).catch(function(){});
}
if (document.readyState === 'loading') document.addEventListener('DOMContentLoaded', poll);
else poll();
setInterval(poll, 60000);
})();