|
《使用模塊化工具打包自己開發(fā)的JS庫》 文章中有提到,當(dāng)時(shí)需要寫一個(gè)SDK,監(jiān)控小程序的后臺(tái)接口調(diào)用和頁面報(bào)錯(cuò),今天就來說下實(shí)現(xiàn)原理吧! 原理之前也做過瀏覽器web端的SDK數(shù)據(jù)埋點(diǎn)上報(bào),其實(shí)原理大同小異:通過劫持原始方法,獲取需要上報(bào)的數(shù)據(jù),最后再執(zhí)行原始方法,這樣就能實(shí)現(xiàn)無痕埋點(diǎn)。 舉個(gè)例子:我希望監(jiān)控所有web頁面的ajax請(qǐng)求,每次發(fā)送ajax,都需要在控制臺(tái)打印出發(fā)送的url 平時(shí)我們開發(fā),發(fā)送ajax一般用的都是封裝好的庫,例如jQuery,Axios等,然而這些庫,底層仍然用的是瀏覽器原生的XMLHttpRequest對(duì)象,因此,我們只需要修改XMLHttpRequest對(duì)象即可 注意:由于JS的靈活性,修改原生方法是一件很容易的事,然而并不鼓勵(lì)這樣做!
// 把這段代碼放在所有JS代碼之前,我們就實(shí)現(xiàn)了攔截ajax的需求
window.XMLHttpRequest.prototype.open = (function(originOpen) {
return function(method, url, async) {
console.log('發(fā)送了ajax,url是: ', url);
return originOpen.apply(this, arguments);
};
})(window.XMLHttpRequest.prototype.open);
在這個(gè)立即執(zhí)行函數(shù)中,我們把原生的 open 方法通過 originOpen 暫時(shí)存儲(chǔ)起來,然后在外面包裹一層函數(shù),實(shí)現(xiàn)了打印輸出url的功能,最后通過 originOpen.apply 讓原生方法運(yùn)行,這樣就實(shí)現(xiàn)了無痕攔截。 監(jiān)控小程序攔截wx.request小程序的運(yùn)行環(huán)境并沒有 window 和 document 對(duì)象,它只暴露了一個(gè) wx 全局對(duì)象,發(fā)送網(wǎng)絡(luò)請(qǐng)求則是通過wx.request這個(gè)api,因此,這次我們需要攔截的就是 wx.request 方法 我們?cè)囍囊幌?nbsp;wx.request
wx.request = function() {
console.log('66666');
}
這時(shí)控制臺(tái)會(huì)報(bào)錯(cuò) TypeError: Cannot set property request of #<Object> which has only a getter 這是因?yàn)椋?nbsp;wx.request 這個(gè)屬性,只有 get 方法而沒有 set 方法,我們可以通過 Object.getOwnPropertyDescriptor 驗(yàn)證:
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('發(fā)送了ajax,url是: ', url);
return originRequest.apply(this, arguments);
}
});
這次就實(shí)現(xiàn)攔截功能了! 監(jiān)控異常小程序的注冊(cè)函數(shù) App 有個(gè)全局的 onError 方法,我們可以在小程序的入口文件 app.js 先注冊(cè)一個(gè)該方法:
App({
onError: function(err) {
console.log('上報(bào)錯(cuò)誤啦!');
wx.request({
url: 'http://monitor.com/monitor/error',
data: err
})
}
})
App({
// 其他邏輯
})
不過需要注意的是:如果后續(xù)的程序重寫了onError的話,將會(huì)導(dǎo)致之前注冊(cè)的onError失效。 解決方法可以是:我們監(jiān)控SDK可以暴露一個(gè)接口,讓接入方自己在onError中調(diào)用我們的接口。
App({
onError: function (err) {
monitor.notifyError(err)
}
})
上報(bào)數(shù)據(jù)收集好需要的數(shù)據(jù)后,當(dāng)然就要上報(bào)后臺(tái)。怎么上報(bào)?當(dāng)然還是用的 wx.request 發(fā)送請(qǐng)求。 這里就容易出現(xiàn)一個(gè) 死循環(huán) : 如果用之前被我們包裝過的 wx.request 上報(bào)數(shù)據(jù),那么上報(bào)數(shù)據(jù)這個(gè)ajax請(qǐng)求,也會(huì)被我們認(rèn)為是普通的ajax請(qǐng)求,然后又會(huì)觸發(fā)上報(bào),這樣來來回回,無窮無盡的發(fā)送上報(bào)數(shù)據(jù)。 解決方法有多種,比如: 方案1可以在包裝 wx.request 的時(shí)候,判斷發(fā)送的url如果是上報(bào)接口,那么就不再上報(bào)了。
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) {
// 直接發(fā)送請(qǐng)求,不上報(bào)
return originRequest.apply(this, arguments);
}
console.log('上報(bào)ajax數(shù)據(jù)啦!');
wx.request({
url: 'http://monitor.com/monitor/ajax',
data: config.data
})
return originRequest.apply(this, arguments);
}
});
方案2在包裝 wx.request 之前,保留一份最原始的 wx.request 方法,所有的上報(bào)請(qǐng)求,就不走被包裝過的方法,而走最原始的方法。
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('上報(bào)數(shù)據(jù)啦!');
// 使用最原始的request方法
myRequest({
url: 'http://monitor.com/monitor/ajax',
data: config.data
})
return originRequest.apply(this, arguments);
}
});
}
wrapRequest();
其他事項(xiàng)實(shí)際開發(fā)中當(dāng)然還有更多的細(xì)節(jié),比如監(jiān)控項(xiàng)目的鑒權(quán),SDK的代碼結(jié)構(gòu),上報(bào)前的數(shù)據(jù)收集和聚合等等,本文就不詳細(xì)展開了。 |