UNPKG

@ustinian-wang/js-monitor

Version:

A universal front-end error monitor and reporting tool for Vue and JS.

2 lines (1 loc) 3.04 kB
(function(d,o){typeof exports=="object"&&typeof module<"u"?o(exports):typeof define=="function"&&define.amd?define(["exports"],o):(d=typeof globalThis<"u"?globalThis:d||self,o(d.JsMonitor={}))})(this,function(d){"use strict";function o(e){return typeof e=="function"}function h(e,r=()=>{}){return function(...n){e.apply(this,n),r.apply(this,n)}}function j(e){t("setupVue start",e);let r=window.Vue;if(r){t("vue proxy warn handler start");let n=(a,u,l)=>{var p,w;(p=e.warnHandler)==null||p.call(e,a,u,l),i(e,{type:"vue-warn",error:a,vmName:(w=u==null?void 0:u.$options)==null?void 0:w.name,info:l})};r.config.warnHandler?r.config.warnHandler=h(r.config.warnHandler,n):r.config.warnHandler=n,t("vue proxy warn handler end"),t("vue proxy error handler start");let s=(a,u,l)=>{var p,w;(p=e.errorHandler)==null||p.call(e,a,u,l),i(e,{type:"vue-error",error:a,vmName:(w=u==null?void 0:u.$options)==null?void 0:w.name,info:l})};r.config.errorHandler?r.config.errorHandler=h(r.config.errorHandler,s):r.config.errorHandler=s,t("vue proxy error handler end")}t("setupVue end",e)}function W(e){t("setupWin start"),t("setupWin onerror start");let r=function(n,s,a,u,l){var p;(p=e.onerror)==null||p.call(e,n,s,a,u,l),i(e,{type:"onerror",message:n,source:s,lineno:a,colno:u,stack:l==null?void 0:l.stack,error:l})};window.onerror?window.onerror=h(window.onerror,r):window.onerror=r,t("setupWin onerror end"),t("setupWin unhandledrejection start"),window.addEventListener("unhandledrejection",function(n){var s,a,u;(s=e.unhandledrejection)==null||s.call(e,n),i(e,{type:"unhandledrejection",reason:n.reason,stack:(a=n.reason)==null?void 0:a.stack,error:n.reason,message:(u=n.reason)==null?void 0:u.message})}),t("setupWin unhandledrejection end"),t("setupWin error start"),window.addEventListener("error",function(n){var l,p;(l=e.error)==null||l.call(e,n);const s=n.target;let a=(p=s==null?void 0:s.tagName)==null?void 0:p.toLowerCase();s&&["img","script","link"].includes(a)&&i(e,{type:"resource-error",tagName:a,src:s.src||s.href})},!0),t("setupWin error end")}function y(e){return{...{},...e}}let H=!1;function t(...e){H&&console.log("[monitor] ",...e)}let m=!1;function b(e){if(t("setup launch start"),!e.force){if(m){t("setup already launch",e);return}m=!0}e=y(e),H=e.debug||!1,t("setup start"),j(e),W(e),t("setup end"),t("setup launch end")}function i(e,r){var a,u,l;(a=e.report)==null||a.call(e,e,r),t("report start"),e=y(e);let{appId:n="",api:s=""}=e;if(o(e==null?void 0:e.filter)&&(u=e==null?void 0:e.filter)!=null&&u.call(e,r)){t("report filter report",r);return}r={...r,appId:n,time:Date.now(),url:location.href,userAgent:navigator.userAgent},o(e==null?void 0:e.transform)&&(t("report transform before",r),r=(l=e==null?void 0:e.transform)==null?void 0:l.call(e,r),t("report transform after",r)),typeof s=="string"?(t("report api function",r),fetch(s,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(r)})):(t("report api fetch",r),s(r))}d.report=i,d.setup=b,Object.defineProperty(d,Symbol.toStringTag,{value:"Module"})});