@whitesev/utils 2.6.8 → 2.6.9
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/dist/index.amd.js +590 -300
- package/dist/index.amd.js.map +1 -1
- package/dist/index.cjs.js +590 -300
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.esm.js +590 -300
- package/dist/index.esm.js.map +1 -1
- package/dist/index.iife.js +590 -300
- package/dist/index.iife.js.map +1 -1
- package/dist/index.system.js +590 -300
- package/dist/index.system.js.map +1 -1
- package/dist/index.umd.js +590 -300
- package/dist/index.umd.js.map +1 -1
- package/dist/types/src/CommonUtil.d.ts +109 -0
- package/dist/types/src/DOMUtils.d.ts +95 -0
- package/dist/types/src/Utils.d.ts +13 -9
- package/package.json +2 -1
- package/src/CommonUtil.ts +302 -0
- package/src/DOMUtils.ts +287 -0
- package/src/Dictionary.ts +2 -2
- package/src/Hooks.ts +11 -12
- package/src/Httpx.ts +22 -16
- package/src/LockFunction.ts +4 -7
- package/src/Progress.ts +2 -2
- package/src/TryCatch.ts +6 -9
- package/src/Utils.ts +20 -213
- package/src/UtilsGMCookie.ts +7 -7
- package/src/UtilsGMMenu.ts +2 -2
package/dist/index.iife.js
CHANGED
|
@@ -227,6 +227,273 @@ var Utils = (function () {
|
|
|
227
227
|
}
|
|
228
228
|
}
|
|
229
229
|
|
|
230
|
+
const TryCatch = function (...args) {
|
|
231
|
+
/* 定义变量和函数 */
|
|
232
|
+
let callbackFunction = null;
|
|
233
|
+
let context = null;
|
|
234
|
+
let handleError = (error) => { };
|
|
235
|
+
let defaultDetails = {
|
|
236
|
+
log: true,
|
|
237
|
+
};
|
|
238
|
+
const TryCatchCore = {
|
|
239
|
+
/**
|
|
240
|
+
*
|
|
241
|
+
* @param paramDetails 配置
|
|
242
|
+
* @returns
|
|
243
|
+
*/
|
|
244
|
+
config(paramDetails) {
|
|
245
|
+
defaultDetails = Object.assign(defaultDetails, paramDetails);
|
|
246
|
+
return TryCatchCore;
|
|
247
|
+
},
|
|
248
|
+
/**
|
|
249
|
+
* 处理错误
|
|
250
|
+
* @param handler
|
|
251
|
+
*/
|
|
252
|
+
error(handler) {
|
|
253
|
+
// @ts-ignore
|
|
254
|
+
handleError = handler;
|
|
255
|
+
return TryCatchCore;
|
|
256
|
+
},
|
|
257
|
+
/**
|
|
258
|
+
* 执行传入的函数并捕获其可能抛出的错误,并通过传入的错误处理函数进行处理。
|
|
259
|
+
* @param callback 待执行函数,可以是 function 或者 string 类型。如果是 string 类型,则会被当做代码进行执行。
|
|
260
|
+
* @param __context__ 待执行函数的作用域,用于apply指定
|
|
261
|
+
* @returns 如果函数有返回值,则返回该返回值;否则返回 tryCatchObj 函数以支持链式调用。
|
|
262
|
+
* @throws {Error} 如果传入参数不符合要求,则会抛出相应类型的错误。
|
|
263
|
+
*/
|
|
264
|
+
run(callback, __context__) {
|
|
265
|
+
callbackFunction = callback;
|
|
266
|
+
context = __context__ || this;
|
|
267
|
+
let result = executeTryCatch(callbackFunction, handleError, context);
|
|
268
|
+
// @ts-ignore
|
|
269
|
+
return result !== undefined ? result : TryCatchCore;
|
|
270
|
+
},
|
|
271
|
+
};
|
|
272
|
+
/**
|
|
273
|
+
* 执行传入的函数并捕获其可能抛出的错误,并通过传入的错误处理函数进行处理。
|
|
274
|
+
* @param callback - 待执行函数,可以是 function 或者 string 类型。如果是 string 类型,则会被当做代码进行执行。
|
|
275
|
+
* @param handleErrorFunc - 错误处理函数,可以是 function 或者 string 类型。如果是 string 类型,则会被当做代码进行执行。
|
|
276
|
+
* @param funcThis - 待执行函数的作用域,用于apply指定
|
|
277
|
+
* @returns 如果函数有返回值,则返回该返回值;否则返回 undefined。
|
|
278
|
+
*/
|
|
279
|
+
function executeTryCatch(callback, handleErrorFunc, funcThis) {
|
|
280
|
+
let result = undefined;
|
|
281
|
+
try {
|
|
282
|
+
if (typeof callback === "string") {
|
|
283
|
+
result = new Function(callback).apply(funcThis, args);
|
|
284
|
+
}
|
|
285
|
+
else {
|
|
286
|
+
result = callback.apply(funcThis, args);
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
catch (error) {
|
|
290
|
+
if (defaultDetails.log) {
|
|
291
|
+
callback = callback;
|
|
292
|
+
console.log(`%c ${callback?.name ? callback?.name : callback + "出现错误"} `, "color: #f20000");
|
|
293
|
+
console.log(`%c 错误原因:${error}`, "color: #f20000");
|
|
294
|
+
console.trace(callback);
|
|
295
|
+
}
|
|
296
|
+
if (handleErrorFunc) {
|
|
297
|
+
if (typeof handleErrorFunc === "string") {
|
|
298
|
+
result = new Function(handleErrorFunc).apply(funcThis, [
|
|
299
|
+
...args,
|
|
300
|
+
error,
|
|
301
|
+
]);
|
|
302
|
+
}
|
|
303
|
+
else {
|
|
304
|
+
result = handleErrorFunc.apply(funcThis, [...args, error]);
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
return result;
|
|
309
|
+
}
|
|
310
|
+
return TryCatchCore;
|
|
311
|
+
};
|
|
312
|
+
|
|
313
|
+
class CommonUtil {
|
|
314
|
+
assign(target = {}, source = {}, isAdd = false) {
|
|
315
|
+
let UtilsContext = this;
|
|
316
|
+
if (Array.isArray(source)) {
|
|
317
|
+
let canTraverse = source.filter((item) => {
|
|
318
|
+
return typeof item === "object";
|
|
319
|
+
});
|
|
320
|
+
if (!canTraverse.length) {
|
|
321
|
+
return source;
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
if (source == null) {
|
|
325
|
+
return target;
|
|
326
|
+
}
|
|
327
|
+
if (target == null) {
|
|
328
|
+
target = {};
|
|
329
|
+
}
|
|
330
|
+
if (isAdd) {
|
|
331
|
+
for (const sourceKeyName in source) {
|
|
332
|
+
const targetKeyName = sourceKeyName;
|
|
333
|
+
let targetValue = target[targetKeyName];
|
|
334
|
+
let sourceValue = source[sourceKeyName];
|
|
335
|
+
if (typeof sourceValue === "object" &&
|
|
336
|
+
sourceValue != null &&
|
|
337
|
+
sourceKeyName in target &&
|
|
338
|
+
!UtilsContext.isDOM(sourceValue)) {
|
|
339
|
+
/* 源端的值是object类型,且不是元素节点 */
|
|
340
|
+
target[sourceKeyName] = UtilsContext.assign(targetValue, sourceValue, isAdd);
|
|
341
|
+
continue;
|
|
342
|
+
}
|
|
343
|
+
target[sourceKeyName] = sourceValue;
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
else {
|
|
347
|
+
for (const targetKeyName in target) {
|
|
348
|
+
if (targetKeyName in source) {
|
|
349
|
+
let targetValue = target[targetKeyName];
|
|
350
|
+
let sourceValue = source[targetKeyName];
|
|
351
|
+
if (typeof sourceValue === "object" &&
|
|
352
|
+
sourceValue != null &&
|
|
353
|
+
!UtilsContext.isDOM(sourceValue) &&
|
|
354
|
+
Object.keys(sourceValue).length) {
|
|
355
|
+
/* 源端的值是object类型,且不是元素节点 */
|
|
356
|
+
target[targetKeyName] = UtilsContext.assign(targetValue, sourceValue, isAdd);
|
|
357
|
+
continue;
|
|
358
|
+
}
|
|
359
|
+
/* 直接赋值 */
|
|
360
|
+
target[targetKeyName] = sourceValue;
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
return target;
|
|
365
|
+
}
|
|
366
|
+
isNull(...args) {
|
|
367
|
+
let result = true;
|
|
368
|
+
let checkList = [...args];
|
|
369
|
+
for (const objItem of checkList) {
|
|
370
|
+
let itemResult = false;
|
|
371
|
+
if (objItem === null || objItem === undefined) {
|
|
372
|
+
itemResult = true;
|
|
373
|
+
}
|
|
374
|
+
else {
|
|
375
|
+
switch (typeof objItem) {
|
|
376
|
+
case "object":
|
|
377
|
+
if (typeof objItem[Symbol.iterator] === "function") {
|
|
378
|
+
/* 可迭代 */
|
|
379
|
+
itemResult = objItem.length === 0;
|
|
380
|
+
}
|
|
381
|
+
else {
|
|
382
|
+
itemResult = Object.keys(objItem).length === 0;
|
|
383
|
+
}
|
|
384
|
+
break;
|
|
385
|
+
case "number":
|
|
386
|
+
itemResult = objItem === 0;
|
|
387
|
+
break;
|
|
388
|
+
case "string":
|
|
389
|
+
itemResult =
|
|
390
|
+
objItem.trim() === "" ||
|
|
391
|
+
objItem === "null" ||
|
|
392
|
+
objItem === "undefined";
|
|
393
|
+
break;
|
|
394
|
+
case "boolean":
|
|
395
|
+
itemResult = !objItem;
|
|
396
|
+
break;
|
|
397
|
+
case "function":
|
|
398
|
+
let funcStr = objItem.toString().replace(/\s/g, "");
|
|
399
|
+
/* 排除()=>{}、(xxx="")=>{}、function(){}、function(xxx=""){} */
|
|
400
|
+
itemResult = Boolean(funcStr.match(/^\(.*?\)=>\{\}$|^function.*?\(.*?\)\{\}$/));
|
|
401
|
+
break;
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
result = result && itemResult;
|
|
405
|
+
}
|
|
406
|
+
return result;
|
|
407
|
+
}
|
|
408
|
+
/**
|
|
409
|
+
* 判断对象是否是元素
|
|
410
|
+
* @param target
|
|
411
|
+
* @returns
|
|
412
|
+
* + true 是元素
|
|
413
|
+
* + false 不是元素
|
|
414
|
+
* @example
|
|
415
|
+
* Utils.isDOM(document.querySelector("a"))
|
|
416
|
+
* > true
|
|
417
|
+
*/
|
|
418
|
+
isDOM(target) {
|
|
419
|
+
return target instanceof Node;
|
|
420
|
+
}
|
|
421
|
+
isNotNull(...args) {
|
|
422
|
+
let UtilsContext = this;
|
|
423
|
+
return !UtilsContext.isNull.apply(this, args);
|
|
424
|
+
}
|
|
425
|
+
deepClone(obj) {
|
|
426
|
+
let UtilsContext = this;
|
|
427
|
+
if (obj === undefined)
|
|
428
|
+
return undefined;
|
|
429
|
+
if (obj === null)
|
|
430
|
+
return null;
|
|
431
|
+
let clone = obj instanceof Array ? [] : {};
|
|
432
|
+
for (const [key, value] of Object.entries(obj)) {
|
|
433
|
+
clone[key] =
|
|
434
|
+
typeof value === "object" ? UtilsContext.deepClone(value) : value;
|
|
435
|
+
}
|
|
436
|
+
return clone;
|
|
437
|
+
}
|
|
438
|
+
/**
|
|
439
|
+
* 覆盖对象中的函数this指向
|
|
440
|
+
* @param target 需要覆盖的对象
|
|
441
|
+
* @param [objectThis] 覆盖的this指向,如果为传入,则默认为对象本身
|
|
442
|
+
*/
|
|
443
|
+
coverObjectFunctionThis(target, objectThis) {
|
|
444
|
+
if (typeof target !== "object" || target === null) {
|
|
445
|
+
throw new Error("target must be object");
|
|
446
|
+
}
|
|
447
|
+
objectThis = objectThis || target;
|
|
448
|
+
Object.keys(target).forEach((key) => {
|
|
449
|
+
if (typeof target[key] === "function") {
|
|
450
|
+
target[key] = target[key].bind(objectThis);
|
|
451
|
+
}
|
|
452
|
+
});
|
|
453
|
+
}
|
|
454
|
+
toJSON(data, errorCallBack) {
|
|
455
|
+
let result = {};
|
|
456
|
+
if (typeof data === "object") {
|
|
457
|
+
return data;
|
|
458
|
+
}
|
|
459
|
+
TryCatch()
|
|
460
|
+
.config({ log: false })
|
|
461
|
+
.error((error) => {
|
|
462
|
+
TryCatch()
|
|
463
|
+
.error(() => {
|
|
464
|
+
try {
|
|
465
|
+
result = new Function("return " + data)();
|
|
466
|
+
}
|
|
467
|
+
catch (error2) {
|
|
468
|
+
if (typeof errorCallBack === "function") {
|
|
469
|
+
errorCallBack(error2);
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
})
|
|
473
|
+
.run(() => {
|
|
474
|
+
if (data &&
|
|
475
|
+
/^[\],:{}\s]*$/.test(data
|
|
476
|
+
.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g, "@")
|
|
477
|
+
.replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, "]")
|
|
478
|
+
.replace(/(?:^|:|,)(?:\s*\[)+/g, ""))) {
|
|
479
|
+
result = new Function("return " + data)();
|
|
480
|
+
}
|
|
481
|
+
else {
|
|
482
|
+
if (typeof errorCallBack === "function") {
|
|
483
|
+
errorCallBack(new Error("target is not a JSON"));
|
|
484
|
+
}
|
|
485
|
+
}
|
|
486
|
+
});
|
|
487
|
+
})
|
|
488
|
+
.run(() => {
|
|
489
|
+
data = data.trim();
|
|
490
|
+
result = JSON.parse(data);
|
|
491
|
+
});
|
|
492
|
+
return result;
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
let commonUtil = new CommonUtil();
|
|
496
|
+
|
|
230
497
|
class UtilsGMCookie {
|
|
231
498
|
windowApi = {
|
|
232
499
|
window: window,
|
|
@@ -299,7 +566,7 @@ var Utils = (function () {
|
|
|
299
566
|
name: "",
|
|
300
567
|
path: "/",
|
|
301
568
|
};
|
|
302
|
-
defaultOption =
|
|
569
|
+
defaultOption = commonUtil.assign(defaultOption, option);
|
|
303
570
|
let cookies = this.getCookiesList();
|
|
304
571
|
cookies.forEach((item) => {
|
|
305
572
|
item = item.trim();
|
|
@@ -350,7 +617,7 @@ var Utils = (function () {
|
|
|
350
617
|
name: "",
|
|
351
618
|
path: "/",
|
|
352
619
|
};
|
|
353
|
-
defaultOption =
|
|
620
|
+
defaultOption = commonUtil.assign(defaultOption, option);
|
|
354
621
|
let cookies = this.getCookiesList();
|
|
355
622
|
cookies.forEach((item) => {
|
|
356
623
|
item = item.trim();
|
|
@@ -399,7 +666,7 @@ var Utils = (function () {
|
|
|
399
666
|
*/
|
|
400
667
|
expirationDate: Math.floor(Date.now()) + 60 * 60 * 24 * 30,
|
|
401
668
|
};
|
|
402
|
-
defaultOption =
|
|
669
|
+
defaultOption = commonUtil.assign(defaultOption, option);
|
|
403
670
|
let life = defaultOption.expirationDate
|
|
404
671
|
? defaultOption.expirationDate
|
|
405
672
|
: Math.floor(Date.now()) + 60 * 60 * 24 * 30;
|
|
@@ -409,7 +676,7 @@ var Utils = (function () {
|
|
|
409
676
|
";expires=" +
|
|
410
677
|
new Date(life).toGMTString() +
|
|
411
678
|
"; path=/";
|
|
412
|
-
if (
|
|
679
|
+
if (commonUtil.isNull(defaultOption.domain)) {
|
|
413
680
|
cookieStr += "; domain=" + defaultOption.domain;
|
|
414
681
|
}
|
|
415
682
|
this.windowApi.document.cookie = cookieStr;
|
|
@@ -437,9 +704,9 @@ var Utils = (function () {
|
|
|
437
704
|
path: "/",
|
|
438
705
|
firstPartyDomain: "",
|
|
439
706
|
};
|
|
440
|
-
defaultOption =
|
|
707
|
+
defaultOption = commonUtil.assign(defaultOption, option);
|
|
441
708
|
let cookieStr = `${defaultOption.name}=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=${defaultOption.path}`;
|
|
442
|
-
if (
|
|
709
|
+
if (commonUtil.isNull(defaultOption.firstPartyDomain)) {
|
|
443
710
|
cookieStr += `; domain=${defaultOption.firstPartyDomain};`;
|
|
444
711
|
}
|
|
445
712
|
this.windowApi.document.cookie = cookieStr;
|
|
@@ -1624,7 +1891,7 @@ var Utils = (function () {
|
|
|
1624
1891
|
menuOptions = [menuOptions];
|
|
1625
1892
|
}
|
|
1626
1893
|
for (let index = 0; index < menuOptions.length; index++) {
|
|
1627
|
-
let cloneMenuOptionData =
|
|
1894
|
+
let cloneMenuOptionData = commonUtil.deepClone(menuOptions[index].data);
|
|
1628
1895
|
const { showText, clickCallBack } = this.handleMenuData(cloneMenuOptionData);
|
|
1629
1896
|
let menuId = that.context.GM_Api.registerMenuCommand(showText, clickCallBack);
|
|
1630
1897
|
menuOptions[index].id = menuId;
|
|
@@ -2026,16 +2293,12 @@ var Utils = (function () {
|
|
|
2026
2293
|
return "";
|
|
2027
2294
|
}
|
|
2028
2295
|
try {
|
|
2029
|
-
|
|
2030
|
-
|
|
2031
|
-
|
|
2032
|
-
|
|
2033
|
-
|
|
2034
|
-
|
|
2035
|
-
"return _context['realFunc_" +
|
|
2036
|
-
_funcName +
|
|
2037
|
-
"'].apply(obj,args);\n" +
|
|
2038
|
-
"};");
|
|
2296
|
+
new Function("_context", "_funcName", "hookFunc", `_context[_funcName] = function ${_funcName}() {
|
|
2297
|
+
let args = Array.prototype.slice.call(arguments, 0);
|
|
2298
|
+
let obj = this;
|
|
2299
|
+
hookFunc.apply(obj, args);
|
|
2300
|
+
return _context['realFunc_${_funcName}'].apply(obj, args);
|
|
2301
|
+
};`)(_context, _funcName, hookFunc);
|
|
2039
2302
|
_context[_funcName].prototype.isHooked = true;
|
|
2040
2303
|
return true;
|
|
2041
2304
|
}
|
|
@@ -2295,14 +2558,14 @@ var Utils = (function () {
|
|
|
2295
2558
|
if (typeof args[1] === "object") {
|
|
2296
2559
|
/* 处理第二个参数details */
|
|
2297
2560
|
let optionArg = args[1];
|
|
2298
|
-
|
|
2561
|
+
commonUtil.assign(option, optionArg, true);
|
|
2299
2562
|
option.url = url;
|
|
2300
2563
|
}
|
|
2301
2564
|
}
|
|
2302
2565
|
else {
|
|
2303
2566
|
/* 传入的是配置 */
|
|
2304
2567
|
let optionArg = args[0];
|
|
2305
|
-
|
|
2568
|
+
commonUtil.assign(option, optionArg, true);
|
|
2306
2569
|
}
|
|
2307
2570
|
return option;
|
|
2308
2571
|
},
|
|
@@ -2335,7 +2598,7 @@ var Utils = (function () {
|
|
|
2335
2598
|
responseType: userRequestOption.responseType ||
|
|
2336
2599
|
this.context.#defaultRequestOption.responseType,
|
|
2337
2600
|
/* 对象使用深拷贝 */
|
|
2338
|
-
headers:
|
|
2601
|
+
headers: commonUtil.deepClone(this.context.#defaultRequestOption.headers),
|
|
2339
2602
|
data: userRequestOption.data || this.context.#defaultRequestOption.data,
|
|
2340
2603
|
redirect: userRequestOption.redirect ||
|
|
2341
2604
|
this.context.#defaultRequestOption.redirect,
|
|
@@ -2348,7 +2611,7 @@ var Utils = (function () {
|
|
|
2348
2611
|
revalidate: userRequestOption.revalidate ||
|
|
2349
2612
|
this.context.#defaultRequestOption.revalidate,
|
|
2350
2613
|
/* 对象使用深拷贝 */
|
|
2351
|
-
context:
|
|
2614
|
+
context: commonUtil.deepClone(userRequestOption.context ||
|
|
2352
2615
|
this.context.#defaultRequestOption.context),
|
|
2353
2616
|
overrideMimeType: userRequestOption.overrideMimeType ||
|
|
2354
2617
|
this.context.#defaultRequestOption.overrideMimeType,
|
|
@@ -2356,7 +2619,7 @@ var Utils = (function () {
|
|
|
2356
2619
|
this.context.#defaultRequestOption.anonymous,
|
|
2357
2620
|
fetch: userRequestOption.fetch || this.context.#defaultRequestOption.fetch,
|
|
2358
2621
|
/* 对象使用深拷贝 */
|
|
2359
|
-
fetchInit:
|
|
2622
|
+
fetchInit: commonUtil.deepClone(this.context.#defaultRequestOption.fetchInit),
|
|
2360
2623
|
allowInterceptConfig: {
|
|
2361
2624
|
beforeRequest: this.context.#defaultRequestOption
|
|
2362
2625
|
.allowInterceptConfig.beforeRequest,
|
|
@@ -2582,12 +2845,12 @@ var Utils = (function () {
|
|
|
2582
2845
|
Object.keys(option).forEach((keyName) => {
|
|
2583
2846
|
if (option[keyName] == null ||
|
|
2584
2847
|
(option[keyName] instanceof Function &&
|
|
2585
|
-
|
|
2848
|
+
commonUtil.isNull(option[keyName]))) {
|
|
2586
2849
|
Reflect.deleteProperty(option, keyName);
|
|
2587
2850
|
return;
|
|
2588
2851
|
}
|
|
2589
2852
|
});
|
|
2590
|
-
if (
|
|
2853
|
+
if (commonUtil.isNull(option.url)) {
|
|
2591
2854
|
throw new TypeError(`Utils.Httpx 参数 url不符合要求: ${option.url}`);
|
|
2592
2855
|
}
|
|
2593
2856
|
return option;
|
|
@@ -2786,10 +3049,10 @@ var Utils = (function () {
|
|
|
2786
3049
|
/* X浏览器会因为设置了responseType导致不返回responseText */
|
|
2787
3050
|
let originResponse = argsResult[0];
|
|
2788
3051
|
/* responseText为空,response不为空的情况 */
|
|
2789
|
-
if (
|
|
2790
|
-
|
|
3052
|
+
if (commonUtil.isNull(originResponse["responseText"]) &&
|
|
3053
|
+
commonUtil.isNotNull(originResponse["response"])) {
|
|
2791
3054
|
if (typeof originResponse["response"] === "object") {
|
|
2792
|
-
|
|
3055
|
+
TryCatch().run(() => {
|
|
2793
3056
|
originResponse["responseText"] = JSON.stringify(originResponse["response"]);
|
|
2794
3057
|
});
|
|
2795
3058
|
}
|
|
@@ -2806,7 +3069,7 @@ var Utils = (function () {
|
|
|
2806
3069
|
// 自定义个新的response
|
|
2807
3070
|
let httpxResponse = httpxResponseText;
|
|
2808
3071
|
if (details.responseType === "json") {
|
|
2809
|
-
httpxResponse =
|
|
3072
|
+
httpxResponse = commonUtil.toJSON(httpxResponseText);
|
|
2810
3073
|
}
|
|
2811
3074
|
else if (details.responseType === "document") {
|
|
2812
3075
|
let parser = new DOMParser();
|
|
@@ -3016,7 +3279,7 @@ var Utils = (function () {
|
|
|
3016
3279
|
(typeof fetchResponseType === "string" &&
|
|
3017
3280
|
fetchResponseType.includes("application/json"))) {
|
|
3018
3281
|
// response返回格式是JSON格式
|
|
3019
|
-
response =
|
|
3282
|
+
response = commonUtil.toJSON(responseText);
|
|
3020
3283
|
}
|
|
3021
3284
|
else if (option.responseType === "document" ||
|
|
3022
3285
|
option.responseType == null) {
|
|
@@ -3117,7 +3380,7 @@ var Utils = (function () {
|
|
|
3117
3380
|
if (typeof option.xmlHttpRequest !== "function") {
|
|
3118
3381
|
console.warn("[Httpx-constructor] 未传入GM_xmlhttpRequest函数或传入的GM_xmlhttpRequest不是Function,将默认使用window.fetch");
|
|
3119
3382
|
}
|
|
3120
|
-
|
|
3383
|
+
commonUtil.coverObjectFunctionThis(this);
|
|
3121
3384
|
this.interceptors.request.context = this;
|
|
3122
3385
|
this.interceptors.response.context = this;
|
|
3123
3386
|
this.config(option);
|
|
@@ -3130,8 +3393,8 @@ var Utils = (function () {
|
|
|
3130
3393
|
if (typeof option.xmlHttpRequest === "function") {
|
|
3131
3394
|
this.GM_Api.xmlHttpRequest = option.xmlHttpRequest;
|
|
3132
3395
|
}
|
|
3133
|
-
this.#defaultRequestOption =
|
|
3134
|
-
this.#defaultInitOption =
|
|
3396
|
+
this.#defaultRequestOption = commonUtil.assign(this.#defaultRequestOption, option);
|
|
3397
|
+
this.#defaultInitOption = commonUtil.assign(this.#defaultInitOption, option);
|
|
3135
3398
|
}
|
|
3136
3399
|
/**
|
|
3137
3400
|
* 拦截器
|
|
@@ -3695,7 +3958,7 @@ var Utils = (function () {
|
|
|
3695
3958
|
#flag = false;
|
|
3696
3959
|
#delayTime = 0;
|
|
3697
3960
|
#callback;
|
|
3698
|
-
#
|
|
3961
|
+
#timeId = undefined;
|
|
3699
3962
|
lock;
|
|
3700
3963
|
unlock;
|
|
3701
3964
|
run;
|
|
@@ -3705,23 +3968,22 @@ var Utils = (function () {
|
|
|
3705
3968
|
this.#callback = callback;
|
|
3706
3969
|
if (typeof context === "number") {
|
|
3707
3970
|
this.#delayTime = context;
|
|
3708
|
-
this.#context = utils;
|
|
3709
3971
|
}
|
|
3710
3972
|
else {
|
|
3711
3973
|
this.#delayTime = delayTime;
|
|
3712
|
-
this.#context = context;
|
|
3713
3974
|
}
|
|
3714
3975
|
/**
|
|
3715
3976
|
* 锁
|
|
3716
3977
|
*/
|
|
3717
3978
|
this.lock = function () {
|
|
3718
3979
|
that.#flag = true;
|
|
3980
|
+
clearTimeout(that.#timeId);
|
|
3719
3981
|
};
|
|
3720
3982
|
/**
|
|
3721
3983
|
* 解锁
|
|
3722
3984
|
*/
|
|
3723
3985
|
this.unlock = function () {
|
|
3724
|
-
|
|
3986
|
+
that.#timeId = setTimeout(() => {
|
|
3725
3987
|
that.#flag = false;
|
|
3726
3988
|
}, that.#delayTime);
|
|
3727
3989
|
};
|
|
@@ -3739,7 +4001,7 @@ var Utils = (function () {
|
|
|
3739
4001
|
return;
|
|
3740
4002
|
}
|
|
3741
4003
|
that.lock();
|
|
3742
|
-
await that.#callback.apply(
|
|
4004
|
+
await that.#callback.apply(this, args);
|
|
3743
4005
|
that.unlock();
|
|
3744
4006
|
};
|
|
3745
4007
|
}
|
|
@@ -4027,7 +4289,7 @@ var Utils = (function () {
|
|
|
4027
4289
|
* @param paramConfig 配置信息
|
|
4028
4290
|
*/
|
|
4029
4291
|
constructor(paramConfig) {
|
|
4030
|
-
this.#config =
|
|
4292
|
+
this.#config = commonUtil.assign(this.#config, paramConfig);
|
|
4031
4293
|
if (!(this.#config.canvasNode instanceof HTMLCanvasElement)) {
|
|
4032
4294
|
throw new Error("Utils.Progress 参数 canvasNode 必须是 HTMLCanvasElement");
|
|
4033
4295
|
}
|
|
@@ -4081,95 +4343,10 @@ var Utils = (function () {
|
|
|
4081
4343
|
/* 获取文本宽度 */
|
|
4082
4344
|
let w = this.#ctx.measureText(txt).width;
|
|
4083
4345
|
let h = this.#config.fontSize / 2;
|
|
4084
|
-
this.#ctx.fillStyle = this.#config.textColor;
|
|
4085
|
-
this.#ctx.fillText(txt, this.#width / 2 - w / 2, this.#height / 2 + h / 2);
|
|
4086
|
-
}
|
|
4087
|
-
}
|
|
4088
|
-
|
|
4089
|
-
const TryCatch = function (...args) {
|
|
4090
|
-
/* 定义变量和函数 */
|
|
4091
|
-
let callbackFunction = null;
|
|
4092
|
-
let context = null;
|
|
4093
|
-
let handleError = (error) => { };
|
|
4094
|
-
let defaultDetails = {
|
|
4095
|
-
log: true,
|
|
4096
|
-
};
|
|
4097
|
-
const TryCatchCore = {
|
|
4098
|
-
/**
|
|
4099
|
-
*
|
|
4100
|
-
* @param paramDetails 配置
|
|
4101
|
-
* @returns
|
|
4102
|
-
*/
|
|
4103
|
-
config(paramDetails) {
|
|
4104
|
-
defaultDetails = utils.assign(defaultDetails, paramDetails);
|
|
4105
|
-
return TryCatchCore;
|
|
4106
|
-
},
|
|
4107
|
-
/**
|
|
4108
|
-
* 处理错误
|
|
4109
|
-
* @param handler
|
|
4110
|
-
*/
|
|
4111
|
-
error(handler) {
|
|
4112
|
-
// @ts-ignore
|
|
4113
|
-
handleError = handler;
|
|
4114
|
-
return TryCatchCore;
|
|
4115
|
-
},
|
|
4116
|
-
/**
|
|
4117
|
-
* 执行传入的函数并捕获其可能抛出的错误,并通过传入的错误处理函数进行处理。
|
|
4118
|
-
* @param callback 待执行函数,可以是 function 或者 string 类型。如果是 string 类型,则会被当做代码进行执行。
|
|
4119
|
-
* @param __context__ 待执行函数的作用域,用于apply指定
|
|
4120
|
-
* @returns 如果函数有返回值,则返回该返回值;否则返回 tryCatchObj 函数以支持链式调用。
|
|
4121
|
-
* @throws {Error} 如果传入参数不符合要求,则会抛出相应类型的错误。
|
|
4122
|
-
*/
|
|
4123
|
-
run(callback, __context__) {
|
|
4124
|
-
callbackFunction = callback;
|
|
4125
|
-
context = __context__ || this;
|
|
4126
|
-
let result = executeTryCatch(callbackFunction, handleError, context);
|
|
4127
|
-
// @ts-ignore
|
|
4128
|
-
return result !== undefined ? result : TryCatchCore;
|
|
4129
|
-
},
|
|
4130
|
-
};
|
|
4131
|
-
/**
|
|
4132
|
-
* 执行传入的函数并捕获其可能抛出的错误,并通过传入的错误处理函数进行处理。
|
|
4133
|
-
* @param callback - 待执行函数,可以是 function 或者 string 类型。如果是 string 类型,则会被当做代码进行执行。
|
|
4134
|
-
* @param handleErrorFunc - 错误处理函数,可以是 function 或者 string 类型。如果是 string 类型,则会被当做代码进行执行。
|
|
4135
|
-
* @param funcThis - 待执行函数的作用域,用于apply指定
|
|
4136
|
-
* @returns 如果函数有返回值,则返回该返回值;否则返回 undefined。
|
|
4137
|
-
*/
|
|
4138
|
-
function executeTryCatch(callback, handleErrorFunc, funcThis) {
|
|
4139
|
-
let result = undefined;
|
|
4140
|
-
try {
|
|
4141
|
-
if (typeof callback === "string") {
|
|
4142
|
-
(function () {
|
|
4143
|
-
eval(callback);
|
|
4144
|
-
}).apply(funcThis, args);
|
|
4145
|
-
}
|
|
4146
|
-
else {
|
|
4147
|
-
result = callback.apply(funcThis, args);
|
|
4148
|
-
}
|
|
4149
|
-
}
|
|
4150
|
-
catch (error) {
|
|
4151
|
-
if (defaultDetails.log) {
|
|
4152
|
-
callback = callback;
|
|
4153
|
-
console.log(`%c ${callback?.name ? callback?.name : callback + "出现错误"} `, "color: #f20000");
|
|
4154
|
-
console.log(`%c 错误原因:${error}`, "color: #f20000");
|
|
4155
|
-
console.trace(callback);
|
|
4156
|
-
}
|
|
4157
|
-
if (handleErrorFunc) {
|
|
4158
|
-
if (typeof handleErrorFunc === "string") {
|
|
4159
|
-
result = function () {
|
|
4160
|
-
return eval(handleErrorFunc);
|
|
4161
|
-
// @ts-ignore
|
|
4162
|
-
}.apply(funcThis, [...args, error]);
|
|
4163
|
-
}
|
|
4164
|
-
else {
|
|
4165
|
-
result = handleErrorFunc.apply(funcThis, [...args, error]);
|
|
4166
|
-
}
|
|
4167
|
-
}
|
|
4168
|
-
}
|
|
4169
|
-
return result;
|
|
4346
|
+
this.#ctx.fillStyle = this.#config.textColor;
|
|
4347
|
+
this.#ctx.fillText(txt, this.#width / 2 - w / 2, this.#height / 2 + h / 2);
|
|
4170
4348
|
}
|
|
4171
|
-
|
|
4172
|
-
};
|
|
4349
|
+
}
|
|
4173
4350
|
|
|
4174
4351
|
class UtilsDictionary {
|
|
4175
4352
|
items = {};
|
|
@@ -4286,7 +4463,7 @@ var Utils = (function () {
|
|
|
4286
4463
|
* @param data 需要合并的字典
|
|
4287
4464
|
*/
|
|
4288
4465
|
concat(data) {
|
|
4289
|
-
this.items =
|
|
4466
|
+
this.items = commonUtil.assign(this.items, data.getItems());
|
|
4290
4467
|
}
|
|
4291
4468
|
forEach(callbackfn) {
|
|
4292
4469
|
for (const key in this.getItems()) {
|
|
@@ -4845,7 +5022,7 @@ var Utils = (function () {
|
|
|
4845
5022
|
|
|
4846
5023
|
const loadOrReturnBroker = createLoadOrReturnBroker(load, worker);
|
|
4847
5024
|
const clearInterval = (timerId) => loadOrReturnBroker().clearInterval(timerId);
|
|
4848
|
-
const clearTimeout = (timerId) => loadOrReturnBroker().clearTimeout(timerId);
|
|
5025
|
+
const clearTimeout$1 = (timerId) => loadOrReturnBroker().clearTimeout(timerId);
|
|
4849
5026
|
const setInterval = (...args) => loadOrReturnBroker().setInterval(...args);
|
|
4850
5027
|
const setTimeout$1 = (...args) => loadOrReturnBroker().setTimeout(...args);
|
|
4851
5028
|
|
|
@@ -5247,13 +5424,194 @@ var Utils = (function () {
|
|
|
5247
5424
|
}
|
|
5248
5425
|
}
|
|
5249
5426
|
|
|
5427
|
+
class DOMUtils {
|
|
5428
|
+
windowApi;
|
|
5429
|
+
constructor(option) {
|
|
5430
|
+
this.windowApi = new WindowApi(option);
|
|
5431
|
+
}
|
|
5432
|
+
selector(selector, parent) {
|
|
5433
|
+
return this.selectorAll(selector, parent)[0];
|
|
5434
|
+
}
|
|
5435
|
+
selectorAll(selector, parent) {
|
|
5436
|
+
const context = this;
|
|
5437
|
+
parent = parent || context.windowApi.document;
|
|
5438
|
+
selector = selector.trim();
|
|
5439
|
+
if (selector.match(/[^\s]{1}:empty$/gi)) {
|
|
5440
|
+
// empty 语法
|
|
5441
|
+
selector = selector.replace(/:empty$/gi, "");
|
|
5442
|
+
return Array.from(parent.querySelectorAll(selector)).filter(($ele) => {
|
|
5443
|
+
return $ele?.innerHTML?.trim() === "";
|
|
5444
|
+
});
|
|
5445
|
+
}
|
|
5446
|
+
else if (selector.match(/[^\s]{1}:contains\("(.*)"\)$/i) ||
|
|
5447
|
+
selector.match(/[^\s]{1}:contains\('(.*)'\)$/i)) {
|
|
5448
|
+
// contains 语法
|
|
5449
|
+
let textMatch = selector.match(/:contains\(("|')(.*)("|')\)$/i);
|
|
5450
|
+
let text = textMatch[2];
|
|
5451
|
+
selector = selector.replace(/:contains\(("|')(.*)("|')\)$/gi, "");
|
|
5452
|
+
return Array.from(parent.querySelectorAll(selector)).filter(($ele) => {
|
|
5453
|
+
// @ts-ignore
|
|
5454
|
+
return ($ele?.textContent || $ele?.innerText)?.includes(text);
|
|
5455
|
+
});
|
|
5456
|
+
}
|
|
5457
|
+
else if (selector.match(/[^\s]{1}:regexp\("(.*)"\)$/i) ||
|
|
5458
|
+
selector.match(/[^\s]{1}:regexp\('(.*)'\)$/i)) {
|
|
5459
|
+
// regexp 语法
|
|
5460
|
+
let textMatch = selector.match(/:regexp\(("|')(.*)("|')\)$/i);
|
|
5461
|
+
let pattern = textMatch[2];
|
|
5462
|
+
let flagMatch = pattern.match(/("|'),[\s]*("|')([igm]{0,3})$/i);
|
|
5463
|
+
let flags = "";
|
|
5464
|
+
if (flagMatch) {
|
|
5465
|
+
pattern = pattern.replace(/("|'),[\s]*("|')([igm]{0,3})$/gi, "");
|
|
5466
|
+
flags = flagMatch[3];
|
|
5467
|
+
}
|
|
5468
|
+
let regexp = new RegExp(pattern, flags);
|
|
5469
|
+
selector = selector.replace(/:regexp\(("|')(.*)("|')\)$/gi, "");
|
|
5470
|
+
return Array.from(parent.querySelectorAll(selector)).filter(($ele) => {
|
|
5471
|
+
// @ts-ignore
|
|
5472
|
+
return Boolean(($ele?.textContent || $ele?.innerText)?.match(regexp));
|
|
5473
|
+
});
|
|
5474
|
+
}
|
|
5475
|
+
else {
|
|
5476
|
+
// 普通语法
|
|
5477
|
+
return Array.from(parent.querySelectorAll(selector));
|
|
5478
|
+
}
|
|
5479
|
+
}
|
|
5480
|
+
/**
|
|
5481
|
+
* 匹配元素,可使用以下的额外语法
|
|
5482
|
+
*
|
|
5483
|
+
* + :contains([text]) 作用: 找到包含指定文本内容的指定元素
|
|
5484
|
+
* + :empty 作用:找到既没有文本内容也没有子元素的指定元素
|
|
5485
|
+
* + :regexp([text]) 作用: 找到符合正则表达式的内容的指定元素
|
|
5486
|
+
* @param $el 元素
|
|
5487
|
+
* @param selector 选择器
|
|
5488
|
+
* @example
|
|
5489
|
+
* DOMUtils.matches("div:contains('测试')")
|
|
5490
|
+
* > true
|
|
5491
|
+
* @example
|
|
5492
|
+
* DOMUtils.matches("div:empty")
|
|
5493
|
+
* > true
|
|
5494
|
+
* @example
|
|
5495
|
+
* DOMUtils.matches("div:regexp('^xxxx$')")
|
|
5496
|
+
* > true
|
|
5497
|
+
* @example
|
|
5498
|
+
* DOMUtils.matches("div:regexp(/^xxx/ig)")
|
|
5499
|
+
* > false
|
|
5500
|
+
*/
|
|
5501
|
+
matches($el, selector) {
|
|
5502
|
+
selector = selector.trim();
|
|
5503
|
+
if ($el == null) {
|
|
5504
|
+
return false;
|
|
5505
|
+
}
|
|
5506
|
+
if (selector.match(/[^\s]{1}:empty$/gi)) {
|
|
5507
|
+
// empty 语法
|
|
5508
|
+
selector = selector.replace(/:empty$/gi, "");
|
|
5509
|
+
return $el.matches(selector) && $el?.innerHTML?.trim() === "";
|
|
5510
|
+
}
|
|
5511
|
+
else if (selector.match(/[^\s]{1}:contains\("(.*)"\)$/i) ||
|
|
5512
|
+
selector.match(/[^\s]{1}:contains\('(.*)'\)$/i)) {
|
|
5513
|
+
// contains 语法
|
|
5514
|
+
let textMatch = selector.match(/:contains\(("|')(.*)("|')\)$/i);
|
|
5515
|
+
let text = textMatch[2];
|
|
5516
|
+
selector = selector.replace(/:contains\(("|')(.*)("|')\)$/gi, "");
|
|
5517
|
+
// @ts-ignore
|
|
5518
|
+
let content = $el?.textContent || $el?.innerText;
|
|
5519
|
+
if (typeof content !== "string") {
|
|
5520
|
+
content = "";
|
|
5521
|
+
}
|
|
5522
|
+
return $el.matches(selector) && content?.includes(text);
|
|
5523
|
+
}
|
|
5524
|
+
else if (selector.match(/[^\s]{1}:regexp\("(.*)"\)$/i) ||
|
|
5525
|
+
selector.match(/[^\s]{1}:regexp\('(.*)'\)$/i)) {
|
|
5526
|
+
// regexp 语法
|
|
5527
|
+
let textMatch = selector.match(/:regexp\(("|')(.*)("|')\)$/i);
|
|
5528
|
+
let pattern = textMatch[2];
|
|
5529
|
+
let flagMatch = pattern.match(/("|'),[\s]*("|')([igm]{0,3})$/i);
|
|
5530
|
+
let flags = "";
|
|
5531
|
+
if (flagMatch) {
|
|
5532
|
+
pattern = pattern.replace(/("|'),[\s]*("|')([igm]{0,3})$/gi, "");
|
|
5533
|
+
flags = flagMatch[3];
|
|
5534
|
+
}
|
|
5535
|
+
let regexp = new RegExp(pattern, flags);
|
|
5536
|
+
selector = selector.replace(/:regexp\(("|')(.*)("|')\)$/gi, "");
|
|
5537
|
+
// @ts-ignore
|
|
5538
|
+
let content = $el?.textContent || $el?.innerText;
|
|
5539
|
+
if (typeof content !== "string") {
|
|
5540
|
+
content = "";
|
|
5541
|
+
}
|
|
5542
|
+
return $el.matches(selector) && Boolean(content?.match(regexp));
|
|
5543
|
+
}
|
|
5544
|
+
else {
|
|
5545
|
+
// 普通语法
|
|
5546
|
+
return $el.matches(selector);
|
|
5547
|
+
}
|
|
5548
|
+
}
|
|
5549
|
+
closest($el, selector) {
|
|
5550
|
+
selector = selector.trim();
|
|
5551
|
+
if (selector.match(/[^\s]{1}:empty$/gi)) {
|
|
5552
|
+
// empty 语法
|
|
5553
|
+
selector = selector.replace(/:empty$/gi, "");
|
|
5554
|
+
let $closest = $el?.closest(selector);
|
|
5555
|
+
if ($closest && $closest?.innerHTML?.trim() === "") {
|
|
5556
|
+
return $closest;
|
|
5557
|
+
}
|
|
5558
|
+
return null;
|
|
5559
|
+
}
|
|
5560
|
+
else if (selector.match(/[^\s]{1}:contains\("(.*)"\)$/i) ||
|
|
5561
|
+
selector.match(/[^\s]{1}:contains\('(.*)'\)$/i)) {
|
|
5562
|
+
// contains 语法
|
|
5563
|
+
let textMatch = selector.match(/:contains\(("|')(.*)("|')\)$/i);
|
|
5564
|
+
let text = textMatch[2];
|
|
5565
|
+
selector = selector.replace(/:contains\(("|')(.*)("|')\)$/gi, "");
|
|
5566
|
+
let $closest = $el?.closest(selector);
|
|
5567
|
+
if ($closest) {
|
|
5568
|
+
// @ts-ignore
|
|
5569
|
+
let content = $el?.textContent || $el?.innerText;
|
|
5570
|
+
if (typeof content === "string" && content.includes(text)) {
|
|
5571
|
+
return $closest;
|
|
5572
|
+
}
|
|
5573
|
+
}
|
|
5574
|
+
return null;
|
|
5575
|
+
}
|
|
5576
|
+
else if (selector.match(/[^\s]{1}:regexp\("(.*)"\)$/i) ||
|
|
5577
|
+
selector.match(/[^\s]{1}:regexp\('(.*)'\)$/i)) {
|
|
5578
|
+
// regexp 语法
|
|
5579
|
+
let textMatch = selector.match(/:regexp\(("|')(.*)("|')\)$/i);
|
|
5580
|
+
let pattern = textMatch[2];
|
|
5581
|
+
let flagMatch = pattern.match(/("|'),[\s]*("|')([igm]{0,3})$/i);
|
|
5582
|
+
let flags = "";
|
|
5583
|
+
if (flagMatch) {
|
|
5584
|
+
pattern = pattern.replace(/("|'),[\s]*("|')([igm]{0,3})$/gi, "");
|
|
5585
|
+
flags = flagMatch[3];
|
|
5586
|
+
}
|
|
5587
|
+
let regexp = new RegExp(pattern, flags);
|
|
5588
|
+
selector = selector.replace(/:regexp\(("|')(.*)("|')\)$/gi, "");
|
|
5589
|
+
let $closest = $el?.closest(selector);
|
|
5590
|
+
if ($closest) {
|
|
5591
|
+
// @ts-ignore
|
|
5592
|
+
let content = $el?.textContent || $el?.innerText;
|
|
5593
|
+
if (typeof content === "string" && content.match(regexp)) {
|
|
5594
|
+
return $closest;
|
|
5595
|
+
}
|
|
5596
|
+
}
|
|
5597
|
+
return null;
|
|
5598
|
+
}
|
|
5599
|
+
else {
|
|
5600
|
+
// 普通语法
|
|
5601
|
+
let $closest = $el?.closest(selector);
|
|
5602
|
+
return $closest;
|
|
5603
|
+
}
|
|
5604
|
+
}
|
|
5605
|
+
}
|
|
5606
|
+
let domUtils = new DOMUtils();
|
|
5607
|
+
|
|
5250
5608
|
class Utils {
|
|
5251
5609
|
windowApi;
|
|
5252
5610
|
constructor(option) {
|
|
5253
5611
|
this.windowApi = new WindowApi(option);
|
|
5254
5612
|
}
|
|
5255
5613
|
/** 版本号 */
|
|
5256
|
-
version = "2025.
|
|
5614
|
+
version = "2025.6.7";
|
|
5257
5615
|
addStyle(cssText) {
|
|
5258
5616
|
if (typeof cssText !== "string") {
|
|
5259
5617
|
throw new Error("Utils.addStyle 参数cssText 必须为String类型");
|
|
@@ -5279,58 +5637,22 @@ var Utils = (function () {
|
|
|
5279
5637
|
}
|
|
5280
5638
|
return cssNode;
|
|
5281
5639
|
}
|
|
5282
|
-
|
|
5283
|
-
|
|
5284
|
-
|
|
5285
|
-
|
|
5286
|
-
|
|
5287
|
-
|
|
5288
|
-
|
|
5289
|
-
|
|
5290
|
-
|
|
5291
|
-
|
|
5292
|
-
|
|
5293
|
-
|
|
5294
|
-
}
|
|
5295
|
-
if (target == null) {
|
|
5296
|
-
target = {};
|
|
5297
|
-
}
|
|
5298
|
-
if (isAdd) {
|
|
5299
|
-
for (const sourceKeyName in source) {
|
|
5300
|
-
const targetKeyName = sourceKeyName;
|
|
5301
|
-
let targetValue = target[targetKeyName];
|
|
5302
|
-
let sourceValue = source[sourceKeyName];
|
|
5303
|
-
if (typeof sourceValue === "object" &&
|
|
5304
|
-
sourceValue != null &&
|
|
5305
|
-
sourceKeyName in target &&
|
|
5306
|
-
!UtilsContext.isDOM(sourceValue)) {
|
|
5307
|
-
/* 源端的值是object类型,且不是元素节点 */
|
|
5308
|
-
target[sourceKeyName] = UtilsContext.assign(targetValue, sourceValue, isAdd);
|
|
5309
|
-
continue;
|
|
5310
|
-
}
|
|
5311
|
-
target[sourceKeyName] = sourceValue;
|
|
5312
|
-
}
|
|
5313
|
-
}
|
|
5314
|
-
else {
|
|
5315
|
-
for (const targetKeyName in target) {
|
|
5316
|
-
if (targetKeyName in source) {
|
|
5317
|
-
let targetValue = target[targetKeyName];
|
|
5318
|
-
let sourceValue = source[targetKeyName];
|
|
5319
|
-
if (typeof sourceValue === "object" &&
|
|
5320
|
-
sourceValue != null &&
|
|
5321
|
-
!UtilsContext.isDOM(sourceValue) &&
|
|
5322
|
-
Object.keys(sourceValue).length) {
|
|
5323
|
-
/* 源端的值是object类型,且不是元素节点 */
|
|
5324
|
-
target[targetKeyName] = UtilsContext.assign(targetValue, sourceValue, isAdd);
|
|
5325
|
-
continue;
|
|
5326
|
-
}
|
|
5327
|
-
/* 直接赋值 */
|
|
5328
|
-
target[targetKeyName] = sourceValue;
|
|
5329
|
-
}
|
|
5640
|
+
/**
|
|
5641
|
+
* JSON数据从源端替换到目标端中,如果目标端存在该数据则替换,不添加,返回结果为目标端替换完毕的结果
|
|
5642
|
+
* @param target 目标数据
|
|
5643
|
+
* @param source 源数据
|
|
5644
|
+
* @param isAdd 是否可以追加键,默认false
|
|
5645
|
+
* @example
|
|
5646
|
+
* Utils.assign({"1":1,"2":{"3":3}}, {"2":{"3":4}});
|
|
5647
|
+
* >
|
|
5648
|
+
* {
|
|
5649
|
+
"1": 1,
|
|
5650
|
+
"2": {
|
|
5651
|
+
"3": 4
|
|
5330
5652
|
}
|
|
5331
5653
|
}
|
|
5332
|
-
|
|
5333
|
-
|
|
5654
|
+
*/
|
|
5655
|
+
assign = commonUtil.assign.bind(commonUtil);
|
|
5334
5656
|
async asyncReplaceAll(string, pattern, asyncFn) {
|
|
5335
5657
|
let UtilsContext = this;
|
|
5336
5658
|
if (typeof string !== "string") {
|
|
@@ -5483,19 +5805,11 @@ var Utils = (function () {
|
|
|
5483
5805
|
* @returns
|
|
5484
5806
|
*/
|
|
5485
5807
|
ColorConversion = ColorConversion;
|
|
5486
|
-
|
|
5487
|
-
|
|
5488
|
-
|
|
5489
|
-
|
|
5490
|
-
|
|
5491
|
-
return null;
|
|
5492
|
-
let clone = obj instanceof Array ? [] : {};
|
|
5493
|
-
for (const [key, value] of Object.entries(obj)) {
|
|
5494
|
-
clone[key] =
|
|
5495
|
-
typeof value === "object" ? UtilsContext.deepClone(value) : value;
|
|
5496
|
-
}
|
|
5497
|
-
return clone;
|
|
5498
|
-
}
|
|
5808
|
+
/**
|
|
5809
|
+
* 深拷贝
|
|
5810
|
+
* @param obj 对象
|
|
5811
|
+
*/
|
|
5812
|
+
deepClone = commonUtil.deepClone.bind(commonUtil);
|
|
5499
5813
|
debounce(fn, delay = 0) {
|
|
5500
5814
|
let timer = null;
|
|
5501
5815
|
let UtilsContext = this;
|
|
@@ -5518,7 +5832,7 @@ var Utils = (function () {
|
|
|
5518
5832
|
throw new Error("Utils.deleteParentNode 参数 targetSelector 必须为 string 类型");
|
|
5519
5833
|
}
|
|
5520
5834
|
let result = false;
|
|
5521
|
-
let needRemoveDOM =
|
|
5835
|
+
let needRemoveDOM = domUtils.closest(element, targetSelector);
|
|
5522
5836
|
if (needRemoveDOM) {
|
|
5523
5837
|
needRemoveDOM.remove();
|
|
5524
5838
|
result = true;
|
|
@@ -6506,9 +6820,17 @@ var Utils = (function () {
|
|
|
6506
6820
|
throw new TypeError("参数1类型错误" + typeof firstArg);
|
|
6507
6821
|
}
|
|
6508
6822
|
}
|
|
6509
|
-
|
|
6510
|
-
|
|
6511
|
-
|
|
6823
|
+
/**
|
|
6824
|
+
* 判断对象是否是元素
|
|
6825
|
+
* @param target
|
|
6826
|
+
* @returns
|
|
6827
|
+
* + true 是元素
|
|
6828
|
+
* + false 不是元素
|
|
6829
|
+
* @example
|
|
6830
|
+
* Utils.isDOM(document.querySelector("a"))
|
|
6831
|
+
* > true
|
|
6832
|
+
*/
|
|
6833
|
+
isDOM = commonUtil.isDOM.bind(commonUtil);
|
|
6512
6834
|
isFullscreenEnabled() {
|
|
6513
6835
|
return !!(this.windowApi.document.fullscreenEnabled ||
|
|
6514
6836
|
this.windowApi.document.webkitFullScreenEnabled ||
|
|
@@ -6699,52 +7021,62 @@ var Utils = (function () {
|
|
|
6699
7021
|
}
|
|
6700
7022
|
return result;
|
|
6701
7023
|
}
|
|
6702
|
-
|
|
6703
|
-
|
|
6704
|
-
|
|
6705
|
-
|
|
6706
|
-
|
|
6707
|
-
|
|
6708
|
-
|
|
6709
|
-
|
|
6710
|
-
|
|
6711
|
-
|
|
6712
|
-
|
|
6713
|
-
|
|
6714
|
-
|
|
6715
|
-
|
|
6716
|
-
|
|
6717
|
-
|
|
6718
|
-
|
|
6719
|
-
|
|
6720
|
-
|
|
6721
|
-
|
|
6722
|
-
|
|
6723
|
-
|
|
6724
|
-
|
|
6725
|
-
|
|
6726
|
-
|
|
6727
|
-
|
|
6728
|
-
|
|
6729
|
-
|
|
6730
|
-
|
|
6731
|
-
|
|
6732
|
-
|
|
6733
|
-
|
|
6734
|
-
|
|
6735
|
-
|
|
6736
|
-
|
|
6737
|
-
|
|
6738
|
-
|
|
6739
|
-
|
|
6740
|
-
|
|
6741
|
-
|
|
6742
|
-
|
|
6743
|
-
|
|
6744
|
-
|
|
6745
|
-
|
|
6746
|
-
|
|
6747
|
-
|
|
7024
|
+
/**
|
|
7025
|
+
* 判断对象是否不为空
|
|
7026
|
+
* @returns {boolean}
|
|
7027
|
+
* + true 不为空
|
|
7028
|
+
* + false 为空
|
|
7029
|
+
* @example
|
|
7030
|
+
* Utils.isNotNull("123");
|
|
7031
|
+
* > true
|
|
7032
|
+
*/
|
|
7033
|
+
isNotNull = commonUtil.isNotNull.bind(commonUtil);
|
|
7034
|
+
/**
|
|
7035
|
+
* 判断对象或数据是否为空
|
|
7036
|
+
* + `String`判空的值,如 ""、"null"、"undefined"、" "
|
|
7037
|
+
* + `Number`判空的值,如 0
|
|
7038
|
+
* + `Object`判空的值,如 {}、null、undefined
|
|
7039
|
+
* + `Array`(存在属性Symbol.iterator)判空的值,如 []
|
|
7040
|
+
* + `Boolean`判空的值,如false
|
|
7041
|
+
* + `Function`判空的值,如()=>{}、(xxx="")=>{}、function(){}、function(xxx=""){}
|
|
7042
|
+
* @returns
|
|
7043
|
+
* + true 为空
|
|
7044
|
+
* + false 不为空
|
|
7045
|
+
* @example
|
|
7046
|
+
Utils.isNull({});
|
|
7047
|
+
> true
|
|
7048
|
+
* @example
|
|
7049
|
+
Utils.isNull([]);
|
|
7050
|
+
> true
|
|
7051
|
+
* @example
|
|
7052
|
+
Utils.isNull(" ");
|
|
7053
|
+
> true
|
|
7054
|
+
* @example
|
|
7055
|
+
Utils.isNull(function(){});
|
|
7056
|
+
> true
|
|
7057
|
+
* @example
|
|
7058
|
+
Utils.isNull(()=>{}));
|
|
7059
|
+
> true
|
|
7060
|
+
* @example
|
|
7061
|
+
Utils.isNull("undefined");
|
|
7062
|
+
> true
|
|
7063
|
+
* @example
|
|
7064
|
+
Utils.isNull("null");
|
|
7065
|
+
> true
|
|
7066
|
+
* @example
|
|
7067
|
+
Utils.isNull(" ", false);
|
|
7068
|
+
> true
|
|
7069
|
+
* @example
|
|
7070
|
+
Utils.isNull([1],[]);
|
|
7071
|
+
> false
|
|
7072
|
+
* @example
|
|
7073
|
+
Utils.isNull([],[1]);
|
|
7074
|
+
> false
|
|
7075
|
+
* @example
|
|
7076
|
+
Utils.isNull(false,[123]);
|
|
7077
|
+
> false
|
|
7078
|
+
**/
|
|
7079
|
+
isNull = commonUtil.isNull.bind(commonUtil);
|
|
6748
7080
|
isThemeDark() {
|
|
6749
7081
|
return this.windowApi.globalThis.matchMedia("(prefers-color-scheme: dark)")
|
|
6750
7082
|
.matches;
|
|
@@ -7553,7 +7885,7 @@ var Utils = (function () {
|
|
|
7553
7885
|
return mouseEvent;
|
|
7554
7886
|
}
|
|
7555
7887
|
let sliderElement = typeof selector === "string"
|
|
7556
|
-
?
|
|
7888
|
+
? domUtils.selector(selector)
|
|
7557
7889
|
: selector;
|
|
7558
7890
|
if (!(sliderElement instanceof Node) ||
|
|
7559
7891
|
!(sliderElement instanceof Element)) {
|
|
@@ -7763,47 +8095,15 @@ var Utils = (function () {
|
|
|
7763
8095
|
}
|
|
7764
8096
|
return newTargetString;
|
|
7765
8097
|
}
|
|
7766
|
-
|
|
7767
|
-
|
|
7768
|
-
|
|
7769
|
-
|
|
7770
|
-
|
|
7771
|
-
|
|
7772
|
-
|
|
7773
|
-
|
|
7774
|
-
|
|
7775
|
-
UtilsContext.tryCatch()
|
|
7776
|
-
.error(() => {
|
|
7777
|
-
try {
|
|
7778
|
-
result = UtilsContext.windowApi.window.eval("(" + data + ")");
|
|
7779
|
-
}
|
|
7780
|
-
catch (error2) {
|
|
7781
|
-
if (typeof errorCallBack === "function") {
|
|
7782
|
-
errorCallBack(error2);
|
|
7783
|
-
}
|
|
7784
|
-
}
|
|
7785
|
-
})
|
|
7786
|
-
.run(() => {
|
|
7787
|
-
if (data &&
|
|
7788
|
-
/^[\],:{}\s]*$/.test(data
|
|
7789
|
-
.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g, "@")
|
|
7790
|
-
.replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, "]")
|
|
7791
|
-
.replace(/(?:^|:|,)(?:\s*\[)+/g, ""))) {
|
|
7792
|
-
result = new Function("return " + data)();
|
|
7793
|
-
}
|
|
7794
|
-
else {
|
|
7795
|
-
if (typeof errorCallBack === "function") {
|
|
7796
|
-
errorCallBack(new Error("target is not a JSON"));
|
|
7797
|
-
}
|
|
7798
|
-
}
|
|
7799
|
-
});
|
|
7800
|
-
})
|
|
7801
|
-
.run(() => {
|
|
7802
|
-
data = data.trim();
|
|
7803
|
-
result = JSON.parse(data);
|
|
7804
|
-
});
|
|
7805
|
-
return result;
|
|
7806
|
-
}
|
|
8098
|
+
/**
|
|
8099
|
+
* 字符串转Object对象,类似'{"test":""}' => {"test":""}
|
|
8100
|
+
* @param data
|
|
8101
|
+
* @param errorCallBack (可选)错误回调
|
|
8102
|
+
* @example
|
|
8103
|
+
* Utils.toJSON("{123:123}")
|
|
8104
|
+
* > {123:123}
|
|
8105
|
+
*/
|
|
8106
|
+
toJSON = commonUtil.toJSON.bind(commonUtil);
|
|
7807
8107
|
toSearchParamsStr(obj, addPrefix) {
|
|
7808
8108
|
let UtilsContext = this;
|
|
7809
8109
|
let searhParamsStr = "";
|
|
@@ -7969,7 +8269,7 @@ var Utils = (function () {
|
|
|
7969
8269
|
if (Array.isArray(selector)) {
|
|
7970
8270
|
let result = [];
|
|
7971
8271
|
for (let index = 0; index < selector.length; index++) {
|
|
7972
|
-
let node =
|
|
8272
|
+
let node = domUtils.selector(selector[index]);
|
|
7973
8273
|
if (node) {
|
|
7974
8274
|
result.push(node);
|
|
7975
8275
|
}
|
|
@@ -7982,7 +8282,7 @@ var Utils = (function () {
|
|
|
7982
8282
|
return selector();
|
|
7983
8283
|
}
|
|
7984
8284
|
else {
|
|
7985
|
-
return
|
|
8285
|
+
return domUtils.selector(selector, parent);
|
|
7986
8286
|
}
|
|
7987
8287
|
}
|
|
7988
8288
|
return UtilsContext.wait(() => {
|
|
@@ -8112,7 +8412,7 @@ var Utils = (function () {
|
|
|
8112
8412
|
if (Array.isArray(selector)) {
|
|
8113
8413
|
let result = [];
|
|
8114
8414
|
for (let index = 0; index < selector.length; index++) {
|
|
8115
|
-
let nodeList =
|
|
8415
|
+
let nodeList = domUtils.selectorAll(selector[index], parent);
|
|
8116
8416
|
if (nodeList.length) {
|
|
8117
8417
|
result.push(nodeList);
|
|
8118
8418
|
}
|
|
@@ -8122,7 +8422,7 @@ var Utils = (function () {
|
|
|
8122
8422
|
}
|
|
8123
8423
|
}
|
|
8124
8424
|
else {
|
|
8125
|
-
let nodeList =
|
|
8425
|
+
let nodeList = domUtils.selectorAll(selector, parent);
|
|
8126
8426
|
if (nodeList.length) {
|
|
8127
8427
|
return nodeList;
|
|
8128
8428
|
}
|
|
@@ -8431,17 +8731,7 @@ var Utils = (function () {
|
|
|
8431
8731
|
* @param target 需要覆盖的对象
|
|
8432
8732
|
* @param [objectThis] 覆盖的this指向,如果为传入,则默认为对象本身
|
|
8433
8733
|
*/
|
|
8434
|
-
coverObjectFunctionThis(
|
|
8435
|
-
if (typeof target !== "object" || target === null) {
|
|
8436
|
-
throw new Error("target must be object");
|
|
8437
|
-
}
|
|
8438
|
-
objectThis = objectThis || target;
|
|
8439
|
-
Object.keys(target).forEach((key) => {
|
|
8440
|
-
if (typeof target[key] === "function") {
|
|
8441
|
-
target[key] = target[key].bind(objectThis);
|
|
8442
|
-
}
|
|
8443
|
-
});
|
|
8444
|
-
}
|
|
8734
|
+
coverObjectFunctionThis = commonUtil.coverObjectFunctionThis.bind(commonUtil);
|
|
8445
8735
|
/**
|
|
8446
8736
|
* 生成uuid
|
|
8447
8737
|
* @example
|
|
@@ -8482,7 +8772,7 @@ var Utils = (function () {
|
|
|
8482
8772
|
workerClearTimeout(timeId) {
|
|
8483
8773
|
try {
|
|
8484
8774
|
if (timeId != null) {
|
|
8485
|
-
clearTimeout(timeId);
|
|
8775
|
+
clearTimeout$1(timeId);
|
|
8486
8776
|
}
|
|
8487
8777
|
}
|
|
8488
8778
|
catch (error) {
|