博客地址javascript
《使用模块化工具打包本身开发的JS库》文章中有提到,当时须要写一个SDK,监控小程序的后台接口调用和页面报错,今天就来讲下实现原理吧!html
以前也作过浏览器web端的SDK数据埋点上报,其实原理大同小异:经过劫持原始方法,获取须要上报的数据,最后再执行原始方法,这样就能实现无痕埋点。java
举个例子:我但愿监控全部web页面的ajax请求,每次发送ajax,都须要在控制台打印出发送的urlios
平时咱们开发,发送ajax通常用的都是封装好的库,例如jQuery,Axios等,然而这些库,底层仍然用的是浏览器原生的XMLHttpRequest对象,所以,咱们只须要修改XMLHttpRequest对象便可web
注意:因为JS的灵活性,修改原生方法是一件很容易的事,然而并不鼓励这样作!ajax
// 把这段代码放在全部JS代码以前,咱们就实现了拦截ajax的需求
window.XMLHttpRequest.prototype.open = (function(originOpen) {
return function(method, url, async) {
console.log('发送了ajax,url是: ', url);
return originOpen.apply(this, arguments);
};
})(window.XMLHttpRequest.prototype.open);
复制代码
在这个当即执行函数中,咱们把原生的open
方法经过originOpen
暂时存储起来,而后在外面包裹一层函数,实现了打印输出url的功能,最后经过originOpen.apply
让原生方法运行,这样就实现了无痕拦截。小程序
小程序的运行环境并无window
和document
对象,它只暴露了一个wx
全局对象,发送网络请求则是经过wx.request这个api,所以,此次咱们须要拦截的就是wx.request
方法api
咱们试着更改一下wx.request
浏览器
wx.request = function() {
console.log('66666');
}
复制代码
这时控制台会报错TypeError: Cannot set property request of #<Object> which has only a getter
网络
这是由于,wx.request
这个属性,只有get
方法而没有set
方法,咱们能够经过Object.getOwnPropertyDescriptor
验证:
const des = Object.getOwnPropertyDescriptor(wx, 'request');
// des {
// configurable: true,
// enumerable: true,
// get: f(),
// set: undefined
// }
复制代码
咱们能够换种方式修改:
const originRequest = wx.request;
Object.defineProperty(wx, 'request', {
configurable: true,
enumerable: true,
writable: true,
value: function() {
const config = arguments[0] || {};
const url = config.url;
console.log('发送了ajax,url是: ', url);
return originRequest.apply(this, arguments);
}
});
复制代码
此次就实现拦截功能了!
小程序的注册函数App
有个全局的onError
方法,咱们能够在小程序的入口文件app.js
先注册一个该方法:
App({
onError: function(err) {
console.log('上报错误啦!');
wx.request({
url: 'http://monitor.com/monitor/error',
data: err
})
}
})
App({
// 其余逻辑
})
复制代码
不过须要注意的是:若是后续的程序重写了onError的话,将会致使以前注册的onError失效。
解决方法能够是:咱们监控SDK能够暴露一个接口,让接入方本身在onError中调用咱们的接口。
App({
onError: function (err) {
monitor.notifyError(err)
}
})
复制代码
收集好须要的数据后,固然就要上报后台。怎么上报?固然仍是用的wx.request
发送请求。
这里就容易出现一个死循环: 若是用以前被咱们包装过的wx.request
上报数据,那么上报数据这个ajax请求,也会被咱们认为是普通的ajax请求,而后又会触发上报,这样来来回回,无穷无尽的发送上报数据。
解决方法有多种,好比:
方案1
能够在包装wx.request
的时候,判断发送的url若是是上报接口,那么就再也不上报了。
const originRequest = wx.request;
Object.defineProperty(wx, 'request', {
configurable: true,
enumerable: true,
writable: true,
value: function() {
const config = arguments[0] || {};
const url = config.url;
if (url.indexOf('http://monitor.com') > -1) {
// 直接发送请求,不上报
return originRequest.apply(this, arguments);
}
console.log('上报ajax数据啦!');
wx.request({
url: 'http://monitor.com/monitor/ajax',
data: config.data
})
return originRequest.apply(this, arguments);
}
});
复制代码
方案2
在包装wx.request
以前,保留一份最原始的wx.request
方法,全部的上报请求,就不走被包装过的方法,而走最原始的方法。
const myRequest = wx.request;
const wrapRequest = function () {
const originRequest = wx.request;
Object.defineProperty(wx, 'request', {
configurable: true,
enumerable: true,
writable: true,
value: function() {
const config = arguments[0] || {};
const url = config.url;
console.log('上报数据啦!');
// 使用最原始的request方法
myRequest({
url: 'http://monitor.com/monitor/ajax',
data: config.data
})
return originRequest.apply(this, arguments);
}
});
}
wrapRequest();
复制代码
实际开发中固然还有更多的细节,好比监控项目的鉴权,SDK的代码结构,上报前的数据收集和聚合等等,本文就不详细展开了。