UNPKG

@lpjs/kuririn

Version:

前端业务跟踪错误上报注入

2 lines (1 loc) 5.68 kB
var Kuririn=function(){"use strict";var n=function(){return(n=Object.assign||function(t){for(var e,r=1,n=arguments.length;r<n;r++)for(var o in e=arguments[r])Object.prototype.hasOwnProperty.call(e,o)&&(t[o]=e[o]);return t}).apply(this,arguments)};var a="error",u="match.response",r="action";function o(){if(!1=="performance"in window)return{};var t=performance.timing,e=t.navigationStart;return{dnsTime:t.domainLookupEnd-t.domainLookupStart,tcpTime:t.connectEnd-t.connectStart,firstPaintTime:t.responseStart-e,domRenderTime:t.domContentLoadedEventEnd-e,loadTime:t.loadEventEnd-e}}var i="onpopstate"in window,s=["error","userinfo","performance","action"],c={qs:function(t){void 0===t&&(t={});var e=n({},t);return Object.keys(e).map(function(t){return~Object.prototype.toString.call(e[t]).search(/Array|Object/)&&(e[t]=JSON.stringify(e[t])),encodeURIComponent(t)+"="+encodeURIComponent(e[t])}).join("&")},resetHistory:function(r){var n=history[r];return function(){var t=n.apply(this,arguments),e=new Event(r);return e.arguments=arguments,window.dispatchEvent(e),t}}};function t(t,e){var r=this;this.debug=!1,this.modules=["error","userinfo","action"],this.userinfo={url:"",history:[],title:"",agent:navigator.userAgent,platform:navigator.platform},this.performance={},this.reportUrl="http://api-s1.dynv6.net:10010/kuririn/test",this.injectAPI=function(){return{}},this.matchAPI={},i&&(window.history.pushState=c.resetHistory("pushState"),window.history.replaceState=c.resetHistory("replaceState"),window.addEventListener("pushState",this.func_onPopstate.bind(this),!1),window.addEventListener("replaceState",this.func_onPopstate.bind(this),!1),window.addEventListener("popstate",this.func_onPopstate.bind(this),!1)),document.addEventListener("DOMContentLoaded",function(){document.body.addEventListener("DOMSubtreeModified",r.func_actionAddEventListener.bind(r))}),this.appId=t.appId,this.appName=t.appName,t.debug&&(this.debug=t.debug),t.injectAPI&&(this.injectAPI=t.injectAPI),t.matchAPI&&(this.matchAPI=t.matchAPI),t.modules&&(this.modules=t.modules.filter(function(t){return~s.indexOf(t)})),t.reportUrl&&(this.reportUrl=t.reportUrl),e&&(this.userinfo=n(n({},this.userinfo),e)),this.mod_logicTracker(),this.modules.map(function(t){r["mod_"+t].apply(r,[])})}return t.prototype.func_log=function(){for(var t=[],e=0;e<arguments.length;e++)t[e]=arguments[e];this.debug&&console.log.apply(console,t)},t.prototype.func_onError=function(t){var r,e=this;return r=t,new Promise(function(e){setTimeout(function(){var t={filename:r.filename,lineno:r.lineno||0,colno:r.colno||0,message:r.message||"无法获取详细错误信息"};r.error&&r.error.stack&&(t.message=r.error.stack.toString()),e({type:a,reason:"js file",error:t})},10)}).then(function(t){return e.func_report(t)}),!1},t.prototype.func_onPopstate=function(t){var e,r;~this.modules.indexOf("userinfo")&&(this.userinfo=(e=t.type,""!==(r=this.userinfo).url&&("pushState"===e?r.history.unshift(r.url):"replaceState"===e?r.history[0]=r.url:r.history[0]===document.URL?r.history=r.history.slice(1,5):r.history.unshift(r.url),r.history=r.history.slice(0,5)),r.url=document.URL,r.title=document.title,r)),~this.modules.indexOf("performance")&&(this.performance=o()),this.modules.indexOf("action"),this.func_log("kuririn debug:",n(n({},this),{injectAPI:this.injectAPI()}))},t.prototype.func_actionAddEventListener=function(){var e=this;this._actionTimer&&clearTimeout(this._actionTimer),this._actionTimer=setTimeout(function(){var t=document.querySelectorAll("[krin-eid]");Array.prototype.forEach.call(t,function(t){t.addEventListener("click",function(){e.func_report({type:r,event:{id:t.getAttribute("krin-eid"),name:t.getAttribute("krin-ename")}})})})},100)},t.prototype.func_report=function(t){var e=n({appId:this.appId,appName:this.appName,userinfo:this.userinfo},t);this.func_log("kuririn report:",e);var r=new XMLHttpRequest;r.open("POST",this.reportUrl,!0),r.setRequestHeader("Content-Type","application/json"),r.send(JSON.stringify(e))},t.prototype.func_injectXHR=function(){var t="",e="",r=window.XMLHttpRequest.prototype.open,n=window.XMLHttpRequest.prototype.send,o=this.injectAPI(),i="string"==typeof o?c.qs({_krin_:o}):c.qs(this.injectAPI()),s=this;window.XMLHttpRequest.prototype.open=function(){e=arguments[0],(t=arguments[1])!==s.reportUrl&&i&&(t+=-1!==t.indexOf("?")?"&":"?",arguments[1]=t+=i),r.apply(this,[].slice.call(arguments))},window.XMLHttpRequest.prototype.send=function(){var o={url:t,method:e,status:"",res:"",data:arguments[0]||{}};this.addEventListener("readystatechange",function(){if(o.url!==s.reportUrl&&4===this.readyState){if(o.res=this.response,o.status=this.status,400<=this.status)return s.func_report({type:a,reason:"xhr status >= 400",error:o});try{var n=JSON.parse(o.res);Object.keys(s.matchAPI).map(function(t){var e=s.matchAPI[t].map(function(t){return String(t)}),r=String(n[t]);n[t]&&-1!==e.indexOf(r)&&s.func_report({type:u,match:t+"="+r,response:o})})}catch(t){s.func_report({type:a,reason:"xhr response json parse",error:o})}}}),this.addEventListener("error",function(){o.url!==s.reportUrl&&s.func_report({type:a,reason:"xhr send error",error:o})}),n.apply(this,[].slice.call(arguments))}},t.prototype.mod_logicTracker=function(){this.func_log("kuririn: mod_logicTracker"),this.func_injectXHR()},t.prototype.mod_action=function(){this.func_log("kuririn: mod_action")},t.prototype.mod_error=function(){window.addEventListener("error",this.func_onError.bind(this),!1)},t.prototype.mod_userinfo=function(){this.func_log("kuririn: mod_userinfo"),this.userinfo.agent=navigator.userAgent},t.prototype.mod_performance=function(){this.func_log("kuririn: mod_performance"),this.performance=o()},t}();