-
Notifications
You must be signed in to change notification settings - Fork 9
/
index.js
75 lines (75 loc) · 2.9 KB
/
index.js
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
var details = [], ignoreEleList = ['script', 'style', 'link', 'br'];
function getFirstScreenTime() {
return new Promise(function (resolve, reject) {
// 5s之内先收集所有的dom变化,并以key(时间戳)、value(dom list)的结构存起来。
var observeDom = new MutationObserver(function (mutations) {
if (!mutations || !mutations.forEach)
return;
var detail = {
time: performance.now(),
roots: []
};
mutations.forEach(function (mutation) {
if (!mutation || !mutation.addedNodes || !mutation.addedNodes.forEach)
return;
mutation.addedNodes.forEach(function (ele) {
if (ele.nodeType === 1 && ignoreEleList.indexOf(ele.nodeName.toLocaleLowerCase()) === -1) {
if (!isEleInArray(ele, detail.roots)) {
detail.roots.push(ele);
}
}
});
});
if (detail.roots.length) {
details.push(detail);
}
});
observeDom.observe(document, {
childList: true,
subtree: true
});
setTimeout(function () {
observeDom.disconnect();
resolve(details);
}, 5000);
}).then(function (details) {
// 分析上面收集到的数据,返回最终的结果
var result;
details.forEach(function (detail) {
for (var i = 0; i < detail.roots.length; i++) {
if (isInFirstScreen(detail.roots[i])) {
result = detail.time;
break;
}
}
});
// 遍历当前请求的图片中,如果有开始请求时间在首屏dom渲染期间的,则表明该图片是首屏渲染中的一部分,
// 所以dom渲染时间和图片返回时间中大的为首屏渲染时间
window.performance.getEntriesByType('resource').forEach(function (resource) {
if (resource.initiatorType === 'img' && (resource.fetchStart < result || resource.startTime < result) && resource.responseEnd > result) {
result = resource.responseEnd;
}
});
return result;
});
}
function isEleInArray(target, arr) {
if (!target || target === document.documentElement) {
return false;
}
else if (arr.indexOf(target) !== -1) {
return true;
}
else {
return isEleInArray(target.parentElement, arr);
}
}
function isInFirstScreen(target) {
if (!target || !target.getBoundingClientRect)
return false;
var rect = target.getBoundingClientRect(), screenHeight = window.innerHeight, screenWidth = window.innerWidth;
return rect.left >= 0
&& rect.left < screenWidth
&& rect.top >= 0
&& rect.top < screenHeight;
}