UNPKG

spy-client

Version:

spy client

2 lines (1 loc) 5.07 kB
((e,n)=>{"object"==typeof exports&&"undefined"!=typeof module?module.exports=n():"function"==typeof define&&define.amd?define(n):(e="undefined"!=typeof globalThis?globalThis:e||self).__spyHead=n()})(this,function(){function E(e){var n,r=(e=>{if(URL&&e)try{var n=new URL(e);if(void 0!==n.host)return{protocol:n.protocol,host:n.host,pathname:n.pathname,ext:""}}catch(e){console.error(e)}})(e),e=(r||((n=document.createElement("a")).href=e,r={protocol:n.protocol,host:n.host||location.host,pathname:n.pathname,ext:""}),r.pathname.split("."));return r.ext=e[e.length-1],r}function n(e){return+e.toFixed(1)}var T={conf:{},winerrors:[],errorDestroy:function(){},observerDestroy:function(){},entryMap:{},init:function(e){this.conf=e},addError:function(e){if(0<this.winerrors.length){var n=this.winerrors[this.winerrors.length-1];if(e.info.msg===n.info.msg)return void(n.info.count+=n.info.count||0)}this.winerrors.length<1e3&&this.winerrors.push(e)},send:function(e,n,r){var t,o=this.conf;e.type=e.type||"except",e.pid=o.pid,e.lid=o.lid,e.ts=Date.now(),this.addError(e),this.interceptor&&this.interceptor(e),!1!==n&&(r=r||o.logServer,n="".concat(r,"?pid=").concat(e.pid,"&lid=").concat(e.lid,"&ts=").concat(e.ts)+"&type=".concat(e.type,"&group=").concat(e.group,"&info=").concat(encodeURIComponent(JSON.stringify(e.info))),e.dim&&(n+="&dim="+encodeURIComponent(JSON.stringify(e.dim))),(t=new Image).src=n,t.onload=t.onerror=function(){t=null})}};function r(e){var h=e.resourceError||{},m=e.jsError||{},g=Math.random()<(m.sample||0),w=Math.random()<(h.sample||0),v=T.winerrors,y=0;function n(e){try{var n=e.target,r={info:{},dim:{},group:""},t=r.info,o=e.srcElement,i=navigator.connection||{};if(t.downlink=i.downlink,t.effectiveType=i.effectiveType,t.rtt=i.rtt,t.deviceMemory=navigator.deviceMemory||0,t.hardwareConcurrency=navigator.hardwareConcurrency||0,o===window){r.group=m.group;for(var a,s=e.error||e.reason||{},c=(t.msg=e.message||s.message||"",t.file=e.filename,t.ln=e.lineno,t.col=e.colno,t.stack=(s.stack||"").split("\n").slice(0,3).join("\n"),-1!==t.msg.indexOf("MODULE_TIMEOUT")&&(a=t.msg.match(/^.*Hang:(.*); Miss:(.*)/))&&a[2]&&(t.msg="MODULE_TIMEOUT for miss:"+a[2]),[]),d=0;d<v.length;d++){var f=v[d],p=1<f.info.count?"(".concat(f.info.count,")"):"";c.push(p+f.info.msg)}t.hisErrors=c.join("----");var u=!0;!1!==(u=m.handler?m.handler(r):u)&&T.send(r,g)}else{r.group=h.group,r.dim.type=o.tagName.toLowerCase();var l=o.src||o.href||"";-1<l.indexOf("/mwb2.gif?")||(t.msg=l||"unknown load eror",r.dim.host=E(l).host,n&&"IMG"===n.tagName&&(t.xpath=(e=>{if(!e)return{xpath:""};for(var n=[];e&&1===e.nodeType&&e!==e.parentNode;){var r=e.tagName.toLowerCase();if(e.getAttribute("id")?r+="[#"+e.getAttribute("id")+"]":e.classList&&e.classList.length&&(r+="[."+e.classList[e.classList.length-1]+"]"),n.push(r),e===document.body)break;e=e.parentNode}return{xpath:n.join("<")}})(n).xpath),y&&(t.hisErrCount=y),!(u=!0)!==(u=h.handler?h.handler(r):u)&&T.send(r,w),y++)}}catch(e){console.error(e)}}window.addEventListener("error",n,!0),window.addEventListener("unhandledrejection",n,!0),T.errorDestroy=function(){window.removeEventListener("error",n,!0),window.removeEventListener("unhandledrejection",n,!0),T.winerrors=[]}}function t(e){var t=e.whiteScreenError||{},o=t.handler,i=t.selector,a=t.subSelector,e=t.timeout||6e3,s=Math.random()<(t.sample||0);function c(){var e;return!!window.performance&&(e={wait:n((e=window.performance.timing).domainLookupStart-(e.navigationStart||e.fetchStart||e.startTime)),dns:n(e.domainLookupEnd-e.domainLookupStart),connect:n(e.connectEnd-e.connectStart),req:n(e.responseStart-e.requestStart),res:n(e.responseEnd-e.responseStart)},"&wait=".concat(e.wait)+"&dns=".concat(e.dns)+"&connect=".concat(e.connect)+"&requestTime=".concat(e.req)+"&resoneTime=".concat(e.res))}i&&setTimeout(function(){var e,n,r;(r=document.querySelector(i))&&r.querySelector(a)&&!(r.clientHeight<2*window.innerHeight/3)||(r=!0,!((n={group:t.group,info:{msg:"",netTime:c(),hisErrors:(()=>{if(!T.winerrors)return!1;for(var e=T.winerrors,n=[],r=0;r<e.length;r++){var t=(e[r].info.stack||"").split("\n")[0];n.push("(".concat(r,")").concat(t||e[r].info.msg))}return n.join(";;")})(),deviceInfo:(e={},n=navigator.connection||{},e.downlink=n.downlink,e.effectiveType=n.effectiveType,e.rtt=n.rtt,e.deviceMemory=navigator.deviceMemory||0,e.hardwareConcurrency=navigator.hardwareConcurrency||0,e)}}).info.msg="WhiteScren Error")!==(r=o?o(n):r)&&n.info.msg&&T&&T.send(n,s))},e)}return T.init=function(e){if(e.logServer||(e.logServer="https://sp1.baidu.com/5b1ZeDe5KgQFm2e88IuM_a/mwb2.gif"),r(this.conf=e),window.PerformanceObserver){var n=new window.PerformanceObserver(function(e){for(var n=T.entryMap,r=e.getEntries(),t=0;t<r.length;t++){var o=r[t];n[o.entryType]||(n[o.entryType]=[]),n[o.entryType].push(o)}});T.observerDestroy=function(){n.disconnect()};try{n.observe({entryTypes:["longtask","layout-shift","first-input","largest-contentful-paint"]})}catch(e){}}t(e)},window.__spyclientConf&&T.init(window.__spyclientConf),window.__spyHead=window.__spyHead||T,T});