Site Builder
Editing:
geofence-guardoook.js
writable 0666
/* geo/geofence‑lock.js – zero‑markup universal version ------------------------------------------------------ Load AFTER /geo/locate.js on any page that should be protected. */ (function(){ /* ---- helper to pull slug ---- */ const slug = (()=>{ const qs = new URLSearchParams(location.search).get('user'); if(qs) return qs.replace(/[^a-z0-9_]/ig,''); const parts = location.pathname.split('/').filter(Boolean); const i = parts.lastIndexOf('social'); return i!==-1 && parts[i+1] ? parts[i+1] : ''; })(); if(!slug) return; /* ---- lock the UI immediately ---- */ const lockCss = ` 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;text-align:center; } #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 }`; const style = document.createElement('style'); style.textContent = lockCss; const cover = document.createElement('div'); cover.id='geoLockOverlay'; cover.innerHTML = `<h2>🔒 Loading geofence…</h2>`; document.head.appendChild(style); document.body.appendChild(cover); /* ---- fetch boundary.json ---- */ fetch(`/social/${slug}/boundary.json`,{cache:'no-cache'}) .then(r=>r.ok?r.json():null) .then(zone=>{ if(!zone){ cover.querySelector('h2').textContent = '🔒 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(pos=>{ const d=hav(pos.coords.latitude,pos.coords.longitude,zone.lat,zone.lon); if(d<=zone.radius_km){ unlock(pos.coords); }else{ cover.querySelector('h2').textContent = '❌ You are outside the allowed area.'; } },err=>{ cover.querySelector('h2').textContent = '❌ Unable to get location ('+err.message+')'; },{timeout:8000}); }); /* ---- unlock routine ---- */ function unlock(coords){ /* update hidden lat/lon fields if present */ const lat=document.querySelector('input[name="lat"]'); const lon=document.querySelector('input[name="lon"]'); if(lat&&lon){ lat.value=coords.latitude.toFixed(6); lon.value=coords.longitude.toFixed(6); document.dispatchEvent(new Event('latlonReady')); } /* enable everything that was disabled */ document.querySelectorAll('form,input,button,select,textarea,a') .forEach(el=>el.removeAttribute('disabled')); /* remove overlay & restore scroll */ cover.remove(); style.remove(); document.documentElement.style.overflow=''; document.body.style.overflow=''; } /* ---- haversine km ---- */ 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