@whitesev/utils 2.6.7 → 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 +655 -297
- package/dist/index.amd.js.map +1 -1
- package/dist/index.cjs.js +655 -297
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.esm.js +655 -297
- package/dist/index.esm.js.map +1 -1
- package/dist/index.iife.js +655 -297
- package/dist/index.iife.js.map +1 -1
- package/dist/index.system.js +655 -297
- package/dist/index.system.js.map +1 -1
- package/dist/index.umd.js +655 -297
- 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 +26 -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 +100 -213
- package/src/UtilsGMCookie.ts +7 -7
- package/src/UtilsGMMenu.ts +2 -2
package/dist/index.umd.js
CHANGED
|
@@ -230,6 +230,273 @@
|
|
|
230
230
|
}
|
|
231
231
|
}
|
|
232
232
|
|
|
233
|
+
const TryCatch = function (...args) {
|
|
234
|
+
/* 定义变量和函数 */
|
|
235
|
+
let callbackFunction = null;
|
|
236
|
+
let context = null;
|
|
237
|
+
let handleError = (error) => { };
|
|
238
|
+
let defaultDetails = {
|
|
239
|
+
log: true,
|
|
240
|
+
};
|
|
241
|
+
const TryCatchCore = {
|
|
242
|
+
/**
|
|
243
|
+
*
|
|
244
|
+
* @param paramDetails 配置
|
|
245
|
+
* @returns
|
|
246
|
+
*/
|
|
247
|
+
config(paramDetails) {
|
|
248
|
+
defaultDetails = Object.assign(defaultDetails, paramDetails);
|
|
249
|
+
return TryCatchCore;
|
|
250
|
+
},
|
|
251
|
+
/**
|
|
252
|
+
* 处理错误
|
|
253
|
+
* @param handler
|
|
254
|
+
*/
|
|
255
|
+
error(handler) {
|
|
256
|
+
// @ts-ignore
|
|
257
|
+
handleError = handler;
|
|
258
|
+
return TryCatchCore;
|
|
259
|
+
},
|
|
260
|
+
/**
|
|
261
|
+
* 执行传入的函数并捕获其可能抛出的错误,并通过传入的错误处理函数进行处理。
|
|
262
|
+
* @param callback 待执行函数,可以是 function 或者 string 类型。如果是 string 类型,则会被当做代码进行执行。
|
|
263
|
+
* @param __context__ 待执行函数的作用域,用于apply指定
|
|
264
|
+
* @returns 如果函数有返回值,则返回该返回值;否则返回 tryCatchObj 函数以支持链式调用。
|
|
265
|
+
* @throws {Error} 如果传入参数不符合要求,则会抛出相应类型的错误。
|
|
266
|
+
*/
|
|
267
|
+
run(callback, __context__) {
|
|
268
|
+
callbackFunction = callback;
|
|
269
|
+
context = __context__ || this;
|
|
270
|
+
let result = executeTryCatch(callbackFunction, handleError, context);
|
|
271
|
+
// @ts-ignore
|
|
272
|
+
return result !== undefined ? result : TryCatchCore;
|
|
273
|
+
},
|
|
274
|
+
};
|
|
275
|
+
/**
|
|
276
|
+
* 执行传入的函数并捕获其可能抛出的错误,并通过传入的错误处理函数进行处理。
|
|
277
|
+
* @param callback - 待执行函数,可以是 function 或者 string 类型。如果是 string 类型,则会被当做代码进行执行。
|
|
278
|
+
* @param handleErrorFunc - 错误处理函数,可以是 function 或者 string 类型。如果是 string 类型,则会被当做代码进行执行。
|
|
279
|
+
* @param funcThis - 待执行函数的作用域,用于apply指定
|
|
280
|
+
* @returns 如果函数有返回值,则返回该返回值;否则返回 undefined。
|
|
281
|
+
*/
|
|
282
|
+
function executeTryCatch(callback, handleErrorFunc, funcThis) {
|
|
283
|
+
let result = undefined;
|
|
284
|
+
try {
|
|
285
|
+
if (typeof callback === "string") {
|
|
286
|
+
result = new Function(callback).apply(funcThis, args);
|
|
287
|
+
}
|
|
288
|
+
else {
|
|
289
|
+
result = callback.apply(funcThis, args);
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
catch (error) {
|
|
293
|
+
if (defaultDetails.log) {
|
|
294
|
+
callback = callback;
|
|
295
|
+
console.log(`%c ${callback?.name ? callback?.name : callback + "出现错误"} `, "color: #f20000");
|
|
296
|
+
console.log(`%c 错误原因:${error}`, "color: #f20000");
|
|
297
|
+
console.trace(callback);
|
|
298
|
+
}
|
|
299
|
+
if (handleErrorFunc) {
|
|
300
|
+
if (typeof handleErrorFunc === "string") {
|
|
301
|
+
result = new Function(handleErrorFunc).apply(funcThis, [
|
|
302
|
+
...args,
|
|
303
|
+
error,
|
|
304
|
+
]);
|
|
305
|
+
}
|
|
306
|
+
else {
|
|
307
|
+
result = handleErrorFunc.apply(funcThis, [...args, error]);
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
return result;
|
|
312
|
+
}
|
|
313
|
+
return TryCatchCore;
|
|
314
|
+
};
|
|
315
|
+
|
|
316
|
+
class CommonUtil {
|
|
317
|
+
assign(target = {}, source = {}, isAdd = false) {
|
|
318
|
+
let UtilsContext = this;
|
|
319
|
+
if (Array.isArray(source)) {
|
|
320
|
+
let canTraverse = source.filter((item) => {
|
|
321
|
+
return typeof item === "object";
|
|
322
|
+
});
|
|
323
|
+
if (!canTraverse.length) {
|
|
324
|
+
return source;
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
if (source == null) {
|
|
328
|
+
return target;
|
|
329
|
+
}
|
|
330
|
+
if (target == null) {
|
|
331
|
+
target = {};
|
|
332
|
+
}
|
|
333
|
+
if (isAdd) {
|
|
334
|
+
for (const sourceKeyName in source) {
|
|
335
|
+
const targetKeyName = sourceKeyName;
|
|
336
|
+
let targetValue = target[targetKeyName];
|
|
337
|
+
let sourceValue = source[sourceKeyName];
|
|
338
|
+
if (typeof sourceValue === "object" &&
|
|
339
|
+
sourceValue != null &&
|
|
340
|
+
sourceKeyName in target &&
|
|
341
|
+
!UtilsContext.isDOM(sourceValue)) {
|
|
342
|
+
/* 源端的值是object类型,且不是元素节点 */
|
|
343
|
+
target[sourceKeyName] = UtilsContext.assign(targetValue, sourceValue, isAdd);
|
|
344
|
+
continue;
|
|
345
|
+
}
|
|
346
|
+
target[sourceKeyName] = sourceValue;
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
else {
|
|
350
|
+
for (const targetKeyName in target) {
|
|
351
|
+
if (targetKeyName in source) {
|
|
352
|
+
let targetValue = target[targetKeyName];
|
|
353
|
+
let sourceValue = source[targetKeyName];
|
|
354
|
+
if (typeof sourceValue === "object" &&
|
|
355
|
+
sourceValue != null &&
|
|
356
|
+
!UtilsContext.isDOM(sourceValue) &&
|
|
357
|
+
Object.keys(sourceValue).length) {
|
|
358
|
+
/* 源端的值是object类型,且不是元素节点 */
|
|
359
|
+
target[targetKeyName] = UtilsContext.assign(targetValue, sourceValue, isAdd);
|
|
360
|
+
continue;
|
|
361
|
+
}
|
|
362
|
+
/* 直接赋值 */
|
|
363
|
+
target[targetKeyName] = sourceValue;
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
return target;
|
|
368
|
+
}
|
|
369
|
+
isNull(...args) {
|
|
370
|
+
let result = true;
|
|
371
|
+
let checkList = [...args];
|
|
372
|
+
for (const objItem of checkList) {
|
|
373
|
+
let itemResult = false;
|
|
374
|
+
if (objItem === null || objItem === undefined) {
|
|
375
|
+
itemResult = true;
|
|
376
|
+
}
|
|
377
|
+
else {
|
|
378
|
+
switch (typeof objItem) {
|
|
379
|
+
case "object":
|
|
380
|
+
if (typeof objItem[Symbol.iterator] === "function") {
|
|
381
|
+
/* 可迭代 */
|
|
382
|
+
itemResult = objItem.length === 0;
|
|
383
|
+
}
|
|
384
|
+
else {
|
|
385
|
+
itemResult = Object.keys(objItem).length === 0;
|
|
386
|
+
}
|
|
387
|
+
break;
|
|
388
|
+
case "number":
|
|
389
|
+
itemResult = objItem === 0;
|
|
390
|
+
break;
|
|
391
|
+
case "string":
|
|
392
|
+
itemResult =
|
|
393
|
+
objItem.trim() === "" ||
|
|
394
|
+
objItem === "null" ||
|
|
395
|
+
objItem === "undefined";
|
|
396
|
+
break;
|
|
397
|
+
case "boolean":
|
|
398
|
+
itemResult = !objItem;
|
|
399
|
+
break;
|
|
400
|
+
case "function":
|
|
401
|
+
let funcStr = objItem.toString().replace(/\s/g, "");
|
|
402
|
+
/* 排除()=>{}、(xxx="")=>{}、function(){}、function(xxx=""){} */
|
|
403
|
+
itemResult = Boolean(funcStr.match(/^\(.*?\)=>\{\}$|^function.*?\(.*?\)\{\}$/));
|
|
404
|
+
break;
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
result = result && itemResult;
|
|
408
|
+
}
|
|
409
|
+
return result;
|
|
410
|
+
}
|
|
411
|
+
/**
|
|
412
|
+
* 判断对象是否是元素
|
|
413
|
+
* @param target
|
|
414
|
+
* @returns
|
|
415
|
+
* + true 是元素
|
|
416
|
+
* + false 不是元素
|
|
417
|
+
* @example
|
|
418
|
+
* Utils.isDOM(document.querySelector("a"))
|
|
419
|
+
* > true
|
|
420
|
+
*/
|
|
421
|
+
isDOM(target) {
|
|
422
|
+
return target instanceof Node;
|
|
423
|
+
}
|
|
424
|
+
isNotNull(...args) {
|
|
425
|
+
let UtilsContext = this;
|
|
426
|
+
return !UtilsContext.isNull.apply(this, args);
|
|
427
|
+
}
|
|
428
|
+
deepClone(obj) {
|
|
429
|
+
let UtilsContext = this;
|
|
430
|
+
if (obj === undefined)
|
|
431
|
+
return undefined;
|
|
432
|
+
if (obj === null)
|
|
433
|
+
return null;
|
|
434
|
+
let clone = obj instanceof Array ? [] : {};
|
|
435
|
+
for (const [key, value] of Object.entries(obj)) {
|
|
436
|
+
clone[key] =
|
|
437
|
+
typeof value === "object" ? UtilsContext.deepClone(value) : value;
|
|
438
|
+
}
|
|
439
|
+
return clone;
|
|
440
|
+
}
|
|
441
|
+
/**
|
|
442
|
+
* 覆盖对象中的函数this指向
|
|
443
|
+
* @param target 需要覆盖的对象
|
|
444
|
+
* @param [objectThis] 覆盖的this指向,如果为传入,则默认为对象本身
|
|
445
|
+
*/
|
|
446
|
+
coverObjectFunctionThis(target, objectThis) {
|
|
447
|
+
if (typeof target !== "object" || target === null) {
|
|
448
|
+
throw new Error("target must be object");
|
|
449
|
+
}
|
|
450
|
+
objectThis = objectThis || target;
|
|
451
|
+
Object.keys(target).forEach((key) => {
|
|
452
|
+
if (typeof target[key] === "function") {
|
|
453
|
+
target[key] = target[key].bind(objectThis);
|
|
454
|
+
}
|
|
455
|
+
});
|
|
456
|
+
}
|
|
457
|
+
toJSON(data, errorCallBack) {
|
|
458
|
+
let result = {};
|
|
459
|
+
if (typeof data === "object") {
|
|
460
|
+
return data;
|
|
461
|
+
}
|
|
462
|
+
TryCatch()
|
|
463
|
+
.config({ log: false })
|
|
464
|
+
.error((error) => {
|
|
465
|
+
TryCatch()
|
|
466
|
+
.error(() => {
|
|
467
|
+
try {
|
|
468
|
+
result = new Function("return " + data)();
|
|
469
|
+
}
|
|
470
|
+
catch (error2) {
|
|
471
|
+
if (typeof errorCallBack === "function") {
|
|
472
|
+
errorCallBack(error2);
|
|
473
|
+
}
|
|
474
|
+
}
|
|
475
|
+
})
|
|
476
|
+
.run(() => {
|
|
477
|
+
if (data &&
|
|
478
|
+
/^[\],:{}\s]*$/.test(data
|
|
479
|
+
.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g, "@")
|
|
480
|
+
.replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, "]")
|
|
481
|
+
.replace(/(?:^|:|,)(?:\s*\[)+/g, ""))) {
|
|
482
|
+
result = new Function("return " + data)();
|
|
483
|
+
}
|
|
484
|
+
else {
|
|
485
|
+
if (typeof errorCallBack === "function") {
|
|
486
|
+
errorCallBack(new Error("target is not a JSON"));
|
|
487
|
+
}
|
|
488
|
+
}
|
|
489
|
+
});
|
|
490
|
+
})
|
|
491
|
+
.run(() => {
|
|
492
|
+
data = data.trim();
|
|
493
|
+
result = JSON.parse(data);
|
|
494
|
+
});
|
|
495
|
+
return result;
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
let commonUtil = new CommonUtil();
|
|
499
|
+
|
|
233
500
|
class UtilsGMCookie {
|
|
234
501
|
windowApi = {
|
|
235
502
|
window: window,
|
|
@@ -302,7 +569,7 @@
|
|
|
302
569
|
name: "",
|
|
303
570
|
path: "/",
|
|
304
571
|
};
|
|
305
|
-
defaultOption =
|
|
572
|
+
defaultOption = commonUtil.assign(defaultOption, option);
|
|
306
573
|
let cookies = this.getCookiesList();
|
|
307
574
|
cookies.forEach((item) => {
|
|
308
575
|
item = item.trim();
|
|
@@ -353,7 +620,7 @@
|
|
|
353
620
|
name: "",
|
|
354
621
|
path: "/",
|
|
355
622
|
};
|
|
356
|
-
defaultOption =
|
|
623
|
+
defaultOption = commonUtil.assign(defaultOption, option);
|
|
357
624
|
let cookies = this.getCookiesList();
|
|
358
625
|
cookies.forEach((item) => {
|
|
359
626
|
item = item.trim();
|
|
@@ -402,7 +669,7 @@
|
|
|
402
669
|
*/
|
|
403
670
|
expirationDate: Math.floor(Date.now()) + 60 * 60 * 24 * 30,
|
|
404
671
|
};
|
|
405
|
-
defaultOption =
|
|
672
|
+
defaultOption = commonUtil.assign(defaultOption, option);
|
|
406
673
|
let life = defaultOption.expirationDate
|
|
407
674
|
? defaultOption.expirationDate
|
|
408
675
|
: Math.floor(Date.now()) + 60 * 60 * 24 * 30;
|
|
@@ -412,7 +679,7 @@
|
|
|
412
679
|
";expires=" +
|
|
413
680
|
new Date(life).toGMTString() +
|
|
414
681
|
"; path=/";
|
|
415
|
-
if (
|
|
682
|
+
if (commonUtil.isNull(defaultOption.domain)) {
|
|
416
683
|
cookieStr += "; domain=" + defaultOption.domain;
|
|
417
684
|
}
|
|
418
685
|
this.windowApi.document.cookie = cookieStr;
|
|
@@ -440,9 +707,9 @@
|
|
|
440
707
|
path: "/",
|
|
441
708
|
firstPartyDomain: "",
|
|
442
709
|
};
|
|
443
|
-
defaultOption =
|
|
710
|
+
defaultOption = commonUtil.assign(defaultOption, option);
|
|
444
711
|
let cookieStr = `${defaultOption.name}=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=${defaultOption.path}`;
|
|
445
|
-
if (
|
|
712
|
+
if (commonUtil.isNull(defaultOption.firstPartyDomain)) {
|
|
446
713
|
cookieStr += `; domain=${defaultOption.firstPartyDomain};`;
|
|
447
714
|
}
|
|
448
715
|
this.windowApi.document.cookie = cookieStr;
|
|
@@ -1627,7 +1894,7 @@
|
|
|
1627
1894
|
menuOptions = [menuOptions];
|
|
1628
1895
|
}
|
|
1629
1896
|
for (let index = 0; index < menuOptions.length; index++) {
|
|
1630
|
-
let cloneMenuOptionData =
|
|
1897
|
+
let cloneMenuOptionData = commonUtil.deepClone(menuOptions[index].data);
|
|
1631
1898
|
const { showText, clickCallBack } = this.handleMenuData(cloneMenuOptionData);
|
|
1632
1899
|
let menuId = that.context.GM_Api.registerMenuCommand(showText, clickCallBack);
|
|
1633
1900
|
menuOptions[index].id = menuId;
|
|
@@ -2029,16 +2296,12 @@
|
|
|
2029
2296
|
return "";
|
|
2030
2297
|
}
|
|
2031
2298
|
try {
|
|
2032
|
-
|
|
2033
|
-
|
|
2034
|
-
|
|
2035
|
-
|
|
2036
|
-
|
|
2037
|
-
|
|
2038
|
-
"return _context['realFunc_" +
|
|
2039
|
-
_funcName +
|
|
2040
|
-
"'].apply(obj,args);\n" +
|
|
2041
|
-
"};");
|
|
2299
|
+
new Function("_context", "_funcName", "hookFunc", `_context[_funcName] = function ${_funcName}() {
|
|
2300
|
+
let args = Array.prototype.slice.call(arguments, 0);
|
|
2301
|
+
let obj = this;
|
|
2302
|
+
hookFunc.apply(obj, args);
|
|
2303
|
+
return _context['realFunc_${_funcName}'].apply(obj, args);
|
|
2304
|
+
};`)(_context, _funcName, hookFunc);
|
|
2042
2305
|
_context[_funcName].prototype.isHooked = true;
|
|
2043
2306
|
return true;
|
|
2044
2307
|
}
|
|
@@ -2298,14 +2561,14 @@
|
|
|
2298
2561
|
if (typeof args[1] === "object") {
|
|
2299
2562
|
/* 处理第二个参数details */
|
|
2300
2563
|
let optionArg = args[1];
|
|
2301
|
-
|
|
2564
|
+
commonUtil.assign(option, optionArg, true);
|
|
2302
2565
|
option.url = url;
|
|
2303
2566
|
}
|
|
2304
2567
|
}
|
|
2305
2568
|
else {
|
|
2306
2569
|
/* 传入的是配置 */
|
|
2307
2570
|
let optionArg = args[0];
|
|
2308
|
-
|
|
2571
|
+
commonUtil.assign(option, optionArg, true);
|
|
2309
2572
|
}
|
|
2310
2573
|
return option;
|
|
2311
2574
|
},
|
|
@@ -2338,7 +2601,7 @@
|
|
|
2338
2601
|
responseType: userRequestOption.responseType ||
|
|
2339
2602
|
this.context.#defaultRequestOption.responseType,
|
|
2340
2603
|
/* 对象使用深拷贝 */
|
|
2341
|
-
headers:
|
|
2604
|
+
headers: commonUtil.deepClone(this.context.#defaultRequestOption.headers),
|
|
2342
2605
|
data: userRequestOption.data || this.context.#defaultRequestOption.data,
|
|
2343
2606
|
redirect: userRequestOption.redirect ||
|
|
2344
2607
|
this.context.#defaultRequestOption.redirect,
|
|
@@ -2351,7 +2614,7 @@
|
|
|
2351
2614
|
revalidate: userRequestOption.revalidate ||
|
|
2352
2615
|
this.context.#defaultRequestOption.revalidate,
|
|
2353
2616
|
/* 对象使用深拷贝 */
|
|
2354
|
-
context:
|
|
2617
|
+
context: commonUtil.deepClone(userRequestOption.context ||
|
|
2355
2618
|
this.context.#defaultRequestOption.context),
|
|
2356
2619
|
overrideMimeType: userRequestOption.overrideMimeType ||
|
|
2357
2620
|
this.context.#defaultRequestOption.overrideMimeType,
|
|
@@ -2359,7 +2622,7 @@
|
|
|
2359
2622
|
this.context.#defaultRequestOption.anonymous,
|
|
2360
2623
|
fetch: userRequestOption.fetch || this.context.#defaultRequestOption.fetch,
|
|
2361
2624
|
/* 对象使用深拷贝 */
|
|
2362
|
-
fetchInit:
|
|
2625
|
+
fetchInit: commonUtil.deepClone(this.context.#defaultRequestOption.fetchInit),
|
|
2363
2626
|
allowInterceptConfig: {
|
|
2364
2627
|
beforeRequest: this.context.#defaultRequestOption
|
|
2365
2628
|
.allowInterceptConfig.beforeRequest,
|
|
@@ -2585,12 +2848,12 @@
|
|
|
2585
2848
|
Object.keys(option).forEach((keyName) => {
|
|
2586
2849
|
if (option[keyName] == null ||
|
|
2587
2850
|
(option[keyName] instanceof Function &&
|
|
2588
|
-
|
|
2851
|
+
commonUtil.isNull(option[keyName]))) {
|
|
2589
2852
|
Reflect.deleteProperty(option, keyName);
|
|
2590
2853
|
return;
|
|
2591
2854
|
}
|
|
2592
2855
|
});
|
|
2593
|
-
if (
|
|
2856
|
+
if (commonUtil.isNull(option.url)) {
|
|
2594
2857
|
throw new TypeError(`Utils.Httpx 参数 url不符合要求: ${option.url}`);
|
|
2595
2858
|
}
|
|
2596
2859
|
return option;
|
|
@@ -2789,10 +3052,10 @@
|
|
|
2789
3052
|
/* X浏览器会因为设置了responseType导致不返回responseText */
|
|
2790
3053
|
let originResponse = argsResult[0];
|
|
2791
3054
|
/* responseText为空,response不为空的情况 */
|
|
2792
|
-
if (
|
|
2793
|
-
|
|
3055
|
+
if (commonUtil.isNull(originResponse["responseText"]) &&
|
|
3056
|
+
commonUtil.isNotNull(originResponse["response"])) {
|
|
2794
3057
|
if (typeof originResponse["response"] === "object") {
|
|
2795
|
-
|
|
3058
|
+
TryCatch().run(() => {
|
|
2796
3059
|
originResponse["responseText"] = JSON.stringify(originResponse["response"]);
|
|
2797
3060
|
});
|
|
2798
3061
|
}
|
|
@@ -2809,7 +3072,7 @@
|
|
|
2809
3072
|
// 自定义个新的response
|
|
2810
3073
|
let httpxResponse = httpxResponseText;
|
|
2811
3074
|
if (details.responseType === "json") {
|
|
2812
|
-
httpxResponse =
|
|
3075
|
+
httpxResponse = commonUtil.toJSON(httpxResponseText);
|
|
2813
3076
|
}
|
|
2814
3077
|
else if (details.responseType === "document") {
|
|
2815
3078
|
let parser = new DOMParser();
|
|
@@ -3019,7 +3282,7 @@
|
|
|
3019
3282
|
(typeof fetchResponseType === "string" &&
|
|
3020
3283
|
fetchResponseType.includes("application/json"))) {
|
|
3021
3284
|
// response返回格式是JSON格式
|
|
3022
|
-
response =
|
|
3285
|
+
response = commonUtil.toJSON(responseText);
|
|
3023
3286
|
}
|
|
3024
3287
|
else if (option.responseType === "document" ||
|
|
3025
3288
|
option.responseType == null) {
|
|
@@ -3120,7 +3383,7 @@
|
|
|
3120
3383
|
if (typeof option.xmlHttpRequest !== "function") {
|
|
3121
3384
|
console.warn("[Httpx-constructor] 未传入GM_xmlhttpRequest函数或传入的GM_xmlhttpRequest不是Function,将默认使用window.fetch");
|
|
3122
3385
|
}
|
|
3123
|
-
|
|
3386
|
+
commonUtil.coverObjectFunctionThis(this);
|
|
3124
3387
|
this.interceptors.request.context = this;
|
|
3125
3388
|
this.interceptors.response.context = this;
|
|
3126
3389
|
this.config(option);
|
|
@@ -3133,8 +3396,8 @@
|
|
|
3133
3396
|
if (typeof option.xmlHttpRequest === "function") {
|
|
3134
3397
|
this.GM_Api.xmlHttpRequest = option.xmlHttpRequest;
|
|
3135
3398
|
}
|
|
3136
|
-
this.#defaultRequestOption =
|
|
3137
|
-
this.#defaultInitOption =
|
|
3399
|
+
this.#defaultRequestOption = commonUtil.assign(this.#defaultRequestOption, option);
|
|
3400
|
+
this.#defaultInitOption = commonUtil.assign(this.#defaultInitOption, option);
|
|
3138
3401
|
}
|
|
3139
3402
|
/**
|
|
3140
3403
|
* 拦截器
|
|
@@ -3698,7 +3961,7 @@
|
|
|
3698
3961
|
#flag = false;
|
|
3699
3962
|
#delayTime = 0;
|
|
3700
3963
|
#callback;
|
|
3701
|
-
#
|
|
3964
|
+
#timeId = undefined;
|
|
3702
3965
|
lock;
|
|
3703
3966
|
unlock;
|
|
3704
3967
|
run;
|
|
@@ -3708,23 +3971,22 @@
|
|
|
3708
3971
|
this.#callback = callback;
|
|
3709
3972
|
if (typeof context === "number") {
|
|
3710
3973
|
this.#delayTime = context;
|
|
3711
|
-
this.#context = utils;
|
|
3712
3974
|
}
|
|
3713
3975
|
else {
|
|
3714
3976
|
this.#delayTime = delayTime;
|
|
3715
|
-
this.#context = context;
|
|
3716
3977
|
}
|
|
3717
3978
|
/**
|
|
3718
3979
|
* 锁
|
|
3719
3980
|
*/
|
|
3720
3981
|
this.lock = function () {
|
|
3721
3982
|
that.#flag = true;
|
|
3983
|
+
clearTimeout(that.#timeId);
|
|
3722
3984
|
};
|
|
3723
3985
|
/**
|
|
3724
3986
|
* 解锁
|
|
3725
3987
|
*/
|
|
3726
3988
|
this.unlock = function () {
|
|
3727
|
-
|
|
3989
|
+
that.#timeId = setTimeout(() => {
|
|
3728
3990
|
that.#flag = false;
|
|
3729
3991
|
}, that.#delayTime);
|
|
3730
3992
|
};
|
|
@@ -3742,7 +4004,7 @@
|
|
|
3742
4004
|
return;
|
|
3743
4005
|
}
|
|
3744
4006
|
that.lock();
|
|
3745
|
-
await that.#callback.apply(
|
|
4007
|
+
await that.#callback.apply(this, args);
|
|
3746
4008
|
that.unlock();
|
|
3747
4009
|
};
|
|
3748
4010
|
}
|
|
@@ -4030,7 +4292,7 @@
|
|
|
4030
4292
|
* @param paramConfig 配置信息
|
|
4031
4293
|
*/
|
|
4032
4294
|
constructor(paramConfig) {
|
|
4033
|
-
this.#config =
|
|
4295
|
+
this.#config = commonUtil.assign(this.#config, paramConfig);
|
|
4034
4296
|
if (!(this.#config.canvasNode instanceof HTMLCanvasElement)) {
|
|
4035
4297
|
throw new Error("Utils.Progress 参数 canvasNode 必须是 HTMLCanvasElement");
|
|
4036
4298
|
}
|
|
@@ -4089,91 +4351,6 @@
|
|
|
4089
4351
|
}
|
|
4090
4352
|
}
|
|
4091
4353
|
|
|
4092
|
-
const TryCatch = function (...args) {
|
|
4093
|
-
/* 定义变量和函数 */
|
|
4094
|
-
let callbackFunction = null;
|
|
4095
|
-
let context = null;
|
|
4096
|
-
let handleError = (error) => { };
|
|
4097
|
-
let defaultDetails = {
|
|
4098
|
-
log: true,
|
|
4099
|
-
};
|
|
4100
|
-
const TryCatchCore = {
|
|
4101
|
-
/**
|
|
4102
|
-
*
|
|
4103
|
-
* @param paramDetails 配置
|
|
4104
|
-
* @returns
|
|
4105
|
-
*/
|
|
4106
|
-
config(paramDetails) {
|
|
4107
|
-
defaultDetails = utils.assign(defaultDetails, paramDetails);
|
|
4108
|
-
return TryCatchCore;
|
|
4109
|
-
},
|
|
4110
|
-
/**
|
|
4111
|
-
* 处理错误
|
|
4112
|
-
* @param handler
|
|
4113
|
-
*/
|
|
4114
|
-
error(handler) {
|
|
4115
|
-
// @ts-ignore
|
|
4116
|
-
handleError = handler;
|
|
4117
|
-
return TryCatchCore;
|
|
4118
|
-
},
|
|
4119
|
-
/**
|
|
4120
|
-
* 执行传入的函数并捕获其可能抛出的错误,并通过传入的错误处理函数进行处理。
|
|
4121
|
-
* @param callback 待执行函数,可以是 function 或者 string 类型。如果是 string 类型,则会被当做代码进行执行。
|
|
4122
|
-
* @param __context__ 待执行函数的作用域,用于apply指定
|
|
4123
|
-
* @returns 如果函数有返回值,则返回该返回值;否则返回 tryCatchObj 函数以支持链式调用。
|
|
4124
|
-
* @throws {Error} 如果传入参数不符合要求,则会抛出相应类型的错误。
|
|
4125
|
-
*/
|
|
4126
|
-
run(callback, __context__) {
|
|
4127
|
-
callbackFunction = callback;
|
|
4128
|
-
context = __context__ || this;
|
|
4129
|
-
let result = executeTryCatch(callbackFunction, handleError, context);
|
|
4130
|
-
// @ts-ignore
|
|
4131
|
-
return result !== undefined ? result : TryCatchCore;
|
|
4132
|
-
},
|
|
4133
|
-
};
|
|
4134
|
-
/**
|
|
4135
|
-
* 执行传入的函数并捕获其可能抛出的错误,并通过传入的错误处理函数进行处理。
|
|
4136
|
-
* @param callback - 待执行函数,可以是 function 或者 string 类型。如果是 string 类型,则会被当做代码进行执行。
|
|
4137
|
-
* @param handleErrorFunc - 错误处理函数,可以是 function 或者 string 类型。如果是 string 类型,则会被当做代码进行执行。
|
|
4138
|
-
* @param funcThis - 待执行函数的作用域,用于apply指定
|
|
4139
|
-
* @returns 如果函数有返回值,则返回该返回值;否则返回 undefined。
|
|
4140
|
-
*/
|
|
4141
|
-
function executeTryCatch(callback, handleErrorFunc, funcThis) {
|
|
4142
|
-
let result = undefined;
|
|
4143
|
-
try {
|
|
4144
|
-
if (typeof callback === "string") {
|
|
4145
|
-
(function () {
|
|
4146
|
-
eval(callback);
|
|
4147
|
-
}).apply(funcThis, args);
|
|
4148
|
-
}
|
|
4149
|
-
else {
|
|
4150
|
-
result = callback.apply(funcThis, args);
|
|
4151
|
-
}
|
|
4152
|
-
}
|
|
4153
|
-
catch (error) {
|
|
4154
|
-
if (defaultDetails.log) {
|
|
4155
|
-
callback = callback;
|
|
4156
|
-
console.log(`%c ${callback?.name ? callback?.name : callback + "出现错误"} `, "color: #f20000");
|
|
4157
|
-
console.log(`%c 错误原因:${error}`, "color: #f20000");
|
|
4158
|
-
console.trace(callback);
|
|
4159
|
-
}
|
|
4160
|
-
if (handleErrorFunc) {
|
|
4161
|
-
if (typeof handleErrorFunc === "string") {
|
|
4162
|
-
result = function () {
|
|
4163
|
-
return eval(handleErrorFunc);
|
|
4164
|
-
// @ts-ignore
|
|
4165
|
-
}.apply(funcThis, [...args, error]);
|
|
4166
|
-
}
|
|
4167
|
-
else {
|
|
4168
|
-
result = handleErrorFunc.apply(funcThis, [...args, error]);
|
|
4169
|
-
}
|
|
4170
|
-
}
|
|
4171
|
-
}
|
|
4172
|
-
return result;
|
|
4173
|
-
}
|
|
4174
|
-
return TryCatchCore;
|
|
4175
|
-
};
|
|
4176
|
-
|
|
4177
4354
|
class UtilsDictionary {
|
|
4178
4355
|
items = {};
|
|
4179
4356
|
constructor(key, value) {
|
|
@@ -4289,7 +4466,7 @@
|
|
|
4289
4466
|
* @param data 需要合并的字典
|
|
4290
4467
|
*/
|
|
4291
4468
|
concat(data) {
|
|
4292
|
-
this.items =
|
|
4469
|
+
this.items = commonUtil.assign(this.items, data.getItems());
|
|
4293
4470
|
}
|
|
4294
4471
|
forEach(callbackfn) {
|
|
4295
4472
|
for (const key in this.getItems()) {
|
|
@@ -4848,7 +5025,7 @@
|
|
|
4848
5025
|
|
|
4849
5026
|
const loadOrReturnBroker = createLoadOrReturnBroker(load, worker);
|
|
4850
5027
|
const clearInterval = (timerId) => loadOrReturnBroker().clearInterval(timerId);
|
|
4851
|
-
const clearTimeout = (timerId) => loadOrReturnBroker().clearTimeout(timerId);
|
|
5028
|
+
const clearTimeout$1 = (timerId) => loadOrReturnBroker().clearTimeout(timerId);
|
|
4852
5029
|
const setInterval = (...args) => loadOrReturnBroker().setInterval(...args);
|
|
4853
5030
|
const setTimeout$1 = (...args) => loadOrReturnBroker().setTimeout(...args);
|
|
4854
5031
|
|
|
@@ -5250,13 +5427,194 @@
|
|
|
5250
5427
|
}
|
|
5251
5428
|
}
|
|
5252
5429
|
|
|
5430
|
+
class DOMUtils {
|
|
5431
|
+
windowApi;
|
|
5432
|
+
constructor(option) {
|
|
5433
|
+
this.windowApi = new WindowApi(option);
|
|
5434
|
+
}
|
|
5435
|
+
selector(selector, parent) {
|
|
5436
|
+
return this.selectorAll(selector, parent)[0];
|
|
5437
|
+
}
|
|
5438
|
+
selectorAll(selector, parent) {
|
|
5439
|
+
const context = this;
|
|
5440
|
+
parent = parent || context.windowApi.document;
|
|
5441
|
+
selector = selector.trim();
|
|
5442
|
+
if (selector.match(/[^\s]{1}:empty$/gi)) {
|
|
5443
|
+
// empty 语法
|
|
5444
|
+
selector = selector.replace(/:empty$/gi, "");
|
|
5445
|
+
return Array.from(parent.querySelectorAll(selector)).filter(($ele) => {
|
|
5446
|
+
return $ele?.innerHTML?.trim() === "";
|
|
5447
|
+
});
|
|
5448
|
+
}
|
|
5449
|
+
else if (selector.match(/[^\s]{1}:contains\("(.*)"\)$/i) ||
|
|
5450
|
+
selector.match(/[^\s]{1}:contains\('(.*)'\)$/i)) {
|
|
5451
|
+
// contains 语法
|
|
5452
|
+
let textMatch = selector.match(/:contains\(("|')(.*)("|')\)$/i);
|
|
5453
|
+
let text = textMatch[2];
|
|
5454
|
+
selector = selector.replace(/:contains\(("|')(.*)("|')\)$/gi, "");
|
|
5455
|
+
return Array.from(parent.querySelectorAll(selector)).filter(($ele) => {
|
|
5456
|
+
// @ts-ignore
|
|
5457
|
+
return ($ele?.textContent || $ele?.innerText)?.includes(text);
|
|
5458
|
+
});
|
|
5459
|
+
}
|
|
5460
|
+
else if (selector.match(/[^\s]{1}:regexp\("(.*)"\)$/i) ||
|
|
5461
|
+
selector.match(/[^\s]{1}:regexp\('(.*)'\)$/i)) {
|
|
5462
|
+
// regexp 语法
|
|
5463
|
+
let textMatch = selector.match(/:regexp\(("|')(.*)("|')\)$/i);
|
|
5464
|
+
let pattern = textMatch[2];
|
|
5465
|
+
let flagMatch = pattern.match(/("|'),[\s]*("|')([igm]{0,3})$/i);
|
|
5466
|
+
let flags = "";
|
|
5467
|
+
if (flagMatch) {
|
|
5468
|
+
pattern = pattern.replace(/("|'),[\s]*("|')([igm]{0,3})$/gi, "");
|
|
5469
|
+
flags = flagMatch[3];
|
|
5470
|
+
}
|
|
5471
|
+
let regexp = new RegExp(pattern, flags);
|
|
5472
|
+
selector = selector.replace(/:regexp\(("|')(.*)("|')\)$/gi, "");
|
|
5473
|
+
return Array.from(parent.querySelectorAll(selector)).filter(($ele) => {
|
|
5474
|
+
// @ts-ignore
|
|
5475
|
+
return Boolean(($ele?.textContent || $ele?.innerText)?.match(regexp));
|
|
5476
|
+
});
|
|
5477
|
+
}
|
|
5478
|
+
else {
|
|
5479
|
+
// 普通语法
|
|
5480
|
+
return Array.from(parent.querySelectorAll(selector));
|
|
5481
|
+
}
|
|
5482
|
+
}
|
|
5483
|
+
/**
|
|
5484
|
+
* 匹配元素,可使用以下的额外语法
|
|
5485
|
+
*
|
|
5486
|
+
* + :contains([text]) 作用: 找到包含指定文本内容的指定元素
|
|
5487
|
+
* + :empty 作用:找到既没有文本内容也没有子元素的指定元素
|
|
5488
|
+
* + :regexp([text]) 作用: 找到符合正则表达式的内容的指定元素
|
|
5489
|
+
* @param $el 元素
|
|
5490
|
+
* @param selector 选择器
|
|
5491
|
+
* @example
|
|
5492
|
+
* DOMUtils.matches("div:contains('测试')")
|
|
5493
|
+
* > true
|
|
5494
|
+
* @example
|
|
5495
|
+
* DOMUtils.matches("div:empty")
|
|
5496
|
+
* > true
|
|
5497
|
+
* @example
|
|
5498
|
+
* DOMUtils.matches("div:regexp('^xxxx$')")
|
|
5499
|
+
* > true
|
|
5500
|
+
* @example
|
|
5501
|
+
* DOMUtils.matches("div:regexp(/^xxx/ig)")
|
|
5502
|
+
* > false
|
|
5503
|
+
*/
|
|
5504
|
+
matches($el, selector) {
|
|
5505
|
+
selector = selector.trim();
|
|
5506
|
+
if ($el == null) {
|
|
5507
|
+
return false;
|
|
5508
|
+
}
|
|
5509
|
+
if (selector.match(/[^\s]{1}:empty$/gi)) {
|
|
5510
|
+
// empty 语法
|
|
5511
|
+
selector = selector.replace(/:empty$/gi, "");
|
|
5512
|
+
return $el.matches(selector) && $el?.innerHTML?.trim() === "";
|
|
5513
|
+
}
|
|
5514
|
+
else if (selector.match(/[^\s]{1}:contains\("(.*)"\)$/i) ||
|
|
5515
|
+
selector.match(/[^\s]{1}:contains\('(.*)'\)$/i)) {
|
|
5516
|
+
// contains 语法
|
|
5517
|
+
let textMatch = selector.match(/:contains\(("|')(.*)("|')\)$/i);
|
|
5518
|
+
let text = textMatch[2];
|
|
5519
|
+
selector = selector.replace(/:contains\(("|')(.*)("|')\)$/gi, "");
|
|
5520
|
+
// @ts-ignore
|
|
5521
|
+
let content = $el?.textContent || $el?.innerText;
|
|
5522
|
+
if (typeof content !== "string") {
|
|
5523
|
+
content = "";
|
|
5524
|
+
}
|
|
5525
|
+
return $el.matches(selector) && content?.includes(text);
|
|
5526
|
+
}
|
|
5527
|
+
else if (selector.match(/[^\s]{1}:regexp\("(.*)"\)$/i) ||
|
|
5528
|
+
selector.match(/[^\s]{1}:regexp\('(.*)'\)$/i)) {
|
|
5529
|
+
// regexp 语法
|
|
5530
|
+
let textMatch = selector.match(/:regexp\(("|')(.*)("|')\)$/i);
|
|
5531
|
+
let pattern = textMatch[2];
|
|
5532
|
+
let flagMatch = pattern.match(/("|'),[\s]*("|')([igm]{0,3})$/i);
|
|
5533
|
+
let flags = "";
|
|
5534
|
+
if (flagMatch) {
|
|
5535
|
+
pattern = pattern.replace(/("|'),[\s]*("|')([igm]{0,3})$/gi, "");
|
|
5536
|
+
flags = flagMatch[3];
|
|
5537
|
+
}
|
|
5538
|
+
let regexp = new RegExp(pattern, flags);
|
|
5539
|
+
selector = selector.replace(/:regexp\(("|')(.*)("|')\)$/gi, "");
|
|
5540
|
+
// @ts-ignore
|
|
5541
|
+
let content = $el?.textContent || $el?.innerText;
|
|
5542
|
+
if (typeof content !== "string") {
|
|
5543
|
+
content = "";
|
|
5544
|
+
}
|
|
5545
|
+
return $el.matches(selector) && Boolean(content?.match(regexp));
|
|
5546
|
+
}
|
|
5547
|
+
else {
|
|
5548
|
+
// 普通语法
|
|
5549
|
+
return $el.matches(selector);
|
|
5550
|
+
}
|
|
5551
|
+
}
|
|
5552
|
+
closest($el, selector) {
|
|
5553
|
+
selector = selector.trim();
|
|
5554
|
+
if (selector.match(/[^\s]{1}:empty$/gi)) {
|
|
5555
|
+
// empty 语法
|
|
5556
|
+
selector = selector.replace(/:empty$/gi, "");
|
|
5557
|
+
let $closest = $el?.closest(selector);
|
|
5558
|
+
if ($closest && $closest?.innerHTML?.trim() === "") {
|
|
5559
|
+
return $closest;
|
|
5560
|
+
}
|
|
5561
|
+
return null;
|
|
5562
|
+
}
|
|
5563
|
+
else if (selector.match(/[^\s]{1}:contains\("(.*)"\)$/i) ||
|
|
5564
|
+
selector.match(/[^\s]{1}:contains\('(.*)'\)$/i)) {
|
|
5565
|
+
// contains 语法
|
|
5566
|
+
let textMatch = selector.match(/:contains\(("|')(.*)("|')\)$/i);
|
|
5567
|
+
let text = textMatch[2];
|
|
5568
|
+
selector = selector.replace(/:contains\(("|')(.*)("|')\)$/gi, "");
|
|
5569
|
+
let $closest = $el?.closest(selector);
|
|
5570
|
+
if ($closest) {
|
|
5571
|
+
// @ts-ignore
|
|
5572
|
+
let content = $el?.textContent || $el?.innerText;
|
|
5573
|
+
if (typeof content === "string" && content.includes(text)) {
|
|
5574
|
+
return $closest;
|
|
5575
|
+
}
|
|
5576
|
+
}
|
|
5577
|
+
return null;
|
|
5578
|
+
}
|
|
5579
|
+
else if (selector.match(/[^\s]{1}:regexp\("(.*)"\)$/i) ||
|
|
5580
|
+
selector.match(/[^\s]{1}:regexp\('(.*)'\)$/i)) {
|
|
5581
|
+
// regexp 语法
|
|
5582
|
+
let textMatch = selector.match(/:regexp\(("|')(.*)("|')\)$/i);
|
|
5583
|
+
let pattern = textMatch[2];
|
|
5584
|
+
let flagMatch = pattern.match(/("|'),[\s]*("|')([igm]{0,3})$/i);
|
|
5585
|
+
let flags = "";
|
|
5586
|
+
if (flagMatch) {
|
|
5587
|
+
pattern = pattern.replace(/("|'),[\s]*("|')([igm]{0,3})$/gi, "");
|
|
5588
|
+
flags = flagMatch[3];
|
|
5589
|
+
}
|
|
5590
|
+
let regexp = new RegExp(pattern, flags);
|
|
5591
|
+
selector = selector.replace(/:regexp\(("|')(.*)("|')\)$/gi, "");
|
|
5592
|
+
let $closest = $el?.closest(selector);
|
|
5593
|
+
if ($closest) {
|
|
5594
|
+
// @ts-ignore
|
|
5595
|
+
let content = $el?.textContent || $el?.innerText;
|
|
5596
|
+
if (typeof content === "string" && content.match(regexp)) {
|
|
5597
|
+
return $closest;
|
|
5598
|
+
}
|
|
5599
|
+
}
|
|
5600
|
+
return null;
|
|
5601
|
+
}
|
|
5602
|
+
else {
|
|
5603
|
+
// 普通语法
|
|
5604
|
+
let $closest = $el?.closest(selector);
|
|
5605
|
+
return $closest;
|
|
5606
|
+
}
|
|
5607
|
+
}
|
|
5608
|
+
}
|
|
5609
|
+
let domUtils = new DOMUtils();
|
|
5610
|
+
|
|
5253
5611
|
class Utils {
|
|
5254
5612
|
windowApi;
|
|
5255
5613
|
constructor(option) {
|
|
5256
5614
|
this.windowApi = new WindowApi(option);
|
|
5257
5615
|
}
|
|
5258
5616
|
/** 版本号 */
|
|
5259
|
-
version = "2025.
|
|
5617
|
+
version = "2025.6.7";
|
|
5260
5618
|
addStyle(cssText) {
|
|
5261
5619
|
if (typeof cssText !== "string") {
|
|
5262
5620
|
throw new Error("Utils.addStyle 参数cssText 必须为String类型");
|
|
@@ -5282,58 +5640,22 @@
|
|
|
5282
5640
|
}
|
|
5283
5641
|
return cssNode;
|
|
5284
5642
|
}
|
|
5285
|
-
|
|
5286
|
-
|
|
5287
|
-
|
|
5288
|
-
|
|
5289
|
-
|
|
5290
|
-
|
|
5291
|
-
|
|
5292
|
-
|
|
5293
|
-
|
|
5294
|
-
|
|
5295
|
-
|
|
5296
|
-
|
|
5297
|
-
}
|
|
5298
|
-
if (target == null) {
|
|
5299
|
-
target = {};
|
|
5300
|
-
}
|
|
5301
|
-
if (isAdd) {
|
|
5302
|
-
for (const sourceKeyName in source) {
|
|
5303
|
-
const targetKeyName = sourceKeyName;
|
|
5304
|
-
let targetValue = target[targetKeyName];
|
|
5305
|
-
let sourceValue = source[sourceKeyName];
|
|
5306
|
-
if (typeof sourceValue === "object" &&
|
|
5307
|
-
sourceValue != null &&
|
|
5308
|
-
sourceKeyName in target &&
|
|
5309
|
-
!UtilsContext.isDOM(sourceValue)) {
|
|
5310
|
-
/* 源端的值是object类型,且不是元素节点 */
|
|
5311
|
-
target[sourceKeyName] = UtilsContext.assign(targetValue, sourceValue, isAdd);
|
|
5312
|
-
continue;
|
|
5313
|
-
}
|
|
5314
|
-
target[sourceKeyName] = sourceValue;
|
|
5315
|
-
}
|
|
5316
|
-
}
|
|
5317
|
-
else {
|
|
5318
|
-
for (const targetKeyName in target) {
|
|
5319
|
-
if (targetKeyName in source) {
|
|
5320
|
-
let targetValue = target[targetKeyName];
|
|
5321
|
-
let sourceValue = source[targetKeyName];
|
|
5322
|
-
if (typeof sourceValue === "object" &&
|
|
5323
|
-
sourceValue != null &&
|
|
5324
|
-
!UtilsContext.isDOM(sourceValue) &&
|
|
5325
|
-
Object.keys(sourceValue).length) {
|
|
5326
|
-
/* 源端的值是object类型,且不是元素节点 */
|
|
5327
|
-
target[targetKeyName] = UtilsContext.assign(targetValue, sourceValue, isAdd);
|
|
5328
|
-
continue;
|
|
5329
|
-
}
|
|
5330
|
-
/* 直接赋值 */
|
|
5331
|
-
target[targetKeyName] = sourceValue;
|
|
5332
|
-
}
|
|
5643
|
+
/**
|
|
5644
|
+
* JSON数据从源端替换到目标端中,如果目标端存在该数据则替换,不添加,返回结果为目标端替换完毕的结果
|
|
5645
|
+
* @param target 目标数据
|
|
5646
|
+
* @param source 源数据
|
|
5647
|
+
* @param isAdd 是否可以追加键,默认false
|
|
5648
|
+
* @example
|
|
5649
|
+
* Utils.assign({"1":1,"2":{"3":3}}, {"2":{"3":4}});
|
|
5650
|
+
* >
|
|
5651
|
+
* {
|
|
5652
|
+
"1": 1,
|
|
5653
|
+
"2": {
|
|
5654
|
+
"3": 4
|
|
5333
5655
|
}
|
|
5334
5656
|
}
|
|
5335
|
-
|
|
5336
|
-
|
|
5657
|
+
*/
|
|
5658
|
+
assign = commonUtil.assign.bind(commonUtil);
|
|
5337
5659
|
async asyncReplaceAll(string, pattern, asyncFn) {
|
|
5338
5660
|
let UtilsContext = this;
|
|
5339
5661
|
if (typeof string !== "string") {
|
|
@@ -5486,19 +5808,11 @@
|
|
|
5486
5808
|
* @returns
|
|
5487
5809
|
*/
|
|
5488
5810
|
ColorConversion = ColorConversion;
|
|
5489
|
-
|
|
5490
|
-
|
|
5491
|
-
|
|
5492
|
-
|
|
5493
|
-
|
|
5494
|
-
return null;
|
|
5495
|
-
let clone = obj instanceof Array ? [] : {};
|
|
5496
|
-
for (const [key, value] of Object.entries(obj)) {
|
|
5497
|
-
clone[key] =
|
|
5498
|
-
typeof value === "object" ? UtilsContext.deepClone(value) : value;
|
|
5499
|
-
}
|
|
5500
|
-
return clone;
|
|
5501
|
-
}
|
|
5811
|
+
/**
|
|
5812
|
+
* 深拷贝
|
|
5813
|
+
* @param obj 对象
|
|
5814
|
+
*/
|
|
5815
|
+
deepClone = commonUtil.deepClone.bind(commonUtil);
|
|
5502
5816
|
debounce(fn, delay = 0) {
|
|
5503
5817
|
let timer = null;
|
|
5504
5818
|
let UtilsContext = this;
|
|
@@ -5521,7 +5835,7 @@
|
|
|
5521
5835
|
throw new Error("Utils.deleteParentNode 参数 targetSelector 必须为 string 类型");
|
|
5522
5836
|
}
|
|
5523
5837
|
let result = false;
|
|
5524
|
-
let needRemoveDOM =
|
|
5838
|
+
let needRemoveDOM = domUtils.closest(element, targetSelector);
|
|
5525
5839
|
if (needRemoveDOM) {
|
|
5526
5840
|
needRemoveDOM.remove();
|
|
5527
5841
|
result = true;
|
|
@@ -6509,9 +6823,17 @@
|
|
|
6509
6823
|
throw new TypeError("参数1类型错误" + typeof firstArg);
|
|
6510
6824
|
}
|
|
6511
6825
|
}
|
|
6512
|
-
|
|
6513
|
-
|
|
6514
|
-
|
|
6826
|
+
/**
|
|
6827
|
+
* 判断对象是否是元素
|
|
6828
|
+
* @param target
|
|
6829
|
+
* @returns
|
|
6830
|
+
* + true 是元素
|
|
6831
|
+
* + false 不是元素
|
|
6832
|
+
* @example
|
|
6833
|
+
* Utils.isDOM(document.querySelector("a"))
|
|
6834
|
+
* > true
|
|
6835
|
+
*/
|
|
6836
|
+
isDOM = commonUtil.isDOM.bind(commonUtil);
|
|
6515
6837
|
isFullscreenEnabled() {
|
|
6516
6838
|
return !!(this.windowApi.document.fullscreenEnabled ||
|
|
6517
6839
|
this.windowApi.document.webkitFullScreenEnabled ||
|
|
@@ -6702,52 +7024,62 @@
|
|
|
6702
7024
|
}
|
|
6703
7025
|
return result;
|
|
6704
7026
|
}
|
|
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
|
-
|
|
6748
|
-
|
|
6749
|
-
|
|
6750
|
-
|
|
7027
|
+
/**
|
|
7028
|
+
* 判断对象是否不为空
|
|
7029
|
+
* @returns {boolean}
|
|
7030
|
+
* + true 不为空
|
|
7031
|
+
* + false 为空
|
|
7032
|
+
* @example
|
|
7033
|
+
* Utils.isNotNull("123");
|
|
7034
|
+
* > true
|
|
7035
|
+
*/
|
|
7036
|
+
isNotNull = commonUtil.isNotNull.bind(commonUtil);
|
|
7037
|
+
/**
|
|
7038
|
+
* 判断对象或数据是否为空
|
|
7039
|
+
* + `String`判空的值,如 ""、"null"、"undefined"、" "
|
|
7040
|
+
* + `Number`判空的值,如 0
|
|
7041
|
+
* + `Object`判空的值,如 {}、null、undefined
|
|
7042
|
+
* + `Array`(存在属性Symbol.iterator)判空的值,如 []
|
|
7043
|
+
* + `Boolean`判空的值,如false
|
|
7044
|
+
* + `Function`判空的值,如()=>{}、(xxx="")=>{}、function(){}、function(xxx=""){}
|
|
7045
|
+
* @returns
|
|
7046
|
+
* + true 为空
|
|
7047
|
+
* + false 不为空
|
|
7048
|
+
* @example
|
|
7049
|
+
Utils.isNull({});
|
|
7050
|
+
> true
|
|
7051
|
+
* @example
|
|
7052
|
+
Utils.isNull([]);
|
|
7053
|
+
> true
|
|
7054
|
+
* @example
|
|
7055
|
+
Utils.isNull(" ");
|
|
7056
|
+
> true
|
|
7057
|
+
* @example
|
|
7058
|
+
Utils.isNull(function(){});
|
|
7059
|
+
> true
|
|
7060
|
+
* @example
|
|
7061
|
+
Utils.isNull(()=>{}));
|
|
7062
|
+
> true
|
|
7063
|
+
* @example
|
|
7064
|
+
Utils.isNull("undefined");
|
|
7065
|
+
> true
|
|
7066
|
+
* @example
|
|
7067
|
+
Utils.isNull("null");
|
|
7068
|
+
> true
|
|
7069
|
+
* @example
|
|
7070
|
+
Utils.isNull(" ", false);
|
|
7071
|
+
> true
|
|
7072
|
+
* @example
|
|
7073
|
+
Utils.isNull([1],[]);
|
|
7074
|
+
> false
|
|
7075
|
+
* @example
|
|
7076
|
+
Utils.isNull([],[1]);
|
|
7077
|
+
> false
|
|
7078
|
+
* @example
|
|
7079
|
+
Utils.isNull(false,[123]);
|
|
7080
|
+
> false
|
|
7081
|
+
**/
|
|
7082
|
+
isNull = commonUtil.isNull.bind(commonUtil);
|
|
6751
7083
|
isThemeDark() {
|
|
6752
7084
|
return this.windowApi.globalThis.matchMedia("(prefers-color-scheme: dark)")
|
|
6753
7085
|
.matches;
|
|
@@ -7556,7 +7888,7 @@
|
|
|
7556
7888
|
return mouseEvent;
|
|
7557
7889
|
}
|
|
7558
7890
|
let sliderElement = typeof selector === "string"
|
|
7559
|
-
?
|
|
7891
|
+
? domUtils.selector(selector)
|
|
7560
7892
|
: selector;
|
|
7561
7893
|
if (!(sliderElement instanceof Node) ||
|
|
7562
7894
|
!(sliderElement instanceof Element)) {
|
|
@@ -7766,47 +8098,15 @@
|
|
|
7766
8098
|
}
|
|
7767
8099
|
return newTargetString;
|
|
7768
8100
|
}
|
|
7769
|
-
|
|
7770
|
-
|
|
7771
|
-
|
|
7772
|
-
|
|
7773
|
-
|
|
7774
|
-
|
|
7775
|
-
|
|
7776
|
-
|
|
7777
|
-
|
|
7778
|
-
UtilsContext.tryCatch()
|
|
7779
|
-
.error(() => {
|
|
7780
|
-
try {
|
|
7781
|
-
result = UtilsContext.windowApi.window.eval("(" + data + ")");
|
|
7782
|
-
}
|
|
7783
|
-
catch (error2) {
|
|
7784
|
-
if (typeof errorCallBack === "function") {
|
|
7785
|
-
errorCallBack(error2);
|
|
7786
|
-
}
|
|
7787
|
-
}
|
|
7788
|
-
})
|
|
7789
|
-
.run(() => {
|
|
7790
|
-
if (data &&
|
|
7791
|
-
/^[\],:{}\s]*$/.test(data
|
|
7792
|
-
.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g, "@")
|
|
7793
|
-
.replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, "]")
|
|
7794
|
-
.replace(/(?:^|:|,)(?:\s*\[)+/g, ""))) {
|
|
7795
|
-
result = new Function("return " + data)();
|
|
7796
|
-
}
|
|
7797
|
-
else {
|
|
7798
|
-
if (typeof errorCallBack === "function") {
|
|
7799
|
-
errorCallBack(new Error("target is not a JSON"));
|
|
7800
|
-
}
|
|
7801
|
-
}
|
|
7802
|
-
});
|
|
7803
|
-
})
|
|
7804
|
-
.run(() => {
|
|
7805
|
-
data = data.trim();
|
|
7806
|
-
result = JSON.parse(data);
|
|
7807
|
-
});
|
|
7808
|
-
return result;
|
|
7809
|
-
}
|
|
8101
|
+
/**
|
|
8102
|
+
* 字符串转Object对象,类似'{"test":""}' => {"test":""}
|
|
8103
|
+
* @param data
|
|
8104
|
+
* @param errorCallBack (可选)错误回调
|
|
8105
|
+
* @example
|
|
8106
|
+
* Utils.toJSON("{123:123}")
|
|
8107
|
+
* > {123:123}
|
|
8108
|
+
*/
|
|
8109
|
+
toJSON = commonUtil.toJSON.bind(commonUtil);
|
|
7810
8110
|
toSearchParamsStr(obj, addPrefix) {
|
|
7811
8111
|
let UtilsContext = this;
|
|
7812
8112
|
let searhParamsStr = "";
|
|
@@ -7972,7 +8272,7 @@
|
|
|
7972
8272
|
if (Array.isArray(selector)) {
|
|
7973
8273
|
let result = [];
|
|
7974
8274
|
for (let index = 0; index < selector.length; index++) {
|
|
7975
|
-
let node =
|
|
8275
|
+
let node = domUtils.selector(selector[index]);
|
|
7976
8276
|
if (node) {
|
|
7977
8277
|
result.push(node);
|
|
7978
8278
|
}
|
|
@@ -7985,7 +8285,7 @@
|
|
|
7985
8285
|
return selector();
|
|
7986
8286
|
}
|
|
7987
8287
|
else {
|
|
7988
|
-
return
|
|
8288
|
+
return domUtils.selector(selector, parent);
|
|
7989
8289
|
}
|
|
7990
8290
|
}
|
|
7991
8291
|
return UtilsContext.wait(() => {
|
|
@@ -8115,7 +8415,7 @@
|
|
|
8115
8415
|
if (Array.isArray(selector)) {
|
|
8116
8416
|
let result = [];
|
|
8117
8417
|
for (let index = 0; index < selector.length; index++) {
|
|
8118
|
-
let nodeList =
|
|
8418
|
+
let nodeList = domUtils.selectorAll(selector[index], parent);
|
|
8119
8419
|
if (nodeList.length) {
|
|
8120
8420
|
result.push(nodeList);
|
|
8121
8421
|
}
|
|
@@ -8125,7 +8425,7 @@
|
|
|
8125
8425
|
}
|
|
8126
8426
|
}
|
|
8127
8427
|
else {
|
|
8128
|
-
let nodeList =
|
|
8428
|
+
let nodeList = domUtils.selectorAll(selector, parent);
|
|
8129
8429
|
if (nodeList.length) {
|
|
8130
8430
|
return nodeList;
|
|
8131
8431
|
}
|
|
@@ -8434,17 +8734,7 @@
|
|
|
8434
8734
|
* @param target 需要覆盖的对象
|
|
8435
8735
|
* @param [objectThis] 覆盖的this指向,如果为传入,则默认为对象本身
|
|
8436
8736
|
*/
|
|
8437
|
-
coverObjectFunctionThis(
|
|
8438
|
-
if (typeof target !== "object" || target === null) {
|
|
8439
|
-
throw new Error("target must be object");
|
|
8440
|
-
}
|
|
8441
|
-
objectThis = objectThis || target;
|
|
8442
|
-
Object.keys(target).forEach((key) => {
|
|
8443
|
-
if (typeof target[key] === "function") {
|
|
8444
|
-
target[key] = target[key].bind(objectThis);
|
|
8445
|
-
}
|
|
8446
|
-
});
|
|
8447
|
-
}
|
|
8737
|
+
coverObjectFunctionThis = commonUtil.coverObjectFunctionThis.bind(commonUtil);
|
|
8448
8738
|
/**
|
|
8449
8739
|
* 生成uuid
|
|
8450
8740
|
* @example
|
|
@@ -8485,7 +8775,7 @@
|
|
|
8485
8775
|
workerClearTimeout(timeId) {
|
|
8486
8776
|
try {
|
|
8487
8777
|
if (timeId != null) {
|
|
8488
|
-
clearTimeout(timeId);
|
|
8778
|
+
clearTimeout$1(timeId);
|
|
8489
8779
|
}
|
|
8490
8780
|
}
|
|
8491
8781
|
catch (error) {
|
|
@@ -8523,6 +8813,74 @@
|
|
|
8523
8813
|
globalThis.clearInterval(timeId);
|
|
8524
8814
|
}
|
|
8525
8815
|
}
|
|
8816
|
+
/**
|
|
8817
|
+
* 获取剪贴板信息
|
|
8818
|
+
*/
|
|
8819
|
+
async getClipboardInfo() {
|
|
8820
|
+
return new Promise((resolve) => {
|
|
8821
|
+
/** 读取剪贴板 */
|
|
8822
|
+
function readClipboardText() {
|
|
8823
|
+
navigator.clipboard
|
|
8824
|
+
.readText()
|
|
8825
|
+
.then((clipboardText) => {
|
|
8826
|
+
resolve({
|
|
8827
|
+
error: null,
|
|
8828
|
+
content: clipboardText,
|
|
8829
|
+
});
|
|
8830
|
+
})
|
|
8831
|
+
.catch((error) => {
|
|
8832
|
+
resolve({
|
|
8833
|
+
error: error,
|
|
8834
|
+
content: "",
|
|
8835
|
+
});
|
|
8836
|
+
});
|
|
8837
|
+
}
|
|
8838
|
+
/** 申请读取剪贴板的权限 */
|
|
8839
|
+
function requestPermissionsWithClipboard() {
|
|
8840
|
+
navigator.permissions
|
|
8841
|
+
.query({
|
|
8842
|
+
// @ts-ignore
|
|
8843
|
+
name: "clipboard-read",
|
|
8844
|
+
})
|
|
8845
|
+
.then((permissionStatus) => {
|
|
8846
|
+
readClipboardText();
|
|
8847
|
+
})
|
|
8848
|
+
.catch((error) => {
|
|
8849
|
+
/* 该权限申请Api可能在该环境下不生效,尝试直接读取剪贴板 */
|
|
8850
|
+
readClipboardText();
|
|
8851
|
+
});
|
|
8852
|
+
}
|
|
8853
|
+
/**
|
|
8854
|
+
* 检查当前环境是否支持读取剪贴板Api
|
|
8855
|
+
*/
|
|
8856
|
+
function checkClipboardApi() {
|
|
8857
|
+
if (typeof navigator?.clipboard?.readText !== "function") {
|
|
8858
|
+
return false;
|
|
8859
|
+
}
|
|
8860
|
+
if (typeof navigator?.permissions?.query !== "function") {
|
|
8861
|
+
return false;
|
|
8862
|
+
}
|
|
8863
|
+
return true;
|
|
8864
|
+
}
|
|
8865
|
+
if (!checkClipboardApi()) {
|
|
8866
|
+
resolve({
|
|
8867
|
+
error: new Error("当前环境不支持读取剪贴板Api"),
|
|
8868
|
+
content: "",
|
|
8869
|
+
});
|
|
8870
|
+
return;
|
|
8871
|
+
}
|
|
8872
|
+
if (document.hasFocus()) {
|
|
8873
|
+
requestPermissionsWithClipboard();
|
|
8874
|
+
}
|
|
8875
|
+
else {
|
|
8876
|
+
window.addEventListener("focus", () => {
|
|
8877
|
+
requestPermissionsWithClipboard();
|
|
8878
|
+
}, {
|
|
8879
|
+
once: true,
|
|
8880
|
+
});
|
|
8881
|
+
}
|
|
8882
|
+
});
|
|
8883
|
+
}
|
|
8526
8884
|
}
|
|
8527
8885
|
let utils = new Utils();
|
|
8528
8886
|
|