Site Builder
Editing:
geofence-guard.js
writable 0666
/* geo/geofence‑lock.js (universal: slug OR 10‑digit phone) */ (function(){ /* ---------- detect target & build path ------------------ */ let slug = '', path = ''; const qs = new URLSearchParams(location.search); const user = qs.get('user'); const ph = qs.get('ph'); /* 1) explicit query‑string first */ if(user && /^[a-z0-9_]+$/i.test(user)){ slug = user.replace(/[^a-z0-9_]/ig,''); path = `/social/${slug}/boundary.json`; }else if(ph && /^\d{10}$/.test(ph)){ slug = ph; /* phone folder */ path = `/ph/${slug}/boundary.json`; }else{ /* 2) fallback: infer from pathname */ const parts = location.pathname.split('/').filter(Boolean); const idxSocial = parts.indexOf('social'); const idxPh = parts.indexOf('ph'); if(idxSocial!==-1 && parts[idxSocial+1]){ slug = parts[idxSocial+1].toLowerCase(); if(/^[a-z0-9_]+$/.test(slug)) path = `/social/${slug}/boundary.json`; }else if(idxPh!==-1 && /^\d{10}$/.test(parts[idxPh+1])){ slug = parts[idxPh+1]; path = `/ph/${slug}/boundary.json`; } } if(!path){ lockForever('🔒 Account Locked.'); return; } /* ---------- build overlay & lock UI --------------------- */ const style = document.createElement('style'); style.textContent = ` html,body{overflow:hidden!important} #geoLockOverlay{position:fixed;inset:0;background:#f5f8fb; display:flex;flex-direction:column;justify-content:center; align-items:center;z-index:99999;font-family:system-ui,Arial,sans-serif} #geoLockOverlay h2{margin:0 0 .9rem;font-size:1.35rem;color:#1a3260} #geoLockOverlay button{padding:.6rem 1.5rem;font-weight:700;border:none; border-radius:8px;background:#4caf50;color:#fff;cursor:pointer;font-size:1.05rem}`; document.head.appendChild(style); const cover = document.createElement('div'); cover.id='geoLockOverlay'; cover.innerHTML='<h2>🔒 Loading geofence…</h2>'; document.body.appendChild(cover); /* ---------- fetch boundary ------------------------------ */ fetch(path,{cache:'no-cache'}) .then(r=>r.ok?r.json():null) .then(z=>{ if(!z){ lockForever('🔒 This account is locked.'); return;} cover.querySelector('h2').textContent = '🛰️ Geofence enabled – click below to unlock.'; const btn=document.createElement('button'); btn.textContent='Locate Me to Unlock'; cover.appendChild(btn); btn.onclick=()=>navigator.geolocation.getCurrentPosition(p=>{ const d=hav(p.coords.latitude,p.coords.longitude,z.lat,z.lon); if(d<=z.radius_km) unlock(p.coords); else cover.querySelector('h2').textContent='❌ Outside allowed area.'; },e=>cover.querySelector('h2').textContent='❌ '+e.message,{timeout:8000}); }); /* ---------- helpers ------------------------------------- */ function lockForever(msg){ cover.innerHTML='<h2>'+msg+'</h2>'; } function unlock(c){ /* push coords into any hidden fields & fire event */ const la=document.querySelector('input[name="lat"]'); const lo=document.querySelector('input[name="lon"]'); if(la&&lo){ la.value=c.latitude.toFixed(6); lo.value=c.longitude.toFixed(6); document.dispatchEvent(new Event('latlonReady')); } cover.remove(); style.remove(); document.documentElement.style.overflow=''; document.body.style.overflow=''; /* re‑enable disabled controls */ document.querySelectorAll('[disabled]').forEach(el=>el.removeAttribute('disabled')); } function hav(a,b,c,d){ const R=6371,rad=x=>x*Math.PI/180; const dLa=rad(c-a), dLo=rad(d-b); const s=Math.sin(dLa/2)**2 + Math.cos(rad(a))*Math.cos(rad(c))*Math.sin(dLo/2)**2; return 2*R*Math.atan2(Math.sqrt(s),Math.sqrt(1-s)); } })();
Save changes
Create folder
writable 0777
Create
Cancel