spy-client 2.1.1 → 2.1.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +9 -3
- package/coverage/Chrome 92.0.4515.159 (Mac OS 10.15.7)/html/base.css +224 -0
- package/coverage/Chrome 92.0.4515.159 (Mac OS 10.15.7)/html/basicSpec.ts.html +740 -0
- package/coverage/Chrome 92.0.4515.159 (Mac OS 10.15.7)/html/block-navigation.js +79 -0
- package/coverage/Chrome 92.0.4515.159 (Mac OS 10.15.7)/html/checkSpec.ts.html +479 -0
- package/coverage/Chrome 92.0.4515.159 (Mac OS 10.15.7)/html/favicon.png +0 -0
- package/coverage/Chrome 92.0.4515.159 (Mac OS 10.15.7)/html/headSpec.ts.html +695 -0
- package/coverage/Chrome 92.0.4515.159 (Mac OS 10.15.7)/html/index.html +171 -0
- package/coverage/Chrome 92.0.4515.159 (Mac OS 10.15.7)/html/markSpec.ts.html +284 -0
- package/coverage/Chrome 92.0.4515.159 (Mac OS 10.15.7)/html/metricSpec.ts.html +977 -0
- package/coverage/Chrome 92.0.4515.159 (Mac OS 10.15.7)/html/prettify.css +1 -0
- package/coverage/Chrome 92.0.4515.159 (Mac OS 10.15.7)/html/prettify.js +2 -0
- package/coverage/Chrome 92.0.4515.159 (Mac OS 10.15.7)/html/sort-arrow-sprite.png +0 -0
- package/coverage/Chrome 92.0.4515.159 (Mac OS 10.15.7)/html/sorter.js +170 -0
- package/coverage/Chrome 95.0.4638.69 (Mac OS 10.15.7)/html/base.css +224 -0
- package/coverage/Chrome 95.0.4638.69 (Mac OS 10.15.7)/html/basicSpec.ts.html +740 -0
- package/coverage/Chrome 95.0.4638.69 (Mac OS 10.15.7)/html/block-navigation.js +79 -0
- package/coverage/Chrome 95.0.4638.69 (Mac OS 10.15.7)/html/checkSpec.ts.html +479 -0
- package/coverage/Chrome 95.0.4638.69 (Mac OS 10.15.7)/html/favicon.png +0 -0
- package/coverage/Chrome 95.0.4638.69 (Mac OS 10.15.7)/html/headSpec.ts.html +695 -0
- package/coverage/Chrome 95.0.4638.69 (Mac OS 10.15.7)/html/index.html +171 -0
- package/coverage/Chrome 95.0.4638.69 (Mac OS 10.15.7)/html/markSpec.ts.html +284 -0
- package/coverage/Chrome 95.0.4638.69 (Mac OS 10.15.7)/html/metricSpec.ts.html +977 -0
- package/coverage/Chrome 95.0.4638.69 (Mac OS 10.15.7)/html/prettify.css +1 -0
- package/coverage/Chrome 95.0.4638.69 (Mac OS 10.15.7)/html/prettify.js +2 -0
- package/coverage/Chrome 95.0.4638.69 (Mac OS 10.15.7)/html/sort-arrow-sprite.png +0 -0
- package/coverage/Chrome 95.0.4638.69 (Mac OS 10.15.7)/html/sorter.js +170 -0
- package/coverage/Chrome 96.0.4664.55 (Mac OS 10.15.7)/html/base.css +224 -0
- package/coverage/Chrome 96.0.4664.55 (Mac OS 10.15.7)/html/basicSpec.ts.html +740 -0
- package/coverage/Chrome 96.0.4664.55 (Mac OS 10.15.7)/html/block-navigation.js +79 -0
- package/coverage/Chrome 96.0.4664.55 (Mac OS 10.15.7)/html/checkSpec.ts.html +479 -0
- package/coverage/Chrome 96.0.4664.55 (Mac OS 10.15.7)/html/favicon.png +0 -0
- package/coverage/Chrome 96.0.4664.55 (Mac OS 10.15.7)/html/headSpec.ts.html +695 -0
- package/coverage/Chrome 96.0.4664.55 (Mac OS 10.15.7)/html/index.html +171 -0
- package/coverage/Chrome 96.0.4664.55 (Mac OS 10.15.7)/html/markSpec.ts.html +284 -0
- package/coverage/Chrome 96.0.4664.55 (Mac OS 10.15.7)/html/metricSpec.ts.html +977 -0
- package/coverage/Chrome 96.0.4664.55 (Mac OS 10.15.7)/html/prettify.css +1 -0
- package/coverage/Chrome 96.0.4664.55 (Mac OS 10.15.7)/html/prettify.js +2 -0
- package/coverage/Chrome 96.0.4664.55 (Mac OS 10.15.7)/html/sort-arrow-sprite.png +0 -0
- package/coverage/Chrome 96.0.4664.55 (Mac OS 10.15.7)/html/sorter.js +170 -0
- package/dist/head/base.d.ts +12 -7
- package/dist/lib/interface.d.ts +4 -0
- package/dist/spy-client-basic.d.ts +5 -1
- package/dist/spy-client-basic.esm.js +82 -21
- package/dist/spy-client-basic.iife.js +82 -21
- package/dist/spy-client-basic.iife.min.js +1 -1
- package/dist/spy-client-basic.js +82 -21
- package/dist/spy-client-basic.min.js +1 -1
- package/dist/spy-client-basic.mjs +82 -21
- package/dist/spy-client.d.ts +1 -0
- package/dist/spy-client.esm.js +109 -31
- package/dist/spy-client.iife.js +109 -31
- package/dist/spy-client.iife.min.js +1 -1
- package/dist/spy-client.js +109 -31
- package/dist/spy-client.min.js +1 -1
- package/dist/spy-client.mjs +109 -31
- package/dist/spy-head.js +46 -34
- package/dist/spy-head.min.js +1 -1
- package/dist/spy-local-cache.js +126 -64
- package/dist/spy-local-cache.min.js +1 -1
- package/example/local-cache.html +77 -15
- package/package.json +4 -12
- package/src/head/base.ts +37 -4
- package/src/head/error.ts +9 -19
- package/src/head/whitescreen.ts +14 -14
- package/src/lib/huffman.ts +37 -56
- package/src/lib/interface.ts +5 -0
- package/src/spy-client-basic.ts +95 -26
- package/src/spy-client.ts +19 -1
- package/src/spy-local-cache.ts +103 -10
package/dist/spy-client.iife.js
CHANGED
|
@@ -2,18 +2,18 @@ var SpyClient = (function () {
|
|
|
2
2
|
'use strict';
|
|
3
3
|
|
|
4
4
|
/*! *****************************************************************************
|
|
5
|
-
Copyright (c) Microsoft Corporation.
|
|
5
|
+
Copyright (c) Microsoft Corporation. All rights reserved.
|
|
6
|
+
Licensed under the Apache License, Version 2.0 (the "License"); you may not use
|
|
7
|
+
this file except in compliance with the License. You may obtain a copy of the
|
|
8
|
+
License at http://www.apache.org/licenses/LICENSE-2.0
|
|
6
9
|
|
|
7
|
-
|
|
8
|
-
|
|
10
|
+
THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
11
|
+
KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
|
|
12
|
+
WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
|
|
13
|
+
MERCHANTABLITY OR NON-INFRINGEMENT.
|
|
9
14
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
13
|
-
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
14
|
-
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
15
|
-
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
16
|
-
PERFORMANCE OF THIS SOFTWARE.
|
|
15
|
+
See the Apache Version 2.0 License for specific language governing permissions
|
|
16
|
+
and limitations under the License.
|
|
17
17
|
***************************************************************************** */
|
|
18
18
|
/* global Reflect, Promise */
|
|
19
19
|
|
|
@@ -135,7 +135,8 @@ var SpyClient = (function () {
|
|
|
135
135
|
* @author kaivean
|
|
136
136
|
*/
|
|
137
137
|
var defaultLogServer = 'https://sp1.baidu.com/5b1ZeDe5KgQFm2e88IuM_a/mwb2.gif?';
|
|
138
|
-
var
|
|
138
|
+
var ver = navigator.userAgent.toLowerCase().match(/cpu iphone os (.*?)_/);
|
|
139
|
+
var isLtIos14 = ver && ver[2] && (+ver[2] < 14);
|
|
139
140
|
function err(msg) {
|
|
140
141
|
console.error("[SpyClient_log]" + msg);
|
|
141
142
|
// throw new Error(msg);
|
|
@@ -152,6 +153,9 @@ var SpyClient = (function () {
|
|
|
152
153
|
return encodeURIComponent(key) + '=' + encodeURIComponent(value);
|
|
153
154
|
}).join('&');
|
|
154
155
|
}
|
|
156
|
+
function isArray(arr) {
|
|
157
|
+
return Object.prototype.toString.call(arr) === '[object Array]';
|
|
158
|
+
}
|
|
155
159
|
var SpyClient$1 = /** @class */ (function () {
|
|
156
160
|
function SpyClient(option) {
|
|
157
161
|
this.sample = {};
|
|
@@ -164,38 +168,55 @@ var SpyClient = (function () {
|
|
|
164
168
|
lid: option.lid,
|
|
165
169
|
check: option.check !== false,
|
|
166
170
|
sample: option.sample,
|
|
171
|
+
localCache: option.localCache,
|
|
167
172
|
logServer: option.logServer || defaultLogServer,
|
|
168
173
|
};
|
|
169
174
|
}
|
|
170
|
-
SpyClient.prototype.
|
|
171
|
-
if (!this.check(
|
|
175
|
+
SpyClient.prototype.handle = function (logItem) {
|
|
176
|
+
if (!this.check(logItem)) {
|
|
172
177
|
return;
|
|
173
178
|
}
|
|
179
|
+
logItem = assign({
|
|
180
|
+
pid: this.option.pid,
|
|
181
|
+
lid: this.option.lid,
|
|
182
|
+
ts: Date.now(),
|
|
183
|
+
group: 'common',
|
|
184
|
+
}, logItem);
|
|
185
|
+
if (this.option.localCache) {
|
|
186
|
+
this.option.localCache.addLog(logItem);
|
|
187
|
+
}
|
|
174
188
|
// 当前api设置了抽样,
|
|
175
|
-
if (typeof
|
|
176
|
-
if (Math.random() >
|
|
189
|
+
if (typeof logItem.sample === 'number') {
|
|
190
|
+
if (Math.random() > logItem.sample) {
|
|
177
191
|
return;
|
|
178
192
|
}
|
|
179
193
|
}
|
|
180
194
|
else if (typeof this.option.sample === 'number' && Math.random() > this.option.sample) { // 否则,用全局抽样
|
|
181
195
|
return;
|
|
182
196
|
}
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
var
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
197
|
+
delete logItem.sample;
|
|
198
|
+
return logItem;
|
|
199
|
+
};
|
|
200
|
+
SpyClient.prototype.send = function (data, post) {
|
|
201
|
+
if (post === void 0) { post = false; }
|
|
202
|
+
var logItems = isArray(data) ? data : [data];
|
|
203
|
+
var postData = [];
|
|
204
|
+
for (var _i = 0, logItems_1 = logItems; _i < logItems_1.length; _i++) {
|
|
205
|
+
var logItem = logItems_1[_i];
|
|
206
|
+
logItem = this.handle(logItem);
|
|
207
|
+
if (!logItem) {
|
|
208
|
+
continue;
|
|
209
|
+
}
|
|
210
|
+
postData.push(logItem);
|
|
211
|
+
// 期望通过post方式上传日志
|
|
212
|
+
if (post) {
|
|
213
|
+
continue;
|
|
214
|
+
}
|
|
215
|
+
var url = this.option.logServer + stringify(logItem);
|
|
216
|
+
this.request(url);
|
|
217
|
+
}
|
|
218
|
+
if (post) {
|
|
219
|
+
this.sendPost(postData);
|
|
199
220
|
}
|
|
200
221
|
};
|
|
201
222
|
SpyClient.prototype.check = function (query) {
|
|
@@ -337,6 +358,46 @@ var SpyClient = (function () {
|
|
|
337
358
|
SpyClient.prototype.clearAllMark = function () {
|
|
338
359
|
this.markCache = {};
|
|
339
360
|
};
|
|
361
|
+
// send(data, true) 也能以post发送,但是会有严格校验
|
|
362
|
+
// sendPost能以post发送,但没有校验
|
|
363
|
+
SpyClient.prototype.sendPost = function (data) {
|
|
364
|
+
var logItems = isArray(data) ? data : [data];
|
|
365
|
+
var first = logItems[0];
|
|
366
|
+
// 需要在页面暴漏出来pid等基本信息,方式调试查看基本信息与兼容目前的监控
|
|
367
|
+
var query = {
|
|
368
|
+
pid: first.pid,
|
|
369
|
+
type: first.type,
|
|
370
|
+
group: first.group,
|
|
371
|
+
};
|
|
372
|
+
var url = this.option.logServer + stringify(query);
|
|
373
|
+
this.request(url, data);
|
|
374
|
+
};
|
|
375
|
+
// 有data时,意味着要用post发送请求
|
|
376
|
+
SpyClient.prototype.request = function (url, data) {
|
|
377
|
+
if (!(!isLtIos14
|
|
378
|
+
&& navigator.sendBeacon
|
|
379
|
+
&& navigator.sendBeacon(url, data ? JSON.stringify(data) : undefined))) {
|
|
380
|
+
if (data) {
|
|
381
|
+
this.fetch(url, data);
|
|
382
|
+
}
|
|
383
|
+
else {
|
|
384
|
+
(new Image()).src = url;
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
};
|
|
388
|
+
SpyClient.prototype.fetch = function (url, data) {
|
|
389
|
+
if (!window.fetch) {
|
|
390
|
+
return;
|
|
391
|
+
}
|
|
392
|
+
fetch(url, {
|
|
393
|
+
method: 'POST',
|
|
394
|
+
credentials: 'include',
|
|
395
|
+
headers: {
|
|
396
|
+
'Content-Type': 'application/json',
|
|
397
|
+
},
|
|
398
|
+
body: JSON.stringify(data),
|
|
399
|
+
});
|
|
400
|
+
};
|
|
340
401
|
return SpyClient;
|
|
341
402
|
}());
|
|
342
403
|
|
|
@@ -1310,8 +1371,25 @@ var SpyClient = (function () {
|
|
|
1310
1371
|
document.addEventListener('visibilitychange', _this.visibilitychangeCB);
|
|
1311
1372
|
window.addEventListener('beforeunload', _this.leave, false);
|
|
1312
1373
|
window.addEventListener('unload', _this.leave, false);
|
|
1374
|
+
_this.handleHead();
|
|
1313
1375
|
return _this;
|
|
1314
1376
|
}
|
|
1377
|
+
SpyClient.prototype.handleHead = function () {
|
|
1378
|
+
var _this = this;
|
|
1379
|
+
if (this.option.localCache) {
|
|
1380
|
+
var spyHead = window.__spyHead;
|
|
1381
|
+
if (spyHead && spyHead.winerrors) {
|
|
1382
|
+
for (var index = 0; index < spyHead.winerrors.length; index++) {
|
|
1383
|
+
var obj = spyHead.winerrors[index];
|
|
1384
|
+
this.option.localCache.addLog(obj);
|
|
1385
|
+
}
|
|
1386
|
+
// Head发送的异常,也保存一份到本地,主要是JS错误和资源加载异常
|
|
1387
|
+
spyHead.interceptor = function (obj) {
|
|
1388
|
+
_this.option.localCache.addLog(obj);
|
|
1389
|
+
};
|
|
1390
|
+
}
|
|
1391
|
+
}
|
|
1392
|
+
};
|
|
1315
1393
|
SpyClient.prototype.listenFID = function (cb) {
|
|
1316
1394
|
this.invoke('listenFID', cb);
|
|
1317
1395
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
var SpyClient=function(){"use strict";var i=function(t,e){return(i=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var i in e)e.hasOwnProperty(i)&&(t[i]=e[i])})(t,e)};var l=function(){return(l=Object.assign||function(t){for(var e,i=1,o=arguments.length;i<o;i++)for(var n in e=arguments[i])Object.prototype.hasOwnProperty.call(e,n)&&(t[n]=e[n]);return t}).apply(this,arguments)};function o(){for(var t=[],e=0;e<arguments.length;e++)t[e]=arguments[e];return(Object.assign||function(t){for(var e,i=1,o=arguments.length;i<o;i++)for(var n in e=arguments[i])Object.prototype.hasOwnProperty.call(e,n)&&(t[n]=e[n]);return t}).apply(this,t)}function r(t){var e=function(t){if(URL){t=new URL(t);if(void 0!==t.host)return{protocol:t.protocol,host:t.host,pathname:t.pathname,ext:""}}}(t);e||((i=document.createElement("a")).href=t,e={protocol:i.protocol,host:i.host||location.host,pathname:i.pathname,ext:""});var i=e.pathname.split(".");return e.ext=i[i.length-1],e}function u(t){return+t.toFixed(1)}function f(t){if(!t)return{xpath:""};for(var e=[];t&&1===t.nodeType&&t!==t.parentNode;){var i=t.tagName.toLowerCase();if(t.getAttribute("id")?i+="[#"+t.getAttribute("id")+"]":t.classList&&t.classList.length&&(i+="[."+t.classList[t.classList.length-1]+"]"),e.push(i),t===document.body)break;t=t.parentNode}return{xpath:e.join("<")}}var e=/(iPhone|iPod|iPad)/.test(navigator.userAgent);function d(t){console.error("[SpyClient_log]"+t)}var t=(n.prototype.send=function(t){if(this.check(t)){if("number"==typeof t.sample){if(Math.random()>t.sample)return}else if("number"==typeof this.option.sample&&Math.random()>this.option.sample)return;delete(t=o({pid:this.option.pid,lid:this.option.lid,ts:Date.now(),group:"common"},t)).sample;var i,t=this.option.logServer+(i=t,Object.keys(i).map(function(t){var e=i[t];return void 0===e?e="":"string"!=typeof e&&(e=JSON.stringify(e)),encodeURIComponent(t)+"="+encodeURIComponent(e)}).join("&"));!e&&navigator.sendBeacon&&navigator.sendBeacon(t)||((new Image).src=t)}},n.prototype.check=function(t){if(!this.option.check)return!0;var e=["perf","except","dist","count"];if(-1===e.indexOf(t.type))return d("type only is one of "+e.join(", ")),!1;if(t.group&&30<t.group.length)return d("group length execeeds 30"),!1;var i=/^[a-zA-Z0-9-_]{0,30}$/;if("except"===t.type){if("string"!=typeof t.info.msg||!t.info.msg.length)return d("info.msg field must be not empty and is String"),!1}else for(var o=0,n=Object.keys(t.info);o<n.length;o++){var r=n[o];if(!i.test(r))return d("info."+r+" is unexpected. Length must be not more than 30. Supported chars: a-zA-Z0-9-_"),!1;var s=t.info[r];if("dist"===t.type){if(30<s.length)return d("info."+r+" value length execeeds 30 when type == 'dist'"),!1}else if("number"!=typeof s)return d("info."+r+" value must be number"),!1}if(t.dim)for(var a=0,c=Object.keys(t.dim);a<c.length;a++){var h=c[a];if(!i.test(h))return d("dim key ["+h+"] is unexpected. Length must be not more than 30. Supported chars: a-zA-Z0-9-_"),!1;var p=t.dim[h];if(!/^[a-zA-Z0-9\-_\*\.\s\/#\+@\&\u4e00-\u9fa5]{0,30}$/.test(p))return d("dim."+h+" value ["+p+"] is unexpected. Length must be not more than 30. Supported chars: a-zA-Z0-9-_*. /#+@& and Chinese"),!1}return!0},n.prototype.sendPerf=function(t){this.send(o({type:"perf"},t))},n.prototype.sendExcept=function(t){this.send(o({type:"except"},t))},n.prototype.sendDist=function(t){this.send(o({type:"dist"},t))},n.prototype.sendCount=function(t){this.send(o({type:"count"},t))},n.prototype.sendExceptForError=function(t,e){var i=o({},e);i.info=o({},e.info||{},{msg:t.message,stack:t.stack}),this.sendExcept(i)},n.prototype.startMark=function(t){this.markCache[t]={start:Date.now()}},n.prototype.endMark=function(t){return this.markCache[t]?(this.markCache[t].total=Date.now()-this.markCache[t].start,this.markCache[t].total):0},n.prototype.clearMark=function(t){this.markCache[t]&&delete this.markCache[t]},n.prototype.getAllMark=function(){for(var t={},e=0,i=Object.keys(this.markCache);e<i.length;e++){var o=i[e];t[o]=this.markCache[o].total}return t},n.prototype.clearAllMark=function(){this.markCache={}},n);function n(t){if(this.sample={},this.markCache={},!t.pid)throw new Error("pid is required");this.option={pid:t.pid,lid:t.lid,check:!1!==t.check,sample:t.sample,logServer:t.logServer||"https://sp1.baidu.com/5b1ZeDe5KgQFm2e88IuM_a/mwb2.gif?"}}var s=window.__spyHead,a="first-input",c=(h.prototype.listenFID=function(t){this.cb=t,this.callCB()},h.prototype.callCB=function(){void 0!==this.finalValue&&this.cb&&this.cb({fid:this.finalValue})},h.prototype.destroy=function(){this.observer&&this.observer.disconnect(),this.observer=null},h.prototype.handle=function(t){t=t.pop();t&&(this.finalValue=t.duration,this.callCB(),this.destroy())},h);function h(){var e=this;if(this.observer=null,window.PerformanceObserver){s&&s.entryMap&&s.entryMap[a]&&this.handle(s.entryMap[a]);try{this.observer=new PerformanceObserver(function(t){e.handle(t.getEntries())}),this.observer.observe({entryTypes:[a]})}catch(t){}}}var p=window.__spyHead,m="layout-shift",g=(v.prototype.listenLayoutShift=function(t){this.cb=t},v.prototype.leave=function(){this.onceLeave||(this.onceLeave=!0,this.observer&&this.observer.takeRecords&&this.observer.takeRecords(),this.finalValue=this.value,this.callCB(),this.destroy())},v.prototype.callCB=function(){void 0!==this.finalValue&&this.cb&&this.cb({layoutShift:this.finalValue})},v.prototype.destroy=function(){this.observer&&this.observer.disconnect(),this.observer=null},v.prototype.handle=function(t){var e=this;t.map(function(t){t.hadRecentInput||(e.value=(e.value||0)+(t.value||0))})},v);function v(){var e=this;if(this.observer=null,this.onceLeave=!1,window.PerformanceObserver){p&&p.entryMap&&p.entryMap[m]&&this.handle(p.entryMap[m]);try{this.observer=new PerformanceObserver(function(t){e.handle(t.getEntries())}),this.observer.observe({entryTypes:[m]})}catch(t){}}}var y={};var b=window.__spyHead,k="largest-contentful-paint",L=(S.prototype.listenLCP=function(t){this.cb=t,this.callCB()},S.prototype.callCB=function(){this.finalValue&&this.cb&&this.cb({lcp:this.finalValue})},S.prototype.load=function(){var t,e;this.observer&&this.observer.takeRecords&&this.observer.takeRecords(),this.finalValue=this.value,t="lcp",e=this.value,y[t]=e,this.callCB(),this.destroy()},S.prototype.destroy=function(){this.observer&&this.observer.disconnect(),this.observer=null},S.prototype.handle=function(t){var e=this;t.map(function(t){e.value=t.renderTime||t.loadTime})},S);function S(){var e=this;if(this.observer=null,window.PerformanceObserver){b&&b.entryMap&&b.entryMap[k]&&this.handle(b.entryMap[k]);try{this.observer=new PerformanceObserver(function(t){e.handle(t.getEntries())}),this.observer.observe({entryTypes:[k]})}catch(t){}}}function w(t){return t.name.indexOf(".gif?")<0}var T=(C.prototype.check=function(){return window.PerformanceObserver&&performance&&performance.timing&&performance.timing.navigationStart},C.prototype.listenTTI=function(t,e){this.check()&&(this.observeLongtask(2e4),this.cb=t,(e=e||{}).interval&&(this.interval=e.interval),e.filterRequest&&(this.filterRequest=e.filterRequest))},C.prototype.load=function(){this.check()&&(this.ttiTimer=setInterval(this.ttiCheck,this.interval))},C.prototype.ttiCheck=function(){var t,e;this.cb&&((t=performance.now())-this.lastLongTask<this.interval||!1!==(e=this.getNetworkSilenceAt())&&(t-e<this.interval||(clearTimeout(this.ttiTimer),e=(e=this.lastLongTask)||performance.timing.domContentLoadedEventEnd-performance.timing.navigationStart,this.stopObserveLongTask(),this.cb({tti:e}))))},C.prototype.observerCallback=function(t){t=t.getEntries(),t=t[t.length-1];this.lastLongTask=t.startTime+t.duration},C.prototype.stopObserveLongTask=function(){this.observer&&(this.observer.disconnect(),this.observer=null),this.stopLongTaskTimeoutId&&clearTimeout(this.stopLongTaskTimeoutId)},C.prototype.getNetworkSilenceAt=function(){for(var t=0,e=0,i=performance.getEntriesByType("resource").filter(this.filterRequest);e<i.length;e++){var o=i[e];if(!o.responseEnd)return!1;o.responseEnd>t&&(t=o.responseEnd)}return t},C.prototype.observeLongtask=function(t){this.stopObserveLongTask();try{this.observer=new PerformanceObserver(this.observerCallback),this.observer.observe({entryTypes:["longtask"]}),this.stopLongTaskTimeoutId=setTimeout(this.stopObserveLongTask,t)}catch(t){}},C);function C(){this.lastLongTask=0,this.observer=null,this.interval=5e3,this.filterRequest=w,this.observerCallback=this.observerCallback.bind(this),this.ttiCheck=this.ttiCheck.bind(this)}var O,B,R,I=(z.prototype.listenTiming=function(t){window.performance&&window.performance.timing&&(this.cb=t)},z.prototype.load=function(){var t=this;setTimeout(function(){t.cb&&t.cb(t.getMetric())},500)},z.prototype.getMetric=function(){var t,o={},e=window.performance.timing,i=e.navigationStart;return o.dns=e.domainLookupEnd-e.domainLookupStart,o.tcp=e.connectEnd-e.connectStart,o.request=e.responseStart-e.requestStart,o.response=e.responseEnd-e.responseStart,o.parseHtml=e.domInteractive-e.domLoading,o.domReady=e.domContentLoadedEventEnd-i,o.loadEventHandle=e.loadEventEnd-e.loadEventStart,o.load=e.loadEventEnd-i,!performance.getEntriesByType||(t=performance.getEntriesByType("paint"))&&t.length&&t.forEach(function(t){var e=t.name,i=t.duration,t=t.startTime,t=Math.ceil(i+t);"first-paint"===e&&(o.fp=t),"first-contentful-paint"===e&&(o.fcp=t)}),e.domFirstPaint&&(o.t7FirstPaint=e.domFirstPaint-i),e.domFirstScreenPaint&&(o.t7FirstScreen=e.domFirstScreenPaint-i),o},z);function z(){}(R=O=O||{}).JS="js",R.CSS="css",R.IMG="img",R.FONT="font",(R=B=B||{})[R.ttf=0]="ttf",R[R.eot=1]="eot",R[R.woff=2]="woff",R[R.woff2=3]="woff2";var E=["/mwb2.gif"];function P(t,e){for(var i=0,o=e;i<o.length;i++){var n=o[i];if(-1<t.indexOf(n))return 1}}function M(t){return+t.toFixed(1)}var x=(j.prototype.check=function(){return performance&&performance.getEntriesByType},j.prototype.listenResource=function(t,e){void 0===e&&(e={}),this.check()&&(this.resOption=o(this.resOption,e),this.trigger=this.resOption.trigger,this.cb=t)},j.prototype.listenBigImg=function(t,e){void 0===e&&(e={}),this.check()&&(this.bigImgOption=o(this.bigImgOption,e),this.trigger=this.bigImgOption.trigger,this.bigImgCB=t)},j.prototype.listenHttpResource=function(t,e){void 0===e&&(e={}),this.check()&&(this.httpResOption=o(this.httpResOption,e),this.trigger=this.httpResOption.trigger,this.httpResCB=t)},j.prototype.listenSlowResource=function(t,e){void 0===e&&(e={}),this.check()&&(this.slowOption=o(this.slowOption,e),this.trigger=this.slowOption.trigger,this.slowResCB=t)},j.prototype.report=function(){var t,p=this,e=this.getMetric();e&&(t=e.metric,e=e.hostMetric,this.cb&&this.cb(t,e)),this.bigImgCB&&"function"==typeof window.requestIdleCallback&&Object.keys(this.bigImgList).length&&window.requestIdleCallback(function(){for(var t=0,e=Object.keys(p.bigImgList);t<e.length;t++)for(var i=e[t],o=0,n=p.bigImgList[i];o<n.length;o++){var r=n[o],s=r.timing,r=r.type;try{var a=document.body.querySelector('img[src="'+s.name+'"]');p.bigImgCB({msg:s.name,dur:s.duration,xpath:f(a).xpath,host:i,type:r})}catch(t){console.error(t)}}}),this.httpResCB&&"function"==typeof window.requestIdleCallback&&Object.keys(this.httpResList).length&&window.requestIdleCallback(function(){for(var t=0,e=Object.keys(p.httpResList);t<e.length;t++)for(var i=e[t],o=0,n=p.httpResList[i];o<n.length;o++){var r=n[o],s=r.timing,r=r.type;try{var a=document.body.querySelector('[src="'+s.name+'"]');p.httpResCB({msg:s.name,dur:s.duration,xpath:f(a).xpath,host:i,type:r})}catch(t){console.error(t)}}}),this.slowResCB&&"function"==typeof window.requestIdleCallback&&Object.keys(this.slowList).length&&window.requestIdleCallback(function(){for(var t,e=0,i=Object.keys(p.slowList);e<i.length;e++)for(var o=i[e],n=0,r=p.slowList[o];n<r.length;n++){var s=r[n],a=s.timing,s=s.type;try{var c=document.body.querySelector('[src="'+a.name+'"]'),h={wait:u((t=a).domainLookupStart-(t.navigationStart||t.fetchStart||t.startTime)),dns:u(t.domainLookupEnd-t.domainLookupStart),connect:u(t.connectEnd-t.connectStart),req:u(t.responseStart-t.requestStart),res:u(t.responseEnd-t.responseStart)};p.slowResCB(l(l({},h),{dur:a.duration,msg:a.name,xpath:f(c).xpath,host:o,type:s}))}catch(t){console.error(t)}}})},j.prototype.load=function(){var t=this;"load"===this.trigger&&this.check()&&setTimeout(function(){t.report()},500)},j.prototype.leave=function(){"leave"===this.trigger&&this.report()},j.prototype.push=function(t,e,i,o){P(o.pathname,this.resOption.ignorePaths||[])||(e.push(i),this.pushWithHost(t,this.hostList,i,o)),P(o.pathname,this.slowOption.ignorePaths||[])||i.duration>this.slowOption.threshold&&this.pushWithHost(t,this.slowList,i,o)},j.prototype.pushWithHost=function(t,e,i,o){o=o.host;e[o]||(e[o]=[]),e[o].push({timing:i,type:t})},j.prototype.collectHttpResInHttps=function(t,e,i){"https:"!==location.protocol||0!==e.name.indexOf("http://")||P(i.pathname,this.httpResOption.ignorePaths||[])||this.pushWithHost(t,this.httpResList,e,i)},j.prototype.addScript=function(t,e){"js"===e.ext&&0!==t.decodedBodySize&&this.push("js",this.jsList,t,e)},j.prototype.addResFromCss=function(t,e){e.ext&&B.hasOwnProperty(e.ext)?this.push("font",this.fontList,t,e):this.addImg(t,e)},j.prototype.addLink=function(t,e){"css"===e.ext?this.push("css",this.cssList,t,e):"js"===e.ext&&this.push("js",this.jsList,t,e)},j.prototype.addImg=function(t,e){this.push("img",this.imgList,t,e),t.decodedBodySize>(this.bigImgOption.maxSize||0)&&!P(e.pathname,this.bigImgOption.ignorePaths||[])&&this.pushWithHost("img",this.bigImgList,t,e)},j.prototype.handleTimings=function(t){for(var e=t.length,i=0;i<e;i++){var o=t[i],n=r(o.name);if(!P(n.pathname,E))switch(t[i].initiatorType){case"script":this.addScript(o,n);break;case"css":this.addResFromCss(o,n);break;case"img":this.addImg(o,n);break;case"link":this.addLink(o,n);break;case"audio":this.collectHttpResInHttps("audio",o,n);break;case"video":this.collectHttpResInHttps("video",o,n)}}},j.prototype.getNumAndSize=function(t,e){var i={},o=t+"Num",n=t+"Size",r=t+"TransferSize",s=t+"CacheRate",t=t+"Duration";i[o]=0,i[n]=0;var a=i[r]=0;return e.forEach(function(t){i[o]++,i[n]+=t.decodedBodySize/1024,i[r]+=t.transferSize/1024,a+=t.duration}),i[t]=M(0<i[o]?a/i[o]:0),i[n]&&(t=i[n]-i[r],i[s]=M(0<=t?100*t/i[n]:0)),i[n]=M(i[n]),i[r]=M(i[r]),i},j.prototype.getMetric=function(){var t=performance.getEntriesByType("navigation")[0],e=performance.getEntriesByType("resource");if(t&&e&&e.length){this.handleTimings(e);e=l(l(l(l({},this.getNumAndSize(O.JS,this.jsList)),this.getNumAndSize(O.CSS,this.cssList)),this.getNumAndSize(O.IMG,this.imgList)),this.getNumAndSize(O.FONT,this.fontList)),t=t;e.docSize=M(t.decodedBodySize/1024),e.docTransferSize=M(t.transferSize/1024),e.headerSize=M((t.transferSize-t.encodedBodySize||0)/1024),e.allSize=M(e.docSize+e.jsSize+e.cssSize+e.imgSize+e.fontSize),e.allTransferSize=M(e.docTransferSize+e.jsTransferSize+e.cssTransferSize+e.imgTransferSize+e.fontTransferSize);for(var i={},o=0,n=Object.keys(this.hostList);o<n.length;o++){var r=n[o],s=this.hostList[r].map(function(t){return t.timing});i[r]=this.getNumAndSize("host",s)}return{metric:e,hostMetric:i}}},j);function j(){this.trigger="load",this.jsList=[],this.cssList=[],this.imgList=[],this.fontList=[],this.hostList={},this.bigImgList={},this.httpResList={},this.slowList={},this.resOption={ignorePaths:[],trigger:"load"},this.bigImgOption={ignorePaths:[],maxSize:153600,trigger:"load"},this.httpResOption={ignorePaths:[],trigger:"load"},this.slowOption={ignorePaths:[],trigger:"load",threshold:1e3}}var H=null;window.performance&&window.performance.memory&&(H=window.performance.memory);var N=(_.prototype.listenMemory=function(t){this.cb=t},_.prototype.leave=function(){var t;!this.onceLeave&&H&&(this.onceLeave=!0,(t=window.performance.memory).usedJSHeapSize===H.usedJSHeapSize&&t.totalJSHeapSize===H.totalJSHeapSize||(t=window.performance.memory,this.cb&&this.cb({usedJSHeapSize:t.usedJSHeapSize/1024,totalJSHeapSize:t.totalJSHeapSize/1024,jsHeapSizeLimit:t.jsHeapSizeLimit/1024,usedJSHeapRate:100*t.usedJSHeapSize/t.totalJSHeapSize})))},_);function _(){this.onceLeave=!1}var F=(q.prototype.getNavigatorInfo=function(){var t={},e=navigator.connection;return"object"==typeof e&&(t={downlink:e.downlink,effectiveType:e.effectiveType,rtt:e.rtt,saveData:!!e.saveData}),navigator.deviceMemory&&(t.deviceMemory=navigator.deviceMemory),navigator.hardwareConcurrency&&(t.hardwareConcurrency=navigator.deviceMemory),t},q.prototype.load=function(){},q);function q(){}var A,D,J,V=window.__spyHead,Z="longtask",W=(U.prototype.check=function(){return window.PerformanceObserver&&performance&&performance.timing&&performance.timing.navigationStart},U.prototype.listenFSPLongTask=function(t){this.check()&&(this.fspCB=t)},U.prototype.listenLCPLongTask=function(t){this.check()&&(this.lcpCB=t)},U.prototype.listenLoadLongTask=function(t){this.check()&&(this.loadCB=t)},U.prototype.listenPageLongTask=function(t){this.check()&&(this.pageCB=t)},U.prototype.load=function(){var e,i=this;this.check()&&(e=this.getStatData(Date.now()),setTimeout(function(){var t;i.loadCB&&i.loadCB({loadLongtaskTime:e.time,loadTBT:e.tbt,loadTotalTime:e.totalTime,loadLongtaskRate:e.rate,loadLongtaskNum:e.num}),performance.timing&&performance.timing.domFirstScreenPaint&&(t=i.getStatData(performance.timing.domFirstScreenPaint),i.fspCB&&i.fspCB({fspLongtaskTime:t.time,fspTBT:t.tbt,fspTotalTime:t.totalTime,fspLongtaskRate:t.rate,fspLongtaskNum:t.num})),y.lcp&&(t=i.getStatData(performance.timing.navigationStart+y.lcp),i.lcpCB&&i.lcpCB({lcpLongtaskTime:t.time,lcpTBT:t.tbt,lcpTotalTime:t.totalTime,lcpLongtaskRate:t.rate,lcpLongtaskNum:t.num}))},200))},U.prototype.leave=function(){var t;this.onceLeave||(this.onceLeave=!0,t=this.getStatData(Date.now()),this.pageCB&&this.pageCB({pageLongtaskTime:t.time,pageTBT:t.tbt,pageTotalTime:t.totalTime,pageLongtaskRate:t.rate,pageLongtaskNum:t.num,pageIframeLongtaskTime:t.iframeTime,pageIframeLongtaskRate:t.iframeRate,pageIframeLongtaskNum:t.iframeNum}))},U.prototype.destroy=function(){this.lts=[],this.observer&&this.observer.disconnect(),this.observer=null},U.prototype.getStatData=function(t){for(var e=performance.timing.navigationStart,i=0,o=0,n=0,r=0,s=0,a={},c=0;c<this.lts.length;c++){var h=this.lts[c],p=h.duration;e+h.startTime+p<t&&(i+=p,o+=0<p-50?p-50:0,n++,h.attribution&&h.attribution[0]&&((h=h.attribution[0].containerSrc)&&h!==location.href&&(a[h]||(a[h]=[]),a[h].push(p),r+=p,s++)))}var l=t-e;return{num:n,time:i,tbt:o,totalTime:l,rate:100*i/l,iframeTime:r,iframeNum:s,iframeRate:100*r/l,iframeLongtasks:a}},U);function U(){var e=this;this.lts=[],this.observer=null,this.onceLeave=!1,V&&V.entryMap&&V.entryMap[Z]&&(this.lts=this.lts.concat(V.entryMap[Z]));try{this.observer=new PerformanceObserver(function(t){e.lts=e.lts.concat(t.getEntries())}),this.observer.observe({entryTypes:[Z]})}catch(t){}}function G(){this.constructor=D}function $(t){t=A.call(this,t)||this;return t.modules=[],t.register(new c),t.register(new L),t.register(new g),t.register(new T),t.register(new I),t.register(new x),t.register(new N),t.register(new F),t.register(new W),t.visibilitychangeCB=t.visibilitychangeCB.bind(t),t.load=t.load.bind(t),t.leave=t.leave.bind(t),"complete"===document.readyState?t.load():window.addEventListener("load",t.load),document.addEventListener("visibilitychange",t.visibilitychangeCB),window.addEventListener("beforeunload",t.leave,!1),window.addEventListener("unload",t.leave,!1),t}return i(D=$,J=A=t),D.prototype=null===J?Object.create(J):(G.prototype=J.prototype,new G),$.prototype.listenFID=function(t){this.invoke("listenFID",t)},$.prototype.listenLayoutShift=function(t){this.invoke("listenLayoutShift",t)},$.prototype.listenLCP=function(t){this.invoke("listenLCP",t)},$.prototype.listenFSPLongTask=function(t){this.invoke("listenFSPLongTask",t)},$.prototype.listenLCPLongTask=function(t){this.invoke("listenLCPLongTask",t)},$.prototype.listenLoadLongTask=function(t){this.invoke("listenLoadLongTask",t)},$.prototype.listenPageLongTask=function(t){this.invoke("listenPageLongTask",t)},$.prototype.listenMemory=function(t){this.invoke("listenMemory",t)},$.prototype.getNavigatorInfo=function(){return this.invoke("getNavigatorInfo")},$.prototype.listenResource=function(t,e){this.invoke("listenResource",t,e)},$.prototype.listenBigImg=function(t,e){this.invoke("listenBigImg",t,e)},$.prototype.listenHttpResource=function(t,e){this.invoke("listenHttpResource",t,e)},$.prototype.listenSlowResource=function(t,e){this.invoke("listenSlowResource",t,e)},$.prototype.listenTiming=function(t){this.invoke("listenTiming",t)},$.prototype.listenTTI=function(t,e){this.invoke("listenTTI",t,e)},$.prototype.invoke=function(t,e,i){for(var o=0;o<this.modules.length;o++){var n=this.modules[o];if("function"==typeof n[t])return n[t].apply(n,[e,i])}console.error("no method",t)},$.prototype.register=function(t){this.modules.push(t)},$.prototype.load=function(){for(var t=0;t<this.modules.length;t++){var e=this.modules[t];e.load&&e.load()}},$.prototype.leave=function(){for(var t=0;t<this.modules.length;t++){var e=this.modules[t];e.leave&&e.leave()}},$.prototype.destroy=function(){for(var t=0;t<this.modules.length;t++){var e=this.modules[t];e.destroy&&e.destroy()}document.removeEventListener("visibilitychange",this.visibilitychangeCB),window.removeEventListener("load",this.load),window.removeEventListener("beforeunload",this.leave),window.removeEventListener("unload",this.destroy)},$.prototype.visibilitychangeCB=function(){"hidden"===document.visibilityState&&this.leave()},$}();
|
|
1
|
+
var SpyClient=function(){"use strict";var i=function(t,e){return(i=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var i in e)e.hasOwnProperty(i)&&(t[i]=e[i])})(t,e)};var u=function(){return(u=Object.assign||function(t){for(var e,i=1,o=arguments.length;i<o;i++)for(var n in e=arguments[i])Object.prototype.hasOwnProperty.call(e,n)&&(t[n]=e[n]);return t}).apply(this,arguments)};function o(){for(var t=[],e=0;e<arguments.length;e++)t[e]=arguments[e];return(Object.assign||function(t){for(var e=void 0,i=1,o=arguments.length;i<o;i++)for(var n in e=arguments[i])Object.prototype.hasOwnProperty.call(e,n)&&(t[n]=e[n]);return t}).apply(this,t)}function r(t){var e=function(t){if(URL){var e=new URL(t);if(void 0!==e.host)return{protocol:e.protocol,host:e.host,pathname:e.pathname,ext:""}}}(t);if(!e){var i=document.createElement("a");i.href=t,e={protocol:i.protocol,host:i.host||location.host,pathname:i.pathname,ext:""}}var o=e.pathname.split(".");return e.ext=o[o.length-1],e}function f(t){return+t.toFixed(1)}function d(t){if(!t)return{xpath:""};for(var e=[];t&&1===t.nodeType&&t!==t.parentNode;){var i=t.tagName.toLowerCase();if(t.getAttribute("id")?i+="[#"+t.getAttribute("id")+"]":t.classList&&t.classList.length&&(i+="[."+t.classList[t.classList.length-1]+"]"),e.push(i),t===document.body)break;t=t.parentNode}return{xpath:e.join("<")}}var t=navigator.userAgent.toLowerCase().match(/cpu iphone os (.*?)_/),n=t&&t[2]&&+t[2]<14;function l(t){console.error("[SpyClient_log]"+t)}function a(i){return Object.keys(i).map(function(t){var e=i[t];return void 0===e?e="":"string"!=typeof e&&(e=JSON.stringify(e)),encodeURIComponent(t)+"="+encodeURIComponent(e)}).join("&")}function c(t){return"[object Array]"===Object.prototype.toString.call(t)}var e=(s.prototype.handle=function(t){if(this.check(t)){if(t=o({pid:this.option.pid,lid:this.option.lid,ts:Date.now(),group:"common"},t),this.option.localCache&&this.option.localCache.addLog(t),"number"==typeof t.sample){if(Math.random()>t.sample)return}else if("number"==typeof this.option.sample&&Math.random()>this.option.sample)return;return delete t.sample,t}},s.prototype.send=function(t,e){void 0===e&&(e=!1);for(var i=[],o=0,n=c(t)?t:[t];o<n.length;o++){var r=n[o];if((r=this.handle(r))&&(i.push(r),!e)){var s=this.option.logServer+a(r);this.request(s)}}e&&this.sendPost(i)},s.prototype.check=function(t){if(!this.option.check)return!0;var e=["perf","except","dist","count"];if(-1===e.indexOf(t.type))return l("type only is one of "+e.join(", ")),!1;if(t.group&&30<t.group.length)return l("group length execeeds 30"),!1;var i=/^[a-zA-Z0-9-_]{0,30}$/;if("except"===t.type){if("string"!=typeof t.info.msg||!t.info.msg.length)return l("info.msg field must be not empty and is String"),!1}else for(var o=0,n=Object.keys(t.info);o<n.length;o++){var r=n[o];if(!i.test(r))return l("info."+r+" is unexpected. Length must be not more than 30. Supported chars: a-zA-Z0-9-_"),!1;var s=t.info[r];if("dist"===t.type){if(30<s.length)return l("info."+r+" value length execeeds 30 when type == 'dist'"),!1}else if("number"!=typeof s)return l("info."+r+" value must be number"),!1}if(t.dim)for(var a=0,c=Object.keys(t.dim);a<c.length;a++){var h=c[a];if(!i.test(h))return l("dim key ["+h+"] is unexpected. Length must be not more than 30. Supported chars: a-zA-Z0-9-_"),!1;var p=t.dim[h];if(!/^[a-zA-Z0-9\-_\*\.\s\/#\+@\&\u4e00-\u9fa5]{0,30}$/.test(p))return l("dim."+h+" value ["+p+"] is unexpected. Length must be not more than 30. Supported chars: a-zA-Z0-9-_*. /#+@& and Chinese"),!1}return!0},s.prototype.sendPerf=function(t){this.send(o({type:"perf"},t))},s.prototype.sendExcept=function(t){this.send(o({type:"except"},t))},s.prototype.sendDist=function(t){this.send(o({type:"dist"},t))},s.prototype.sendCount=function(t){this.send(o({type:"count"},t))},s.prototype.sendExceptForError=function(t,e){var i=o({},e);i.info=o({},e.info||{},{msg:t.message,stack:t.stack}),this.sendExcept(i)},s.prototype.startMark=function(t){this.markCache[t]={start:Date.now()}},s.prototype.endMark=function(t){return this.markCache[t]?(this.markCache[t].total=Date.now()-this.markCache[t].start,this.markCache[t].total):0},s.prototype.clearMark=function(t){this.markCache[t]&&delete this.markCache[t]},s.prototype.getAllMark=function(){for(var t={},e=0,i=Object.keys(this.markCache);e<i.length;e++){var o=i[e];t[o]=this.markCache[o].total}return t},s.prototype.clearAllMark=function(){this.markCache={}},s.prototype.sendPost=function(t){var e=(c(t)?t:[t])[0],i={pid:e.pid,type:e.type,group:e.group},o=this.option.logServer+a(i);this.request(o,t)},s.prototype.request=function(t,e){!n&&navigator.sendBeacon&&navigator.sendBeacon(t,e?JSON.stringify(e):void 0)||(e?this.fetch(t,e):(new Image).src=t)},s.prototype.fetch=function(t,e){window.fetch&&fetch(t,{method:"POST",credentials:"include",headers:{"Content-Type":"application/json"},body:JSON.stringify(e)})},s);function s(t){if(this.sample={},this.markCache={},!t.pid)throw new Error("pid is required");this.option={pid:t.pid,lid:t.lid,check:!1!==t.check,sample:t.sample,localCache:t.localCache,logServer:t.logServer||"https://sp1.baidu.com/5b1ZeDe5KgQFm2e88IuM_a/mwb2.gif?"}}var h=window.__spyHead,p="first-input",g=(m.prototype.listenFID=function(t){this.cb=t,this.callCB()},m.prototype.callCB=function(){void 0!==this.finalValue&&this.cb&&this.cb({fid:this.finalValue})},m.prototype.destroy=function(){this.observer&&this.observer.disconnect(),this.observer=null},m.prototype.handle=function(t){var e=t.pop();e&&(this.finalValue=e.duration,this.callCB(),this.destroy())},m);function m(){var e=this;if(this.observer=null,window.PerformanceObserver){h&&h.entryMap&&h.entryMap[p]&&this.handle(h.entryMap[p]);try{this.observer=new PerformanceObserver(function(t){e.handle(t.getEntries())}),this.observer.observe({entryTypes:[p]})}catch(t){}}}var v=window.__spyHead,y="layout-shift",b=(k.prototype.listenLayoutShift=function(t){this.cb=t},k.prototype.leave=function(){this.onceLeave||(this.onceLeave=!0,this.observer&&this.observer.takeRecords&&this.observer.takeRecords(),this.finalValue=this.value,this.callCB(),this.destroy())},k.prototype.callCB=function(){void 0!==this.finalValue&&this.cb&&this.cb({layoutShift:this.finalValue})},k.prototype.destroy=function(){this.observer&&this.observer.disconnect(),this.observer=null},k.prototype.handle=function(t){var e=this;t.map(function(t){t.hadRecentInput||(e.value=(e.value||0)+(t.value||0))})},k);function k(){var e=this;if(this.observer=null,this.onceLeave=!1,window.PerformanceObserver){v&&v.entryMap&&v.entryMap[y]&&this.handle(v.entryMap[y]);try{this.observer=new PerformanceObserver(function(t){e.handle(t.getEntries())}),this.observer.observe({entryTypes:[y]})}catch(t){}}}var w={};function L(t){return w[t]}var S=window.__spyHead,T="largest-contentful-paint",C=(O.prototype.listenLCP=function(t){this.cb=t,this.callCB()},O.prototype.callCB=function(){this.finalValue&&this.cb&&this.cb({lcp:this.finalValue})},O.prototype.load=function(){this.observer&&this.observer.takeRecords&&this.observer.takeRecords(),this.finalValue=this.value,function(t,e){w[t]=e}("lcp",this.value),this.callCB(),this.destroy()},O.prototype.destroy=function(){this.observer&&this.observer.disconnect(),this.observer=null},O.prototype.handle=function(t){var e=this;t.map(function(t){e.value=t.renderTime||t.loadTime})},O);function O(){var e=this;if(this.observer=null,window.PerformanceObserver){S&&S.entryMap&&S.entryMap[T]&&this.handle(S.entryMap[T]);try{this.observer=new PerformanceObserver(function(t){e.handle(t.getEntries())}),this.observer.observe({entryTypes:[T]})}catch(t){}}}function B(t){return t.name.indexOf(".gif?")<0}var R=(I.prototype.check=function(){return window.PerformanceObserver&&performance&&performance.timing&&performance.timing.navigationStart},I.prototype.listenTTI=function(t,e){this.check()&&(this.observeLongtask(2e4),this.cb=t,(e=e||{}).interval&&(this.interval=e.interval),e.filterRequest&&(this.filterRequest=e.filterRequest))},I.prototype.load=function(){this.check()&&(this.ttiTimer=setInterval(this.ttiCheck,this.interval))},I.prototype.ttiCheck=function(){if(this.cb){var t=performance.now();if(!(t-this.lastLongTask<this.interval)){var e=this.getNetworkSilenceAt();if(!1!==e&&!(t-e<this.interval)){clearTimeout(this.ttiTimer);var i=this.lastLongTask;i=i||performance.timing.domContentLoadedEventEnd-performance.timing.navigationStart,this.stopObserveLongTask(),this.cb({tti:i})}}}},I.prototype.observerCallback=function(t){var e=t.getEntries(),i=e[e.length-1];this.lastLongTask=i.startTime+i.duration},I.prototype.stopObserveLongTask=function(){this.observer&&(this.observer.disconnect(),this.observer=null),this.stopLongTaskTimeoutId&&clearTimeout(this.stopLongTaskTimeoutId)},I.prototype.getNetworkSilenceAt=function(){for(var t=0,e=0,i=performance.getEntriesByType("resource").filter(this.filterRequest);e<i.length;e++){var o=i[e];if(!o.responseEnd)return!1;o.responseEnd>t&&(t=o.responseEnd)}return t},I.prototype.observeLongtask=function(t){this.stopObserveLongTask();try{this.observer=new PerformanceObserver(this.observerCallback),this.observer.observe({entryTypes:["longtask"]}),this.stopLongTaskTimeoutId=setTimeout(this.stopObserveLongTask,t)}catch(t){}},I);function I(){this.lastLongTask=0,this.observer=null,this.interval=5e3,this.filterRequest=B,this.observerCallback=this.observerCallback.bind(this),this.ttiCheck=this.ttiCheck.bind(this)}var z,E,P,j,H=(M.prototype.listenTiming=function(t){window.performance&&window.performance.timing&&(this.cb=t)},M.prototype.load=function(){var t=this;setTimeout(function(){t.cb&&t.cb(t.getMetric())},500)},M.prototype.getMetric=function(){var r={},t=window.performance.timing,e=t.navigationStart;if(r.dns=t.domainLookupEnd-t.domainLookupStart,r.tcp=t.connectEnd-t.connectStart,r.request=t.responseStart-t.requestStart,r.response=t.responseEnd-t.responseStart,r.parseHtml=t.domInteractive-t.domLoading,r.domReady=t.domContentLoadedEventEnd-e,r.loadEventHandle=t.loadEventEnd-t.loadEventStart,r.load=t.loadEventEnd-e,performance.getEntriesByType){var i=performance.getEntriesByType("paint");i&&i.length&&i.forEach(function(t){var e=t.name,i=t.duration,o=t.startTime,n=Math.ceil(i+o);"first-paint"===e&&(r.fp=n),"first-contentful-paint"===e&&(r.fcp=n)})}return t.domFirstPaint&&(r.t7FirstPaint=t.domFirstPaint-e),t.domFirstScreenPaint&&(r.t7FirstScreen=t.domFirstScreenPaint-e),r},M);function M(){}(E=z=z||{}).JS="js",E.CSS="css",E.IMG="img",E.FONT="font",(j=P=P||{})[j.ttf=0]="ttf",j[j.eot=1]="eot",j[j.woff=2]="woff",j[j.woff2=3]="woff2";var x=["/mwb2.gif"];function N(t,e){for(var i=0,o=e;i<o.length;i++){var n=o[i];if(-1<t.indexOf(n))return!0}return!1}function _(t){return+t.toFixed(1)}var q=(F.prototype.check=function(){return performance&&performance.getEntriesByType},F.prototype.listenResource=function(t,e){void 0===e&&(e={}),this.check()&&(this.resOption=o(this.resOption,e),this.trigger=this.resOption.trigger,this.cb=t)},F.prototype.listenBigImg=function(t,e){void 0===e&&(e={}),this.check()&&(this.bigImgOption=o(this.bigImgOption,e),this.trigger=this.bigImgOption.trigger,this.bigImgCB=t)},F.prototype.listenHttpResource=function(t,e){void 0===e&&(e={}),this.check()&&(this.httpResOption=o(this.httpResOption,e),this.trigger=this.httpResOption.trigger,this.httpResCB=t)},F.prototype.listenSlowResource=function(t,e){void 0===e&&(e={}),this.check()&&(this.slowOption=o(this.slowOption,e),this.trigger=this.slowOption.trigger,this.slowResCB=t)},F.prototype.report=function(){var l=this,t=this.getMetric();if(t){var e=t.metric,i=t.hostMetric;this.cb&&this.cb(e,i)}this.bigImgCB&&"function"==typeof window.requestIdleCallback&&Object.keys(this.bigImgList).length&&window.requestIdleCallback(function(){for(var t=0,e=Object.keys(l.bigImgList);t<e.length;t++)for(var i=e[t],o=0,n=l.bigImgList[i];o<n.length;o++){var r=n[o],s=r.timing,a=r.type;try{var c=document.body.querySelector('img[src="'+s.name+'"]');l.bigImgCB({msg:s.name,dur:s.duration,xpath:d(c).xpath,host:i,type:a})}catch(t){console.error(t)}}}),this.httpResCB&&"function"==typeof window.requestIdleCallback&&Object.keys(this.httpResList).length&&window.requestIdleCallback(function(){for(var t=0,e=Object.keys(l.httpResList);t<e.length;t++)for(var i=e[t],o=0,n=l.httpResList[i];o<n.length;o++){var r=n[o],s=r.timing,a=r.type;try{var c=document.body.querySelector('[src="'+s.name+'"]');l.httpResCB({msg:s.name,dur:s.duration,xpath:d(c).xpath,host:i,type:a})}catch(t){console.error(t)}}}),this.slowResCB&&"function"==typeof window.requestIdleCallback&&Object.keys(this.slowList).length&&window.requestIdleCallback(function(){for(var t=0,e=Object.keys(l.slowList);t<e.length;t++)for(var i=e[t],o=0,n=l.slowList[i];o<n.length;o++){var r=n[o],s=r.timing,a=r.type;try{var c=document.body.querySelector('[src="'+s.name+'"]'),h={wait:f((p=s).domainLookupStart-(p.navigationStart||p.fetchStart||p.startTime)),dns:f(p.domainLookupEnd-p.domainLookupStart),connect:f(p.connectEnd-p.connectStart),req:f(p.responseStart-p.requestStart),res:f(p.responseEnd-p.responseStart)};l.slowResCB(u(u({},h),{dur:s.duration,msg:s.name,xpath:d(c).xpath,host:i,type:a}))}catch(t){console.error(t)}}var p})},F.prototype.load=function(){var t=this;"load"===this.trigger&&this.check()&&setTimeout(function(){t.report()},500)},F.prototype.leave=function(){"leave"===this.trigger&&this.report()},F.prototype.push=function(t,e,i,o){N(o.pathname,this.resOption.ignorePaths||[])||(e.push(i),this.pushWithHost(t,this.hostList,i,o)),N(o.pathname,this.slowOption.ignorePaths||[])||i.duration>this.slowOption.threshold&&this.pushWithHost(t,this.slowList,i,o)},F.prototype.pushWithHost=function(t,e,i,o){var n=o.host;e[n]||(e[n]=[]),e[n].push({timing:i,type:t})},F.prototype.collectHttpResInHttps=function(t,e,i){"https:"!==location.protocol||0!==e.name.indexOf("http://")||N(i.pathname,this.httpResOption.ignorePaths||[])||this.pushWithHost(t,this.httpResList,e,i)},F.prototype.addScript=function(t,e){"js"===e.ext&&0!==t.decodedBodySize&&this.push("js",this.jsList,t,e)},F.prototype.addResFromCss=function(t,e){e.ext&&P.hasOwnProperty(e.ext)?this.push("font",this.fontList,t,e):this.addImg(t,e)},F.prototype.addLink=function(t,e){"css"===e.ext?this.push("css",this.cssList,t,e):"js"===e.ext&&this.push("js",this.jsList,t,e)},F.prototype.addImg=function(t,e){this.push("img",this.imgList,t,e),t.decodedBodySize>(this.bigImgOption.maxSize||0)&&!N(e.pathname,this.bigImgOption.ignorePaths||[])&&this.pushWithHost("img",this.bigImgList,t,e)},F.prototype.handleTimings=function(t){for(var e=t.length,i=0;i<e;i++){var o=t[i],n=r(o.name);if(!N(n.pathname,x))switch(t[i].initiatorType){case"script":this.addScript(o,n);break;case"css":this.addResFromCss(o,n);break;case"img":this.addImg(o,n);break;case"link":this.addLink(o,n);break;case"audio":this.collectHttpResInHttps("audio",o,n);break;case"video":this.collectHttpResInHttps("video",o,n)}}},F.prototype.getNumAndSize=function(t,e){var i={},o=t+"Num",n=t+"Size",r=t+"TransferSize",s=t+"CacheRate",a=t+"Duration";i[o]=0,i[n]=0;var c=i[r]=0;if(e.forEach(function(t){i[o]++,i[n]+=t.decodedBodySize/1024,i[r]+=t.transferSize/1024,c+=t.duration}),i[a]=_(0<i[o]?c/i[o]:0),i[n]){var h=i[n]-i[r];i[s]=_(0<=h?100*h/i[n]:0)}return i[n]=_(i[n]),i[r]=_(i[r]),i},F.prototype.getMetric=function(){var t=performance.getEntriesByType("navigation")[0],e=performance.getEntriesByType("resource");if(t&&e&&e.length){this.handleTimings(e);var i=u(u(u(u({},this.getNumAndSize(z.JS,this.jsList)),this.getNumAndSize(z.CSS,this.cssList)),this.getNumAndSize(z.IMG,this.imgList)),this.getNumAndSize(z.FONT,this.fontList)),o=t;i.docSize=_(o.decodedBodySize/1024),i.docTransferSize=_(o.transferSize/1024),i.headerSize=_((o.transferSize-o.encodedBodySize||0)/1024),i.allSize=_(i.docSize+i.jsSize+i.cssSize+i.imgSize+i.fontSize),i.allTransferSize=_(i.docTransferSize+i.jsTransferSize+i.cssTransferSize+i.imgTransferSize+i.fontTransferSize);for(var n={},r=0,s=Object.keys(this.hostList);r<s.length;r++){var a=s[r],c=this.hostList[a].map(function(t){return t.timing});n[a]=this.getNumAndSize("host",c)}return{metric:i,hostMetric:n}}},F);function F(){this.trigger="load",this.jsList=[],this.cssList=[],this.imgList=[],this.fontList=[],this.hostList={},this.bigImgList={},this.httpResList={},this.slowList={},this.resOption={ignorePaths:[],trigger:"load"},this.bigImgOption={ignorePaths:[],maxSize:153600,trigger:"load"},this.httpResOption={ignorePaths:[],trigger:"load"},this.slowOption={ignorePaths:[],trigger:"load",threshold:1e3}}var A=null;window.performance&&window.performance.memory&&(A=window.performance.memory);var D=(J.prototype.listenMemory=function(t){this.cb=t},J.prototype.leave=function(){if(!this.onceLeave&&A){this.onceLeave=!0;var t=window.performance.memory;if(t.usedJSHeapSize===A.usedJSHeapSize&&t.totalJSHeapSize===A.totalJSHeapSize)return;var e=window.performance.memory;this.cb&&this.cb({usedJSHeapSize:e.usedJSHeapSize/1024,totalJSHeapSize:e.totalJSHeapSize/1024,jsHeapSizeLimit:e.jsHeapSizeLimit/1024,usedJSHeapRate:100*e.usedJSHeapSize/e.totalJSHeapSize})}},J);function J(){this.onceLeave=!1}var V=(Z.prototype.getNavigatorInfo=function(){var t={},e=navigator.connection;return"object"==typeof e&&(t={downlink:e.downlink,effectiveType:e.effectiveType,rtt:e.rtt,saveData:!!e.saveData}),navigator.deviceMemory&&(t.deviceMemory=navigator.deviceMemory),navigator.hardwareConcurrency&&(t.hardwareConcurrency=navigator.deviceMemory),t},Z.prototype.load=function(){},Z);function Z(){}var W,U,G,$=window.__spyHead,K="longtask",Q=(X.prototype.check=function(){return window.PerformanceObserver&&performance&&performance.timing&&performance.timing.navigationStart},X.prototype.listenFSPLongTask=function(t){this.check()&&(this.fspCB=t)},X.prototype.listenLCPLongTask=function(t){this.check()&&(this.lcpCB=t)},X.prototype.listenLoadLongTask=function(t){this.check()&&(this.loadCB=t)},X.prototype.listenPageLongTask=function(t){this.check()&&(this.pageCB=t)},X.prototype.load=function(){var i=this;if(this.check()){var o=this.getStatData(Date.now());setTimeout(function(){if(i.loadCB&&i.loadCB({loadLongtaskTime:o.time,loadTBT:o.tbt,loadTotalTime:o.totalTime,loadLongtaskRate:o.rate,loadLongtaskNum:o.num}),performance.timing&&performance.timing.domFirstScreenPaint){var t=i.getStatData(performance.timing.domFirstScreenPaint);i.fspCB&&i.fspCB({fspLongtaskTime:t.time,fspTBT:t.tbt,fspTotalTime:t.totalTime,fspLongtaskRate:t.rate,fspLongtaskNum:t.num})}if(L("lcp")){var e=i.getStatData(performance.timing.navigationStart+L("lcp"));i.lcpCB&&i.lcpCB({lcpLongtaskTime:e.time,lcpTBT:e.tbt,lcpTotalTime:e.totalTime,lcpLongtaskRate:e.rate,lcpLongtaskNum:e.num})}},200)}},X.prototype.leave=function(){if(!this.onceLeave){this.onceLeave=!0;var t=this.getStatData(Date.now());this.pageCB&&this.pageCB({pageLongtaskTime:t.time,pageTBT:t.tbt,pageTotalTime:t.totalTime,pageLongtaskRate:t.rate,pageLongtaskNum:t.num,pageIframeLongtaskTime:t.iframeTime,pageIframeLongtaskRate:t.iframeRate,pageIframeLongtaskNum:t.iframeNum})}},X.prototype.destroy=function(){this.lts=[],this.observer&&this.observer.disconnect(),this.observer=null},X.prototype.getStatData=function(t){for(var e=performance.timing.navigationStart,i=0,o=0,n=0,r=0,s=0,a={},c=0;c<this.lts.length;c++){var h=this.lts[c],p=h.duration;if(e+h.startTime+p<t&&(i+=p,o+=0<p-50?p-50:0,n++,h.attribution&&h.attribution[0])){var l=h.attribution[0].containerSrc;l&&l!==location.href&&(a[l]||(a[l]=[]),a[l].push(p),r+=p,s++)}}var u=t-e;return{num:n,time:i,tbt:o,totalTime:u,rate:100*i/u,iframeTime:r,iframeNum:s,iframeRate:100*r/u,iframeLongtasks:a}},X);function X(){var e=this;this.lts=[],this.observer=null,this.onceLeave=!1,$&&$.entryMap&&$.entryMap[K]&&(this.lts=this.lts.concat($.entryMap[K]));try{this.observer=new PerformanceObserver(function(t){e.lts=e.lts.concat(t.getEntries())}),this.observer.observe({entryTypes:[K]})}catch(t){}}function Y(){this.constructor=W}function tt(t){var e=G.call(this,t)||this;return e.modules=[],e.register(new g),e.register(new C),e.register(new b),e.register(new R),e.register(new H),e.register(new q),e.register(new D),e.register(new V),e.register(new Q),e.visibilitychangeCB=e.visibilitychangeCB.bind(e),e.load=e.load.bind(e),e.leave=e.leave.bind(e),"complete"===document.readyState?e.load():window.addEventListener("load",e.load),document.addEventListener("visibilitychange",e.visibilitychangeCB),window.addEventListener("beforeunload",e.leave,!1),window.addEventListener("unload",e.leave,!1),e.handleHead(),e}return i(W=tt,U=G=e),void(W.prototype=null===U?Object.create(U):(Y.prototype=U.prototype,new Y)),tt.prototype.handleHead=function(){var e=this;if(this.option.localCache){var t=window.__spyHead;if(t&&t.winerrors){for(var i=0;i<t.winerrors.length;i++){var o=t.winerrors[i];this.option.localCache.addLog(o)}t.interceptor=function(t){e.option.localCache.addLog(t)}}}},tt.prototype.listenFID=function(t){this.invoke("listenFID",t)},tt.prototype.listenLayoutShift=function(t){this.invoke("listenLayoutShift",t)},tt.prototype.listenLCP=function(t){this.invoke("listenLCP",t)},tt.prototype.listenFSPLongTask=function(t){this.invoke("listenFSPLongTask",t)},tt.prototype.listenLCPLongTask=function(t){this.invoke("listenLCPLongTask",t)},tt.prototype.listenLoadLongTask=function(t){this.invoke("listenLoadLongTask",t)},tt.prototype.listenPageLongTask=function(t){this.invoke("listenPageLongTask",t)},tt.prototype.listenMemory=function(t){this.invoke("listenMemory",t)},tt.prototype.getNavigatorInfo=function(){return this.invoke("getNavigatorInfo")},tt.prototype.listenResource=function(t,e){this.invoke("listenResource",t,e)},tt.prototype.listenBigImg=function(t,e){this.invoke("listenBigImg",t,e)},tt.prototype.listenHttpResource=function(t,e){this.invoke("listenHttpResource",t,e)},tt.prototype.listenSlowResource=function(t,e){this.invoke("listenSlowResource",t,e)},tt.prototype.listenTiming=function(t){this.invoke("listenTiming",t)},tt.prototype.listenTTI=function(t,e){this.invoke("listenTTI",t,e)},tt.prototype.invoke=function(t,e,i){for(var o=0;o<this.modules.length;o++){var n=this.modules[o];if("function"==typeof n[t])return n[t].apply(n,[e,i])}console.error("no method",t)},tt.prototype.register=function(t){this.modules.push(t)},tt.prototype.load=function(){for(var t=0;t<this.modules.length;t++){var e=this.modules[t];e.load&&e.load()}},tt.prototype.leave=function(){for(var t=0;t<this.modules.length;t++){var e=this.modules[t];e.leave&&e.leave()}},tt.prototype.destroy=function(){for(var t=0;t<this.modules.length;t++){var e=this.modules[t];e.destroy&&e.destroy()}document.removeEventListener("visibilitychange",this.visibilitychangeCB),window.removeEventListener("load",this.load),window.removeEventListener("beforeunload",this.leave),window.removeEventListener("unload",this.destroy)},tt.prototype.visibilitychangeCB=function(){"hidden"===document.visibilityState&&this.leave()},tt}();
|
package/dist/spy-client.js
CHANGED
|
@@ -5,18 +5,18 @@
|
|
|
5
5
|
}(this, (function () { 'use strict';
|
|
6
6
|
|
|
7
7
|
/*! *****************************************************************************
|
|
8
|
-
Copyright (c) Microsoft Corporation.
|
|
8
|
+
Copyright (c) Microsoft Corporation. All rights reserved.
|
|
9
|
+
Licensed under the Apache License, Version 2.0 (the "License"); you may not use
|
|
10
|
+
this file except in compliance with the License. You may obtain a copy of the
|
|
11
|
+
License at http://www.apache.org/licenses/LICENSE-2.0
|
|
9
12
|
|
|
10
|
-
|
|
11
|
-
|
|
13
|
+
THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
14
|
+
KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
|
|
15
|
+
WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
|
|
16
|
+
MERCHANTABLITY OR NON-INFRINGEMENT.
|
|
12
17
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
16
|
-
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
17
|
-
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
18
|
-
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
19
|
-
PERFORMANCE OF THIS SOFTWARE.
|
|
18
|
+
See the Apache Version 2.0 License for specific language governing permissions
|
|
19
|
+
and limitations under the License.
|
|
20
20
|
***************************************************************************** */
|
|
21
21
|
/* global Reflect, Promise */
|
|
22
22
|
|
|
@@ -138,7 +138,8 @@
|
|
|
138
138
|
* @author kaivean
|
|
139
139
|
*/
|
|
140
140
|
var defaultLogServer = 'https://sp1.baidu.com/5b1ZeDe5KgQFm2e88IuM_a/mwb2.gif?';
|
|
141
|
-
var
|
|
141
|
+
var ver = navigator.userAgent.toLowerCase().match(/cpu iphone os (.*?)_/);
|
|
142
|
+
var isLtIos14 = ver && ver[2] && (+ver[2] < 14);
|
|
142
143
|
function err(msg) {
|
|
143
144
|
console.error("[SpyClient_log]" + msg);
|
|
144
145
|
// throw new Error(msg);
|
|
@@ -155,6 +156,9 @@
|
|
|
155
156
|
return encodeURIComponent(key) + '=' + encodeURIComponent(value);
|
|
156
157
|
}).join('&');
|
|
157
158
|
}
|
|
159
|
+
function isArray(arr) {
|
|
160
|
+
return Object.prototype.toString.call(arr) === '[object Array]';
|
|
161
|
+
}
|
|
158
162
|
var SpyClient$1 = /** @class */ (function () {
|
|
159
163
|
function SpyClient(option) {
|
|
160
164
|
this.sample = {};
|
|
@@ -167,38 +171,55 @@
|
|
|
167
171
|
lid: option.lid,
|
|
168
172
|
check: option.check !== false,
|
|
169
173
|
sample: option.sample,
|
|
174
|
+
localCache: option.localCache,
|
|
170
175
|
logServer: option.logServer || defaultLogServer,
|
|
171
176
|
};
|
|
172
177
|
}
|
|
173
|
-
SpyClient.prototype.
|
|
174
|
-
if (!this.check(
|
|
178
|
+
SpyClient.prototype.handle = function (logItem) {
|
|
179
|
+
if (!this.check(logItem)) {
|
|
175
180
|
return;
|
|
176
181
|
}
|
|
182
|
+
logItem = assign({
|
|
183
|
+
pid: this.option.pid,
|
|
184
|
+
lid: this.option.lid,
|
|
185
|
+
ts: Date.now(),
|
|
186
|
+
group: 'common',
|
|
187
|
+
}, logItem);
|
|
188
|
+
if (this.option.localCache) {
|
|
189
|
+
this.option.localCache.addLog(logItem);
|
|
190
|
+
}
|
|
177
191
|
// 当前api设置了抽样,
|
|
178
|
-
if (typeof
|
|
179
|
-
if (Math.random() >
|
|
192
|
+
if (typeof logItem.sample === 'number') {
|
|
193
|
+
if (Math.random() > logItem.sample) {
|
|
180
194
|
return;
|
|
181
195
|
}
|
|
182
196
|
}
|
|
183
197
|
else if (typeof this.option.sample === 'number' && Math.random() > this.option.sample) { // 否则,用全局抽样
|
|
184
198
|
return;
|
|
185
199
|
}
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
var
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
200
|
+
delete logItem.sample;
|
|
201
|
+
return logItem;
|
|
202
|
+
};
|
|
203
|
+
SpyClient.prototype.send = function (data, post) {
|
|
204
|
+
if (post === void 0) { post = false; }
|
|
205
|
+
var logItems = isArray(data) ? data : [data];
|
|
206
|
+
var postData = [];
|
|
207
|
+
for (var _i = 0, logItems_1 = logItems; _i < logItems_1.length; _i++) {
|
|
208
|
+
var logItem = logItems_1[_i];
|
|
209
|
+
logItem = this.handle(logItem);
|
|
210
|
+
if (!logItem) {
|
|
211
|
+
continue;
|
|
212
|
+
}
|
|
213
|
+
postData.push(logItem);
|
|
214
|
+
// 期望通过post方式上传日志
|
|
215
|
+
if (post) {
|
|
216
|
+
continue;
|
|
217
|
+
}
|
|
218
|
+
var url = this.option.logServer + stringify(logItem);
|
|
219
|
+
this.request(url);
|
|
220
|
+
}
|
|
221
|
+
if (post) {
|
|
222
|
+
this.sendPost(postData);
|
|
202
223
|
}
|
|
203
224
|
};
|
|
204
225
|
SpyClient.prototype.check = function (query) {
|
|
@@ -340,6 +361,46 @@
|
|
|
340
361
|
SpyClient.prototype.clearAllMark = function () {
|
|
341
362
|
this.markCache = {};
|
|
342
363
|
};
|
|
364
|
+
// send(data, true) 也能以post发送,但是会有严格校验
|
|
365
|
+
// sendPost能以post发送,但没有校验
|
|
366
|
+
SpyClient.prototype.sendPost = function (data) {
|
|
367
|
+
var logItems = isArray(data) ? data : [data];
|
|
368
|
+
var first = logItems[0];
|
|
369
|
+
// 需要在页面暴漏出来pid等基本信息,方式调试查看基本信息与兼容目前的监控
|
|
370
|
+
var query = {
|
|
371
|
+
pid: first.pid,
|
|
372
|
+
type: first.type,
|
|
373
|
+
group: first.group,
|
|
374
|
+
};
|
|
375
|
+
var url = this.option.logServer + stringify(query);
|
|
376
|
+
this.request(url, data);
|
|
377
|
+
};
|
|
378
|
+
// 有data时,意味着要用post发送请求
|
|
379
|
+
SpyClient.prototype.request = function (url, data) {
|
|
380
|
+
if (!(!isLtIos14
|
|
381
|
+
&& navigator.sendBeacon
|
|
382
|
+
&& navigator.sendBeacon(url, data ? JSON.stringify(data) : undefined))) {
|
|
383
|
+
if (data) {
|
|
384
|
+
this.fetch(url, data);
|
|
385
|
+
}
|
|
386
|
+
else {
|
|
387
|
+
(new Image()).src = url;
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
};
|
|
391
|
+
SpyClient.prototype.fetch = function (url, data) {
|
|
392
|
+
if (!window.fetch) {
|
|
393
|
+
return;
|
|
394
|
+
}
|
|
395
|
+
fetch(url, {
|
|
396
|
+
method: 'POST',
|
|
397
|
+
credentials: 'include',
|
|
398
|
+
headers: {
|
|
399
|
+
'Content-Type': 'application/json',
|
|
400
|
+
},
|
|
401
|
+
body: JSON.stringify(data),
|
|
402
|
+
});
|
|
403
|
+
};
|
|
343
404
|
return SpyClient;
|
|
344
405
|
}());
|
|
345
406
|
|
|
@@ -1313,8 +1374,25 @@
|
|
|
1313
1374
|
document.addEventListener('visibilitychange', _this.visibilitychangeCB);
|
|
1314
1375
|
window.addEventListener('beforeunload', _this.leave, false);
|
|
1315
1376
|
window.addEventListener('unload', _this.leave, false);
|
|
1377
|
+
_this.handleHead();
|
|
1316
1378
|
return _this;
|
|
1317
1379
|
}
|
|
1380
|
+
SpyClient.prototype.handleHead = function () {
|
|
1381
|
+
var _this = this;
|
|
1382
|
+
if (this.option.localCache) {
|
|
1383
|
+
var spyHead = window.__spyHead;
|
|
1384
|
+
if (spyHead && spyHead.winerrors) {
|
|
1385
|
+
for (var index = 0; index < spyHead.winerrors.length; index++) {
|
|
1386
|
+
var obj = spyHead.winerrors[index];
|
|
1387
|
+
this.option.localCache.addLog(obj);
|
|
1388
|
+
}
|
|
1389
|
+
// Head发送的异常,也保存一份到本地,主要是JS错误和资源加载异常
|
|
1390
|
+
spyHead.interceptor = function (obj) {
|
|
1391
|
+
_this.option.localCache.addLog(obj);
|
|
1392
|
+
};
|
|
1393
|
+
}
|
|
1394
|
+
}
|
|
1395
|
+
};
|
|
1318
1396
|
SpyClient.prototype.listenFID = function (cb) {
|
|
1319
1397
|
this.invoke('listenFID', cb);
|
|
1320
1398
|
};
|
package/dist/spy-client.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t="undefined"!=typeof globalThis?globalThis:t||self).SpyClient=e()}(this,function(){"use strict";var i=function(t,e){return(i=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var i in e)e.hasOwnProperty(i)&&(t[i]=e[i])})(t,e)};var l=function(){return(l=Object.assign||function(t){for(var e,i=1,o=arguments.length;i<o;i++)for(var n in e=arguments[i])Object.prototype.hasOwnProperty.call(e,n)&&(t[n]=e[n]);return t}).apply(this,arguments)};function o(){for(var t=[],e=0;e<arguments.length;e++)t[e]=arguments[e];return(Object.assign||function(t){for(var e,i=1,o=arguments.length;i<o;i++)for(var n in e=arguments[i])Object.prototype.hasOwnProperty.call(e,n)&&(t[n]=e[n]);return t}).apply(this,t)}function r(t){var e=function(t){if(URL){t=new URL(t);if(void 0!==t.host)return{protocol:t.protocol,host:t.host,pathname:t.pathname,ext:""}}}(t);e||((i=document.createElement("a")).href=t,e={protocol:i.protocol,host:i.host||location.host,pathname:i.pathname,ext:""});var i=e.pathname.split(".");return e.ext=i[i.length-1],e}function u(t){return+t.toFixed(1)}function f(t){if(!t)return{xpath:""};for(var e=[];t&&1===t.nodeType&&t!==t.parentNode;){var i=t.tagName.toLowerCase();if(t.getAttribute("id")?i+="[#"+t.getAttribute("id")+"]":t.classList&&t.classList.length&&(i+="[."+t.classList[t.classList.length-1]+"]"),e.push(i),t===document.body)break;t=t.parentNode}return{xpath:e.join("<")}}var e=/(iPhone|iPod|iPad)/.test(navigator.userAgent);function d(t){console.error("[SpyClient_log]"+t)}var t=(n.prototype.send=function(t){if(this.check(t)){if("number"==typeof t.sample){if(Math.random()>t.sample)return}else if("number"==typeof this.option.sample&&Math.random()>this.option.sample)return;delete(t=o({pid:this.option.pid,lid:this.option.lid,ts:Date.now(),group:"common"},t)).sample;var i,t=this.option.logServer+(i=t,Object.keys(i).map(function(t){var e=i[t];return void 0===e?e="":"string"!=typeof e&&(e=JSON.stringify(e)),encodeURIComponent(t)+"="+encodeURIComponent(e)}).join("&"));!e&&navigator.sendBeacon&&navigator.sendBeacon(t)||((new Image).src=t)}},n.prototype.check=function(t){if(!this.option.check)return!0;var e=["perf","except","dist","count"];if(-1===e.indexOf(t.type))return d("type only is one of "+e.join(", ")),!1;if(t.group&&30<t.group.length)return d("group length execeeds 30"),!1;var i=/^[a-zA-Z0-9-_]{0,30}$/;if("except"===t.type){if("string"!=typeof t.info.msg||!t.info.msg.length)return d("info.msg field must be not empty and is String"),!1}else for(var o=0,n=Object.keys(t.info);o<n.length;o++){var r=n[o];if(!i.test(r))return d("info."+r+" is unexpected. Length must be not more than 30. Supported chars: a-zA-Z0-9-_"),!1;var s=t.info[r];if("dist"===t.type){if(30<s.length)return d("info."+r+" value length execeeds 30 when type == 'dist'"),!1}else if("number"!=typeof s)return d("info."+r+" value must be number"),!1}if(t.dim)for(var a=0,c=Object.keys(t.dim);a<c.length;a++){var p=c[a];if(!i.test(p))return d("dim key ["+p+"] is unexpected. Length must be not more than 30. Supported chars: a-zA-Z0-9-_"),!1;var h=t.dim[p];if(!/^[a-zA-Z0-9\-_\*\.\s\/#\+@\&\u4e00-\u9fa5]{0,30}$/.test(h))return d("dim."+p+" value ["+h+"] is unexpected. Length must be not more than 30. Supported chars: a-zA-Z0-9-_*. /#+@& and Chinese"),!1}return!0},n.prototype.sendPerf=function(t){this.send(o({type:"perf"},t))},n.prototype.sendExcept=function(t){this.send(o({type:"except"},t))},n.prototype.sendDist=function(t){this.send(o({type:"dist"},t))},n.prototype.sendCount=function(t){this.send(o({type:"count"},t))},n.prototype.sendExceptForError=function(t,e){var i=o({},e);i.info=o({},e.info||{},{msg:t.message,stack:t.stack}),this.sendExcept(i)},n.prototype.startMark=function(t){this.markCache[t]={start:Date.now()}},n.prototype.endMark=function(t){return this.markCache[t]?(this.markCache[t].total=Date.now()-this.markCache[t].start,this.markCache[t].total):0},n.prototype.clearMark=function(t){this.markCache[t]&&delete this.markCache[t]},n.prototype.getAllMark=function(){for(var t={},e=0,i=Object.keys(this.markCache);e<i.length;e++){var o=i[e];t[o]=this.markCache[o].total}return t},n.prototype.clearAllMark=function(){this.markCache={}},n);function n(t){if(this.sample={},this.markCache={},!t.pid)throw new Error("pid is required");this.option={pid:t.pid,lid:t.lid,check:!1!==t.check,sample:t.sample,logServer:t.logServer||"https://sp1.baidu.com/5b1ZeDe5KgQFm2e88IuM_a/mwb2.gif?"}}var s=window.__spyHead,a="first-input",c=(p.prototype.listenFID=function(t){this.cb=t,this.callCB()},p.prototype.callCB=function(){void 0!==this.finalValue&&this.cb&&this.cb({fid:this.finalValue})},p.prototype.destroy=function(){this.observer&&this.observer.disconnect(),this.observer=null},p.prototype.handle=function(t){t=t.pop();t&&(this.finalValue=t.duration,this.callCB(),this.destroy())},p);function p(){var e=this;if(this.observer=null,window.PerformanceObserver){s&&s.entryMap&&s.entryMap[a]&&this.handle(s.entryMap[a]);try{this.observer=new PerformanceObserver(function(t){e.handle(t.getEntries())}),this.observer.observe({entryTypes:[a]})}catch(t){}}}var h=window.__spyHead,m="layout-shift",g=(v.prototype.listenLayoutShift=function(t){this.cb=t},v.prototype.leave=function(){this.onceLeave||(this.onceLeave=!0,this.observer&&this.observer.takeRecords&&this.observer.takeRecords(),this.finalValue=this.value,this.callCB(),this.destroy())},v.prototype.callCB=function(){void 0!==this.finalValue&&this.cb&&this.cb({layoutShift:this.finalValue})},v.prototype.destroy=function(){this.observer&&this.observer.disconnect(),this.observer=null},v.prototype.handle=function(t){var e=this;t.map(function(t){t.hadRecentInput||(e.value=(e.value||0)+(t.value||0))})},v);function v(){var e=this;if(this.observer=null,this.onceLeave=!1,window.PerformanceObserver){h&&h.entryMap&&h.entryMap[m]&&this.handle(h.entryMap[m]);try{this.observer=new PerformanceObserver(function(t){e.handle(t.getEntries())}),this.observer.observe({entryTypes:[m]})}catch(t){}}}var y={};var b=window.__spyHead,k="largest-contentful-paint",L=(S.prototype.listenLCP=function(t){this.cb=t,this.callCB()},S.prototype.callCB=function(){this.finalValue&&this.cb&&this.cb({lcp:this.finalValue})},S.prototype.load=function(){var t,e;this.observer&&this.observer.takeRecords&&this.observer.takeRecords(),this.finalValue=this.value,t="lcp",e=this.value,y[t]=e,this.callCB(),this.destroy()},S.prototype.destroy=function(){this.observer&&this.observer.disconnect(),this.observer=null},S.prototype.handle=function(t){var e=this;t.map(function(t){e.value=t.renderTime||t.loadTime})},S);function S(){var e=this;if(this.observer=null,window.PerformanceObserver){b&&b.entryMap&&b.entryMap[k]&&this.handle(b.entryMap[k]);try{this.observer=new PerformanceObserver(function(t){e.handle(t.getEntries())}),this.observer.observe({entryTypes:[k]})}catch(t){}}}function w(t){return t.name.indexOf(".gif?")<0}var T=(C.prototype.check=function(){return window.PerformanceObserver&&performance&&performance.timing&&performance.timing.navigationStart},C.prototype.listenTTI=function(t,e){this.check()&&(this.observeLongtask(2e4),this.cb=t,(e=e||{}).interval&&(this.interval=e.interval),e.filterRequest&&(this.filterRequest=e.filterRequest))},C.prototype.load=function(){this.check()&&(this.ttiTimer=setInterval(this.ttiCheck,this.interval))},C.prototype.ttiCheck=function(){var t,e;this.cb&&((t=performance.now())-this.lastLongTask<this.interval||!1!==(e=this.getNetworkSilenceAt())&&(t-e<this.interval||(clearTimeout(this.ttiTimer),e=(e=this.lastLongTask)||performance.timing.domContentLoadedEventEnd-performance.timing.navigationStart,this.stopObserveLongTask(),this.cb({tti:e}))))},C.prototype.observerCallback=function(t){t=t.getEntries(),t=t[t.length-1];this.lastLongTask=t.startTime+t.duration},C.prototype.stopObserveLongTask=function(){this.observer&&(this.observer.disconnect(),this.observer=null),this.stopLongTaskTimeoutId&&clearTimeout(this.stopLongTaskTimeoutId)},C.prototype.getNetworkSilenceAt=function(){for(var t=0,e=0,i=performance.getEntriesByType("resource").filter(this.filterRequest);e<i.length;e++){var o=i[e];if(!o.responseEnd)return!1;o.responseEnd>t&&(t=o.responseEnd)}return t},C.prototype.observeLongtask=function(t){this.stopObserveLongTask();try{this.observer=new PerformanceObserver(this.observerCallback),this.observer.observe({entryTypes:["longtask"]}),this.stopLongTaskTimeoutId=setTimeout(this.stopObserveLongTask,t)}catch(t){}},C);function C(){this.lastLongTask=0,this.observer=null,this.interval=5e3,this.filterRequest=w,this.observerCallback=this.observerCallback.bind(this),this.ttiCheck=this.ttiCheck.bind(this)}var O,B,R,I=(z.prototype.listenTiming=function(t){window.performance&&window.performance.timing&&(this.cb=t)},z.prototype.load=function(){var t=this;setTimeout(function(){t.cb&&t.cb(t.getMetric())},500)},z.prototype.getMetric=function(){var t,o={},e=window.performance.timing,i=e.navigationStart;return o.dns=e.domainLookupEnd-e.domainLookupStart,o.tcp=e.connectEnd-e.connectStart,o.request=e.responseStart-e.requestStart,o.response=e.responseEnd-e.responseStart,o.parseHtml=e.domInteractive-e.domLoading,o.domReady=e.domContentLoadedEventEnd-i,o.loadEventHandle=e.loadEventEnd-e.loadEventStart,o.load=e.loadEventEnd-i,!performance.getEntriesByType||(t=performance.getEntriesByType("paint"))&&t.length&&t.forEach(function(t){var e=t.name,i=t.duration,t=t.startTime,t=Math.ceil(i+t);"first-paint"===e&&(o.fp=t),"first-contentful-paint"===e&&(o.fcp=t)}),e.domFirstPaint&&(o.t7FirstPaint=e.domFirstPaint-i),e.domFirstScreenPaint&&(o.t7FirstScreen=e.domFirstScreenPaint-i),o},z);function z(){}(R=O=O||{}).JS="js",R.CSS="css",R.IMG="img",R.FONT="font",(R=B=B||{})[R.ttf=0]="ttf",R[R.eot=1]="eot",R[R.woff=2]="woff",R[R.woff2=3]="woff2";var E=["/mwb2.gif"];function P(t,e){for(var i=0,o=e;i<o.length;i++){var n=o[i];if(-1<t.indexOf(n))return 1}}function x(t){return+t.toFixed(1)}var M=(j.prototype.check=function(){return performance&&performance.getEntriesByType},j.prototype.listenResource=function(t,e){void 0===e&&(e={}),this.check()&&(this.resOption=o(this.resOption,e),this.trigger=this.resOption.trigger,this.cb=t)},j.prototype.listenBigImg=function(t,e){void 0===e&&(e={}),this.check()&&(this.bigImgOption=o(this.bigImgOption,e),this.trigger=this.bigImgOption.trigger,this.bigImgCB=t)},j.prototype.listenHttpResource=function(t,e){void 0===e&&(e={}),this.check()&&(this.httpResOption=o(this.httpResOption,e),this.trigger=this.httpResOption.trigger,this.httpResCB=t)},j.prototype.listenSlowResource=function(t,e){void 0===e&&(e={}),this.check()&&(this.slowOption=o(this.slowOption,e),this.trigger=this.slowOption.trigger,this.slowResCB=t)},j.prototype.report=function(){var t,h=this,e=this.getMetric();e&&(t=e.metric,e=e.hostMetric,this.cb&&this.cb(t,e)),this.bigImgCB&&"function"==typeof window.requestIdleCallback&&Object.keys(this.bigImgList).length&&window.requestIdleCallback(function(){for(var t=0,e=Object.keys(h.bigImgList);t<e.length;t++)for(var i=e[t],o=0,n=h.bigImgList[i];o<n.length;o++){var r=n[o],s=r.timing,r=r.type;try{var a=document.body.querySelector('img[src="'+s.name+'"]');h.bigImgCB({msg:s.name,dur:s.duration,xpath:f(a).xpath,host:i,type:r})}catch(t){console.error(t)}}}),this.httpResCB&&"function"==typeof window.requestIdleCallback&&Object.keys(this.httpResList).length&&window.requestIdleCallback(function(){for(var t=0,e=Object.keys(h.httpResList);t<e.length;t++)for(var i=e[t],o=0,n=h.httpResList[i];o<n.length;o++){var r=n[o],s=r.timing,r=r.type;try{var a=document.body.querySelector('[src="'+s.name+'"]');h.httpResCB({msg:s.name,dur:s.duration,xpath:f(a).xpath,host:i,type:r})}catch(t){console.error(t)}}}),this.slowResCB&&"function"==typeof window.requestIdleCallback&&Object.keys(this.slowList).length&&window.requestIdleCallback(function(){for(var t,e=0,i=Object.keys(h.slowList);e<i.length;e++)for(var o=i[e],n=0,r=h.slowList[o];n<r.length;n++){var s=r[n],a=s.timing,s=s.type;try{var c=document.body.querySelector('[src="'+a.name+'"]'),p={wait:u((t=a).domainLookupStart-(t.navigationStart||t.fetchStart||t.startTime)),dns:u(t.domainLookupEnd-t.domainLookupStart),connect:u(t.connectEnd-t.connectStart),req:u(t.responseStart-t.requestStart),res:u(t.responseEnd-t.responseStart)};h.slowResCB(l(l({},p),{dur:a.duration,msg:a.name,xpath:f(c).xpath,host:o,type:s}))}catch(t){console.error(t)}}})},j.prototype.load=function(){var t=this;"load"===this.trigger&&this.check()&&setTimeout(function(){t.report()},500)},j.prototype.leave=function(){"leave"===this.trigger&&this.report()},j.prototype.push=function(t,e,i,o){P(o.pathname,this.resOption.ignorePaths||[])||(e.push(i),this.pushWithHost(t,this.hostList,i,o)),P(o.pathname,this.slowOption.ignorePaths||[])||i.duration>this.slowOption.threshold&&this.pushWithHost(t,this.slowList,i,o)},j.prototype.pushWithHost=function(t,e,i,o){o=o.host;e[o]||(e[o]=[]),e[o].push({timing:i,type:t})},j.prototype.collectHttpResInHttps=function(t,e,i){"https:"!==location.protocol||0!==e.name.indexOf("http://")||P(i.pathname,this.httpResOption.ignorePaths||[])||this.pushWithHost(t,this.httpResList,e,i)},j.prototype.addScript=function(t,e){"js"===e.ext&&0!==t.decodedBodySize&&this.push("js",this.jsList,t,e)},j.prototype.addResFromCss=function(t,e){e.ext&&B.hasOwnProperty(e.ext)?this.push("font",this.fontList,t,e):this.addImg(t,e)},j.prototype.addLink=function(t,e){"css"===e.ext?this.push("css",this.cssList,t,e):"js"===e.ext&&this.push("js",this.jsList,t,e)},j.prototype.addImg=function(t,e){this.push("img",this.imgList,t,e),t.decodedBodySize>(this.bigImgOption.maxSize||0)&&!P(e.pathname,this.bigImgOption.ignorePaths||[])&&this.pushWithHost("img",this.bigImgList,t,e)},j.prototype.handleTimings=function(t){for(var e=t.length,i=0;i<e;i++){var o=t[i],n=r(o.name);if(!P(n.pathname,E))switch(t[i].initiatorType){case"script":this.addScript(o,n);break;case"css":this.addResFromCss(o,n);break;case"img":this.addImg(o,n);break;case"link":this.addLink(o,n);break;case"audio":this.collectHttpResInHttps("audio",o,n);break;case"video":this.collectHttpResInHttps("video",o,n)}}},j.prototype.getNumAndSize=function(t,e){var i={},o=t+"Num",n=t+"Size",r=t+"TransferSize",s=t+"CacheRate",t=t+"Duration";i[o]=0,i[n]=0;var a=i[r]=0;return e.forEach(function(t){i[o]++,i[n]+=t.decodedBodySize/1024,i[r]+=t.transferSize/1024,a+=t.duration}),i[t]=x(0<i[o]?a/i[o]:0),i[n]&&(t=i[n]-i[r],i[s]=x(0<=t?100*t/i[n]:0)),i[n]=x(i[n]),i[r]=x(i[r]),i},j.prototype.getMetric=function(){var t=performance.getEntriesByType("navigation")[0],e=performance.getEntriesByType("resource");if(t&&e&&e.length){this.handleTimings(e);e=l(l(l(l({},this.getNumAndSize(O.JS,this.jsList)),this.getNumAndSize(O.CSS,this.cssList)),this.getNumAndSize(O.IMG,this.imgList)),this.getNumAndSize(O.FONT,this.fontList)),t=t;e.docSize=x(t.decodedBodySize/1024),e.docTransferSize=x(t.transferSize/1024),e.headerSize=x((t.transferSize-t.encodedBodySize||0)/1024),e.allSize=x(e.docSize+e.jsSize+e.cssSize+e.imgSize+e.fontSize),e.allTransferSize=x(e.docTransferSize+e.jsTransferSize+e.cssTransferSize+e.imgTransferSize+e.fontTransferSize);for(var i={},o=0,n=Object.keys(this.hostList);o<n.length;o++){var r=n[o],s=this.hostList[r].map(function(t){return t.timing});i[r]=this.getNumAndSize("host",s)}return{metric:e,hostMetric:i}}},j);function j(){this.trigger="load",this.jsList=[],this.cssList=[],this.imgList=[],this.fontList=[],this.hostList={},this.bigImgList={},this.httpResList={},this.slowList={},this.resOption={ignorePaths:[],trigger:"load"},this.bigImgOption={ignorePaths:[],maxSize:153600,trigger:"load"},this.httpResOption={ignorePaths:[],trigger:"load"},this.slowOption={ignorePaths:[],trigger:"load",threshold:1e3}}var H=null;window.performance&&window.performance.memory&&(H=window.performance.memory);var N=(_.prototype.listenMemory=function(t){this.cb=t},_.prototype.leave=function(){var t;!this.onceLeave&&H&&(this.onceLeave=!0,(t=window.performance.memory).usedJSHeapSize===H.usedJSHeapSize&&t.totalJSHeapSize===H.totalJSHeapSize||(t=window.performance.memory,this.cb&&this.cb({usedJSHeapSize:t.usedJSHeapSize/1024,totalJSHeapSize:t.totalJSHeapSize/1024,jsHeapSizeLimit:t.jsHeapSizeLimit/1024,usedJSHeapRate:100*t.usedJSHeapSize/t.totalJSHeapSize})))},_);function _(){this.onceLeave=!1}var F=(q.prototype.getNavigatorInfo=function(){var t={},e=navigator.connection;return"object"==typeof e&&(t={downlink:e.downlink,effectiveType:e.effectiveType,rtt:e.rtt,saveData:!!e.saveData}),navigator.deviceMemory&&(t.deviceMemory=navigator.deviceMemory),navigator.hardwareConcurrency&&(t.hardwareConcurrency=navigator.deviceMemory),t},q.prototype.load=function(){},q);function q(){}var A,D,J,V=window.__spyHead,Z="longtask",W=(U.prototype.check=function(){return window.PerformanceObserver&&performance&&performance.timing&&performance.timing.navigationStart},U.prototype.listenFSPLongTask=function(t){this.check()&&(this.fspCB=t)},U.prototype.listenLCPLongTask=function(t){this.check()&&(this.lcpCB=t)},U.prototype.listenLoadLongTask=function(t){this.check()&&(this.loadCB=t)},U.prototype.listenPageLongTask=function(t){this.check()&&(this.pageCB=t)},U.prototype.load=function(){var e,i=this;this.check()&&(e=this.getStatData(Date.now()),setTimeout(function(){var t;i.loadCB&&i.loadCB({loadLongtaskTime:e.time,loadTBT:e.tbt,loadTotalTime:e.totalTime,loadLongtaskRate:e.rate,loadLongtaskNum:e.num}),performance.timing&&performance.timing.domFirstScreenPaint&&(t=i.getStatData(performance.timing.domFirstScreenPaint),i.fspCB&&i.fspCB({fspLongtaskTime:t.time,fspTBT:t.tbt,fspTotalTime:t.totalTime,fspLongtaskRate:t.rate,fspLongtaskNum:t.num})),y.lcp&&(t=i.getStatData(performance.timing.navigationStart+y.lcp),i.lcpCB&&i.lcpCB({lcpLongtaskTime:t.time,lcpTBT:t.tbt,lcpTotalTime:t.totalTime,lcpLongtaskRate:t.rate,lcpLongtaskNum:t.num}))},200))},U.prototype.leave=function(){var t;this.onceLeave||(this.onceLeave=!0,t=this.getStatData(Date.now()),this.pageCB&&this.pageCB({pageLongtaskTime:t.time,pageTBT:t.tbt,pageTotalTime:t.totalTime,pageLongtaskRate:t.rate,pageLongtaskNum:t.num,pageIframeLongtaskTime:t.iframeTime,pageIframeLongtaskRate:t.iframeRate,pageIframeLongtaskNum:t.iframeNum}))},U.prototype.destroy=function(){this.lts=[],this.observer&&this.observer.disconnect(),this.observer=null},U.prototype.getStatData=function(t){for(var e=performance.timing.navigationStart,i=0,o=0,n=0,r=0,s=0,a={},c=0;c<this.lts.length;c++){var p=this.lts[c],h=p.duration;e+p.startTime+h<t&&(i+=h,o+=0<h-50?h-50:0,n++,p.attribution&&p.attribution[0]&&((p=p.attribution[0].containerSrc)&&p!==location.href&&(a[p]||(a[p]=[]),a[p].push(h),r+=h,s++)))}var l=t-e;return{num:n,time:i,tbt:o,totalTime:l,rate:100*i/l,iframeTime:r,iframeNum:s,iframeRate:100*r/l,iframeLongtasks:a}},U);function U(){var e=this;this.lts=[],this.observer=null,this.onceLeave=!1,V&&V.entryMap&&V.entryMap[Z]&&(this.lts=this.lts.concat(V.entryMap[Z]));try{this.observer=new PerformanceObserver(function(t){e.lts=e.lts.concat(t.getEntries())}),this.observer.observe({entryTypes:[Z]})}catch(t){}}function G(){this.constructor=D}function $(t){t=A.call(this,t)||this;return t.modules=[],t.register(new c),t.register(new L),t.register(new g),t.register(new T),t.register(new I),t.register(new M),t.register(new N),t.register(new F),t.register(new W),t.visibilitychangeCB=t.visibilitychangeCB.bind(t),t.load=t.load.bind(t),t.leave=t.leave.bind(t),"complete"===document.readyState?t.load():window.addEventListener("load",t.load),document.addEventListener("visibilitychange",t.visibilitychangeCB),window.addEventListener("beforeunload",t.leave,!1),window.addEventListener("unload",t.leave,!1),t}return i(D=$,J=A=t),D.prototype=null===J?Object.create(J):(G.prototype=J.prototype,new G),$.prototype.listenFID=function(t){this.invoke("listenFID",t)},$.prototype.listenLayoutShift=function(t){this.invoke("listenLayoutShift",t)},$.prototype.listenLCP=function(t){this.invoke("listenLCP",t)},$.prototype.listenFSPLongTask=function(t){this.invoke("listenFSPLongTask",t)},$.prototype.listenLCPLongTask=function(t){this.invoke("listenLCPLongTask",t)},$.prototype.listenLoadLongTask=function(t){this.invoke("listenLoadLongTask",t)},$.prototype.listenPageLongTask=function(t){this.invoke("listenPageLongTask",t)},$.prototype.listenMemory=function(t){this.invoke("listenMemory",t)},$.prototype.getNavigatorInfo=function(){return this.invoke("getNavigatorInfo")},$.prototype.listenResource=function(t,e){this.invoke("listenResource",t,e)},$.prototype.listenBigImg=function(t,e){this.invoke("listenBigImg",t,e)},$.prototype.listenHttpResource=function(t,e){this.invoke("listenHttpResource",t,e)},$.prototype.listenSlowResource=function(t,e){this.invoke("listenSlowResource",t,e)},$.prototype.listenTiming=function(t){this.invoke("listenTiming",t)},$.prototype.listenTTI=function(t,e){this.invoke("listenTTI",t,e)},$.prototype.invoke=function(t,e,i){for(var o=0;o<this.modules.length;o++){var n=this.modules[o];if("function"==typeof n[t])return n[t].apply(n,[e,i])}console.error("no method",t)},$.prototype.register=function(t){this.modules.push(t)},$.prototype.load=function(){for(var t=0;t<this.modules.length;t++){var e=this.modules[t];e.load&&e.load()}},$.prototype.leave=function(){for(var t=0;t<this.modules.length;t++){var e=this.modules[t];e.leave&&e.leave()}},$.prototype.destroy=function(){for(var t=0;t<this.modules.length;t++){var e=this.modules[t];e.destroy&&e.destroy()}document.removeEventListener("visibilitychange",this.visibilitychangeCB),window.removeEventListener("load",this.load),window.removeEventListener("beforeunload",this.leave),window.removeEventListener("unload",this.destroy)},$.prototype.visibilitychangeCB=function(){"hidden"===document.visibilityState&&this.leave()},$});
|
|
1
|
+
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t="undefined"!=typeof globalThis?globalThis:t||self).SpyClient=e()}(this,function(){"use strict";var i=function(t,e){return(i=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var i in e)e.hasOwnProperty(i)&&(t[i]=e[i])})(t,e)};var u=function(){return(u=Object.assign||function(t){for(var e,i=1,o=arguments.length;i<o;i++)for(var n in e=arguments[i])Object.prototype.hasOwnProperty.call(e,n)&&(t[n]=e[n]);return t}).apply(this,arguments)};function o(){for(var t=[],e=0;e<arguments.length;e++)t[e]=arguments[e];return(Object.assign||function(t){for(var e=void 0,i=1,o=arguments.length;i<o;i++)for(var n in e=arguments[i])Object.prototype.hasOwnProperty.call(e,n)&&(t[n]=e[n]);return t}).apply(this,t)}function r(t){var e=function(t){if(URL){var e=new URL(t);if(void 0!==e.host)return{protocol:e.protocol,host:e.host,pathname:e.pathname,ext:""}}}(t);if(!e){var i=document.createElement("a");i.href=t,e={protocol:i.protocol,host:i.host||location.host,pathname:i.pathname,ext:""}}var o=e.pathname.split(".");return e.ext=o[o.length-1],e}function f(t){return+t.toFixed(1)}function d(t){if(!t)return{xpath:""};for(var e=[];t&&1===t.nodeType&&t!==t.parentNode;){var i=t.tagName.toLowerCase();if(t.getAttribute("id")?i+="[#"+t.getAttribute("id")+"]":t.classList&&t.classList.length&&(i+="[."+t.classList[t.classList.length-1]+"]"),e.push(i),t===document.body)break;t=t.parentNode}return{xpath:e.join("<")}}var t=navigator.userAgent.toLowerCase().match(/cpu iphone os (.*?)_/),n=t&&t[2]&&+t[2]<14;function l(t){console.error("[SpyClient_log]"+t)}function a(i){return Object.keys(i).map(function(t){var e=i[t];return void 0===e?e="":"string"!=typeof e&&(e=JSON.stringify(e)),encodeURIComponent(t)+"="+encodeURIComponent(e)}).join("&")}function c(t){return"[object Array]"===Object.prototype.toString.call(t)}var e=(s.prototype.handle=function(t){if(this.check(t)){if(t=o({pid:this.option.pid,lid:this.option.lid,ts:Date.now(),group:"common"},t),this.option.localCache&&this.option.localCache.addLog(t),"number"==typeof t.sample){if(Math.random()>t.sample)return}else if("number"==typeof this.option.sample&&Math.random()>this.option.sample)return;return delete t.sample,t}},s.prototype.send=function(t,e){void 0===e&&(e=!1);for(var i=[],o=0,n=c(t)?t:[t];o<n.length;o++){var r=n[o];if((r=this.handle(r))&&(i.push(r),!e)){var s=this.option.logServer+a(r);this.request(s)}}e&&this.sendPost(i)},s.prototype.check=function(t){if(!this.option.check)return!0;var e=["perf","except","dist","count"];if(-1===e.indexOf(t.type))return l("type only is one of "+e.join(", ")),!1;if(t.group&&30<t.group.length)return l("group length execeeds 30"),!1;var i=/^[a-zA-Z0-9-_]{0,30}$/;if("except"===t.type){if("string"!=typeof t.info.msg||!t.info.msg.length)return l("info.msg field must be not empty and is String"),!1}else for(var o=0,n=Object.keys(t.info);o<n.length;o++){var r=n[o];if(!i.test(r))return l("info."+r+" is unexpected. Length must be not more than 30. Supported chars: a-zA-Z0-9-_"),!1;var s=t.info[r];if("dist"===t.type){if(30<s.length)return l("info."+r+" value length execeeds 30 when type == 'dist'"),!1}else if("number"!=typeof s)return l("info."+r+" value must be number"),!1}if(t.dim)for(var a=0,c=Object.keys(t.dim);a<c.length;a++){var p=c[a];if(!i.test(p))return l("dim key ["+p+"] is unexpected. Length must be not more than 30. Supported chars: a-zA-Z0-9-_"),!1;var h=t.dim[p];if(!/^[a-zA-Z0-9\-_\*\.\s\/#\+@\&\u4e00-\u9fa5]{0,30}$/.test(h))return l("dim."+p+" value ["+h+"] is unexpected. Length must be not more than 30. Supported chars: a-zA-Z0-9-_*. /#+@& and Chinese"),!1}return!0},s.prototype.sendPerf=function(t){this.send(o({type:"perf"},t))},s.prototype.sendExcept=function(t){this.send(o({type:"except"},t))},s.prototype.sendDist=function(t){this.send(o({type:"dist"},t))},s.prototype.sendCount=function(t){this.send(o({type:"count"},t))},s.prototype.sendExceptForError=function(t,e){var i=o({},e);i.info=o({},e.info||{},{msg:t.message,stack:t.stack}),this.sendExcept(i)},s.prototype.startMark=function(t){this.markCache[t]={start:Date.now()}},s.prototype.endMark=function(t){return this.markCache[t]?(this.markCache[t].total=Date.now()-this.markCache[t].start,this.markCache[t].total):0},s.prototype.clearMark=function(t){this.markCache[t]&&delete this.markCache[t]},s.prototype.getAllMark=function(){for(var t={},e=0,i=Object.keys(this.markCache);e<i.length;e++){var o=i[e];t[o]=this.markCache[o].total}return t},s.prototype.clearAllMark=function(){this.markCache={}},s.prototype.sendPost=function(t){var e=(c(t)?t:[t])[0],i={pid:e.pid,type:e.type,group:e.group},o=this.option.logServer+a(i);this.request(o,t)},s.prototype.request=function(t,e){!n&&navigator.sendBeacon&&navigator.sendBeacon(t,e?JSON.stringify(e):void 0)||(e?this.fetch(t,e):(new Image).src=t)},s.prototype.fetch=function(t,e){window.fetch&&fetch(t,{method:"POST",credentials:"include",headers:{"Content-Type":"application/json"},body:JSON.stringify(e)})},s);function s(t){if(this.sample={},this.markCache={},!t.pid)throw new Error("pid is required");this.option={pid:t.pid,lid:t.lid,check:!1!==t.check,sample:t.sample,localCache:t.localCache,logServer:t.logServer||"https://sp1.baidu.com/5b1ZeDe5KgQFm2e88IuM_a/mwb2.gif?"}}var p=window.__spyHead,h="first-input",g=(m.prototype.listenFID=function(t){this.cb=t,this.callCB()},m.prototype.callCB=function(){void 0!==this.finalValue&&this.cb&&this.cb({fid:this.finalValue})},m.prototype.destroy=function(){this.observer&&this.observer.disconnect(),this.observer=null},m.prototype.handle=function(t){var e=t.pop();e&&(this.finalValue=e.duration,this.callCB(),this.destroy())},m);function m(){var e=this;if(this.observer=null,window.PerformanceObserver){p&&p.entryMap&&p.entryMap[h]&&this.handle(p.entryMap[h]);try{this.observer=new PerformanceObserver(function(t){e.handle(t.getEntries())}),this.observer.observe({entryTypes:[h]})}catch(t){}}}var v=window.__spyHead,y="layout-shift",b=(k.prototype.listenLayoutShift=function(t){this.cb=t},k.prototype.leave=function(){this.onceLeave||(this.onceLeave=!0,this.observer&&this.observer.takeRecords&&this.observer.takeRecords(),this.finalValue=this.value,this.callCB(),this.destroy())},k.prototype.callCB=function(){void 0!==this.finalValue&&this.cb&&this.cb({layoutShift:this.finalValue})},k.prototype.destroy=function(){this.observer&&this.observer.disconnect(),this.observer=null},k.prototype.handle=function(t){var e=this;t.map(function(t){t.hadRecentInput||(e.value=(e.value||0)+(t.value||0))})},k);function k(){var e=this;if(this.observer=null,this.onceLeave=!1,window.PerformanceObserver){v&&v.entryMap&&v.entryMap[y]&&this.handle(v.entryMap[y]);try{this.observer=new PerformanceObserver(function(t){e.handle(t.getEntries())}),this.observer.observe({entryTypes:[y]})}catch(t){}}}var w={};function L(t){return w[t]}var S=window.__spyHead,T="largest-contentful-paint",C=(O.prototype.listenLCP=function(t){this.cb=t,this.callCB()},O.prototype.callCB=function(){this.finalValue&&this.cb&&this.cb({lcp:this.finalValue})},O.prototype.load=function(){this.observer&&this.observer.takeRecords&&this.observer.takeRecords(),this.finalValue=this.value,function(t,e){w[t]=e}("lcp",this.value),this.callCB(),this.destroy()},O.prototype.destroy=function(){this.observer&&this.observer.disconnect(),this.observer=null},O.prototype.handle=function(t){var e=this;t.map(function(t){e.value=t.renderTime||t.loadTime})},O);function O(){var e=this;if(this.observer=null,window.PerformanceObserver){S&&S.entryMap&&S.entryMap[T]&&this.handle(S.entryMap[T]);try{this.observer=new PerformanceObserver(function(t){e.handle(t.getEntries())}),this.observer.observe({entryTypes:[T]})}catch(t){}}}function B(t){return t.name.indexOf(".gif?")<0}var R=(I.prototype.check=function(){return window.PerformanceObserver&&performance&&performance.timing&&performance.timing.navigationStart},I.prototype.listenTTI=function(t,e){this.check()&&(this.observeLongtask(2e4),this.cb=t,(e=e||{}).interval&&(this.interval=e.interval),e.filterRequest&&(this.filterRequest=e.filterRequest))},I.prototype.load=function(){this.check()&&(this.ttiTimer=setInterval(this.ttiCheck,this.interval))},I.prototype.ttiCheck=function(){if(this.cb){var t=performance.now();if(!(t-this.lastLongTask<this.interval)){var e=this.getNetworkSilenceAt();if(!1!==e&&!(t-e<this.interval)){clearTimeout(this.ttiTimer);var i=this.lastLongTask;i=i||performance.timing.domContentLoadedEventEnd-performance.timing.navigationStart,this.stopObserveLongTask(),this.cb({tti:i})}}}},I.prototype.observerCallback=function(t){var e=t.getEntries(),i=e[e.length-1];this.lastLongTask=i.startTime+i.duration},I.prototype.stopObserveLongTask=function(){this.observer&&(this.observer.disconnect(),this.observer=null),this.stopLongTaskTimeoutId&&clearTimeout(this.stopLongTaskTimeoutId)},I.prototype.getNetworkSilenceAt=function(){for(var t=0,e=0,i=performance.getEntriesByType("resource").filter(this.filterRequest);e<i.length;e++){var o=i[e];if(!o.responseEnd)return!1;o.responseEnd>t&&(t=o.responseEnd)}return t},I.prototype.observeLongtask=function(t){this.stopObserveLongTask();try{this.observer=new PerformanceObserver(this.observerCallback),this.observer.observe({entryTypes:["longtask"]}),this.stopLongTaskTimeoutId=setTimeout(this.stopObserveLongTask,t)}catch(t){}},I);function I(){this.lastLongTask=0,this.observer=null,this.interval=5e3,this.filterRequest=B,this.observerCallback=this.observerCallback.bind(this),this.ttiCheck=this.ttiCheck.bind(this)}var z,E,P,j,x=(H.prototype.listenTiming=function(t){window.performance&&window.performance.timing&&(this.cb=t)},H.prototype.load=function(){var t=this;setTimeout(function(){t.cb&&t.cb(t.getMetric())},500)},H.prototype.getMetric=function(){var r={},t=window.performance.timing,e=t.navigationStart;if(r.dns=t.domainLookupEnd-t.domainLookupStart,r.tcp=t.connectEnd-t.connectStart,r.request=t.responseStart-t.requestStart,r.response=t.responseEnd-t.responseStart,r.parseHtml=t.domInteractive-t.domLoading,r.domReady=t.domContentLoadedEventEnd-e,r.loadEventHandle=t.loadEventEnd-t.loadEventStart,r.load=t.loadEventEnd-e,performance.getEntriesByType){var i=performance.getEntriesByType("paint");i&&i.length&&i.forEach(function(t){var e=t.name,i=t.duration,o=t.startTime,n=Math.ceil(i+o);"first-paint"===e&&(r.fp=n),"first-contentful-paint"===e&&(r.fcp=n)})}return t.domFirstPaint&&(r.t7FirstPaint=t.domFirstPaint-e),t.domFirstScreenPaint&&(r.t7FirstScreen=t.domFirstScreenPaint-e),r},H);function H(){}(E=z=z||{}).JS="js",E.CSS="css",E.IMG="img",E.FONT="font",(j=P=P||{})[j.ttf=0]="ttf",j[j.eot=1]="eot",j[j.woff=2]="woff",j[j.woff2=3]="woff2";var M=["/mwb2.gif"];function N(t,e){for(var i=0,o=e;i<o.length;i++){var n=o[i];if(-1<t.indexOf(n))return!0}return!1}function _(t){return+t.toFixed(1)}var q=(F.prototype.check=function(){return performance&&performance.getEntriesByType},F.prototype.listenResource=function(t,e){void 0===e&&(e={}),this.check()&&(this.resOption=o(this.resOption,e),this.trigger=this.resOption.trigger,this.cb=t)},F.prototype.listenBigImg=function(t,e){void 0===e&&(e={}),this.check()&&(this.bigImgOption=o(this.bigImgOption,e),this.trigger=this.bigImgOption.trigger,this.bigImgCB=t)},F.prototype.listenHttpResource=function(t,e){void 0===e&&(e={}),this.check()&&(this.httpResOption=o(this.httpResOption,e),this.trigger=this.httpResOption.trigger,this.httpResCB=t)},F.prototype.listenSlowResource=function(t,e){void 0===e&&(e={}),this.check()&&(this.slowOption=o(this.slowOption,e),this.trigger=this.slowOption.trigger,this.slowResCB=t)},F.prototype.report=function(){var l=this,t=this.getMetric();if(t){var e=t.metric,i=t.hostMetric;this.cb&&this.cb(e,i)}this.bigImgCB&&"function"==typeof window.requestIdleCallback&&Object.keys(this.bigImgList).length&&window.requestIdleCallback(function(){for(var t=0,e=Object.keys(l.bigImgList);t<e.length;t++)for(var i=e[t],o=0,n=l.bigImgList[i];o<n.length;o++){var r=n[o],s=r.timing,a=r.type;try{var c=document.body.querySelector('img[src="'+s.name+'"]');l.bigImgCB({msg:s.name,dur:s.duration,xpath:d(c).xpath,host:i,type:a})}catch(t){console.error(t)}}}),this.httpResCB&&"function"==typeof window.requestIdleCallback&&Object.keys(this.httpResList).length&&window.requestIdleCallback(function(){for(var t=0,e=Object.keys(l.httpResList);t<e.length;t++)for(var i=e[t],o=0,n=l.httpResList[i];o<n.length;o++){var r=n[o],s=r.timing,a=r.type;try{var c=document.body.querySelector('[src="'+s.name+'"]');l.httpResCB({msg:s.name,dur:s.duration,xpath:d(c).xpath,host:i,type:a})}catch(t){console.error(t)}}}),this.slowResCB&&"function"==typeof window.requestIdleCallback&&Object.keys(this.slowList).length&&window.requestIdleCallback(function(){for(var t=0,e=Object.keys(l.slowList);t<e.length;t++)for(var i=e[t],o=0,n=l.slowList[i];o<n.length;o++){var r=n[o],s=r.timing,a=r.type;try{var c=document.body.querySelector('[src="'+s.name+'"]'),p={wait:f((h=s).domainLookupStart-(h.navigationStart||h.fetchStart||h.startTime)),dns:f(h.domainLookupEnd-h.domainLookupStart),connect:f(h.connectEnd-h.connectStart),req:f(h.responseStart-h.requestStart),res:f(h.responseEnd-h.responseStart)};l.slowResCB(u(u({},p),{dur:s.duration,msg:s.name,xpath:d(c).xpath,host:i,type:a}))}catch(t){console.error(t)}}var h})},F.prototype.load=function(){var t=this;"load"===this.trigger&&this.check()&&setTimeout(function(){t.report()},500)},F.prototype.leave=function(){"leave"===this.trigger&&this.report()},F.prototype.push=function(t,e,i,o){N(o.pathname,this.resOption.ignorePaths||[])||(e.push(i),this.pushWithHost(t,this.hostList,i,o)),N(o.pathname,this.slowOption.ignorePaths||[])||i.duration>this.slowOption.threshold&&this.pushWithHost(t,this.slowList,i,o)},F.prototype.pushWithHost=function(t,e,i,o){var n=o.host;e[n]||(e[n]=[]),e[n].push({timing:i,type:t})},F.prototype.collectHttpResInHttps=function(t,e,i){"https:"!==location.protocol||0!==e.name.indexOf("http://")||N(i.pathname,this.httpResOption.ignorePaths||[])||this.pushWithHost(t,this.httpResList,e,i)},F.prototype.addScript=function(t,e){"js"===e.ext&&0!==t.decodedBodySize&&this.push("js",this.jsList,t,e)},F.prototype.addResFromCss=function(t,e){e.ext&&P.hasOwnProperty(e.ext)?this.push("font",this.fontList,t,e):this.addImg(t,e)},F.prototype.addLink=function(t,e){"css"===e.ext?this.push("css",this.cssList,t,e):"js"===e.ext&&this.push("js",this.jsList,t,e)},F.prototype.addImg=function(t,e){this.push("img",this.imgList,t,e),t.decodedBodySize>(this.bigImgOption.maxSize||0)&&!N(e.pathname,this.bigImgOption.ignorePaths||[])&&this.pushWithHost("img",this.bigImgList,t,e)},F.prototype.handleTimings=function(t){for(var e=t.length,i=0;i<e;i++){var o=t[i],n=r(o.name);if(!N(n.pathname,M))switch(t[i].initiatorType){case"script":this.addScript(o,n);break;case"css":this.addResFromCss(o,n);break;case"img":this.addImg(o,n);break;case"link":this.addLink(o,n);break;case"audio":this.collectHttpResInHttps("audio",o,n);break;case"video":this.collectHttpResInHttps("video",o,n)}}},F.prototype.getNumAndSize=function(t,e){var i={},o=t+"Num",n=t+"Size",r=t+"TransferSize",s=t+"CacheRate",a=t+"Duration";i[o]=0,i[n]=0;var c=i[r]=0;if(e.forEach(function(t){i[o]++,i[n]+=t.decodedBodySize/1024,i[r]+=t.transferSize/1024,c+=t.duration}),i[a]=_(0<i[o]?c/i[o]:0),i[n]){var p=i[n]-i[r];i[s]=_(0<=p?100*p/i[n]:0)}return i[n]=_(i[n]),i[r]=_(i[r]),i},F.prototype.getMetric=function(){var t=performance.getEntriesByType("navigation")[0],e=performance.getEntriesByType("resource");if(t&&e&&e.length){this.handleTimings(e);var i=u(u(u(u({},this.getNumAndSize(z.JS,this.jsList)),this.getNumAndSize(z.CSS,this.cssList)),this.getNumAndSize(z.IMG,this.imgList)),this.getNumAndSize(z.FONT,this.fontList)),o=t;i.docSize=_(o.decodedBodySize/1024),i.docTransferSize=_(o.transferSize/1024),i.headerSize=_((o.transferSize-o.encodedBodySize||0)/1024),i.allSize=_(i.docSize+i.jsSize+i.cssSize+i.imgSize+i.fontSize),i.allTransferSize=_(i.docTransferSize+i.jsTransferSize+i.cssTransferSize+i.imgTransferSize+i.fontTransferSize);for(var n={},r=0,s=Object.keys(this.hostList);r<s.length;r++){var a=s[r],c=this.hostList[a].map(function(t){return t.timing});n[a]=this.getNumAndSize("host",c)}return{metric:i,hostMetric:n}}},F);function F(){this.trigger="load",this.jsList=[],this.cssList=[],this.imgList=[],this.fontList=[],this.hostList={},this.bigImgList={},this.httpResList={},this.slowList={},this.resOption={ignorePaths:[],trigger:"load"},this.bigImgOption={ignorePaths:[],maxSize:153600,trigger:"load"},this.httpResOption={ignorePaths:[],trigger:"load"},this.slowOption={ignorePaths:[],trigger:"load",threshold:1e3}}var A=null;window.performance&&window.performance.memory&&(A=window.performance.memory);var D=(J.prototype.listenMemory=function(t){this.cb=t},J.prototype.leave=function(){if(!this.onceLeave&&A){this.onceLeave=!0;var t=window.performance.memory;if(t.usedJSHeapSize===A.usedJSHeapSize&&t.totalJSHeapSize===A.totalJSHeapSize)return;var e=window.performance.memory;this.cb&&this.cb({usedJSHeapSize:e.usedJSHeapSize/1024,totalJSHeapSize:e.totalJSHeapSize/1024,jsHeapSizeLimit:e.jsHeapSizeLimit/1024,usedJSHeapRate:100*e.usedJSHeapSize/e.totalJSHeapSize})}},J);function J(){this.onceLeave=!1}var V=(Z.prototype.getNavigatorInfo=function(){var t={},e=navigator.connection;return"object"==typeof e&&(t={downlink:e.downlink,effectiveType:e.effectiveType,rtt:e.rtt,saveData:!!e.saveData}),navigator.deviceMemory&&(t.deviceMemory=navigator.deviceMemory),navigator.hardwareConcurrency&&(t.hardwareConcurrency=navigator.deviceMemory),t},Z.prototype.load=function(){},Z);function Z(){}var W,U,G,$=window.__spyHead,K="longtask",Q=(X.prototype.check=function(){return window.PerformanceObserver&&performance&&performance.timing&&performance.timing.navigationStart},X.prototype.listenFSPLongTask=function(t){this.check()&&(this.fspCB=t)},X.prototype.listenLCPLongTask=function(t){this.check()&&(this.lcpCB=t)},X.prototype.listenLoadLongTask=function(t){this.check()&&(this.loadCB=t)},X.prototype.listenPageLongTask=function(t){this.check()&&(this.pageCB=t)},X.prototype.load=function(){var i=this;if(this.check()){var o=this.getStatData(Date.now());setTimeout(function(){if(i.loadCB&&i.loadCB({loadLongtaskTime:o.time,loadTBT:o.tbt,loadTotalTime:o.totalTime,loadLongtaskRate:o.rate,loadLongtaskNum:o.num}),performance.timing&&performance.timing.domFirstScreenPaint){var t=i.getStatData(performance.timing.domFirstScreenPaint);i.fspCB&&i.fspCB({fspLongtaskTime:t.time,fspTBT:t.tbt,fspTotalTime:t.totalTime,fspLongtaskRate:t.rate,fspLongtaskNum:t.num})}if(L("lcp")){var e=i.getStatData(performance.timing.navigationStart+L("lcp"));i.lcpCB&&i.lcpCB({lcpLongtaskTime:e.time,lcpTBT:e.tbt,lcpTotalTime:e.totalTime,lcpLongtaskRate:e.rate,lcpLongtaskNum:e.num})}},200)}},X.prototype.leave=function(){if(!this.onceLeave){this.onceLeave=!0;var t=this.getStatData(Date.now());this.pageCB&&this.pageCB({pageLongtaskTime:t.time,pageTBT:t.tbt,pageTotalTime:t.totalTime,pageLongtaskRate:t.rate,pageLongtaskNum:t.num,pageIframeLongtaskTime:t.iframeTime,pageIframeLongtaskRate:t.iframeRate,pageIframeLongtaskNum:t.iframeNum})}},X.prototype.destroy=function(){this.lts=[],this.observer&&this.observer.disconnect(),this.observer=null},X.prototype.getStatData=function(t){for(var e=performance.timing.navigationStart,i=0,o=0,n=0,r=0,s=0,a={},c=0;c<this.lts.length;c++){var p=this.lts[c],h=p.duration;if(e+p.startTime+h<t&&(i+=h,o+=0<h-50?h-50:0,n++,p.attribution&&p.attribution[0])){var l=p.attribution[0].containerSrc;l&&l!==location.href&&(a[l]||(a[l]=[]),a[l].push(h),r+=h,s++)}}var u=t-e;return{num:n,time:i,tbt:o,totalTime:u,rate:100*i/u,iframeTime:r,iframeNum:s,iframeRate:100*r/u,iframeLongtasks:a}},X);function X(){var e=this;this.lts=[],this.observer=null,this.onceLeave=!1,$&&$.entryMap&&$.entryMap[K]&&(this.lts=this.lts.concat($.entryMap[K]));try{this.observer=new PerformanceObserver(function(t){e.lts=e.lts.concat(t.getEntries())}),this.observer.observe({entryTypes:[K]})}catch(t){}}function Y(){this.constructor=W}function tt(t){var e=G.call(this,t)||this;return e.modules=[],e.register(new g),e.register(new C),e.register(new b),e.register(new R),e.register(new x),e.register(new q),e.register(new D),e.register(new V),e.register(new Q),e.visibilitychangeCB=e.visibilitychangeCB.bind(e),e.load=e.load.bind(e),e.leave=e.leave.bind(e),"complete"===document.readyState?e.load():window.addEventListener("load",e.load),document.addEventListener("visibilitychange",e.visibilitychangeCB),window.addEventListener("beforeunload",e.leave,!1),window.addEventListener("unload",e.leave,!1),e.handleHead(),e}return i(W=tt,U=G=e),void(W.prototype=null===U?Object.create(U):(Y.prototype=U.prototype,new Y)),tt.prototype.handleHead=function(){var e=this;if(this.option.localCache){var t=window.__spyHead;if(t&&t.winerrors){for(var i=0;i<t.winerrors.length;i++){var o=t.winerrors[i];this.option.localCache.addLog(o)}t.interceptor=function(t){e.option.localCache.addLog(t)}}}},tt.prototype.listenFID=function(t){this.invoke("listenFID",t)},tt.prototype.listenLayoutShift=function(t){this.invoke("listenLayoutShift",t)},tt.prototype.listenLCP=function(t){this.invoke("listenLCP",t)},tt.prototype.listenFSPLongTask=function(t){this.invoke("listenFSPLongTask",t)},tt.prototype.listenLCPLongTask=function(t){this.invoke("listenLCPLongTask",t)},tt.prototype.listenLoadLongTask=function(t){this.invoke("listenLoadLongTask",t)},tt.prototype.listenPageLongTask=function(t){this.invoke("listenPageLongTask",t)},tt.prototype.listenMemory=function(t){this.invoke("listenMemory",t)},tt.prototype.getNavigatorInfo=function(){return this.invoke("getNavigatorInfo")},tt.prototype.listenResource=function(t,e){this.invoke("listenResource",t,e)},tt.prototype.listenBigImg=function(t,e){this.invoke("listenBigImg",t,e)},tt.prototype.listenHttpResource=function(t,e){this.invoke("listenHttpResource",t,e)},tt.prototype.listenSlowResource=function(t,e){this.invoke("listenSlowResource",t,e)},tt.prototype.listenTiming=function(t){this.invoke("listenTiming",t)},tt.prototype.listenTTI=function(t,e){this.invoke("listenTTI",t,e)},tt.prototype.invoke=function(t,e,i){for(var o=0;o<this.modules.length;o++){var n=this.modules[o];if("function"==typeof n[t])return n[t].apply(n,[e,i])}console.error("no method",t)},tt.prototype.register=function(t){this.modules.push(t)},tt.prototype.load=function(){for(var t=0;t<this.modules.length;t++){var e=this.modules[t];e.load&&e.load()}},tt.prototype.leave=function(){for(var t=0;t<this.modules.length;t++){var e=this.modules[t];e.leave&&e.leave()}},tt.prototype.destroy=function(){for(var t=0;t<this.modules.length;t++){var e=this.modules[t];e.destroy&&e.destroy()}document.removeEventListener("visibilitychange",this.visibilitychangeCB),window.removeEventListener("load",this.load),window.removeEventListener("beforeunload",this.leave),window.removeEventListener("unload",this.destroy)},tt.prototype.visibilitychangeCB=function(){"hidden"===document.visibilityState&&this.leave()},tt});
|