mazey 3.3.10 → 3.3.12
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 +349 -120
- package/examples/index.ts +3 -1
- package/lib/index.cjs.js +2 -2
- package/lib/index.d.ts +358 -47
- package/lib/index.d.ts.map +1 -1
- package/lib/index.esm.js +2 -2
- package/lib/mazey.min.js +2 -2
- package/package.json +2 -1
- package/scripts/build-helper.js +42 -0
- package/scripts/dev.js +4 -3
- package/scripts/github-helper.js +16 -12
- package/src/index.ts +402 -53
package/src/index.ts
CHANGED
|
@@ -1,8 +1,22 @@
|
|
|
1
1
|
/* mazey */
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
|
-
*
|
|
5
|
-
*
|
|
4
|
+
* EN: Computes the longest common substring of two strings.
|
|
5
|
+
*
|
|
6
|
+
* ZH: 计算两个字符串的最长公共子串
|
|
7
|
+
*
|
|
8
|
+
* Usage:
|
|
9
|
+
*
|
|
10
|
+
* ```
|
|
11
|
+
* calLongestCommonSubstring('fish', 'finish');
|
|
12
|
+
* ```
|
|
13
|
+
*
|
|
14
|
+
* Output:
|
|
15
|
+
*
|
|
16
|
+
* ```
|
|
17
|
+
* 3
|
|
18
|
+
* ```
|
|
19
|
+
*
|
|
6
20
|
* @param {string} aStr String
|
|
7
21
|
* @param {string} bStr String
|
|
8
22
|
* @returns {number} Length
|
|
@@ -31,8 +45,22 @@ export function calLongestCommonSubstring(aStr: string, bStr: string): number {
|
|
|
31
45
|
}
|
|
32
46
|
|
|
33
47
|
/**
|
|
34
|
-
*
|
|
35
|
-
*
|
|
48
|
+
* EN: Computes the longest common subsequence of two strings.
|
|
49
|
+
*
|
|
50
|
+
* ZH: 计算两个字符串的最长公共子序列
|
|
51
|
+
*
|
|
52
|
+
* Usage:
|
|
53
|
+
*
|
|
54
|
+
* ```
|
|
55
|
+
* calLongestCommonSubsequence('fish', 'finish');
|
|
56
|
+
* ```
|
|
57
|
+
*
|
|
58
|
+
* Output:
|
|
59
|
+
*
|
|
60
|
+
* ```
|
|
61
|
+
* 4
|
|
62
|
+
* ```
|
|
63
|
+
*
|
|
36
64
|
* @param {string} aStr 字符串
|
|
37
65
|
* @param {string} bStr 字符串
|
|
38
66
|
* @returns {number} 长度
|
|
@@ -73,8 +101,15 @@ export function calLongestCommonSubsequence(
|
|
|
73
101
|
}
|
|
74
102
|
|
|
75
103
|
/**
|
|
76
|
-
*
|
|
77
|
-
*
|
|
104
|
+
* Get the query param's value of the current Web URL(`location.search`).
|
|
105
|
+
*
|
|
106
|
+
* ```
|
|
107
|
+
* // http://example.com/?t1=1&t2=2&t3=3&t4=4#2333
|
|
108
|
+
* // ?t1=1&t2=2&t3=3&t4=4
|
|
109
|
+
* getQueryParam('t3'); // 3
|
|
110
|
+
* getQueryParam('t4'); // 4
|
|
111
|
+
* ```
|
|
112
|
+
*
|
|
78
113
|
* @param {string} param Query param.
|
|
79
114
|
* @returns {string} value
|
|
80
115
|
*/
|
|
@@ -89,8 +124,13 @@ export function getQueryParam(param: string): string {
|
|
|
89
124
|
}
|
|
90
125
|
|
|
91
126
|
/**
|
|
92
|
-
*
|
|
93
|
-
*
|
|
127
|
+
* Get the query param's value of the input URL.
|
|
128
|
+
*
|
|
129
|
+
* ```
|
|
130
|
+
* getUrlParam('http://example.com/?t1=1&t2=2&t3=3&t4=4', 't3'); // 3
|
|
131
|
+
* getUrlParam('http://example.com/?t1=1&t2=2&t3=3&t4=4', 't4'); // 4
|
|
132
|
+
* ```
|
|
133
|
+
*
|
|
94
134
|
* @param {string} url URL string.
|
|
95
135
|
* @param {string} param Query param.
|
|
96
136
|
* @returns {string} value
|
|
@@ -117,8 +157,22 @@ export function getUrlParam(url: string, param: string): string | string[] {
|
|
|
117
157
|
}
|
|
118
158
|
|
|
119
159
|
/**
|
|
120
|
-
*
|
|
121
|
-
*
|
|
160
|
+
* Update the query param's value of the input URL.
|
|
161
|
+
*
|
|
162
|
+
* Usage:
|
|
163
|
+
*
|
|
164
|
+
* ```
|
|
165
|
+
* updateQueryParam('http://example.com/?t1=1&t2=2&t3=3&t4=4', 't3', 'three');
|
|
166
|
+
* updateQueryParam('http://example.com/?t1=1&t2=2&t3=3&t4=4', 't4', 'four');
|
|
167
|
+
* ```
|
|
168
|
+
*
|
|
169
|
+
* Output:
|
|
170
|
+
*
|
|
171
|
+
* ```
|
|
172
|
+
* http://example.com/?t1=1&t2=2&t3=three&t4=4
|
|
173
|
+
* http://example.com/?t1=1&t2=2&t3=3&t4=four
|
|
174
|
+
* ```
|
|
175
|
+
*
|
|
122
176
|
* @param {string} url URL string.
|
|
123
177
|
* @param {string} param Query param.
|
|
124
178
|
* @param {string} value Param's value.
|
|
@@ -139,8 +193,24 @@ export function updateQueryParam(
|
|
|
139
193
|
}
|
|
140
194
|
|
|
141
195
|
/**
|
|
142
|
-
*
|
|
143
|
-
*
|
|
196
|
+
* Get the hash query param's value of the current Web URL(`location.hash`).
|
|
197
|
+
*
|
|
198
|
+
* Usage:
|
|
199
|
+
*
|
|
200
|
+
* ```
|
|
201
|
+
* // http://example.com/?#2333?t1=1&t2=2&t3=3&t4=4
|
|
202
|
+
* // #2333?t1=1&t2=2&t3=3&t4=4
|
|
203
|
+
* getHashQueryParam('t3');
|
|
204
|
+
* getHashQueryParam('t4');
|
|
205
|
+
* ```
|
|
206
|
+
*
|
|
207
|
+
* Output:
|
|
208
|
+
*
|
|
209
|
+
* ```
|
|
210
|
+
* 3
|
|
211
|
+
* 4
|
|
212
|
+
* ```
|
|
213
|
+
*
|
|
144
214
|
* @param {string} param Query param.
|
|
145
215
|
* @returns {string} value
|
|
146
216
|
*/
|
|
@@ -155,8 +225,22 @@ export function getHashQueryParam(param: string): string {
|
|
|
155
225
|
}
|
|
156
226
|
|
|
157
227
|
/**
|
|
158
|
-
*
|
|
159
|
-
*
|
|
228
|
+
* Get the domain of URL, and other params.
|
|
229
|
+
*
|
|
230
|
+
* Usage:
|
|
231
|
+
*
|
|
232
|
+
* ```
|
|
233
|
+
* getDomain('http://example.com/?t1=1&t2=2&t3=3&t4=4');
|
|
234
|
+
* getDomain('http://example.com/test/thanks?t1=1&t2=2&t3=3&t4=4', ['hostname', 'pathname']);
|
|
235
|
+
* ```
|
|
236
|
+
*
|
|
237
|
+
* Output:
|
|
238
|
+
*
|
|
239
|
+
* ```
|
|
240
|
+
* example.com
|
|
241
|
+
* example.com/test/thanks
|
|
242
|
+
* ```
|
|
243
|
+
*
|
|
160
244
|
* @param {string} url
|
|
161
245
|
* @param {array} rules Object.keys(location), ['href', 'protocol', 'host', 'hostname', 'port', 'pathname', 'search', 'hash'], ['hostname', 'pathname'] = 'km.mazey.net/plugins/servlet/mobile'
|
|
162
246
|
*/
|
|
@@ -170,8 +254,13 @@ export function getDomain(url: string, rules = ['hostname']): string {
|
|
|
170
254
|
}
|
|
171
255
|
|
|
172
256
|
/**
|
|
173
|
-
*
|
|
174
|
-
*
|
|
257
|
+
* Transfer CamelCase to KebabCase.
|
|
258
|
+
*
|
|
259
|
+
* ```
|
|
260
|
+
* camelCaseToKebabCase('ABC'); // a-b-c
|
|
261
|
+
* camelCaseToKebabCase('aBC'); // a-b-c
|
|
262
|
+
* ```
|
|
263
|
+
*
|
|
175
264
|
* @param {string} camelCase 'aBC' or 'ABC'
|
|
176
265
|
* @returns {string} 'a-b-c'
|
|
177
266
|
*/
|
|
@@ -181,8 +270,13 @@ export function camelCaseToKebabCase(camelCase: string): string {
|
|
|
181
270
|
}
|
|
182
271
|
|
|
183
272
|
/**
|
|
184
|
-
*
|
|
185
|
-
*
|
|
273
|
+
* Transfer CamelCase to Underscore.
|
|
274
|
+
*
|
|
275
|
+
* ```
|
|
276
|
+
* camelCase2Underscore('ABC'); // a_b_c
|
|
277
|
+
* camelCase2Underscore('aBC'); // a_b_c
|
|
278
|
+
* ```
|
|
279
|
+
*
|
|
186
280
|
* @param {string} camelCase 'aBC' or 'ABC'
|
|
187
281
|
* @returns {string} 'a_b_c'
|
|
188
282
|
*/
|
|
@@ -192,8 +286,13 @@ export function camelCase2Underscore(camelCase: string): string {
|
|
|
192
286
|
}
|
|
193
287
|
|
|
194
288
|
/**
|
|
195
|
-
*
|
|
196
|
-
*
|
|
289
|
+
* Remove leading and trailing whitespace or specified characters from string.
|
|
290
|
+
*
|
|
291
|
+
* ```
|
|
292
|
+
* mTrim(' 1 2 3 '); // '1 2 3'
|
|
293
|
+
* mTrim('abc '); // 'abc'
|
|
294
|
+
* ```
|
|
295
|
+
*
|
|
197
296
|
* @param {string} str The string to trim.
|
|
198
297
|
* @returns {string} Trimmed string.
|
|
199
298
|
*/
|
|
@@ -222,8 +321,22 @@ export function newLine(str: string): string {
|
|
|
222
321
|
}
|
|
223
322
|
|
|
224
323
|
/**
|
|
225
|
-
*
|
|
226
|
-
*
|
|
324
|
+
* Clone Object deeply.
|
|
325
|
+
*
|
|
326
|
+
* Usage:
|
|
327
|
+
*
|
|
328
|
+
* ```
|
|
329
|
+
* deepCopyObject(['a', 'b', 'c']);
|
|
330
|
+
* deepCopyObject('abc');
|
|
331
|
+
* ```
|
|
332
|
+
*
|
|
333
|
+
* Output:
|
|
334
|
+
*
|
|
335
|
+
* ```
|
|
336
|
+
* ['a', 'b', 'c']
|
|
337
|
+
* 'abc'
|
|
338
|
+
* ```
|
|
339
|
+
*
|
|
227
340
|
* @param {object} obj The value to clone.
|
|
228
341
|
* @returns {object} Returns the deep cloned value.
|
|
229
342
|
*/
|
|
@@ -232,10 +345,21 @@ export function deepCopyObject(obj: any): any {
|
|
|
232
345
|
}
|
|
233
346
|
|
|
234
347
|
/**
|
|
235
|
-
*
|
|
236
|
-
*
|
|
237
|
-
*
|
|
238
|
-
*
|
|
348
|
+
* Check whether it is a valid JSON string.
|
|
349
|
+
*
|
|
350
|
+
* Usage:
|
|
351
|
+
*
|
|
352
|
+
* ```
|
|
353
|
+
* isJsonString(`['a', 'b', 'c']`);
|
|
354
|
+
* isJsonString(`["a", "b", "c"]`);
|
|
355
|
+
* ```
|
|
356
|
+
*
|
|
357
|
+
* Output:
|
|
358
|
+
*
|
|
359
|
+
* ```
|
|
360
|
+
* false
|
|
361
|
+
* true
|
|
362
|
+
* ```
|
|
239
363
|
*/
|
|
240
364
|
export function isJsonString(str: string): boolean {
|
|
241
365
|
try {
|
|
@@ -357,6 +481,12 @@ export function removeClass(obj: any, cls: string): void {
|
|
|
357
481
|
*
|
|
358
482
|
* ZH: 节流
|
|
359
483
|
*
|
|
484
|
+
* ```
|
|
485
|
+
* const foo = throttle(() => {
|
|
486
|
+
* console.log('The function will be invoked at most once per every wait 1000 milliseconds.');
|
|
487
|
+
* }, 1000, { leading: true });
|
|
488
|
+
* ```
|
|
489
|
+
*
|
|
360
490
|
* Reference: [Lodash](https://lodash.com/docs/4.17.15#throttle)
|
|
361
491
|
*/
|
|
362
492
|
export function throttle(
|
|
@@ -412,8 +542,6 @@ export function throttle(
|
|
|
412
542
|
*
|
|
413
543
|
* ZH: 去抖
|
|
414
544
|
*
|
|
415
|
-
* Usage:
|
|
416
|
-
*
|
|
417
545
|
* ```
|
|
418
546
|
* const foo = debounce(() => {
|
|
419
547
|
* console.log('The debounced function will only be invoked in 1000 milliseconds, the other invoking will disappear during the wait time.');
|
|
@@ -499,8 +627,20 @@ export function friendlyInterval(
|
|
|
499
627
|
}
|
|
500
628
|
|
|
501
629
|
/**
|
|
502
|
-
*
|
|
503
|
-
*
|
|
630
|
+
* EN: Check whether it is a right number.
|
|
631
|
+
*
|
|
632
|
+
* ZH: 判断是否有效数字
|
|
633
|
+
*
|
|
634
|
+
* ```
|
|
635
|
+
* isNumber(123); // true
|
|
636
|
+
* isNumber('123'); // false
|
|
637
|
+
* // Default: NaN, Infinity is not Number
|
|
638
|
+
* isNumber(Infinity); // false
|
|
639
|
+
* isNumber(Infinity, { isUnFiniteAsNumber: true }); // true
|
|
640
|
+
* isNumber(NaN); // false
|
|
641
|
+
* isNumber(NaN, { isNaNAsNumber: true, isUnFiniteAsNumber: true }); // true
|
|
642
|
+
* ```
|
|
643
|
+
*
|
|
504
644
|
* @param {*} num 被判断的值
|
|
505
645
|
* @param {boolean} options.isNaNAsNumber 是否 NaN 算数字(默认不算)
|
|
506
646
|
* @param {boolean} options.isUnFiniteAsNumber 是否 无限 算数字(默认不算)
|
|
@@ -607,8 +747,29 @@ export function getLocalStorage(key: string): any {
|
|
|
607
747
|
}
|
|
608
748
|
|
|
609
749
|
/**
|
|
610
|
-
*
|
|
611
|
-
*
|
|
750
|
+
* EN: Load a CSS file from the server.
|
|
751
|
+
*
|
|
752
|
+
* ZH: 动态加载 CSS 文件
|
|
753
|
+
*
|
|
754
|
+
* ```
|
|
755
|
+
* loadCSS(
|
|
756
|
+
* 'http://example.com/css/mazey-base.css',
|
|
757
|
+
* {
|
|
758
|
+
* id: 'iamid', // Optional, link ID, default none
|
|
759
|
+
* }
|
|
760
|
+
* )
|
|
761
|
+
* .then(
|
|
762
|
+
* res => {
|
|
763
|
+
* console.log(`Load CSS Success: ${res}`);
|
|
764
|
+
* }
|
|
765
|
+
* )
|
|
766
|
+
* .catch(
|
|
767
|
+
* err => {
|
|
768
|
+
* console.error(`Load CSS Fail: ${err.message}`)
|
|
769
|
+
* }
|
|
770
|
+
* );
|
|
771
|
+
* ```
|
|
772
|
+
*
|
|
612
773
|
* @param {string} url -- css资源路径
|
|
613
774
|
* @param {string} options.id -- link标签id
|
|
614
775
|
* @returns {Promise<boolean>} true -- 加载成功
|
|
@@ -737,9 +898,32 @@ export function loadCSS(
|
|
|
737
898
|
}
|
|
738
899
|
|
|
739
900
|
/**
|
|
740
|
-
*
|
|
741
|
-
*
|
|
742
|
-
*
|
|
901
|
+
* EN: Load a JavaScript file from the server and execute it.
|
|
902
|
+
*
|
|
903
|
+
* ZH: 动态加载 JavaScript 文件
|
|
904
|
+
*
|
|
905
|
+
* ```
|
|
906
|
+
* loadScript(
|
|
907
|
+
* 'http://example.com/static/js/plugin-2.1.1.min.js',
|
|
908
|
+
* {
|
|
909
|
+
* id: 'iamid', // (Optional) script ID, default none
|
|
910
|
+
* timeout: 5000, // (Optional) timeout, default `5000`
|
|
911
|
+
* isDefer: false, // (Optional) defer, default `false`
|
|
912
|
+
* }
|
|
913
|
+
* )
|
|
914
|
+
* .then(
|
|
915
|
+
* res => {
|
|
916
|
+
* console.log(`Load JavaScript script: ${res}`);
|
|
917
|
+
* }
|
|
918
|
+
* )
|
|
919
|
+
* .catch(
|
|
920
|
+
* err => {
|
|
921
|
+
* console.error(`Load JavaScript script: ${err.message}`)
|
|
922
|
+
* }
|
|
923
|
+
* );
|
|
924
|
+
* ```
|
|
925
|
+
*
|
|
926
|
+
* @param {string} url -- JavaScript 资源路径
|
|
743
927
|
* @param {string} options.id -- DOM ID
|
|
744
928
|
* @param {function} options.callback -- 加载后回调函数
|
|
745
929
|
* @param {number} options.timeout -- 超时时长
|
|
@@ -896,8 +1080,44 @@ interface WebPerformance {
|
|
|
896
1080
|
}
|
|
897
1081
|
|
|
898
1082
|
/**
|
|
899
|
-
*
|
|
900
|
-
*
|
|
1083
|
+
* EN: Get page load time(PerformanceTiming).
|
|
1084
|
+
*
|
|
1085
|
+
* ZH: 获取页面加载相关的各项数据
|
|
1086
|
+
*
|
|
1087
|
+
* Usage:
|
|
1088
|
+
*
|
|
1089
|
+
* ```
|
|
1090
|
+
* // `camelCase:true`(Default) Return hump data.
|
|
1091
|
+
* // `camelCase:false` Return underline data.
|
|
1092
|
+
* getPerformance(true)
|
|
1093
|
+
* .then(res => {
|
|
1094
|
+
* console.log(JSON.stringify(res));
|
|
1095
|
+
* })
|
|
1096
|
+
* .catch(console.error);
|
|
1097
|
+
* ```
|
|
1098
|
+
*
|
|
1099
|
+
* Output:
|
|
1100
|
+
*
|
|
1101
|
+
* ```
|
|
1102
|
+
* {"deviceType":"pc","network":"3g","unloadTime":0,"redirectTime":0,"dnsTime":0,"tcpTime":0,"responseTime":65,"downloadTime":1,"domreadyTime":369,"onloadTime":441,"whiteTime":94,"renderTime":441,"decodedBodySize":210,"encodedBodySize":210}
|
|
1103
|
+
* ```
|
|
1104
|
+
*
|
|
1105
|
+
* Results:
|
|
1106
|
+
*
|
|
1107
|
+
* | Index | Field | Description |
|
|
1108
|
+
* | --- | --- | --- |
|
|
1109
|
+
* | DNS lookup | dns_time | domainLookupEnd - domainLookupStart |
|
|
1110
|
+
* | Connection negotiation | tcp_time | connectEnd - connectStart |
|
|
1111
|
+
* | Requests and responses | response_time | responseStart - requestStart |
|
|
1112
|
+
* | White screen | white_time | responseStart - navigationStart |
|
|
1113
|
+
* | DomReady | domready_time | domContentLoadedEventStart - navigationStart |
|
|
1114
|
+
* | Onload | onload_time | loadEventStart - navigationStart |
|
|
1115
|
+
* | EventEnd | render_time | loadEventEnd -navigationStart |
|
|
1116
|
+
* | Unload | unload_time | (Optional) unloadEventEnd - unloadEventStart |
|
|
1117
|
+
* | Redirect | redirect_time | (Optional) redirectEnd - redirectStart |
|
|
1118
|
+
* | SSL | ssl_time | (Optional) connectEnd - secureConnectionStart |
|
|
1119
|
+
* | Download | download_time | (Optional) responseEnd - responseStart |
|
|
1120
|
+
*
|
|
901
1121
|
* @param {boolean} camelCase -- true(默认) 以驼峰形式返回数据 false 以下划线形式返回数据
|
|
902
1122
|
* @returns {Promise<object>} 加载数据
|
|
903
1123
|
*/
|
|
@@ -1152,8 +1372,38 @@ export function getPerformance(
|
|
|
1152
1372
|
}
|
|
1153
1373
|
|
|
1154
1374
|
/**
|
|
1155
|
-
*
|
|
1156
|
-
*
|
|
1375
|
+
* EN: Hit probability (1% ~ 100%).
|
|
1376
|
+
*
|
|
1377
|
+
* ZH: 百分位概率
|
|
1378
|
+
*
|
|
1379
|
+
* Usage:
|
|
1380
|
+
*
|
|
1381
|
+
* ```
|
|
1382
|
+
* inRate(0.5); // 0.01 ~ 1 true/false
|
|
1383
|
+
* ```
|
|
1384
|
+
*
|
|
1385
|
+
* Output:
|
|
1386
|
+
*
|
|
1387
|
+
* ```
|
|
1388
|
+
* true
|
|
1389
|
+
* ```
|
|
1390
|
+
*
|
|
1391
|
+
* Example: Test the precision.
|
|
1392
|
+
*
|
|
1393
|
+
* ```
|
|
1394
|
+
* // Test
|
|
1395
|
+
* let trueCount = 0;
|
|
1396
|
+
* let falseCount = 0;
|
|
1397
|
+
* new Array(1000000).fill(0).forEach(() => {
|
|
1398
|
+
* if (inRate(0.5)) {
|
|
1399
|
+
* trueCount++;
|
|
1400
|
+
* } else {
|
|
1401
|
+
* falseCount++;
|
|
1402
|
+
* }
|
|
1403
|
+
* });
|
|
1404
|
+
* console.log(trueCount, falseCount); // 499994 500006
|
|
1405
|
+
* ```
|
|
1406
|
+
*
|
|
1157
1407
|
* @param {number} rate -- 0.1 ~ 1 => 1% ~ 100%
|
|
1158
1408
|
* @returns {boolean} true 命中
|
|
1159
1409
|
*/
|
|
@@ -1165,8 +1415,22 @@ export function inRate(rate: number): boolean {
|
|
|
1165
1415
|
}
|
|
1166
1416
|
|
|
1167
1417
|
/**
|
|
1168
|
-
*
|
|
1169
|
-
*
|
|
1418
|
+
* EN: Determine if it is a secure PWA environment that it can run.
|
|
1419
|
+
*
|
|
1420
|
+
* ZH: 判断是否是安全的 PWA 环境
|
|
1421
|
+
*
|
|
1422
|
+
* Usage:
|
|
1423
|
+
*
|
|
1424
|
+
* ```
|
|
1425
|
+
* isSafePWAEnv();
|
|
1426
|
+
* ```
|
|
1427
|
+
*
|
|
1428
|
+
* Output:
|
|
1429
|
+
*
|
|
1430
|
+
* ```
|
|
1431
|
+
* true
|
|
1432
|
+
* ```
|
|
1433
|
+
*
|
|
1170
1434
|
* @returns {boolean} true 是
|
|
1171
1435
|
*/
|
|
1172
1436
|
export function isSafePWAEnv(): boolean {
|
|
@@ -1217,10 +1481,20 @@ export function isSafePWAEnv(): boolean {
|
|
|
1217
1481
|
*
|
|
1218
1482
|
* ZH: 返回浏览器信息 https://github.com/JowayYoung/juejin-code/blob/master/browser-type.js
|
|
1219
1483
|
*
|
|
1484
|
+
* Usage:
|
|
1485
|
+
*
|
|
1486
|
+
* ```
|
|
1487
|
+
* getBrowserInfo();
|
|
1488
|
+
* ```
|
|
1489
|
+
*
|
|
1490
|
+
* Output:
|
|
1491
|
+
*
|
|
1220
1492
|
* ```
|
|
1221
|
-
*
|
|
1493
|
+
* {"engine":"webkit","engineVs":"537.36","platform":"desktop","supporter":"chrome","supporterVs":"85.0.4183.121","system":"windows","systemVs":"10"}
|
|
1222
1494
|
* ```
|
|
1223
1495
|
*
|
|
1496
|
+
* Results:
|
|
1497
|
+
*
|
|
1224
1498
|
* | Index | Field | Description |
|
|
1225
1499
|
* | --- | --- | --- |
|
|
1226
1500
|
* | System | system | android, ios, windows, macos, linux |
|
|
@@ -1231,7 +1505,7 @@ export function isSafePWAEnv(): boolean {
|
|
|
1231
1505
|
* | Supporter | supporter | edge, opera, chrome, safari, firefox, iexplore |
|
|
1232
1506
|
* | Supporter version | supporterVs | - |
|
|
1233
1507
|
* | Shell | shell | (Optional) wechat, qq_browser, qq_app, uc, 360, 2345, sougou, liebao, maxthon, bilibili |
|
|
1234
|
-
* | Shell version | shellVs | (Optional) |
|
|
1508
|
+
* | Shell version | shellVs | (Optional) 20/... |
|
|
1235
1509
|
* | Apple device type | appleType | (Optional) iphone, ipad, ipod, iwatch |
|
|
1236
1510
|
*
|
|
1237
1511
|
* Example: Determine the environment of the mobile QQ.
|
|
@@ -1486,8 +1760,20 @@ export function cutCHSString(str: string, len: number, hasDot = false): string {
|
|
|
1486
1760
|
}
|
|
1487
1761
|
|
|
1488
1762
|
/**
|
|
1489
|
-
*
|
|
1490
|
-
*
|
|
1763
|
+
* EN: Check whether the page is loaded successfully (Keepe the compatibility in case that browser's `load` event has been triggered).
|
|
1764
|
+
*
|
|
1765
|
+
* ZH: 页面加载完成
|
|
1766
|
+
*
|
|
1767
|
+
* ```
|
|
1768
|
+
* windowLoaded(30) // second
|
|
1769
|
+
* .then(res => {
|
|
1770
|
+
* console.log(`Load Success: ${res}`); // Load Success: load
|
|
1771
|
+
* })
|
|
1772
|
+
* .catch(err => {
|
|
1773
|
+
* console.log(`Load Timeout or Fail: ${err.message}`);
|
|
1774
|
+
* });
|
|
1775
|
+
* ```
|
|
1776
|
+
*
|
|
1491
1777
|
* @param {number} timeout 超时时间 / 单位:秒
|
|
1492
1778
|
* @returns {Promise<string>} document is loaded? 'complete' 'load' / 'timeout'
|
|
1493
1779
|
*/
|
|
@@ -1595,10 +1881,33 @@ export function addStyle(
|
|
|
1595
1881
|
}
|
|
1596
1882
|
|
|
1597
1883
|
/**
|
|
1598
|
-
*
|
|
1884
|
+
* EN: Custom console printing (`console`).
|
|
1885
|
+
*
|
|
1886
|
+
* ZH: 生成自定义控制台打印
|
|
1887
|
+
*
|
|
1888
|
+
* Usage:
|
|
1889
|
+
*
|
|
1890
|
+
* ```
|
|
1891
|
+
* const myConsole = genCustomConsole('MazeyLog:');
|
|
1892
|
+
* myConsole.log('I am string.');
|
|
1893
|
+
* myConsole.info('I am boolean.', true);
|
|
1894
|
+
* myConsole.info('I am number.', 123, 456);
|
|
1895
|
+
* myConsole.info('I am object.', { a: 123, b: 456});
|
|
1896
|
+
* ```
|
|
1897
|
+
*
|
|
1898
|
+
* Output:
|
|
1899
|
+
*
|
|
1900
|
+
* ```
|
|
1901
|
+
* MazeyLog: I am string.
|
|
1902
|
+
* MazeyLog: I am boolean. true
|
|
1903
|
+
* MazeyLog: I am number. 123 456
|
|
1904
|
+
* MazeyLog: I am object. {a: 123, b: 456}
|
|
1905
|
+
* ```
|
|
1599
1906
|
*
|
|
1600
1907
|
* @param {string} prefix 前缀
|
|
1601
|
-
* @param {
|
|
1908
|
+
* @param {string} locales A locale string.
|
|
1909
|
+
* @param {function} logFn The function with Log.
|
|
1910
|
+
* @param {function} errorFn The function with Error.
|
|
1602
1911
|
* @returns {object} 新实例
|
|
1603
1912
|
*/
|
|
1604
1913
|
export function genCustomConsole(
|
|
@@ -1606,19 +1915,32 @@ export function genCustomConsole(
|
|
|
1606
1915
|
options: {
|
|
1607
1916
|
isClosed?: boolean;
|
|
1608
1917
|
showWrap?: boolean;
|
|
1918
|
+
showDate?: boolean;
|
|
1919
|
+
locales?: string;
|
|
1609
1920
|
logFn?: () => void;
|
|
1610
1921
|
errorFn?: () => void;
|
|
1611
1922
|
} = {
|
|
1612
1923
|
isClosed: false,
|
|
1613
1924
|
showWrap: false,
|
|
1925
|
+
showDate: false,
|
|
1926
|
+
locales: 'en-US',
|
|
1614
1927
|
logFn: () => undefined,
|
|
1615
1928
|
errorFn: () => undefined
|
|
1616
1929
|
}
|
|
1617
1930
|
): Console {
|
|
1618
|
-
const {
|
|
1931
|
+
const {
|
|
1932
|
+
isClosed,
|
|
1933
|
+
showWrap,
|
|
1934
|
+
showDate,
|
|
1935
|
+
locales,
|
|
1936
|
+
logFn,
|
|
1937
|
+
errorFn
|
|
1938
|
+
} = Object.assign(
|
|
1619
1939
|
{
|
|
1620
1940
|
isClosed: false,
|
|
1621
1941
|
showWrap: false,
|
|
1942
|
+
showDate: false,
|
|
1943
|
+
locales: 'en-US',
|
|
1622
1944
|
logFn: () => undefined,
|
|
1623
1945
|
errorFn: () => undefined
|
|
1624
1946
|
},
|
|
@@ -1626,12 +1948,34 @@ export function genCustomConsole(
|
|
|
1626
1948
|
);
|
|
1627
1949
|
const methods = ['log', 'info', 'warn', 'error'];
|
|
1628
1950
|
const newConsole = Object.create(null);
|
|
1951
|
+
// https://stackoverflow.com/questions/3552461/how-do-i-format-a-date-in-javascript
|
|
1952
|
+
const formatDate = () => {
|
|
1953
|
+
const dateOptions: Intl.DateTimeFormatOptions = {
|
|
1954
|
+
weekday: 'short',
|
|
1955
|
+
year: 'numeric',
|
|
1956
|
+
month: 'short',
|
|
1957
|
+
day: 'numeric',
|
|
1958
|
+
hour: 'numeric',
|
|
1959
|
+
// hourCycle: 'h24',
|
|
1960
|
+
minute: 'numeric',
|
|
1961
|
+
second: 'numeric'
|
|
1962
|
+
};
|
|
1963
|
+
const todayDateIns = new Date();
|
|
1964
|
+
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleDateString
|
|
1965
|
+
// https://datatracker.ietf.org/doc/html/rfc4647
|
|
1966
|
+
const dateStr = todayDateIns.toLocaleDateString(locales, dateOptions);
|
|
1967
|
+
return dateStr;
|
|
1968
|
+
};
|
|
1969
|
+
// if (showDate) {
|
|
1970
|
+
// prefix = `${dateStr} ${prefix}`;
|
|
1971
|
+
// }
|
|
1629
1972
|
methods.forEach(method => {
|
|
1630
1973
|
newConsole[method] = function(...argu: any) {
|
|
1631
1974
|
if (isClosed) {
|
|
1632
1975
|
return false;
|
|
1633
1976
|
}
|
|
1634
|
-
let elaboratePrefix =
|
|
1977
|
+
let elaboratePrefix = prefix;
|
|
1978
|
+
let datePrefix = prefix;
|
|
1635
1979
|
if (typeof prefix === 'string' && prefix.length >= 2) {
|
|
1636
1980
|
const len = prefix.length;
|
|
1637
1981
|
if (prefix[len - 1] === ':') {
|
|
@@ -1639,14 +1983,19 @@ export function genCustomConsole(
|
|
|
1639
1983
|
} else {
|
|
1640
1984
|
elaboratePrefix = prefix;
|
|
1641
1985
|
}
|
|
1642
|
-
// console.log('prefix', prefix);
|
|
1643
|
-
// console.log('elaboratePrefix', elaboratePrefix);
|
|
1644
1986
|
}
|
|
1645
1987
|
if (showWrap) {
|
|
1646
1988
|
console.log(`--- ${elaboratePrefix} - begin ---`);
|
|
1647
1989
|
}
|
|
1648
|
-
if (
|
|
1649
|
-
|
|
1990
|
+
if (showDate) {
|
|
1991
|
+
if (prefix) {
|
|
1992
|
+
datePrefix = `${formatDate()} ${prefix}`;
|
|
1993
|
+
} else {
|
|
1994
|
+
datePrefix = `${formatDate()}`;
|
|
1995
|
+
}
|
|
1996
|
+
}
|
|
1997
|
+
if (prefix || showDate) {
|
|
1998
|
+
(console as any)[method](datePrefix, ...argu);
|
|
1650
1999
|
} else {
|
|
1651
2000
|
(console as any)[method](...argu);
|
|
1652
2001
|
}
|