UNPKG

ip-banner

Version:

A lightweight and extensible JavaScript library for displaying the public IP address in a fixed banner at the top of the browser

20 lines (19 loc) 2.23 kB
(()=>{var d=(p,r)=>()=>(r||p((r={exports:{}}).exports,r),r.exports);var m=d(()=>{(function(p){let r={backgroundColor:"#003366",textColor:"#ffffff",iconUrl:"",description:"Your public IP:",height:"2.5em",top:"0",zIndex:9999,ipPosition:"suffix",ipSpacing:8,iconSpacing:8,fontSize:"14px"};function c(a,t){let n={...a};for(let i in t)t[i]!=null&&(n[i]=t[i]);return n}function l(a={}){let t=c(r,a),n=document.createElement("div");if(n.id="ip-banner",Object.assign(n.style,{position:"fixed",top:t.top,left:0,width:"100%",height:t.height,lineHeight:t.height,backgroundColor:t.backgroundColor,color:t.textColor,fontFamily:t.fontFamily,display:"flex",alignItems:"center",justifyContent:"center",padding:"0 1em",boxSizing:"border-box",zIndex:t.zIndex}),n.style.cssText+=` position: fixed !important; top: ${t.top} !important; left: 0 !important; width: 100% !important; height: ${t.height} !important; line-height: ${t.height} !important; background-color: ${t.backgroundColor} !important; color: ${t.textColor} !important; font-family: ${t.fontFamily} !important; font-weight: 500 !important; display: flex !important; align-items: center !important; justify-content: center !important; padding: 0 1em !important; box-sizing: border-box !important; z-index: ${t.zIndex} !important; font-size: ${t.fontSize} !important; `,t.iconUrl){let o=document.createElement("img");o.src=t.iconUrl,o.style.height="1.2em",o.style.marginRight=t.iconSpacing+"px",n.appendChild(o)}let i=document.createElement("span");i.textContent=t.description,i.style.setProperty("color",t.textColor,"important");let e=document.createElement("span");e.textContent="Loading\u2026",e.style.setProperty("color",t.textColor,"important"),t.ipPosition==="prefix"?(e.style.marginRight=t.ipSpacing+"px",n.appendChild(e),n.appendChild(i)):(n.appendChild(i),i.style.marginRight=t.ipSpacing+"px",n.appendChild(e)),document.body.style.paddingTop=t.height,document.body.insertBefore(n,document.body.firstChild),fetch("https://api.ipify.org?format=json").then(o=>o.json()).then(o=>{e.textContent=o.ip}).catch(()=>{e.textContent="Error"})}p.initIPBanner=l})(window)});m();})();