Site Builder
Editing:
geofence-guardok4editonly.js
writable 0666
/* geo/geofence‑guard.js ----------------------------------------------------------- • Looks for boundary.json • Ensures a Locate‑Me button exists (creates one if needed) • When clicked: requests geolocation, checks distance, fires the existing latlonReady event (so locate.js can fill city/state), and finally reveals the edit form. */ (async ()=>{ /* -------- figure out the slug -------- */ const slug = new URLSearchParams(location.search).get('user') || location.pathname.split('/').filter(Boolean).slice(-2,-1)[0]; if(!slug) return; /* -------- UI elements (create if absent) -------- */ const h1 = document.querySelector('h1'); const banner = document.createElement('div'); banner.id = 'geoBanner'; banner.style.cssText = 'font-weight:700;margin:.8rem 0;'; h1?.after(banner); const form = document.getElementById('editForm'); let btn = document.getElementById('locateBtn'); if(!btn){ btn = document.createElement('button'); btn.id = 'locateBtn'; btn.textContent = 'Locate Me to Edit'; btn.style.cssText = 'padding:.6rem 1.4rem;font-weight:700;border:none;border-radius:8px;' + 'background:#4caf50;color:#fff;margin:1rem 0;cursor:pointer'; banner.after(btn); } /* -------- fetch boundary.json -------- */ let zone = null; try{ const r = await fetch(`/social/${slug}/boundary.json`, {cache:'no-cache'}); if(r.ok) zone = await r.json(); }catch{} if(!zone){ banner.textContent = '🔒 This account is locked – no geofence found.'; form?.remove(); btn.remove(); return; } banner.textContent = '🛰️ Geofence enabled – click “Locate Me to Edit”.'; if(form) form.style.display='none'; /* -------- click handler -------- */ btn.addEventListener('click', ()=>{ navigator.geolocation.getCurrentPosition(pos=>{ const dist = hav(pos.coords.latitude, pos.coords.longitude, zone.lat, zone.lon); if(dist <= zone.radius_km){ banner.textContent = '✅ Location verified – you may edit.'; if(form) form.style.display=''; /* ---- expose coords to existing locate.js, if present ---- */ const latInput = document.getElementById('lat'); const lonInput = document.getElementById('lon'); if(latInput && lonInput){ latInput.value = pos.coords.latitude.toFixed(6); lonInput.value = pos.coords.longitude.toFixed(6); /* fire the custom event used by your original script */ document.dispatchEvent(new Event('latlonReady')); } }else{ banner.textContent = '❌ You are outside the allowed area.'; } }, err=>{ banner.textContent = '❌ Unable to get your location.'; }, {enableHighAccuracy:false,timeout:8000}); }); /* -------- distance helper -------- */ 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