@haluo/util 1.0.5 → 1.0.8

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.
Files changed (54) hide show
  1. package/.babelrc +21 -0
  2. package/.eslintrc.js +216 -0
  3. package/__tests__/unit/date/date.spec.js +14 -0
  4. package/__tests__/unit/jest.conf.js +25 -0
  5. package/__tests__/unit/specs/date.test.js +11 -0
  6. package/dist/index.cjs.js +89 -0
  7. package/dist/{types/index.d.ts → index.d.ts} +12 -12
  8. package/dist/index.esm.js +87 -0
  9. package/dist/index.js +6 -7
  10. package/dist/lib-list.d.ts +2 -0
  11. package/dist/{types/modules → modules}/cookie/index.d.ts +27 -25
  12. package/dist/modules/cookie/index.js +4 -1
  13. package/dist/{types/modules → modules}/date/index.d.ts +1 -1
  14. package/dist/{types/modules/match → modules/dom}/index.d.ts +1 -1
  15. package/dist/{types/modules → modules}/filter/index.d.ts +26 -26
  16. package/dist/modules/filter/index.js +4 -6
  17. package/dist/{types/modules → modules}/format/index.d.ts +13 -13
  18. package/dist/{types/modules/dom → modules/match}/index.d.ts +1 -1
  19. package/dist/modules/monitor/index.js +14 -0
  20. package/dist/modules/monitor/lib/jsError.js +60 -0
  21. package/dist/modules/monitor/lib/timing.js +69 -0
  22. package/dist/modules/monitor/lib/xhr.js +41 -0
  23. package/dist/modules/monitor/utils/onload.js +11 -0
  24. package/dist/modules/monitor/utils/tracker.js +35 -0
  25. package/dist/{types/modules → modules}/number/index.d.ts +39 -39
  26. package/dist/{types/modules → modules}/sentry/index.d.ts +15 -15
  27. package/dist/{types/modules → modules}/tools/index.d.ts +1 -1
  28. package/global.d.ts +0 -0
  29. package/package.json +5 -6
  30. package/publish.sh +11 -0
  31. package/specification/CSS.md +25 -0
  32. package/specification/JS.md +9 -0
  33. package/specification/VUE.md +1 -0
  34. package/src/consts/httpCode.js +10 -0
  35. package/src/index.ts +54 -0
  36. package/src/modules/cookie/index.ts +69 -0
  37. package/src/modules/date/index.ts +196 -0
  38. package/src/modules/dom/index.ts +78 -0
  39. package/src/modules/filter/index.ts +57 -0
  40. package/src/modules/format/index.ts +19 -0
  41. package/src/modules/match/index.ts +31 -0
  42. package/src/modules/monitor/index.ts +8 -0
  43. package/src/modules/monitor/lib/jsError.ts +54 -0
  44. package/src/modules/monitor/lib/timing.ts +75 -0
  45. package/src/modules/monitor/lib/xhr.ts +35 -0
  46. package/src/modules/monitor/utils/onload.ts +8 -0
  47. package/src/modules/monitor/utils/tracker.ts +22 -0
  48. package/src/modules/number/index.ts +108 -0
  49. package/src/modules/sentry/index.ts +82 -0
  50. package/src/modules/tools/index.ts +427 -0
  51. package/tsconfig.json +34 -0
  52. package/dist/tsconfig.tsbuildinfo +0 -1980
  53. package/dist/types/index.js +0 -2
  54. package/dist/types/types/index.d.ts +0 -3
