'+(v||'—')+'
نمایش نقشه در این لحظه ممکن نشد. مختصات: '+(+lat).toFixed(4)+', '+(+lng).toFixed(4)+'
مشاهده روی نقشهٔ OpenStreetMap
'; } }, ۴۵۰۰);
} else { $('ipl-map-head').style.display='none';$('ipl-map-box').style.display='none'; }$('ipl-results').classList.add('show');
$('ipl-fail').classList.remove('show');
saveHistory(d,ip);
renderHistory();
}/* ── search flow ── */
var ipInput=$('ipl-ip'),errMsg=$('ipl-err-msg'),btn=$('ipl-search-btn');
function doSearch(){
var ip=ipInput.value.trim();
if(!isValidIP(ip)){ ipInput.classList.add('ipl-invalid'); errMsg.classList.add('show'); return; }
ipInput.classList.remove('ipl-invalid'); errMsg.classList.remove('show');
btn.disabled=true; var orig=btn.innerHTML; btn.innerHTML='در حال جستجو…';
$('ipl-fail').classList.remove('show');
getSources(ip,$('ipl-src').value).then(function(sources){
btn.disabled=false; btn.innerHTML=orig;
if(!sources.length){ showFail('دریافت اطلاعات این آیپی ممکن نشد. ممکن است سرویس مکانیابی موقتاً در دسترس نباشد یا اتصال شما مسدود باشد. کمی بعد دوباره تلاش کنید.'); return; }
render(sources,ip);
try{ $('ipl-results').scrollIntoView({behavior:'smooth',block:'nearest'}); }catch(e){}
}).catch(function(){ btn.disabled=false; btn.innerHTML=orig; showFail('خطا در دریافت اطلاعات. لطفاً دوباره تلاش کنید.'); });
}
function showFail(msg){ $('ipl-fail').textContent=msg; $('ipl-fail').classList.add('show'); $('ipl-results').classList.remove('show'); }
btn.addEventListener('click',doSearch);
ipInput.addEventListener('keydown',function(e){ if(e.key==='Enter') doSearch(); });
ipInput.addEventListener('input',function(){ if(ipInput.classList.contains('ipl-invalid')&&isValidIP(ipInput.value.trim())){ ipInput.classList.remove('ipl-invalid'); errMsg.classList.remove('show'); } });// example chips
root.querySelectorAll('.ipl-ex b[data-ip]').forEach(function(b){ b.addEventListener('click',function(){ ipInput.value=b.getAttribute('data-ip'); doSearch(); }); });// my IP (same-origin trace)
$('ipl-mine').addEventListener('click',function(){
var b=this; var o=b.innerHTML; b.innerHTML='…';
fetch('/cdn-cgi/trace',{cache:'no-store'}).then(function(r){return r.text();}).then(function(txt){
var m=txt.match(/(^|\n)ip=([^\n]+)/); b.innerHTML=o;
if(m){ ipInput.value=m[۲].trim(); doSearch(); } else fallbackMine(b);
}).catch(function(){ b.innerHTML=o; fallbackMine(b); });
});
function fallbackMine(){ fetchJSON('https://ipwho.is/',۶۰۰۰).then(function(d){ if(d&&d.ip){ ipInput.value=d.ip; doSearch(); } }).catch(function(){}); }/* ── copy ── */
function copyText(txt,btn,label){
var ok=function(){ var o=btn.innerHTML; btn.classList.add('copied'); if(label){btn.innerHTML=label;} setTimeout(function(){btn.classList.remove('copied'); if(label){btn.innerHTML=o;}},۱۳۰۰); };
if(navigator.clipboard&&navigator.clipboard.writeText){ navigator.clipboard.writeText(txt).then(ok,function(){fb();ok();}); } else { fb(); ok(); }
function fb(){ try{var ta=document.createElement('textarea');ta.value=txt;ta.style.position='fixed';ta.style.opacity='۰';document.body.appendChild(ta);ta.select();document.execCommand('copy');document.body.removeChild(ta);}catch(e){} }
}
$('ipl-sum-copy').addEventListener('click',function(){ if(current.ip) copyText(current.ip,this); });
$('ipl-copy-all').addEventListener('click',function(){
var d=current; var lines=[
'IP: '+(d.ip||''),'کشور: '+countryFa(d.countryCode,d.country),'استان: '+(d.region||''),'شهر: '+(d.city||''),
'کد پستی: '+(d.postal||''),'مختصات: '+((d.lat!=null?d.lat:'')+', '+(d.lng!=null?d.lng:'')),'منطقهٔ زمانی: '+(d.tz||''),
'ASN: '+(d.asn||''),'سازمان: '+(d.org||''),'ISP: '+(d.isp||''),'https://negarnovin.com/iplocation/'];
copyText(lines.join('\n'),this,'کپی شد ✓');
});/* ── history (localStorage) + export ── */
var HKEY='nnipl_history_v1';
function loadHist(){ try{return JSON.parse(localStorage.getItem(HKEY)||'[]');}catch(e){return [];} }
function saveHistory(d,ip){ try{ var h=loadHist(); h.unshift({t:Date.now(),ip:d.ip||ip,country:countryFa(d.countryCode,d.country),city:d.city||'',isp:d.isp||''}); h=h.slice(۰,۱۵); localStorage.setItem(HKEY,JSON.stringify(h)); }catch(e){} }
function renderHistory(){
var h=loadHist();
if(!h.length){ $('ipl-hist-empty').style.display='block'; $('ipl-hist-table-wrap').style.display='none'; return; }
$('ipl-hist-empty').style.display='none'; $('ipl-hist-table-wrap').style.display='block';
$('ipl-hist-body').innerHTML=h.map(function(r){
var ds; try{ds=new Date(r.t).toLocaleString('fa-IR',{month:'۲-digit',day:'۲-digit',hour:'۲-digit',minute:'۲-digit'});}catch(e){ds=new Date(r.t).toLocaleString();}
return 'مشاهده روی نقشهٔ OpenStreetMap
