(function () {
var ajaxWhiteList = {
1: {
reg: /^https?:\/\/mp\.weixin\.qq\.com\/mp\/appmsg_like/,
times: 0
},
2: {
reg: /^https?:\/\/mp\.weixin\.qq\.com\/mp\/appmsg_comment((\?|&)[^=]*?=[^&]*?)*?(\?|&)action=likecomment/,
times: 0
},
3: {
reg: /^https?:\/\/mp\.weixin\.qq\.com\/mp\/appmsg_comment((\?|&)[^=]*?=[^&]*?)*?(\?|&)action=addcomment/,
times: 0
},
4: {
reg: /^https?:\/\/mp\.weixin\.qq\.com\/mp\/authorreward/,
times: 0
}
};
if (!performance || !performance.getEntries) return;
var hasReported = false;
var reportResLoadTime = function () {
if (hasReported) return;
var notSupport = false;
var ajaxEntries = [];
var entries = performance.getEntries().map(function (entry) {
if (typeof entry !== 'object') {
notSupport = true;
} else if (entry.entryType === undefined) {
notSupport = true;
} else if (
entry.entryType !== 'navigation' &&
entry.entryType !== 'resource'
) {
return null;
} else if (entry.initiatorType === undefined) {
notSupport = true;
} else if (entry.initiatorType === 'xmlhttprequest') {
if (entry.name === undefined || entry.duration === undefined) {
notSupport = true;
} else {
for (var scene in ajaxWhiteList) {
if (Object.prototype.hasOwnProperty.call(ajaxWhiteList, scene)) {
var ajaxItem = ajaxWhiteList[scene];
if (ajaxItem.times < 10 && ajaxItem.reg.test(entry.name)) {
ajaxEntries.push({
scene: scene,
protocol: entry.nextHopProtocol,
is_https: isHttps(entry),
time: entry.duration
});
ajaxItem.times++;
}
}
}
}
return null;
}
return entry;
});
if (notSupport || ajaxEntries.length === 0) return;
var data = {
stat_list: ajaxEntries
};
var img = new Image();
img.src = 'https://mp.weixin.qq.com/mp/timereport?data=' + JSON.stringify(data);
hasReported = true;
};
window.addEventListener('beforeunload', reportResLoadTime, false);
window.addEventListener('unload', reportResLoadTime, false);
function isHttps(entry) {
if (/^https/.test(entry.name)) return 1;
else return 0;
}
})();