hina-cloud-js-sdk 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- package/.eslintrc.js +12 -0
- package/.gitlab-ci.yml +20 -0
- package/.history/build/hotAnalyse.min_20230803204057.js +1 -0
- package/.history/build/hotAnalyse.min_20230803204548.js +338 -0
- package/.history/build/hotAnalyse.min_20230803204555.js +1 -0
- package/.history/build/hotAnalyse.min_20230803204716.js +338 -0
- package/.history/build/hotAnalyse.min_20230803204818.js +1 -0
- package/.history/build/hotAnalyse.min_20230803205007.js +338 -0
- package/.history/build/hotAnalyse.min_20230803205018.js +1 -0
- package/.history/rollup.config_20230803204057.js +57 -0
- package/.history/rollup.config_20230804165355.js +57 -0
- package/babel.config.js +4 -0
- package/build/hina-sa.esm.min.js +1 -0
- package/build/hina-sa.min.js +1 -0
- package/build/hina.esm.min.js +1 -0
- package/build/hina.min.js +1 -0
- package/build/hotAnalyse.min.js +1 -0
- package/package.json +35 -0
- package/report.json +101 -0
- package/rollup.config.js +57 -0
- package/sdk.png +0 -0
- package/sonar-project.properties +3 -0
- package/src/hotAnalyse.js +465 -0
- package/src/monitor/performance-monitor/index.js +103 -0
- package/src/monitor/performance-monitor/web-vitals.js +1 -0
- package/src/pv-sdk/autoTrack.js +648 -0
- package/src/pv-sdk/http.js +425 -0
- package/src/pv-sdk/index.js +405 -0
- package/src/pv-sdk/plugin.js +610 -0
- package/src/pv-sdk/property.js +388 -0
- package/src/pv-sdk/utils.js +1894 -0
@@ -0,0 +1,465 @@
|
|
1
|
+
// jq引入
|
2
|
+
const script = document.createElement("script");
|
3
|
+
script.integrity = "sha384-d7r7Q+K7p1zv6z7dX8fFn3u3pgGz1l4/eUgZGBnKU7gN3k6IjzR4b2q6s7s5+6+7";
|
4
|
+
script.crossOrigin = "anonymous";
|
5
|
+
script.async = "async";
|
6
|
+
script.src = "https://cdn.staticfile.org/jquery/3.0.0/jquery.min.js";
|
7
|
+
script.type = "text/javascript";
|
8
|
+
|
9
|
+
function initHotAnalyse() {
|
10
|
+
// 网页热力点击SDK(勿删勿删勿删)
|
11
|
+
/* eslint-disable */
|
12
|
+
let isClick = false;
|
13
|
+
const infoBoxWidth = 220;
|
14
|
+
const infoBoxheight = 315;
|
15
|
+
const currentWindowHeight =
|
16
|
+
window.innerHeight ||
|
17
|
+
document.documentElement.clientHeight ||
|
18
|
+
document.body.clientHeight ||
|
19
|
+
0;
|
20
|
+
const createShadowBox = (data) => {
|
21
|
+
const {
|
22
|
+
H_element_content,
|
23
|
+
H_element_selector,
|
24
|
+
button_click_prop,
|
25
|
+
button_click_rate,
|
26
|
+
button_click_times,
|
27
|
+
button_click_users,
|
28
|
+
his_H_element_content,
|
29
|
+
} = data;
|
30
|
+
const strContent = `
|
31
|
+
<div style='padding: 8px;'>
|
32
|
+
<div style='color: #CACACA'>当前内容:</div>
|
33
|
+
<div style='white-space:nowrap;overflow:hidden;text-overflow:ellipsis;'>${
|
34
|
+
H_element_content || "-"
|
35
|
+
}</div>
|
36
|
+
</div>
|
37
|
+
<div style='background: #444; height:1px;'></div>
|
38
|
+
<div style='padding: 8px;'>
|
39
|
+
<table style='width:100%;color:#fff;font-size:13px;background:#333;border:1px solid #333;'>
|
40
|
+
<tr>
|
41
|
+
<td>点击次数:</td>
|
42
|
+
<td style='text-align:right;'>${button_click_times}</td>
|
43
|
+
</tr>
|
44
|
+
<tr>
|
45
|
+
<td>点击人数:</td>
|
46
|
+
<td style='text-align:right;'>${button_click_users}</td>
|
47
|
+
</tr>
|
48
|
+
<tr>
|
49
|
+
<td>点击率:</td>
|
50
|
+
<td style='text-align:right;'>${button_click_rate}</td>
|
51
|
+
</tr>
|
52
|
+
<tr>
|
53
|
+
<td>点击占比:</td>
|
54
|
+
<td style='text-align:right;'>${button_click_prop}</td>
|
55
|
+
</tr>
|
56
|
+
</table>
|
57
|
+
</div>
|
58
|
+
<div style='background: #444; height:1px;'></div>
|
59
|
+
<div style='padding: 8px;'>
|
60
|
+
<div style='color: #CACACA;'>历史内容:</div>
|
61
|
+
<div style='white-space:nowrap;overflow:hidden;text-overflow:ellipsis;'>${
|
62
|
+
his_H_element_content || "-"
|
63
|
+
}</div>
|
64
|
+
</div>
|
65
|
+
<div style='background: #444; height:1px;'></div>
|
66
|
+
<div style='padding: 6px 8px;' class='user-list' hina-tag-disable='true'>
|
67
|
+
<p style='color:#2a90e2;text-decoration: none;'>查看用户列表</p>
|
68
|
+
</div>
|
69
|
+
`;
|
70
|
+
const divWrapBox = document.createElement("div");
|
71
|
+
|
72
|
+
document.body.appendChild(divWrapBox);
|
73
|
+
divWrapBox.setAttribute("class", "shadeBoxWrap");
|
74
|
+
// 设置弹出盒子样式
|
75
|
+
divWrapBox.setAttribute(
|
76
|
+
"style",
|
77
|
+
`
|
78
|
+
border-radius:3px;
|
79
|
+
display: none;
|
80
|
+
border:1px solid #000;
|
81
|
+
cursor: pointer;
|
82
|
+
position: absolute;
|
83
|
+
left:0;
|
84
|
+
top:0;
|
85
|
+
background: #333;
|
86
|
+
line-height:24px;
|
87
|
+
font-size:13px;
|
88
|
+
width:${infoBoxWidth}px;
|
89
|
+
height:${infoBoxheight}px;
|
90
|
+
color: #fff;
|
91
|
+
box-shadow: 0 2px 4px rgba(0,0,0,0.24);
|
92
|
+
z-index:9999;
|
93
|
+
`
|
94
|
+
);
|
95
|
+
divWrapBox.innerHTML = strContent;
|
96
|
+
return divWrapBox;
|
97
|
+
};
|
98
|
+
|
99
|
+
function debounce(fn, wait) {
|
100
|
+
let timeout = null;
|
101
|
+
return function () {
|
102
|
+
if (timeout !== null) clearTimeout(timeout);
|
103
|
+
timeout = setTimeout(fn, wait);
|
104
|
+
};
|
105
|
+
}
|
106
|
+
|
107
|
+
function throttle(fn, wait) {
|
108
|
+
let pre = Date.now();
|
109
|
+
return function () {
|
110
|
+
const _this = this;
|
111
|
+
const _arguments = arguments;
|
112
|
+
const now = Date.now();
|
113
|
+
if (now - pre >= wait) {
|
114
|
+
fn.apply(_this, _arguments);
|
115
|
+
pre = Date.now();
|
116
|
+
}
|
117
|
+
};
|
118
|
+
}
|
119
|
+
|
120
|
+
const posEleBetweenspace = (arr, num) => {
|
121
|
+
for (let i = 0; i < arr.length; i++) {
|
122
|
+
if (arr[i] > num) {
|
123
|
+
return i;
|
124
|
+
}
|
125
|
+
}
|
126
|
+
};
|
127
|
+
|
128
|
+
function setCssStyle(css) {
|
129
|
+
const style = document.createElement("style");
|
130
|
+
style.type = "text/css";
|
131
|
+
try {
|
132
|
+
style.appendChild(document.createTextNode(css));
|
133
|
+
} catch (e) {
|
134
|
+
style.styleSheet.cssText = css;
|
135
|
+
}
|
136
|
+
const head = document.getElementsByTagName("head")[0];
|
137
|
+
const firstScript = document.getElementsByTagName("script")[0];
|
138
|
+
if (head) {
|
139
|
+
if (head.children.length) {
|
140
|
+
head.insertBefore(style, head.children[0]);
|
141
|
+
} else {
|
142
|
+
head.appendChild(style);
|
143
|
+
}
|
144
|
+
} else {
|
145
|
+
firstScript.parentNode.insertBefore(style, firstScript);
|
146
|
+
}
|
147
|
+
}
|
148
|
+
|
149
|
+
const customHotClickCss = `
|
150
|
+
.hina-click-area:before{
|
151
|
+
pointer-events: none;
|
152
|
+
cursor: pointer;
|
153
|
+
content: "";
|
154
|
+
width: 100%;
|
155
|
+
position: absolute;
|
156
|
+
left: 0;
|
157
|
+
top: 0;
|
158
|
+
bottom: 0;
|
159
|
+
background: rgba(56, 166, 251, 0.6);
|
160
|
+
box-shadow: rgba(0, 0, 0, 0.6) 0px 2px 4px;
|
161
|
+
cursor: pointer
|
162
|
+
}
|
163
|
+
.hina-click-area:after{
|
164
|
+
pointer-events: none;
|
165
|
+
height: 14px;
|
166
|
+
line-height: 14px;
|
167
|
+
margin: -7px 0 0 -28px;
|
168
|
+
width: 56px;
|
169
|
+
color: #fff;
|
170
|
+
content: attr(data-click);
|
171
|
+
font-size: 14px;
|
172
|
+
font-weight: 700;
|
173
|
+
left: 50%;
|
174
|
+
line-height: 1em;
|
175
|
+
position: absolute;
|
176
|
+
text-align: center;
|
177
|
+
text-indent: 0;
|
178
|
+
text-shadow: 1px 1px 2px #000;
|
179
|
+
top: 50%;
|
180
|
+
z-index: 999;
|
181
|
+
}
|
182
|
+
`;
|
183
|
+
setCssStyle(customHotClickCss);
|
184
|
+
window.addEventListener(
|
185
|
+
"message",
|
186
|
+
(event) => {
|
187
|
+
const type = event.data?.type;
|
188
|
+
// console.log("type===>", type);
|
189
|
+
// 浏览图
|
190
|
+
if (type === "view") {
|
191
|
+
// 清空点击图
|
192
|
+
// 监听屏幕尺寸变化
|
193
|
+
const lastGraphData = event.data?.lastGraph;
|
194
|
+
$("body > .shadeBoxWrap").remove();
|
195
|
+
// 如果为空,则使用上次的缓存数据清空页面渲染点
|
196
|
+
for (let i = 0; i < lastGraphData?.length; i++) {
|
197
|
+
const { H_element_selector } = lastGraphData[i];
|
198
|
+
$(H_element_selector).removeClass("hina-click-area");
|
199
|
+
}
|
200
|
+
isClick = false;
|
201
|
+
window.removeEventListener("scroll", () => {}, true);
|
202
|
+
// console.log(event.data?.viewData);
|
203
|
+
const viewRenderData = event.data?.viewData?.[0];
|
204
|
+
const baseLineArrays = [
|
205
|
+
viewRenderData?.view100,
|
206
|
+
viewRenderData?.view90,
|
207
|
+
viewRenderData?.view80,
|
208
|
+
viewRenderData?.view70,
|
209
|
+
viewRenderData?.view60,
|
210
|
+
viewRenderData?.view50,
|
211
|
+
viewRenderData?.view40,
|
212
|
+
viewRenderData?.view30,
|
213
|
+
viewRenderData?.view20,
|
214
|
+
viewRenderData?.view10,
|
215
|
+
]?.map((i) => i + currentWindowHeight);
|
216
|
+
const percentageArr = new Array(10)
|
217
|
+
.fill(1)
|
218
|
+
.map((item, index) => (index + 1) * 10)
|
219
|
+
.reverse(); // [100,90,80...]
|
220
|
+
const viewBaseInnerSpan = (percent) => `
|
221
|
+
<span
|
222
|
+
style='
|
223
|
+
font-size:12px;
|
224
|
+
position:absolute;
|
225
|
+
padding: 0 12px;
|
226
|
+
top: -24px;
|
227
|
+
height: 26px;
|
228
|
+
line-height: 26px;
|
229
|
+
left: 0;
|
230
|
+
background: #E6F1FB;
|
231
|
+
color: #0174df;
|
232
|
+
border-radius: 2px;
|
233
|
+
'>
|
234
|
+
${percent}%
|
235
|
+
</span>
|
236
|
+
`;
|
237
|
+
for (let i = 0; i < percentageArr.length; i++) {
|
238
|
+
const viewBaseLine = document.createElement("div");
|
239
|
+
viewBaseLine.setAttribute(
|
240
|
+
"style",
|
241
|
+
`
|
242
|
+
border-bottom: 1px dashed #0174df;
|
243
|
+
height: 1px;
|
244
|
+
width: 100%;
|
245
|
+
position: absolute;
|
246
|
+
top: 50px;
|
247
|
+
z-index:999;
|
248
|
+
`
|
249
|
+
);
|
250
|
+
viewBaseLine.setAttribute("class", "viewBaseLineStyle");
|
251
|
+
const strParam = viewRenderData[`view${percentageArr[i]}`];
|
252
|
+
viewBaseLine.style.top = `${strParam + currentWindowHeight}px`;
|
253
|
+
if (i !== 0) {
|
254
|
+
if (
|
255
|
+
viewRenderData[`view${percentageArr[i]}`] -
|
256
|
+
viewRenderData[`view${percentageArr[i - 1]}`] >
|
257
|
+
26
|
258
|
+
) {
|
259
|
+
viewBaseLine.innerHTML = viewBaseInnerSpan(percentageArr[i]);
|
260
|
+
}
|
261
|
+
} else if (viewRenderData[`view${percentageArr[i]}`] > 26) {
|
262
|
+
viewBaseLine.innerHTML = viewBaseInnerSpan(percentageArr[i]);
|
263
|
+
}
|
264
|
+
|
265
|
+
document.body.appendChild(viewBaseLine);
|
266
|
+
}
|
267
|
+
const viewMoveLine = document.createElement("div");
|
268
|
+
const moveLineInnerTag = (val) => `
|
269
|
+
<span
|
270
|
+
style='
|
271
|
+
font-size:12px;
|
272
|
+
height:26px;
|
273
|
+
line-height: 26px;
|
274
|
+
background: #E6F1FB;
|
275
|
+
color: #0174df;
|
276
|
+
border-radius: 2px;
|
277
|
+
left:50%;
|
278
|
+
margin-left:-65px;
|
279
|
+
position: absolute;
|
280
|
+
top:-13px;
|
281
|
+
padding: 0 5px;
|
282
|
+
'>
|
283
|
+
${val || 0}%的用户浏览到这里
|
284
|
+
</span>
|
285
|
+
`;
|
286
|
+
viewMoveLine.setAttribute(
|
287
|
+
"style",
|
288
|
+
`
|
289
|
+
border-bottom: 1px solid #0174df;
|
290
|
+
height: 1px;
|
291
|
+
width: 100%;
|
292
|
+
position: absolute;
|
293
|
+
z-index: 9999;
|
294
|
+
top: 0;
|
295
|
+
`
|
296
|
+
);
|
297
|
+
viewMoveLine.setAttribute("class", "viewMoveLineStyle");
|
298
|
+
document.body.appendChild(viewMoveLine);
|
299
|
+
$(document).on(
|
300
|
+
"mousemove",
|
301
|
+
throttle((e) => {
|
302
|
+
viewMoveLine.style.top = `${e.pageY}px`;
|
303
|
+
// 获取当前值在那个区间的index
|
304
|
+
const spaceIndex = posEleBetweenspace(baseLineArrays, e.pageY);
|
305
|
+
// 获取当前值在那个区间的值
|
306
|
+
const spaceShowValue = percentageArr[spaceIndex];
|
307
|
+
viewMoveLine.innerHTML = moveLineInnerTag(spaceShowValue);
|
308
|
+
}, 150)
|
309
|
+
);
|
310
|
+
} else if (type === "click") {
|
311
|
+
// 清空浏览图横线
|
312
|
+
isClick = true;
|
313
|
+
$("body > .viewBaseLineStyle").remove();
|
314
|
+
$("body > .viewMoveLineStyle").remove();
|
315
|
+
// 点击图渲染数据
|
316
|
+
const renderData = event.data?.hotData;
|
317
|
+
// 监听屏幕尺寸变化
|
318
|
+
const isResize = event.data?.isResize;
|
319
|
+
const lastGraphData = event.data?.lastGraph;
|
320
|
+
isResize && $("body > .shadeBoxWrap").remove();
|
321
|
+
// 如果为空,则使用上次的缓存数据清空页面渲染点
|
322
|
+
if (!renderData?.length) {
|
323
|
+
for (let i = 0; i < lastGraphData?.length; i++) {
|
324
|
+
const { H_element_selector } = lastGraphData[i];
|
325
|
+
$(H_element_selector).removeClass("hina-click-area");
|
326
|
+
}
|
327
|
+
return;
|
328
|
+
}
|
329
|
+
|
330
|
+
for (let i = 0; i < renderData?.length; i++) {
|
331
|
+
const {
|
332
|
+
H_element_content,
|
333
|
+
H_element_selector,
|
334
|
+
button_click_prop,
|
335
|
+
button_click_rate,
|
336
|
+
button_click_times,
|
337
|
+
button_click_users,
|
338
|
+
his_H_element_content,
|
339
|
+
} = renderData[i];
|
340
|
+
const hinClickArea = document.createElement("div");
|
341
|
+
// 设置弹出盒子样式
|
342
|
+
hinClickArea.setAttribute(
|
343
|
+
"style",
|
344
|
+
`position: relative;
|
345
|
+
`
|
346
|
+
);
|
347
|
+
hinClickArea.setAttribute("class", "hina-click-area");
|
348
|
+
// 设置conteng内容
|
349
|
+
const divWrapBox = createShadowBox(renderData[i]);
|
350
|
+
const currentBoxOffsetLeft = $(H_element_selector)?.offset()?.left;
|
351
|
+
const currentBoxScrollY = window.scrollY;
|
352
|
+
const currentWindowWidth = $(window).width();
|
353
|
+
const currentWindowHeight =
|
354
|
+
window.innerHeight ||
|
355
|
+
document.documentElement.clientHeight ||
|
356
|
+
document.body.clientHeight ||
|
357
|
+
0;
|
358
|
+
const currentBoxPosTop = $(H_element_selector)?.position()?.top;
|
359
|
+
const currentBoxOffsetTop = $(H_element_selector)?.offset()?.top;
|
360
|
+
const currentBoxWidth = $(H_element_selector)?.css("width");
|
361
|
+
const currentBoxHeight = $(H_element_selector)?.css("height");
|
362
|
+
|
363
|
+
$(H_element_selector).addClass("hina-click-area");
|
364
|
+
$(H_element_selector).css("position", "relative");
|
365
|
+
$(`${H_element_selector}:before`).css("position", "absolute");
|
366
|
+
$(H_element_selector).attr("data-click", button_click_rate);
|
367
|
+
if (
|
368
|
+
[currentBoxWidth, currentBoxHeight, "undefined"].includes("0px")
|
369
|
+
) {
|
370
|
+
$(H_element_selector).removeClass("display", "inline-block");
|
371
|
+
$(H_element_selector).css("width", "15px");
|
372
|
+
$(H_element_selector).css("height", "15px");
|
373
|
+
}
|
374
|
+
|
375
|
+
$(H_element_selector).mouseenter(() => {
|
376
|
+
$("body > .shadeBoxWrap > .user-list > p").attr(
|
377
|
+
"drillSelector",
|
378
|
+
H_element_selector
|
379
|
+
);
|
380
|
+
// 兼容头部固定,内容滚动场景
|
381
|
+
if (currentBoxOffsetTop < 50) {
|
382
|
+
// console.log(currentBoxOffsetTop)
|
383
|
+
$("body > .shadeBoxWrap").css("position", "fixed");
|
384
|
+
} else {
|
385
|
+
// console.log(currentBoxOffsetTop)
|
386
|
+
$("body > .shadeBoxWrap").css("position", "absolute");
|
387
|
+
}
|
388
|
+
// console.log('222', currentWindowHeight)
|
389
|
+
divWrapBox.style.display = "block";
|
390
|
+
|
391
|
+
const currentBoxLeft = currentWindowWidth - currentBoxOffsetLeft;
|
392
|
+
|
393
|
+
if (currentBoxLeft < infoBoxWidth) {
|
394
|
+
divWrapBox.style.left = `${
|
395
|
+
currentBoxOffsetLeft - infoBoxWidth
|
396
|
+
}px`;
|
397
|
+
if (currentBoxOffsetTop > infoBoxheight) {
|
398
|
+
divWrapBox.style.top = `${
|
399
|
+
currentBoxOffsetTop +
|
400
|
+
parseFloat(currentBoxHeight) -
|
401
|
+
infoBoxheight
|
402
|
+
}px`;
|
403
|
+
} else {
|
404
|
+
divWrapBox.style.top = `${currentBoxOffsetTop}px`;
|
405
|
+
}
|
406
|
+
return;
|
407
|
+
}
|
408
|
+
divWrapBox.style.left = `${currentBoxOffsetLeft}px`;
|
409
|
+
|
410
|
+
// 根据盒子高度设置显示方向
|
411
|
+
if (currentWindowHeight - currentBoxOffsetTop > infoBoxheight) {
|
412
|
+
if (window.scrollY > infoBoxheight) {
|
413
|
+
divWrapBox.style.top = `${
|
414
|
+
currentBoxPosTop + parseFloat(currentBoxHeight)
|
415
|
+
}px`;
|
416
|
+
} else {
|
417
|
+
divWrapBox.style.top = `${
|
418
|
+
currentBoxOffsetTop +
|
419
|
+
parseFloat(currentBoxHeight) -
|
420
|
+
currentBoxScrollY
|
421
|
+
}px`;
|
422
|
+
}
|
423
|
+
} else {
|
424
|
+
divWrapBox.style.top = `${currentBoxOffsetTop - infoBoxheight}px`;
|
425
|
+
}
|
426
|
+
});
|
427
|
+
$(H_element_selector).mouseleave(() => {
|
428
|
+
// divWrapBox.style.display = 'none'
|
429
|
+
$("body > .shadeBoxWrap").css("display", "none");
|
430
|
+
});
|
431
|
+
|
432
|
+
divWrapBox.addEventListener("mouseenter", () => {
|
433
|
+
divWrapBox.style.display = "block";
|
434
|
+
});
|
435
|
+
divWrapBox.addEventListener("mouseleave", () => {
|
436
|
+
// divWrapBox.style.display = 'none'
|
437
|
+
$("body > .shadeBoxWrap").css("display", "none");
|
438
|
+
});
|
439
|
+
}
|
440
|
+
$("body > .shadeBoxWrap > .user-list > p").on("click", (e) => {
|
441
|
+
event.stopPropagation();
|
442
|
+
window.parent.postMessage(
|
443
|
+
{ eleSelector: e?.target?.attributes?.drillselector?.value },
|
444
|
+
"*"
|
445
|
+
);
|
446
|
+
});
|
447
|
+
}
|
448
|
+
},
|
449
|
+
false
|
450
|
+
);
|
451
|
+
|
452
|
+
window.addEventListener(
|
453
|
+
"scroll",
|
454
|
+
debounce(() => {
|
455
|
+
isClick && window.parent.postMessage({ isRefush: true }, "*");
|
456
|
+
}, 800),
|
457
|
+
true
|
458
|
+
);
|
459
|
+
/* eslint-enable */
|
460
|
+
// 网页热力点击SDK(勿删勿删勿删)
|
461
|
+
}
|
462
|
+
|
463
|
+
script.addEventListener("load", initHotAnalyse)
|
464
|
+
|
465
|
+
document.head.appendChild(script);
|
@@ -0,0 +1,103 @@
|
|
1
|
+
import {
|
2
|
+
onCLS,
|
3
|
+
onFCP,
|
4
|
+
onFID,
|
5
|
+
onFP,
|
6
|
+
onINP,
|
7
|
+
onLCP,
|
8
|
+
onTTFB,
|
9
|
+
observe,
|
10
|
+
} from "./web-vitals";
|
11
|
+
|
12
|
+
const { PerformanceObserver, performance } = window;
|
13
|
+
|
14
|
+
const getPerformanceTiming = () => {
|
15
|
+
return new Promise((resolve) => {
|
16
|
+
if (PerformanceObserver.supportedEntryTypes.includes("navigation")) {
|
17
|
+
observe("navigation", (entries) => {
|
18
|
+
entries.forEach((entry) => {
|
19
|
+
const data = {
|
20
|
+
href: entry.name,
|
21
|
+
|
22
|
+
// 首字节
|
23
|
+
firstByteTime: entry.responseStart,
|
24
|
+
// 卸载页面(unload)
|
25
|
+
unloadTime: entry.unloadEventEnd - entry.unloadEventStart,
|
26
|
+
// 重定向(redirect)
|
27
|
+
redirectTime: entry.redirectEnd - entry.redirectStart,
|
28
|
+
// 检查缓存(cache)
|
29
|
+
cacheCheckTime: entry.fetchStart,
|
30
|
+
|
31
|
+
// DNS查询耗时
|
32
|
+
dnsTime: entry.domainLookupEnd - entry.domainLookupStart,
|
33
|
+
// TCP连接耗时
|
34
|
+
tcpTime: entry.connectEnd - entry.connectStart,
|
35
|
+
// ssl耗时
|
36
|
+
sslTime: entry.secureConnectionStart > 0 ? entry.connectEnd - entry.secureConnectionStart : 0,
|
37
|
+
// 首字节响应时间(TTFB)
|
38
|
+
firstByteResponseTime: entry.responseStart - entry.requestStart,
|
39
|
+
|
40
|
+
// DOM Ready
|
41
|
+
domReadyTime: entry.domContentLoadedEventEnd - entry.fetchStart,
|
42
|
+
//内容传输
|
43
|
+
contentTransferTime: entry.responseStart,
|
44
|
+
//DOM解析
|
45
|
+
domParsingTime: entry.domInteractive - entry.responseEnd,
|
46
|
+
|
47
|
+
//页面完全加载
|
48
|
+
pageLoadTime: entry.loadEventEnd - entry.fetchStart,
|
49
|
+
//资源加载
|
50
|
+
resourceLoadTime: entry.loadEventStart - entry.domContentLoadedEventEnd,
|
51
|
+
//load事件时间
|
52
|
+
loadEventTime: entry.loadEventEnd,
|
53
|
+
};
|
54
|
+
|
55
|
+
resolve(data)
|
56
|
+
});
|
57
|
+
});
|
58
|
+
} else {
|
59
|
+
const entry = performance.timing;
|
60
|
+
const data = {
|
61
|
+
href: location.href,
|
62
|
+
|
63
|
+
// 首字节
|
64
|
+
firstByteTime: entry.responseStart - entry.navigationStart,
|
65
|
+
// 卸载页面(unload)
|
66
|
+
unloadTime: entry.unloadEventEnd - entry.unloadEventStart,
|
67
|
+
// 重定向(redirect)
|
68
|
+
redirectTime: entry.redirectEnd - entry.redirectStart,
|
69
|
+
// 检查缓存(cache)
|
70
|
+
cacheCheckTime: entry.fetchStart - entry.navigationStart,
|
71
|
+
|
72
|
+
// DNS查询耗时
|
73
|
+
dnsTime: entry.domainLookupEnd - entry.domainLookupStart,
|
74
|
+
// TCP连接耗时
|
75
|
+
tcpTime: entry.connectEnd - entry.connectStart,
|
76
|
+
// ssl耗时
|
77
|
+
sslTime: entry.secureConnectionStart > 0 ? entry.connectEnd - entry.secureConnectionStart : 0,
|
78
|
+
// 首字节响应时间(TTFB)
|
79
|
+
firstByteResponseTime: entry.responseStart - entry.requestStart,
|
80
|
+
|
81
|
+
// DOM Ready
|
82
|
+
domReadyTime: entry.domContentLoadedEventEnd - entry.fetchStart,
|
83
|
+
//内容传输
|
84
|
+
contentTransferTime: entry.responseStart - entry.navigationStart,
|
85
|
+
// DOM解析
|
86
|
+
domParsingTime: entry.domInteractive - entry.responseEnd,
|
87
|
+
|
88
|
+
//页面完全加载
|
89
|
+
pageLoadTime: entry.loadEventEnd - entry.fetchStart,
|
90
|
+
//资源加载
|
91
|
+
resourceLoadTime: entry.loadEventStart - entry.domContentLoadedEventEnd,
|
92
|
+
//load事件时间
|
93
|
+
loadEventTime: entry.loadEventEnd - entry.navigationStart,
|
94
|
+
};
|
95
|
+
resolve(data)
|
96
|
+
}
|
97
|
+
});
|
98
|
+
};
|
99
|
+
|
100
|
+
const performanceCallback = (data) => {
|
101
|
+
// 获取指标名 评分 数值
|
102
|
+
const { name, rating, value } = data;
|
103
|
+
};
|
@@ -0,0 +1 @@
|
|
1
|
+
var e,n,t,i,r,a=-1,o=function(e){addEventListener("pageshow",(function(n){n.persisted&&(a=n.timeStamp,e(n))}),!0)},c=function(){return window.performance&&performance.getEntriesByType&&performance.getEntriesByType("navigation")[0]},u=function(){var e=c();return e&&e.activationStart||0},f=function(e,n){var t=c(),i="navigate";return a>=0?i="back-forward-cache":t&&(document.prerendering||u()>0?i="prerender":document.wasDiscarded?i="restore":t.type&&(i=t.type.replace(/_/g,"-"))),{name:e,value:void 0===n?-1:n,rating:"good",delta:0,entries:[],id:"v3-".concat(Date.now(),"-").concat(Math.floor(8999999999999*Math.random())+1e12),navigationType:i}},s=function(e,n,t){try{if(PerformanceObserver.supportedEntryTypes.includes(e)){var i=new PerformanceObserver((function(e){Promise.resolve().then((function(){n(e.getEntries())}))}));return i.observe(Object.assign({type:e,buffered:!0},t||{})),i}}catch(e){}},d=function(e,n,t,i){var r,a;return function(o){n.value>=0&&(o||i)&&((a=n.value-(r||0))||void 0===r)&&(r=n.value,n.delta=a,n.rating=function(e,n){return e>n[1]?"poor":e>n[0]?"needs-improvement":"good"}(n.value,t),e(n))}},l=function(e){requestAnimationFrame((function(){return requestAnimationFrame((function(){return e()}))}))},p=function(e){var n=function(n){"pagehide"!==n.type&&"hidden"!==document.visibilityState||e(n)};addEventListener("visibilitychange",n,!0),addEventListener("pagehide",n,!0)},v=function(e){var n=!1;return function(t){n||(e(t),n=!0)}},m=-1,h=function(){return"hidden"!==document.visibilityState||document.prerendering?1/0:0},g=function(e){"hidden"===document.visibilityState&&m>-1&&(m="visibilitychange"===e.type?e.timeStamp:0,T())},y=function(){addEventListener("visibilitychange",g,!0),addEventListener("prerenderingchange",g,!0)},T=function(){removeEventListener("visibilitychange",g,!0),removeEventListener("prerenderingchange",g,!0)},E=function(){return m<0&&(m=h(),y(),o((function(){setTimeout((function(){m=h(),y()}),0)}))),{get firstHiddenTime(){return m}}},C=function(e){document.prerendering?addEventListener("prerenderingchange",(function(){return e()}),!0):e()},L=[1800,3e3],w=function(e,n){n=n||{},C((function(){var t,i=E(),r=f("FCP"),a=s("paint",(function(e){e.forEach((function(e){"first-contentful-paint"===e.name&&(a.disconnect(),e.startTime<i.firstHiddenTime&&(r.value=Math.max(e.startTime-u(),0),r.entries.push(e),t(!0)))}))}));a&&(t=d(e,r,L,n.reportAllChanges),o((function(i){r=f("FCP"),t=d(e,r,L,n.reportAllChanges),l((function(){r.value=performance.now()-i.timeStamp,t(!0)}))})))}))},S=[.1,.25],b=function(e,n){n=n||{},w(v((function(){var t,i=f("CLS",0),r=0,a=[],c=function(e){e.forEach((function(e){if(!e.hadRecentInput){var n=a[0],t=a[a.length-1];r&&e.startTime-t.startTime<1e3&&e.startTime-n.startTime<5e3?(r+=e.value,a.push(e)):(r=e.value,a=[e])}})),r>i.value&&(i.value=r,i.entries=a,t())},u=s("layout-shift",c);u&&(t=d(e,i,S,n.reportAllChanges),p((function(){c(u.takeRecords()),t(!0)})),o((function(){r=0,i=f("CLS",0),t=d(e,i,S,n.reportAllChanges),l((function(){return t()}))})),setTimeout(t,0))})))},A=[1800,3e3],P=function(e,n){n=n||{},C((function(){var t,i=E(),r=f("FP"),a=s("paint",(function(e){e.forEach((function(e){"first-paint"===e.name&&(a.disconnect(),e.startTime<i.firstHiddenTime&&(r.value=Math.max(e.startTime-u(),0),r.entries.push(e),t(!0)))}))}));a&&(t=d(e,r,A,n.reportAllChanges),o((function(i){r=f("FP"),t=d(e,r,A,n.reportAllChanges),l((function(){r.value=performance.now()-i.timeStamp,t(!0)}))})))}))},F={passive:!0,capture:!0},I=new Date,M=function(i,r){e||(e=r,n=i,t=new Date,x(removeEventListener),k())},k=function(){if(n>=0&&n<t-I){var r={entryType:"first-input",name:e.type,target:e.target,cancelable:e.cancelable,startTime:e.timeStamp,processingStart:e.timeStamp+n};i.forEach((function(e){e(r)})),i=[]}},D=function(e){if(e.cancelable){var n=(e.timeStamp>1e12?new Date:performance.now())-e.timeStamp;"pointerdown"==e.type?function(e,n){var t=function(){M(e,n),r()},i=function(){r()},r=function(){removeEventListener("pointerup",t,F),removeEventListener("pointercancel",i,F)};addEventListener("pointerup",t,F),addEventListener("pointercancel",i,F)}(n,e):M(n,e)}},x=function(e){["mousedown","keydown","touchstart","pointerdown"].forEach((function(n){return e(n,D,F)}))},B=[100,300],H=function(t,r){r=r||{},C((function(){var a,c=E(),u=f("FID"),l=function(e){e.startTime<c.firstHiddenTime&&(u.value=e.processingStart-e.startTime,u.entries.push(e),a(!0))},m=function(e){e.forEach(l)},h=s("first-input",m);a=d(t,u,B,r.reportAllChanges),h&&p(v((function(){m(h.takeRecords()),h.disconnect()}))),h&&o((function(){var o;u=f("FID"),a=d(t,u,B,r.reportAllChanges),i=[],n=-1,e=null,x(addEventListener),o=l,i.push(o),k()}))}))},R=0,N=1/0,O=0,q=function(e){e.forEach((function(e){e.interactionId&&(N=Math.min(N,e.interactionId),O=Math.max(O,e.interactionId),R=O?(O-N)/7+1:0)}))},j=function(){return r?R:performance.interactionCount||0},_=function(){"interactionCount"in performance||r||(r=s("event",q,{type:"event",buffered:!0,durationThreshold:0}))},z=[200,500],G=0,J=function(){return j()-G},K=[],Q={},U=function(e){var n=K[K.length-1],t=Q[e.interactionId];if(t||K.length<10||e.duration>n.latency){if(t)t.entries.push(e),t.latency=Math.max(t.latency,e.duration);else{var i={id:e.interactionId,latency:e.duration,entries:[e]};Q[i.id]=i,K.push(i)}K.sort((function(e,n){return n.latency-e.latency})),K.splice(10).forEach((function(e){delete Q[e.id]}))}},V=function(e,n){n=n||{},C((function(){_();var t,i=f("INP"),r=function(e){e.forEach((function(e){(e.interactionId&&U(e),"first-input"===e.entryType)&&(!K.some((function(n){return n.entries.some((function(n){return e.duration===n.duration&&e.startTime===n.startTime}))}))&&U(e))}));var n,r=(n=Math.min(K.length-1,Math.floor(J()/50)),K[n]);r&&r.latency!==i.value&&(i.value=r.latency,i.entries=r.entries,t())},a=s("event",r,{durationThreshold:n.durationThreshold||40});t=d(e,i,z,n.reportAllChanges),a&&(a.observe({type:"first-input",buffered:!0}),p((function(){r(a.takeRecords()),i.value<0&&J()>0&&(i.value=0,i.entries=[]),t(!0)})),o((function(){K=[],G=j(),i=f("INP"),t=d(e,i,z,n.reportAllChanges)})))}))},W=[2500,4e3],X={},Y=function(e,n){n=n||{},C((function(){var t,i=E(),r=f("LCP"),a=function(e){var n=e[e.length-1];n&&n.startTime<i.firstHiddenTime&&(r.value=Math.max(n.startTime-u(),0),r.entries=[n],t())},c=s("largest-contentful-paint",a);if(c){t=d(e,r,W,n.reportAllChanges);var m=v((function(){X[r.id]||(a(c.takeRecords()),c.disconnect(),X[r.id]=!0,t(!0))}));["keydown","click"].forEach((function(e){addEventListener(e,m,!0)})),p(m),o((function(i){r=f("LCP"),t=d(e,r,W,n.reportAllChanges),l((function(){r.value=performance.now()-i.timeStamp,X[r.id]=!0,t(!0)}))}))}}))},Z=[800,1800],$=function e(n){document.prerendering?C((function(){return e(n)})):"complete"!==document.readyState?addEventListener("load",(function(){return e(n)}),!0):setTimeout(n,0)},ee=function(e,n){n=n||{};var t=f("TTFB"),i=d(e,t,Z,n.reportAllChanges);$((function(){var r=c();if(r){var a=r.responseStart;if(a<=0||a>performance.now())return;t.value=Math.max(a-u(),0),t.entries=[r],i(!0),o((function(){t=f("TTFB",0),(i=d(e,t,Z,n.reportAllChanges))(!0)}))}}))};export{S as CLSThresholds,L as FCPThresholds,B as FIDThresholds,A as FPThresholds,z as INPThresholds,W as LCPThresholds,Z as TTFBThresholds,b as getCLS,w as getFCP,H as getFID,P as getFP,V as getINP,Y as getLCP,ee as getTTFB,s as observe,b as onCLS,w as onFCP,H as onFID,P as onFP,V as onINP,Y as onLCP,ee as onTTFB};
|