31 lines
1.7 KiB
PHP
31 lines
1.7 KiB
PHP
<footer><a href="https://bubulescu.org"><span class="footer-cap">B</span>ubulescu.<span class="footer-cap">O</span>rg</a></footer>
|
|
<script>
|
|
(function(){
|
|
const btn=document.getElementById('push-toggle');
|
|
if(!btn || !('serviceWorker' in navigator) || !('PushManager' in window) || !('Notification' in window)) return;
|
|
function b64uToUint8Array(s){ const pad='='.repeat((4-s.length%4)%4); const b64=(s+pad).replace(/-/g,'+').replace(/_/g,'/'); const raw=atob(b64); return Uint8Array.from([...raw].map(c=>c.charCodeAt(0))); }
|
|
async function state(reg){ const sub=await reg.pushManager.getSubscription(); btn.classList.toggle('active', !!sub); btn.title=sub?'Notifications on':'Notifications'; btn.hidden=false; return sub; }
|
|
navigator.serviceWorker.register('/sw.js').then(async reg=>{
|
|
const cfg=await fetch('push_config.php',{cache:'no-store'}).then(r=>r.json()).catch(()=>({enabled:false}));
|
|
if(!cfg.enabled || !cfg.publicKey) return;
|
|
await state(reg);
|
|
btn.addEventListener('click', async ()=>{
|
|
btn.disabled=true;
|
|
try{
|
|
let sub=await reg.pushManager.getSubscription();
|
|
if(sub){
|
|
return;
|
|
}else{
|
|
const perm=await Notification.requestPermission();
|
|
if(perm!=='granted') return;
|
|
sub=await reg.pushManager.subscribe({userVisibleOnly:true,applicationServerKey:b64uToUint8Array(cfg.publicKey)});
|
|
await fetch('push_subscribe.php',{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify(sub)});
|
|
}
|
|
await state(reg);
|
|
}catch(e){ alert(e.message || 'Could not update notifications'); }
|
|
finally{ await state(reg); }
|
|
});
|
|
});
|
|
})();
|
|
</script>
|
|
<script src="js/map_lightbox.js?v=20260531b" defer></script>
|