speed.js
4.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
var reporter = require('./reporter');
// 首屏时间获取:
// 1,获取首屏基线高度
// 2,计算出基线dom元素之上的所有图片元素
// 3,所有图片onload之后为首屏显示时间
// 参考 https://www.zhihu.com/question/23212408/answer/56647975
function getOffsetTop(ele) {
var offsetTop = ele.offsetTop;
if (ele.offsetParent !== null) {
offsetTop += getOffsetTop(ele.offsetParent);
}
return offsetTop;
}
var firstScreenHeight = window.screen.height;
var firstScreenImgs = [];
var isFindLastImg = false;
var allImgLoaded = false;
var firstScreenLoadTime = 0;
var t = setInterval(function() {
var i, img;
if (isFindLastImg) {
if (firstScreenImgs.length) {
for (i = 0; i < firstScreenImgs.length; i++) {
img = firstScreenImgs[i];
if (!img.complete) {
allImgLoaded = false;
break;
} else {
allImgLoaded = true;
}
}
} else {
allImgLoaded = true;
}
if (allImgLoaded) {
firstScreenLoadTime = (new Date()).getTime() - _timeStart
clearInterval(t);
}
} else {
var imgs = document.body && document.body.querySelectorAll('img') || [];
for (i = 0; i < imgs.length; i++) {
img = imgs[i];
var imgOffsetTop = getOffsetTop(img);
if (imgOffsetTop > firstScreenHeight) {
isFindLastImg = true;
break;
} else if (imgOffsetTop <= firstScreenHeight && !img.hasPushed) {
img.hasPushed = 1;
firstScreenImgs.push(img);
}
}
}
}, 0);
var speedReporter = {
reportTime: function(speedList) {
var speedStr = reporter.stringify(speedList);
reporter.report(speedStr);
},
addEvent: function() {
var self = this;
var timeList = [];
var pt = encodeURIComponent(location.href);
var u = reporter.getUid();
var ud = reporter.getUdid();
var rid = reporter.getReqId();
var pStart = window.performance && window.performance.timing.navigationStart || 0;
_timeStart = window._timeStart || 0;
document.addEventListener && document.addEventListener('DOMContentLoaded', function(event) {
timeList.push({
tp: 'dcl', // DOMContentLoaded
t: (new Date()).getTime() - _timeStart,
pt: pt,
u: u,
ud: ud,
rid: rid
});
// 首屏加载速度
var imgs = document.body && document.body.querySelectorAll('img') || [];
if (!imgs.length) {
isFindLastImg = true;
}
}, false);
window.addEventListener && window.addEventListener('load', function(event) {
timeList.push({
tp: 'ld', // load
t: (new Date()).getTime() - _timeStart,
pt: pt,
u: u,
ud: ud,
rid: rid
});
// 首屏加载速度
allImgLoaded = true;
isFindLastImg = true;
if (t) {
clearInterval(t);
}
if (firstScreenLoadTime) {
timeList.push({
tp: 'fs', // firstScreenLoadTime
t: firstScreenLoadTime,
pt: pt,
u: u,
ud: ud,
rid: rid
});
}
timeList.push({
tp: 'pf', // performance
dcl: (window.performance && window.performance.timing.domComplete || 0) - pStart,
ld: (window.performance && window.performance.timing.loadEventStart || 0) - pStart,
pt: pt,
u: u,
ud: ud,
rid: rid
});
self.reportTime(timeList);
}, false);
},
init: function() {
this.addEvent();
}
};
module.exports = speedReporter;