UNPKG

xys-monitor

Version:

A project named xys-monitor

248 lines (214 loc) 7.09 kB
import Tracekit from './tracekit' import config from './config' function formatComponentName (vm, isVue3 = false) { if (vm.$root === vm) { return 'root instance' } if (!vm || !vm.$options) { return '<Anonymous>' } const options = vm.$options // console.log('formatComponentName vm 实例:', vm) // vue3的vm会是一个proxy对象 // console.log('formatComponentName vm_isVue', vm._isVue) let name = vm.name || '' if (isVue3) { name = options.__name } else { name = options.name || options._componentTag } const fileName = options.__file || '' if (!name && fileName) { const match = fileName.match(/([^/\\]+)\.vue$/) if (match) { name = match[1] } } // console.log('formatComponentName name', name) return ( (name ? 'component <' + name + '>' : 'anonymous component') + (vm.$options.__file ? ' at ' + vm.$options.__file : '') ) // return ( // (name ? 'component <' + name + '>' : 'anonymous component') + // (vm._isVue && vm.$options.__file ? ' at ' + vm.$options.__file : '') // ) } function isError (value) { switch ({}.toString.call(value)) { case '[object Error]': return true case '[object Exception]': return true case '[object DOMException]': return true default: return value instanceof Error } } function randomString (len) { let chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefhijklmnopqrstuvwxyz' let maxPos = chars.length let pwd = '' len = len || 32 for (let i = 0; i < len; i++) { pwd += chars.charAt(Math.floor(Math.random() * maxPos)) } return pwd } function ignoreCheck (ignoreList, target) { let isIgnore = false for (let item of ignoreList) { let reg = Object.prototype.toString.call(item) === '[object RegExp]' ? item : new RegExp(item) if (target.match(reg)) { isIgnore = true } } return isIgnore } function hijack (target, key, dosomething) { let old = target[key] target[key] = function (...args) { try { isType(dosomething, 'Function') && dosomething.apply(this, args) } finally { if (old && isType(old, 'Function')) { // eslint-disable-next-line no-unsafe-finally return old.apply(this, args) } } } } function isType (target, type) { return Object.prototype.toString.call(target) === '[object ' + type + ']' } function handleManualMes (mes) { let result = { pageUrl: location.href, errorMsg: '', lineNo: 0, columnNo: 0, errorType: config.errTypeMap['manual_error'], errorStack: '' } if (isType(mes, 'Error')) { let stackArr = Tracekit.computeStackTrace(mes) result.errorMsg = stackArr.message || '' result.lineNo = stackArr.stack[0].line || 0 result.columnNo = stackArr.stack[0].column || 0 result.errorStack = mes.stack || '' } else { result.errorMsg = JSON.stringify(mes) } return result } function isRubbish () { if (window.jQuery && window.jQuery.prototype) { let version = parseInt(window.jQuery.prototype.jquery) return !(version >= 2) } else { return false } } function getFullUrl (url) { if (url.match(/^\/\//)) { return location.protocol + url } else if (url.match(/^\//)) { return location.protocol + '//' + location.host + url } return url } function isCrossDomain (a, b) { let rurl = /^([\w+.-]+:)(?:\/\/([^/?#:]*)(?::(\d+)|)|)/ a = rurl.exec(a.toLowerCase()) || [] b = rurl.exec(b.toLowerCase()) || [] if (!a.length || !b.length) { return true } else { return !(a[1] === b[1] && a[2] === b[2] && a[3] === b[3]) } } function workData (data) { if (!data) { return };//get请求,无需处理 let each = function (object, callback) { let name let i = 0 let length = object.length let isObj = length === undefined || Object.prototype.toString.call(object) === '[object Function]' if (isObj) { for (name in object) { if (callback.call(object[name], name, object[name]) === false) { break } } } else { for (let value = object[0]; i < length && callback.call(value, i, value) !== false; value = object[++i]) { } } return object } let serialize = function (params, obj, scope) { let type; let array = Object.prototype.toString.call(obj) === '[object Array]'; let hash = Object.prototype.toString.call(obj) === '[object Object]' each(obj, function (key, value) { type = Object.prototype.toString.call(value) if (scope) key = scope + '[' + (hash || type === '[object Object]' || type === '[object Array]' ? key : '') + ']' if (!scope && array) params.add(value.name, value.value) else if (type === '[object Array]' || (type === '[object Object]')) { serialize(params, value, key) } else params.add(key, value) }) } let param = function (obj) { let params = [] params.add = function (key, value) { if (Object.prototype.toString.call(value) === '[object Function]') value = value() if (value == null) value = '' this.push(encodeURIComponent(key) + '=' + encodeURIComponent(value)) } serialize(params, obj) return params.join('&').replace(/%20/g, '+') } return param(data) } function myAjax (options) { let { type, url, data } = options let xhr = new XMLHttpRequest() xhr._ignore = true xhr.open(type, url) xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded') xhr.send(workData(data)) } function getDomAttr (target, attr) { if (target.attributes && target.attributes[attr]) { return target.attributes[attr].value } return '' } function getCgidata (type, url, sendargs) { type = type.toLocaleLowerCase() let result = {} if (type === 'post') { result = sendargs } else if (type === 'get') { let query = url.split('#')[0].match(/.+\?(.+)/) if (query) { query[1].split('&').forEach((item, index) => { item = item.split('=') result[item[0]] = item[1] }) } } return result } export { formatComponentName, isError, randomString, ignoreCheck, hijack, isType, handleManualMes, isRubbish, getFullUrl, isCrossDomain, myAjax, getDomAttr, getCgidata }