@@ -0,0 +1,196 @@
1
+ /**
2
+ * @file date 格式化
3
+ * @Author: wanghui
4
+ * @createBy: @2020.05.21
5
+ */
6
+ 'use strict'
7
+
8
+ import { IObjectKey } from '../../types/index'
9
+
10
+ /**
11
+ * 格式化时间 详情内容里的时间格式
12
+ * @param {Object} data 格式,可参考format 中的o属性
13
+ * @param {String} fmt 想要格式化的格式 'YYYY-MM-DD HH:mm:ss'、'YYYY-MM-DD'、'YYYY年MM月DD日 HH时mm分ss秒'、'YYYY年MM月DD日'
14
+ * @return 返回fmt 格式 时间
15
+ */
16
+ function replacementDate(data: IObjectKey<any>, fmt: string) {
17
+ for (var k in data) {
18
+ if (new RegExp('(' + k + ')').test(fmt)) {
19
+ fmt = fmt.replace(RegExp.$1,
20
+ (RegExp.$1.length === 1) ? (data[k]) : ((`00${data[k]}`).substr(('' + data[k]).length))
21
+ )
22
+ }
23
+ }
24
+ return fmt
25
+ }
26
+
27
+ /**
28
+ * 格式化年份
29
+ * @param {String} date Date 格式
30
+ * @param {String} fmt 想要格式化的格式 'YYYY-MM-DD HH:mm:ss'、'YYYY-MM-DD'、'YYYY年MM月DD日 HH时mm分ss秒'、'YYYY年MM月DD日'
31
+ * @return 仅返回年份
32
+ */
33
+ function replacementYear(date: Date, fmt: string) {
34
+ if (/(Y+)/.test(fmt)) {
35
+ fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length));
36
+ }
37
+ return fmt
38
+ }
39
+
40
+ class DateClass {
41
+ /**
42
+ * 格式化时间
43
+ * @param {String|Number} date 需要格式化的时间 2017-11-11、2017/11/11、linux time
44
+ * @param {String} fmt 想要格式化的格式 'YYYY-MM-DD HH:mm:ss'、'YYYY-MM-DD'、'YYYY年MM月DD日 HH时mm分ss秒'、'YYYY年MM月DD日'
45
+ * date.format(new Date()) // 默认格式 'YYYY-MM-DD HH:mm:ss'
46
+ * date.format(1586840260500) // 默认格式,传参为linux时间
47
+ * date.format(new Date(), 'YYYY:MM:DD') // 自定义格式 'YYYY:MM:DD'
48
+ * @return {String} fmt 'YYYY-MM-DD HH:mm:ss'
49
+ */
50
+ format(date: string, fmt: string = 'YYYY-MM-DD HH:mm:ss') {
51
+ if (!date) return ''
52
+ let timeData: any = typeof date === 'string' ? new Date(date.replace(/-/g, '/')) : date
53
+ timeData = typeof date === 'number' ? new Date(date) : timeData
54
+ var o: IObjectKey<number> = {
55
+ 'M+': timeData.getMonth() + 1,
56
+ 'D+': timeData.getDate(),
57
+ 'h+': timeData.getHours() % 12 === 0 ? 12 : timeData.getHours() % 12,
58
+ 'H+': timeData.getHours(),
59
+ 'm+': timeData.getMinutes(),
60
+ 's+': timeData.getSeconds(),
61
+ 'q+': Math.floor((timeData.getMonth() + 3) / 3),
62
+ 'S': timeData.getMilliseconds()
63
+ }
64
+
65
+ const week: IObjectKey<string> = {
66
+ '0': '\u65e5',
67
+ '1': '\u4e00',
68
+ '2': '\u4e8c',
69
+ '3': '\u4e09',
70
+ '4': '\u56db',
71
+ '5': '\u4e94',
72
+ '6': '\u516d'
73
+ }
74
+
75
+ fmt = replacementYear(timeData, fmt)
76
+ if (/(E+)/.test(fmt)) {
77
+ fmt = fmt.replace(RegExp.$1, ((RegExp.$1.length > 1) ? (RegExp.$1.length > 2 ? '\u661f\u671f' : '\u5468') : '') + week[`${timeData.getDay()} `])
78
+ }
79
+ return replacementDate(o, fmt)
80
+ }
81
+
82
+ /**
83
+ * 天数加减
84
+ * @param {string | Date} date 传入的时间 2020-10-15 or Date
85
+ * @param {String} days 天数
86
+ * addDaysToDate('2020-10-15', 10) // '2020-10-25'
87
+ * addDaysToDate('2020-10-15', -10) // '2020-10-05'
88
+ * @return {String} fmt 'YYYY-MM-DD'
89
+ */
90
+ addDaysToDate(date: string | Date, days: number) {
91
+ const d = typeof date === 'object' ? date : new Date(date)
92
+ d.setDate(d.getDate() + days)
93
+ return d.toISOString().split('T')[0]
94
+ }
95
+
96
+ /**
97
+ * 获取倒计时剩余时间
98
+ * @param {Date | Number} endTime 截止时间
99
+ * @param {Date | Number} startTime 开始时间,默认取客户端当前时间
100
+ * date.format(new Date()) // 返回 {dd: '天', hh: '时', mm: '分', ss: '秒'}
101
+ * date.format(1586840260500) // 返回 {dd: '天', hh: '时', mm: '分', ss: '秒'}
102
+ * @return {object | boolean} {dd: '天', hh: '时', mm: '分', ss: '秒'}
103
+ */
104
+ remainTime(endTime: Date | Number, startTime: Date | Number = new Date()) : object | boolean {
105
+ const ts: number = Number(endTime) - Number(startTime) // 计算剩余的毫秒数
106
+ let dd = Math.floor(ts / 1000 / 60 / 60 / 24) // 计算剩余的天数
107
+ let hh = Math.floor(ts / 1000 / 60 / 60 % 24) // 计算剩余的小时数
108
+ let mm = Math.floor(ts / 1000 / 60 % 60) // 计算剩余的分钟数
109
+ let ss = Math.floor(ts / 1000 % 60) // 计算剩余的秒数
110
+ if (ts <= 0) return false
111
+ return {
112
+ dd: (dd < 10 ? `0${dd}` : dd),
113
+ hh: (hh < 10 ? `0${hh}` : hh),
114
+ mm: (mm < 10 ? `0${mm}` : mm),
115
+ ss: (ss < 10 ? `0${ss}` : ss)
116
+ }
117
+ }
118
+
119
+ /**
120
+ * 格式化现在的已过时间
121
+ * @param {Number} startTime
122
+ * @return {String} *年前 *个月前 *天前 *小时前 *分钟前 刚刚
123
+ */
124
+ formatPassTime(startTime: any) {
125
+ const currentTime: any = new Date();
126
+ const time: any = currentTime - startTime;
127
+ const year = Math.floor(time / (1000 * 60 * 60 * 24) / 30 / 12);
128
+ if (year) return `${year}年前`;
129
+ const month = Math.floor(time / (1000 * 60 * 60 * 24) / 30);
130
+ if (month) return `${month}个月前`;
131
+ const day = Math.floor(time / (1000 * 60 * 60 * 24));
132
+ if (day) return `${day}天前`;
133
+ const hour = Math.floor(time / (1000 * 60 * 60));
134
+ if (hour) return `${hour}小时前`;
135
+ const min = Math.floor(time / (1000 * 60))
136
+ if (min) return `${min}分钟前`;
137
+ else return '刚刚';
138
+ }
139
+
140
+ /**
141
+ * 格式化时间 列表里的时间内容格式 待废弃,统一时间格式
142
+ * @param {Number} time 1494141000*1000
143
+ * @return {String} *年*月*日 *月*日 刚刚(1-60秒) 1-60分钟前 1-24小时前 1-3天前
144
+ */
145
+ formatPassTimeForList(time: string) {
146
+ return DateClass.prototype.formatPassTimeForDetail(time, 'YYYY年MM月DD日', true)
147
+ }
148
+
149
+ /**
150
+ * 格式化时间 详情内容里的时间格式
151
+ * @param {Number} time 1494141000*1000
152
+ * @param {String} fmt 想要格式化的格式
153
+ * @param {Boolean} noYear 是否显示年
154
+ * @return {String} *年*月*日 *月*日 刚刚(1-60秒) 1-60分钟前 1-24小时前 1-3天前
155
+ */
156
+ formatPassTimeForDetail(time: string, fmt: string = 'YYYY-MM-DD', noYear: Boolean) {
157
+ const date = (typeof time === 'number') ? new Date(time) : new Date((time || '').replace(/-/g, '/'));
158
+ const diff = (((new Date()).getTime() - date.getTime()) / 1000);
159
+ const dayDiff = Math.floor(diff / 86400);
160
+ const isValidDate = Object.prototype.toString.call(date) === '[object Date]' && !isNaN(date.getTime());
161
+
162
+ if (!isValidDate) return '';
163
+
164
+ const formatDate = function () {
165
+ const today = new Date(date);
166
+ var o: IObjectKey<string | number> = {
167
+ 'Y+': today.getFullYear(),
168
+ 'M+': ('0' + (today.getMonth() + 1)).slice(-2),
169
+ 'D+': ('0' + today.getDate()).slice(-2)
170
+ };
171
+ fmt = replacementYear(date, fmt)
172
+ const year = today.getFullYear();
173
+
174
+ if (!(new Date().getFullYear() > year) && noYear) {
175
+ const backData = replacementDate(o, fmt);
176
+ return backData.split('年')[1];
177
+ }
178
+ return replacementDate(o, fmt);
179
+ };
180
+
181
+ if (dayDiff === -1) {
182
+ return '刚刚';
183
+ } else if (isNaN(dayDiff) || dayDiff < 0 || dayDiff >= 15) {
184
+ return formatDate();
185
+ }
186
+
187
+ return (dayDiff === 0 && (
188
+ (diff < 60 && '刚刚') ||
189
+ (diff < 120 && '1分钟前') ||
190
+ (diff < 3600 && Math.floor(diff / 60) + '分钟前') ||
191
+ (diff < 7200 && '1小时前') ||
192
+ (diff < 86400 && Math.floor(diff / 3600) + '小时前'))) || (dayDiff < 16 && dayDiff + '天前');
193
+ }
194
+ }
195
+
196
+ module.exports = new DateClass()
@@ -0,0 +1,78 @@
1
+ /**
2
+ * @file Cookie
3
+ * @Author: wanghui
4
+ * @createBy: @2021.08.17
5
+ */
6
+ 'use strict'
7
+
8
+ import { IObjectKey } from '../../types/index'
9
+
10
+ interface IDom {
11
+ name: string,
12
+ innerHTML?: string,
13
+ style?: IObjectKey<string>,
14
+ parent?: string,
15
+ }
16
+
17
+ class DomClass {
18
+ /**
19
+ * 创建一个子元素并添加至父节点
20
+ * @param {Object} { name = 'div', innerHTML = '', style = {}, parent, }
21
+ * @return {String}
22
+ */
23
+ createElement({
24
+ name = 'div',
25
+ innerHTML = '',
26
+ style = {},
27
+ parent,
28
+ }: IDom) {
29
+ if (!(window && window.document)) {
30
+ return new Error('仅支持浏览器')
31
+ }
32
+
33
+ const element: any = document.createElement(name)
34
+
35
+ element.innerHTML = innerHTML
36
+ Object.keys(style).map((_: string) => element.style[_] = style[_])
37
+ if (parent) {
38
+ const body = document.querySelector(parent)
39
+ body && body.append(element)
40
+ }
41
+ return element
42
+ }
43
+
44
+ /**
45
+ * 获取文本中的url并用a标签包裹
46
+ * @param {Object} ICookie
47
+ */
48
+ wrapperA(text: string) {
49
+ if (!(window && window.document)) {
50
+ return new Error('仅支持浏览器')
51
+ }
52
+
53
+ return text.replace(/((https|http|ftp|file):\/\/[-A-Za-z0-9+&@#/%?=~_|!:,.;]+[-A-Za-z0-9+&@#/%=~_|])/g, '<a href="$1">$1</a>')
54
+ }
55
+
56
+ /**
57
+ * 对象转化为formdata
58
+ * getFormData({a: 1, b: 2})
59
+ * @param {Object} object
60
+ */
61
+ getFormData(object: IObjectKey<any>) {
62
+ const formData = new FormData()
63
+ Object.keys(object).forEach(key => {
64
+ const value = object[key]
65
+ if (Array.isArray(value)) {
66
+ value.forEach((subValue, i) =>
67
+ formData.append(key + `[${i}]`, subValue)
68
+ )
69
+ } else {
70
+ formData.append(key, object[key])
71
+ }
72
+ })
73
+ return formData
74
+ }
75
+ }
76
+
77
+ module.exports = new DomClass()
78
+
@@ -0,0 +1,57 @@
1
+ /**
2
+ * @file 过滤器
3
+ * @Author: wanghui
4
+ * @createBy: @2020.05.28
5
+ */
6
+ /**
7
+ * 示例:注入所有过滤器
8
+ * import { filter as filters } from '@haluo/util'
9
+ * Object.keys(filters).forEach(key => {
10
+ * Vue.filter(key, filters[key])
11
+ * })
12
+ */
13
+ 'use strict'
14
+
15
+ const dateClass = require('../date')
16
+ const numberClass = require('../number')
17
+ const toolsClass = require('../tools')
18
+
19
+ class FilterClass {
20
+ /**
21
+ * 格式化时间,示例:1586840260500 | format('YYYY-MM-DD HH:mm:ss')
22
+ * @param {String|Number} date
23
+ * @param {String} fmt 'YYYY-MM-DD HH:mm:ss'
24
+ * @return {String} 'YYYY-MM-DD HH:mm:ss'
25
+ */
26
+ format(date: string | number, fmt: string = 'YYYY-MM-DD HH:mm:ss') {
27
+ return dateClass.format(date, fmt)
28
+ }
29
+
30
+ /**
31
+ * 格式化金额,示例:123456 | formatMoney
32
+ * @param {Number} num
33
+ * @return {String} 123,456
34
+ */
35
+ formatMoney(money: number | string): string {
36
+ return numberClass.formatMoney(money)
37
+ }
38
+
39
+ /**
40
+ * 截取数组或字符串,示例:'1234' | slice(3)
41
+ * @param {Array|String} target 数组或字符串
42
+ * @param {Number} length 截取长度,从0开始
43
+ * @return {any}
44
+ */
45
+ slice(target: Array<any> | string = '', length: number = 0) {
46
+ return toolsClass.slice(target, length)
47
+ }
48
+
49
+ install(Vue: any) {
50
+ const _this = this
51
+ Vue.filter('format', _this.format)
52
+ Vue.filter('formatMoney', _this.formatMoney)
53
+ Vue.filter('slice', _this.slice)
54
+ }
55
+ }
56
+
57
+ module.exports = new FilterClass()
@@ -0,0 +1,19 @@
1
+ interface Obj {
2
+ [prop: string]: any
3
+ }
4
+ class Format {
5
+ /**
6
+ * @desc 对于对象非数字与布尔值的value,当其为falsy时,转换成separator
7
+ * @param {object} obj 传入的对象
8
+ * @param {string} separator 替换后的值
9
+ * transformObjectNullVal({ a: null, b: 0}, '23') // {a: "23", b: 0}
10
+ * @return {object}
11
+ */
12
+ transformObjectNullVal<T extends Obj>(obj: T, separator: string = '-'): T {
13
+ return Object.keys(obj).reduce((cur, key) => {
14
+ (cur as Obj)[key] = obj[key] || ((obj[key] === 0 || obj[key] === false) ? obj[key] : separator)
15
+ return cur
16
+ }, {} as { [key in keyof T]: T[key] })
17
+ }
18
+ }
19
+ module.exports = new Format()
@@ -0,0 +1,31 @@
1
+
2
+ /**
3
+ * @file: tools 常用的工具函数
4
+ * @Author: wanghui
5
+ */
6
+ 'use strict'
7
+ import { IObjectKey } from '../../types/index'
8
+
9
+ class MatchClass {
10
+ /**
11
+ * 根据类型返回正则
12
+ * @param {String} str 检测的内容
13
+ * @param {String} type 检测类型
14
+ * checkType('10.120.33.11', 'ip') // true
15
+ * @return {Boolean} true or false
16
+ */
17
+ checkType(str: string, type: string): boolean {
18
+ const regexp: IObjectKey<boolean> = {
19
+ 'ip': /((2(5[0-5]|[0-4]\d))|[0-1]?\d{1,2})(\.((2(5[0-5]|[0-4]\d))|[0-1]?\d{1,2})){3}/.test(str),
20
+ 'port': /^(\d|[1-5]\d{4}|6[1-4]\d{3}|65[1-4]\d{2}|655[1-2]\d|6553[1-5])$/.test(str),
21
+ 'phone': /^1[3|4|5|6|7|8][0-9]{9}$/.test(str), // 手机号
22
+ 'number': /^[0-9]+$/.test(str), // 是否全数字
23
+ 'email': /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/.test(str),
24
+ 'IDCard': /^(^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$)|(^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])((\d{4})|\d{3}[Xx])$)$/.test(str),
25
+ 'url': /[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)/i.test(str)
26
+ };
27
+ return regexp[type];
28
+ }
29
+ }
30
+
31
+ module.exports = new MatchClass()
@@ -0,0 +1,8 @@
1
+ import { injectJsError } from './lib/jsError'
2
+ import injectXhrError from './lib/xhr'
3
+ // import timing from './lib/timing'
4
+ export default function setup() {
5
+ injectJsError()
6
+ injectXhrError()
7
+ }
8
+ // timing()
@@ -0,0 +1,54 @@
1
+ import track from '../utils/tracker'
2
+ export function injectJsError () {
3
+ window.addEventListener('error', (e: any) => {
4
+ console.log(e);
5
+ if (e.target && (e.target.src || e.target.href)) {
6
+ track.send({
7
+ kind: 'stability',
8
+ type: 'error',
9
+ errorType: 'resourceError',
10
+ filename: e.target.src || e.target.href,
11
+ tagName: e.target.tagName
12
+ // selector: lastEvent ? get
13
+ })
14
+ return
15
+ }
16
+ track.send({
17
+ kind: 'stability',
18
+ type: 'error',
19
+ errorType: 'jsError',
20
+ message: e.message,
21
+ filename: e.filename,
22
+ position: `${e.lineno}:${e.colno}`,
23
+ stack: e.error.stack,
24
+ // selector: lastEvent ? get
25
+ })
26
+ }, true)
27
+
28
+ window.addEventListener('unhandledrejection', (e) => {
29
+ let message, filename, line, column, stack
30
+ const reason = e.reason
31
+ if (typeof reason === 'string') {
32
+ message = reason
33
+ } else if (typeof reason === 'object') {
34
+ if (reason.stack) {
35
+ const matchResult = reason.stack.match(/at\s+(.+):(\d+):(\d+)/)
36
+ filename = matchResult[1]
37
+ line = matchResult[2]
38
+ column = matchResult[3]
39
+ }
40
+ message = reason.message
41
+ stack = reason.stack
42
+ }
43
+ track.send({
44
+ kind: 'stability',
45
+ type: 'error',
46
+ errorType: 'promiseError',
47
+ message,
48
+ filename,
49
+ position: `${line}:${column}`,
50
+ stack
51
+ // selector: lastEvent ? get
52
+ })
53
+ })
54
+ }
@@ -0,0 +1,75 @@
1
+ import onload from '../utils/onload'
2
+ import tracker from '../utils/tracker';
3
+ export default function timing() {
4
+ let FMP:any, LCP:any
5
+ new PerformanceObserver((entryList, observer) => {
6
+ const perfEntries = entryList.getEntries()
7
+ FMP = perfEntries[0]
8
+ observer.disconnect() // 不再观察
9
+ }).observe({ entryTypes: ['element'] }) // 观察页面中有意义的元素
10
+ new PerformanceObserver((entryList, observer) => {
11
+ const perfEntries = entryList.getEntries()
12
+ LCP = perfEntries[0]
13
+ observer.disconnect() // 不再观察
14
+ }).observe({ entryTypes: ['largest-contentful-paint'] }) // 观察页面中最大的元素
15
+ new PerformanceObserver((entryList, observer) => {
16
+ const firstInput:any = entryList.getEntries()
17
+ console.log('FID', firstInput);
18
+ if(firstInput) {
19
+ let inputDelay = firstInput.processingStart - firstInput.startTime
20
+ const duration = firstInput.duration
21
+ if(inputDelay > 0 || duration >0) {
22
+ tracker.send({
23
+ kind: 'experience',
24
+ type: 'firstInputDelay',
25
+ inputDelay, // 延迟时间
26
+ duration, // 处理时间
27
+ startTime: firstInput.startTime
28
+ })
29
+ }
30
+ }
31
+ observer.disconnect() // 不再观察
32
+ }).observe({ type: 'first-input', buffered: true }) // 观察页面中最大的元素
33
+ onload(function() {
34
+ setTimeout(() => {
35
+ console.dir(window.performance);
36
+ const {
37
+ fetchStart,
38
+ connectStart,
39
+ connectEnd,
40
+ requestStart,
41
+ responseStart,
42
+ responseEnd,
43
+ domInteractive,
44
+ domContentLoadedEventStart,
45
+ domContentLoadedEventEnd,
46
+ loadEventStart,
47
+ // loadEventEnd
48
+ } = performance.getEntriesByType('navigation')[0] as any
49
+ tracker.send({
50
+ kind: 'experience',
51
+ type: 'timing',
52
+ connectTime: connectEnd - connectStart, // 连接时间
53
+ ttfbTime: responseStart - requestStart, // 首字节到达时间
54
+ responseTime: responseEnd - responseStart, // 响应的读取时间
55
+ domContentLoadedTime: domContentLoadedEventEnd - domContentLoadedEventStart,
56
+ timeToInteractive: domInteractive - fetchStart,// 首次可交互时间
57
+ loadTime: loadEventStart - fetchStart // 完整的页面加载时间
58
+ })
59
+ let FP = performance.getEntriesByName('first-paint')[0]
60
+ let FCP = performance.getEntriesByName('first-contentful-paint')[0]
61
+ console.log('FP', FP);
62
+ console.log('FCP', FCP);
63
+ console.log('FMP', FMP);
64
+ console.log('LCP', LCP);
65
+ tracker.send({
66
+ kind: 'experience',
67
+ type: 'paint',
68
+ FP: FP.startTime,
69
+ FCP: FCP.startTime,
70
+ FMP: FMP.startTime,
71
+ LCP: LCP.startTime,
72
+ })
73
+ }, 3000);
74
+ })
75
+ }
@@ -0,0 +1,35 @@
1
+ import tracker from "../utils/tracker"
2
+
3
+ export default function injectXHR(){
4
+ let XMLHttpRequest:any = window.XMLHttpRequest
5
+ let oldOpen = XMLHttpRequest.prototype.open as any
6
+ XMLHttpRequest.prototype.open = function(method: string, url:string, async:boolean) {
7
+ this.logData = { method, url, async }
8
+ return oldOpen.apply(this, arguments)
9
+ }
10
+ let oldSend = XMLHttpRequest.prototype.send
11
+ XMLHttpRequest.prototype.send = function(body:any) {
12
+ if(this.logData) {
13
+ const startTime = Date.now()
14
+ const handler = (type:string) => (event:any) => {
15
+ let duration = Date.now() - startTime
16
+ let status = this.status
17
+ let statusText = this.statusText
18
+ tracker.send({
19
+ kind: 'stability',
20
+ type: 'xhr',
21
+ eventType: event.type,
22
+ pathname: this.logData.url,
23
+ status: status + '-'+statusText,
24
+ duration,
25
+ response: this.response ? JSON.stringify(this.response) : '',
26
+ params: body || ''
27
+ })
28
+ }
29
+ this.addEventListener('load', handler('load'), false)
30
+ this.addEventListener('error', handler('error'), false)
31
+ this.addEventListener('abort', handler('abort'), false)
32
+ }
33
+ return oldSend.apply(this, arguments)
34
+ }
35
+ }
@@ -0,0 +1,8 @@
1
+ export default function onload(cb:any) {
2
+ if(document.readyState) {
3
+ cb()
4
+ } else {
5
+
6
+ window.addEventListener('load', cb)
7
+ }
8
+ }
@@ -0,0 +1,22 @@
1
+ const Parser = require('ua-parser-js');
2
+ class SendTracker {
3
+ url: string
4
+ xhr: XMLHttpRequest
5
+ constructor() {
6
+ this.url = ''
7
+ this.xhr = new XMLHttpRequest()
8
+ }
9
+ send(data = {}) {
10
+ const log = { ...getExtraData(), ...data }
11
+ console.log(log);
12
+ }
13
+ }
14
+ function getExtraData() {
15
+ return {
16
+ title: document.title,
17
+ url: location.href,
18
+ timestamp: Date.now(),
19
+ userAgent: new Parser().getResult()
20
+ }
21
+ }
22
+ export default new SendTracker()