UNPKG

bmfe-monitor

Version:

前端监测平台

71 lines (62 loc) 2.14 kB
import Fingerprint2 from 'fingerprintjs2' // 取出#后边的路径 function solveHref() { let regStr = /#(\/\w*.*)*/; let arr = regStr.exec(location.href) || []; return arr[1] } function fingerprint2Get(baseConfig,resolve,info,locationHref) { Fingerprint2.get(function (components) { let values = components.map(function (component) { return component.value }) baseConfig.fingerId = Fingerprint2.x64hash128(values.join(''), 31) resolve(Object.assign({}, info, { locationHref, project: baseConfig.project || 'unnamed', fingerId: baseConfig.fingerId, },baseConfig.extra || {})) }) } function baseInfo(baseConfig,info) { let locationHref = solveHref() return new Promise((resolve,reject)=>{ if(baseConfig.fingerId) { resolve(Object.assign({}, info, { locationHref, project: baseConfig.project || 'unnamed', fingerId: baseConfig.fingerId },baseConfig.extra || {})) } if (window.requestIdleCallback) { requestIdleCallback(function () { fingerprint2Get(baseConfig,resolve,info,locationHref) }) } else { setTimeout(function () { fingerprint2Get(baseConfig,resolve,info,locationHref) }, 500) } }) } function Report() { this.baseConfig = { project: '', reportUrl: '', fingerId: '' } } Report.prototype.init = function(config) { this.baseConfig = Object.assign(this.baseConfig, config) } Report.prototype.send = function(msg) { let reportUrl = this.baseConfig.reportUrl || 'http://jartto.wang/report'; baseInfo(this.baseConfig,msg).then((msgInfo)=>{ let xhr = new XMLHttpRequest; xhr.sendByBM = true xhr.open("POST", reportUrl) xhr.setRequestHeader("Content-Type", "application/json") xhr.send(JSON.stringify(msgInfo)) }) // let log = encodeURIComponent(JSON.stringify(msgInfo)) // new Image().src = `${reportUrl}?perLogs=${log}`; } export default Report