bmfe-monitor
Version:
前端监测平台
2 lines (1 loc) • 8.99 kB
JavaScript
!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,i){"use strict";function a(n,o,r,a){i.get(function(t){var e=t.map(function(t){return t.value});n.fingerId=i.x64hash128(e.join(""),31),o(Object.assign({},r,{locationHref:a,project:n.project||"unnamed",fingerId:n.fingerId},n.extra||{}))})}function e(n,o){var r=(/#(\/\w*.*)*/.exec(location.href)||[])[1];return new Promise(function(t,e){n.fingerId&&t(Object.assign({},o,{locationHref:r,project:n.project||"unnamed",fingerId:n.fingerId},n.extra||{})),window.requestIdleCallback?requestIdleCallback(function(){a(n,t,o,r)}):setTimeout(function(){a(n,t,o,r)},500)})}function n(){this.baseConfig={project:"",reportUrl:"",fingerId:""}}i=i&&i.hasOwnProperty("default")?i.default:i,n.prototype.init=function(t){this.baseConfig=Object.assign(this.baseConfig,t)},n.prototype.send=function(t){var n=this.baseConfig.reportUrl||"http://jartto.wang/report";e(this.baseConfig,t).then(function(t){var e=new XMLHttpRequest;e.sendByBM=!0,e.open("POST",n),e.setRequestHeader("Content-Type","application/json"),e.send(JSON.stringify(t))})};var r=new n,o=!1,d=window.performance||window.mozPerformance||window.msPerformance||window.webkitPerformance,s=null,c={addEventListener:function(t,e,n){return window.addEventListener?window.addEventListener(t,e,n):window.attachEvent?window.attachEvent("on"+t,e):void 0},domready:function(t){if(!0!==o){var e=null;"interactive"===document.readyState?n():document.addEventListener?document.addEventListener("DOMContentLoaded",function(){n()},!1):document.attachEvent&&document.attachEvent("onreadystatechange",function(){n()})}function n(){d.timing.domInteractive?(clearTimeout(e),t()):e=setTimeout(n,100)}},onload:function(t){var e=null;function n(){d.timing.loadEventEnd?(clearTimeout(e),t(),o=!0):e=setTimeout(n,100)}"complete"===document.readyState?n():c.addEventListener("load",function(){n()},!1)}};function u(t,e){return 0<t&&0<e&&0<=t-e?t-e:void 0}function f(){return new Promise(function(t,e){window.performance&&window.performance.getEntriesByType?function c(u){clearTimeout(s);s=setTimeout(function(){var t={},e={},n=window.performance.getEntriesByType("paint"),o=!0,r=!1,a=void 0;try{for(var i,d=n[Symbol.iterator]();!(o=(i=d.next()).done);o=!0){var s=i.value;t[s.name]=s.startTime+s.duration}}catch(t){r=!0,a=t}finally{try{o||null==d.return||d.return()}finally{if(r)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()})}function l(t){var e={"上一个页面":{"时间(ms)":t.pervPage},"页面重定向":{"时间(ms)":t.redirect},"DNS缓存查找":{"时间(ms)":t.appCache},"DNS查找":{"时间(ms)":t.dns},"TCP建连":{"时间(ms)":t.connect},"网络总耗时":{"时间(ms)":t.network},"从发送到接收到第一个返回":{"时间(ms)":t.send},"接收数据时长":{"时间(ms)":t.receive},"接收首字节用时":{"时间(ms)":t.ttfb},"dom解析":{"时间(ms)":t.dom},"内置资源加载时长":{"时间(ms)":t.contentLoad},"内置资源执行时长":{"时间(ms)":t.contentExec},"load执行用时":{"时间(ms)":t.loadEvent},"页面完全加载":{"时间(ms)":t.load},"白屏":{"时间(ms)":t.whiteScreen},"可操作":{"时间(ms)":t.interactive}};console.table(e)}var m={init:function(n,o){r.init(n),c.onload(function(){var e=function(t,e){var n=1<arguments.length&&void 0!==e&&e;if(d){var o=d.timing,r={type:t,pervPage:u(o.fetchStart,o.navigationStart),redirect:u(o.redirectEnd,o.redirectStart),appCache:u(o.domainLookupStart,o.fetchStart),dns:u(o.domainLookupEnd,o.domainLookupStart),connect:u(o.connectEnd,o.connectStart),network:u(o.connectEnd,o.navigationStart),send:u(o.responseStart,o.requestStart),receive:u(o.responseEnd,o.responseStart),dom:u(o.domInteractive,o.domLoading),contentLoad:u(o.domContentLoadedEventEnd,o.domContentLoadedEventStart),contentExec:u(o.domComplete,o.domContentLoadedEventEnd),loadEvent:u(o.loadEventEnd,o.loadEventStart),interactive:u(o.domInteractive,o.navigationStart),ttfb:u(o.responseStart,o.navigationStart),load:u(o.loadEventEnd,o.navigationStart)};if(n){var a={navigationStart:o.navigationStart,unloadEventStart:o.unloadEventStart,unloadEventEnd:o.unloadEventEnd,redirectStart:o.redirectStart,redirectEnd:o.redirectEnd,fetchStart:o.fetchStart,domainLookupStart:o.domainLookupStart,domainLookupEnd:o.domainLookupEnd,connectStart:o.connectStart,connectEnd:o.connectEnd,secureConnectionStart:o.secureConnectionStart,requestStart:o.requestStart,responseStart:o.responseStart,responseEnd:o.responseEnd,domLoading:o.domLoading,domInteractive:o.domInteractive,domContentLoadedEventStart:o.domContentLoadedEventStart,domContentLoadedEventEnd:o.domContentLoadedEventEnd,domComplete:o.domComplete,loadEventStart:o.loadEventStart,loadEventEnd:o.loadEventEnd};console.table(a)}return r}}("onload",n.detailShow);f().then(function(t){(e=Object.assign({},e,t)).timestamp=Date.now(),r.send(e),n.analyzeShow&&l(e),o&&o(e)},function(){e.timestamp=Date.now(),r.send(e),n.analyzeShow&&l(e),o&&o(e)})})}};function p(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=[],o=!0,r=!1,a=void 0;try{for(var i,d=t[Symbol.iterator]();!(o=(i=d.next()).done)&&(n.push(i.value),!e||n.length!==e);o=!0);}catch(t){r=!0,a=t}finally{try{o||null==d.return||d.return()}finally{if(r)throw a}}return n}(t,e)||function(){throw new TypeError("Invalid attempt to destructure non-iterable instance")}()}var v=function(i){var t=window.XMLHttpRequest;if(!0!==t._bm_flag){t._bm_flag=!0;var d=t.prototype.open;t.prototype.open=function(t,e,n,o,r){return this._eagle_xhr_info={url:e,method:t,status:null},d.apply(this,arguments)};var s=t.prototype.send;if(t.prototype.send=function(n){var o=this,r=this;this._eagle_start_time=Date.now();function e(e){return function(){if(!r.sendByBM&&!(-1<r._eagle_xhr_info.url.indexOf("/recorder/recordList.do")||200<=r.status&&r.status<=206||304===r.status))if(r.response){var t=null;switch(r.responseType){case"json":t=JSON&&JSON.stringify(_this.response).length;break;case"blob":case"moz-blob":t=r.response.size;break;case"arraybuffer":t=r.response.byteLength;case"document":t=r.response.documentElement&&r.response.documentElement.innerHTML&&r.response.documentElement.innerHTML.length+28;break;default:t=r.response.length}r._eagle_xhr_info.event=e,r._eagle_xhr_info.status=r.status,r._eagle_xhr_info.duration=Date.now()-r._eagle_start_time,r._eagle_xhr_info.responseSize=t,r._eagle_xhr_info.requestSize=n?n.length:0,r._eagle_xhr_info.type="xhr",i(o._eagle_xhr_info)}else r._eagle_xhr_info.event=e,r._eagle_xhr_info.status=r.status,r._eagle_xhr_info.duration=Date.now()-r._eagle_start_time,r._eagle_xhr_info.type="xhr",i(o._eagle_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&&d.apply(this,arguments),4===this.readyState&&e("end")()}}return s.apply(this,arguments)},window.fetch){var c=window.fetch;window.fetch=function(){var t,e=Date.now(),n=[].slice.call(arguments),o=n[0],r="GET";"string"==typeof o?t=o:"Request"in window&&o instanceof window.Request?(t=o.url,o.method&&(r=o.method)):t=""+o,n[1]&&n[1].method&&(r=n[1].method);var a={method:r,url:t,status:null};return c.apply(this,n).then(function(t){return a.status=t.status,a.type="fetch",a.duration=Date.now()-e,i(a),t})}}}},h=new n;window.addEventListener("error",function(t,e,n,o,r){var a=t.target.localName,i="";if("link"===a)i=t.target.href;else{if("script"!==a&&"img"!==a)return;i=t.target.src}t.path||t.composedPath&&t.composedPath();var d={type:"resourceError",tagName:t.target.tagName,outerHTML:t.target.outerHTML,sourceUrl:i},s=new XMLHttpRequest;s.open("HEAD",i),s.send(),s.onload=function(t){200!==t.target.status&&(d.status=t.target.status,d.statusText=t.target.statusText,h.send(d))}},!0),window.addEventListener("unhandledrejection",function(t){var e=t.reason;e.msg||e.resCode||h.send({type:t.type||"unhandledrejection",reason:e})}),window.onerror=function(t){};var g={init:function(t,e){h.init(e),t.config.errorHandler=function(t,e,n){var o=function(t){var e=t.column||t.columnNumber,n=t.line||t.lineNumber,o=t.message,r=null,a=null,i=t.stack;if(i){var d=i.match(/https?:\/\/[^\n]+/),s=(d?d[0]:"").match(/:(\d+):(\d+)/);if(s&&3<=s.length){var c=p(s,3);a=c[1],r=c[2]}}return{type:"caught",col:Number(e||r),row:Number(n||a),message:o}}(t);h.send(o)},v(function(t){h.send(t)})}};t.BM_debug=g,t.BM_perf=m,Object.defineProperty(t,"__esModule",{value:!0})});