UNPKG

bmfe_monitor_tool

Version:

前端监测平台

2 lines (1 loc) 9.44 kB
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("fingerprintjs2")):"function"==typeof define&&define.amd?define(["exports","fingerprintjs2"],e):e((t=t||self).BmfeMonitor={},t.Fingerprint2)}(this,function(t,f){"use strict";function a(t,e){var n,r,o,a,i,s,d=(/#(\/\w*.*)*/.exec(location.href)||[])[1],c=Object.assign({},e,{locationHref:d,project:t.project||"unnamed",fingerId:t.fingerId,extra:t.extra||{}});if("onload"!==e.type){var u=(n=t,r=d,i="runtimeError"===(a=(o=e).type)?o.urlFirstStack+o.message:"xhrError"===a?o.url.split("?")[0]+o.method+o.status:"xhrTimeout"===a?o.url.split("?")[0]+o.method+"timeout":"resourceError"===a?o.sourceUrl:"unhandledrejection"===a?o.reason:void 0,s={project:n.project,locationHref:r,feature:i},f.x64hash128(JSON.stringify(s),31));c.eventId=u}return c}function i(n,r,o){f.get({excludes:{webgl:!0}},function(t){var e=t.map(function(t){return t.value});n.fingerId=f.x64hash128(e.join(""),31),o(a(n,r))})}function e(){this.baseConfig={project:"",reportUrl:"",fingerId:"",rate:1}}f=f&&f.hasOwnProperty("default")?f.default:f,e.prototype.init=function(t){this.baseConfig=Object.assign(this.baseConfig,t)},e.prototype.send=function(t){if(!(Math.random()>this.baseConfig.rate)){var n,r,o=this.baseConfig.reportUrl||"http://jartto.wang/report";(n=this.baseConfig,r=t,new Promise(function(t,e){n.fingerId&&t(a(n,r)),window.requestIdleCallback?requestIdleCallback(function(){i(n,r,t)}):setTimeout(function(){i(n,r,t)},500)})).then(function(t){var e=new XMLHttpRequest;e.sendByBM=!0,e.open("POST",o),e.setRequestHeader("Content-Type","application/json"),e.send(JSON.stringify(t))})}};var l=window.performance||window.mozPerformance||window.msPerformance||window.webkitPerformance,n=null,r=20;function o(){return new Promise(function(t,e){l&&l.getEntriesByType?function c(u){clearTimeout(n);--r;r<0&&u();n=setTimeout(function(){var t={},e={},n=l.getEntriesByType("paint"),r=!0,o=!1,a=void 0;try{for(var i,s=n[Symbol.iterator]();!(r=(i=s.next()).done);r=!0){var d=i.value;t[d.name]=d.startTime+d.duration}}catch(t){o=!0,a=t}finally{try{r||null==s.return||s.return()}finally{if(o)throw a}}t["first-paint"]&&t["first-contentful-paint"]?(e.fp=Math.round(t["first-paint"]),e.fcp=Math.round(t["first-contentful-paint"]),e.whiteScreen=Math.round(t["first-contentful-paint"]-t["first-paint"]),u(e)):c(u)},100)}(t):e()})}var s=["navigationStart","unloadEventStart","unloadEventEnd","redirectStart","redirectEnd","fetchStart","domainLookupStart","domainLookupEnd","connectStart","connectEnd","secureConnectionStart","requestStart","responseStart","responseEnd","domLoading","domInteractive","domContentLoadedEventStart","domContentLoadedEventEnd","domComplete","loadEventStart","loadEventEnd"],d={pervPage:{desc:"上一个页面",start:"fetchStart",end:"navigationStart"},redirect:{desc:"页面重定向",start:"redirectEnd",end:"redirectStart"},appCache:{desc:"DNS缓存查找",start:"domainLookupStart",end:"fetchStart"},dns:{desc:"DNS查找",start:"domainLookupEnd",end:"domainLookupStart"},connect:{desc:"TCP建连",start:"connectEnd",end:"connectStart"},network:{desc:"网络总耗时",start:"connectEnd",end:"navigationStart"},send:{desc:"从发送到接收到第一个返回",start:"responseStart",end:"requestStart"},receive:{desc:"接收数据时长",start:"responseEnd",end:"responseStart"},ttfb:{desc:"接收首字节用时",start:"responseStart",end:"navigationStart"},dom:{desc:"dom解析",start:"domInteractive",end:"domLoading"},contentLoad:{desc:"内置资源加载时长",start:"domContentLoadedEventEnd",end:"domContentLoadedEventStart"},contentExec:{desc:"内置资源执行时长",start:"domComplete",end:"domContentLoadedEventEnd"},loadEvent:{desc:"load执行用时",start:"loadEventEnd",end:"loadEventStart"},load:{desc:"页面完全加载",start:"loadEventEnd",end:"navigationStart"},interactive:{desc:"可操作",start:"domInteractive",end:"navigationStart"}},c=window.performance||window.mozPerformance||window.msPerformance||window.webkitPerformance;function u(t,e){var n=1<arguments.length&&void 0!==e&&e;if(c){var o=c.timing,a={type:t};if(Object.keys(d).forEach(function(t){var e,n,r=d[t];a[t]=(e=o[r.start],n=o[r.end],0<e&&0<n&&0<=e-n?e-n:void 0)}),n){var r={};s.forEach(function(t){r[t]=o[t]}),console.table(r)}return a}}function p(e){var n={};Object.keys(d).forEach(function(t){n[d[t].desc]={"时间(ms)":e[t]}}),e.fp&&(n.fp={"时间(ms)":e.fp}),e.fcp&&(n.fcp={"时间(ms)":e.fcp}),e.whiteScreen&&(n["白屏"]={"时间(ms)":e.whiteScreen}),console.table(n)}var h=window.performance||window.mozPerformance||window.msPerformance||window.webkitPerformance;function m(t){var e,n,r,o=null;function a(){h.timing.loadEventEnd?(clearTimeout(o),t()):o=setTimeout(a,100)}"complete"===document.readyState?a():(e="load",r=!(n=function(){a()}),window.addEventListener?window.addEventListener(e,n,r):window.attachEvent&&window.attachEvent("on"+e,n))}var v=new e,w={init:function(n,r){v.init(n),m(function(){var e=u("onload",n.detailShow);o().then(function(t){e=Object.assign({},e,t),v.send(e),n.analyzeShow&&p(e),r&&r(e)},function(){v.send(e),n.analyzeShow&&p(e),r&&r(e)})})}},g=function(i){var t=window.XMLHttpRequest;if(!0!==t._bm_flag){t._bm_flag=!0;var s=t.prototype.open;t.prototype.open=function(t,e,n,r,o){return this._xhr_info={url:e,method:t,status:null},s.apply(this,arguments)};var d=t.prototype.send;if(t.prototype.send=function(n){var r=this,o=this;this._start_time=Date.now();function e(e){return function(){if(!o.sendByBM&&!(-1<o._xhr_info.url.indexOf("/recorder/recordList.do")||200<=o.status&&o.status<=206||304===o.status))if(o.response){var t=null;switch(o.responseType){case"json":t=JSON&&JSON.stringify(_this.response).length;break;case"blob":case"moz-blob":t=o.response.size;break;case"arraybuffer":t=o.response.byteLength;case"document":t=o.response.documentElement&&o.response.documentElement.innerHTML&&o.response.documentElement.innerHTML.length+28;break;default:t=o.response.length}o._xhr_info.event=e,o._xhr_info.status=o.status,o._xhr_info.duration=Date.now()-o._start_time,o._xhr_info.responseSize=t,o._xhr_info.requestSize=n?n.length:0,o._xhr_info.type="xhrError",i(r._xhr_info)}else o._xhr_info.event=e,o._xhr_info.status=o.status,o._xhr_info.duration=Date.now()-o._start_time,o._xhr_info.type="xhrError",i(r._xhr_info)}}if(this.addEventListener)this.addEventListener("load",e("load"),!1),this.addEventListener("error",e("error"),!1),this.addEventListener("abort",e("abort"),!1),this.addEventListener("timeout",e("timeout"),!1);else{var a=this.onreadystatechange;this.onreadystatechange=function(t){a&&s.apply(this,arguments),4===this.readyState&&e("end")()}}return d.apply(this,arguments)},window.fetch){var c=window.fetch;window.fetch=function(){var t,e=Date.now(),n=[].slice.call(arguments),r=n[0],o="GET";"string"==typeof r?t=r:"Request"in window&&r instanceof window.Request?(t=r.url,r.method&&(o=r.method)):t=""+r,n[1]&&n[1].method&&(o=n[1].method);var a={method:o,url:t,status:null};return c.apply(this,n).then(function(t){return a.status=t.status,a.type="fetchError",a.duration=Date.now()-e,i(a),t})}}}};function y(t,e){return function(t){if(Array.isArray(t))return t}(t)||function(t,e){if(!(Symbol.iterator in Object(t)||"[object Arguments]"===Object.prototype.toString.call(t)))return;var n=[],r=!0,o=!1,a=void 0;try{for(var i,s=t[Symbol.iterator]();!(r=(i=s.next()).done)&&(n.push(i.value),!e||n.length!==e);r=!0);}catch(t){o=!0,a=t}finally{try{r||null==s.return||s.return()}finally{if(o)throw a}}return n}(t,e)||function(){throw new TypeError("Invalid attempt to destructure non-iterable instance")}()}var E=function(t){var e=t.column||t.columnNumber||t.colno,n=t.line||t.lineNumber||t.lineno,r=t.stack,o=t.message;t.error&&(r=r||t.error.stack,o=o||t.error.message);var a=null,i=null,s=null;if(r){var d=r.match(/(https?|http?|webpack?):\/\/[^\n]+/),c=(s=d?d[0]:"").match(/:(\d+):(\d+)/);if(c&&3<=c.length){var u=y(c,3);i=u[1],a=u[2]}}return{type:"runtimeError",col:Number(e||a),row:Number(n||i),urlFirstStack:s,message:o}};var S=new e,b=null,_=null,x=null;var L={init:function(t,e){t.prodHost&&t.prodHost.length&&!Array.prototype.includes.call(t.prodHost,window.location.host)||(_=t,x=e,S.init(t),g(function(t){S.send(t)}))},filterPromiseErr:function(t){t&&"[object Function]"==Object.prototype.toString.call(t)&&(b=t)},install:function(){var u,n,r;if(u=S,window.addEventListener("error",function(t,e,n,r,o){if(t instanceof ErrorEvent){var a=E(t);u.send(a)}else{var i=t.target.localName,s="";if("link"===i)s=t.target.href;else{if("script"!==i&&"img"!==i)return;s=t.target.src}t.path||t.composedPath&&t.composedPath();var d={type:"resourceError",tagName:t.target.tagName,outerHTML:t.target.outerHTML,sourceUrl:s},c=new XMLHttpRequest;c.sendByBM=!0,c.open("HEAD",s),c.send(),c.onload=function(t){200!==t.target.status&&(d.status=t.target.status,d.statusText=t.target.statusText,u.send(d))},c.onerror=function(t){200!==t.target.status&&(d.status=t.target.status,d.statusText=t.target.statusText,u.send(d))}}},!0),n=S,r=b,window.addEventListener("unhandledrejection",function(t){var e=t.reason;r&&r(e)||n.send({type:t.type||"unhandledrejection",reason:JSON.stringify(e)})}),window.addEventListener("rejectionhandled",function(t){}),_.type&&"VUE"===_.type.toLocaleUpperCase()){if(!x)return;x.config.errorHandler=function(t,e,n){var r=E(t);S.send(r)}}}};t.BM_debug=L,t.BM_perf=w,Object.defineProperty(t,"__esModule",{value:!0})});