@whitesev/utils 2.2.0 → 2.2.2
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 +155 -40
- package/dist/index.amd.js.map +1 -1
- package/dist/index.cjs.js +155 -40
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.esm.js +155 -40
- package/dist/index.esm.js.map +1 -1
- package/dist/index.iife.js +155 -40
- package/dist/index.iife.js.map +1 -1
- package/dist/index.system.js +155 -40
- package/dist/index.system.js.map +1 -1
- package/dist/index.umd.js +155 -40
- package/dist/index.umd.js.map +1 -1
- package/dist/types/src/Httpx.d.ts +23 -13
- package/dist/types/src/Utils.d.ts +2 -2
- package/package.json +1 -1
- package/src/Httpx.ts +239 -104
- package/src/Utils.ts +8 -3
package/dist/index.cjs.js
CHANGED
|
@@ -2172,11 +2172,11 @@ class Httpx {
|
|
|
2172
2172
|
}
|
|
2173
2173
|
if (details.fetch) {
|
|
2174
2174
|
const { fetchDetails, fetchRequestInit, abortController } = this.context.HttpxRequestDetails.handleFetchDetail(details);
|
|
2175
|
-
this.fetch(fetchDetails, fetchRequestInit, abortController);
|
|
2175
|
+
return this.fetch(fetchDetails, fetchRequestInit, abortController);
|
|
2176
2176
|
}
|
|
2177
2177
|
else {
|
|
2178
2178
|
Reflect.deleteProperty(details, "fetchInit");
|
|
2179
|
-
this.xmlHttpRequest(details);
|
|
2179
|
+
return this.xmlHttpRequest(details);
|
|
2180
2180
|
}
|
|
2181
2181
|
},
|
|
2182
2182
|
/**
|
|
@@ -2184,7 +2184,7 @@ class Httpx {
|
|
|
2184
2184
|
* @param details
|
|
2185
2185
|
*/
|
|
2186
2186
|
xmlHttpRequest(details) {
|
|
2187
|
-
this.context.GM_Api.xmlHttpRequest(details);
|
|
2187
|
+
return this.context.GM_Api.xmlHttpRequest(details);
|
|
2188
2188
|
},
|
|
2189
2189
|
/**
|
|
2190
2190
|
* 使用fetch发送请求
|
|
@@ -2335,6 +2335,10 @@ class Httpx {
|
|
|
2335
2335
|
* 当前使用请求时,输出请求的配置
|
|
2336
2336
|
*/
|
|
2337
2337
|
#LOG_DETAILS = false;
|
|
2338
|
+
/**
|
|
2339
|
+
* 实例化,可传入GM_xmlhttpRequest,未传入则使用window.fetch
|
|
2340
|
+
* @param __xmlHttpRequest__
|
|
2341
|
+
*/
|
|
2338
2342
|
constructor(__xmlHttpRequest__) {
|
|
2339
2343
|
if (typeof __xmlHttpRequest__ !== "function") {
|
|
2340
2344
|
console.warn("Httpx未传入GM_xmlhttpRequest函数或传入的GM_xmlhttpRequest不是Function,强制使用window.fetch");
|
|
@@ -2430,79 +2434,177 @@ class Httpx {
|
|
|
2430
2434
|
}
|
|
2431
2435
|
/**
|
|
2432
2436
|
* GET 请求
|
|
2437
|
+
* @param url 网址
|
|
2438
|
+
* @param details 配置
|
|
2433
2439
|
*/
|
|
2434
|
-
async get(...args
|
|
2435
|
-
|
|
2440
|
+
async get(...args // @ts-ignore
|
|
2441
|
+
) {
|
|
2436
2442
|
let details = this.HttpxRequestDetails.handleBeforeRequestDetails(...args);
|
|
2437
|
-
|
|
2438
|
-
|
|
2443
|
+
let abortFn = null;
|
|
2444
|
+
const promise = new Promise((resolve, reject) => {
|
|
2445
|
+
let requestDetails = this.HttpxRequestDetails.getDetails("GET", details, resolve, reject);
|
|
2439
2446
|
Reflect.deleteProperty(requestDetails, "onprogress");
|
|
2440
|
-
|
|
2441
|
-
|
|
2447
|
+
// @ts-ignore
|
|
2448
|
+
requestDetails = this.HttpxRequestDetails.handle(requestDetails);
|
|
2449
|
+
const requestResult = this.HttpxRequest.request(requestDetails);
|
|
2450
|
+
if (requestResult != null &&
|
|
2451
|
+
typeof requestResult.abort === "function") {
|
|
2452
|
+
abortFn = requestResult.abort;
|
|
2453
|
+
}
|
|
2454
|
+
});
|
|
2455
|
+
Object.defineProperty(promise, "abort", {
|
|
2456
|
+
value: () => {
|
|
2457
|
+
return () => {
|
|
2458
|
+
if (typeof abortFn === "function") {
|
|
2459
|
+
abortFn();
|
|
2460
|
+
}
|
|
2461
|
+
};
|
|
2462
|
+
},
|
|
2442
2463
|
});
|
|
2464
|
+
return promise;
|
|
2443
2465
|
}
|
|
2444
2466
|
/**
|
|
2445
2467
|
* POST 请求
|
|
2446
2468
|
*/
|
|
2447
|
-
async post(...args
|
|
2448
|
-
|
|
2469
|
+
async post(...args // @ts-ignore
|
|
2470
|
+
) {
|
|
2449
2471
|
let details = this.HttpxRequestDetails.handleBeforeRequestDetails(...args);
|
|
2450
|
-
|
|
2451
|
-
|
|
2452
|
-
requestDetails =
|
|
2453
|
-
|
|
2472
|
+
let abortFn = null;
|
|
2473
|
+
const promise = new Promise((resolve, reject) => {
|
|
2474
|
+
let requestDetails = this.HttpxRequestDetails.getDetails("POST", details, resolve, reject);
|
|
2475
|
+
// @ts-ignore
|
|
2476
|
+
requestDetails = this.HttpxRequestDetails.handle(requestDetails);
|
|
2477
|
+
const requestResult = this.HttpxRequest.request(requestDetails);
|
|
2478
|
+
if (requestResult != null &&
|
|
2479
|
+
typeof requestResult.abort === "function") {
|
|
2480
|
+
abortFn = requestResult.abort;
|
|
2481
|
+
}
|
|
2454
2482
|
});
|
|
2483
|
+
Object.defineProperty(promise, "abort", {
|
|
2484
|
+
value: () => {
|
|
2485
|
+
return () => {
|
|
2486
|
+
if (typeof abortFn === "function") {
|
|
2487
|
+
abortFn();
|
|
2488
|
+
}
|
|
2489
|
+
};
|
|
2490
|
+
},
|
|
2491
|
+
});
|
|
2492
|
+
return promise;
|
|
2455
2493
|
}
|
|
2456
2494
|
/**
|
|
2457
2495
|
* HEAD 请求
|
|
2458
2496
|
*/
|
|
2459
|
-
async head(...args
|
|
2460
|
-
|
|
2497
|
+
async head(...args // @ts-ignore
|
|
2498
|
+
) {
|
|
2461
2499
|
let details = this.HttpxRequestDetails.handleBeforeRequestDetails(...args);
|
|
2462
|
-
|
|
2463
|
-
|
|
2500
|
+
let abortFn = null;
|
|
2501
|
+
const promise = new Promise((resolve, reject) => {
|
|
2502
|
+
let requestDetails = this.HttpxRequestDetails.getDetails("HEAD", details, resolve, reject);
|
|
2464
2503
|
Reflect.deleteProperty(requestDetails, "onprogress");
|
|
2465
|
-
|
|
2466
|
-
|
|
2504
|
+
// @ts-ignore
|
|
2505
|
+
requestDetails = this.HttpxRequestDetails.handle(requestDetails);
|
|
2506
|
+
const requestResult = this.HttpxRequest.request(requestDetails);
|
|
2507
|
+
if (requestResult != null &&
|
|
2508
|
+
typeof requestResult.abort === "function") {
|
|
2509
|
+
abortFn = requestResult.abort;
|
|
2510
|
+
}
|
|
2467
2511
|
});
|
|
2512
|
+
Object.defineProperty(promise, "abort", {
|
|
2513
|
+
value: () => {
|
|
2514
|
+
return () => {
|
|
2515
|
+
if (typeof abortFn === "function") {
|
|
2516
|
+
abortFn();
|
|
2517
|
+
}
|
|
2518
|
+
};
|
|
2519
|
+
},
|
|
2520
|
+
});
|
|
2521
|
+
return promise;
|
|
2468
2522
|
}
|
|
2469
2523
|
/**
|
|
2470
2524
|
* OPTIONS 请求
|
|
2471
2525
|
*/
|
|
2472
|
-
async options(...args
|
|
2473
|
-
|
|
2526
|
+
async options(...args // @ts-ignore
|
|
2527
|
+
) {
|
|
2474
2528
|
let details = this.HttpxRequestDetails.handleBeforeRequestDetails(...args);
|
|
2475
|
-
|
|
2476
|
-
|
|
2529
|
+
let abortFn = null;
|
|
2530
|
+
const promise = new Promise((resolve, reject) => {
|
|
2531
|
+
let requestDetails = this.HttpxRequestDetails.getDetails("OPTIONS", details, resolve, reject);
|
|
2477
2532
|
Reflect.deleteProperty(requestDetails, "onprogress");
|
|
2478
|
-
|
|
2479
|
-
|
|
2533
|
+
// @ts-ignore
|
|
2534
|
+
requestDetails = this.HttpxRequestDetails.handle(requestDetails);
|
|
2535
|
+
const requestResult = this.HttpxRequest.request(requestDetails);
|
|
2536
|
+
if (requestResult != null &&
|
|
2537
|
+
typeof requestResult.abort === "function") {
|
|
2538
|
+
abortFn = requestResult.abort;
|
|
2539
|
+
}
|
|
2480
2540
|
});
|
|
2541
|
+
Object.defineProperty(promise, "abort", {
|
|
2542
|
+
value: () => {
|
|
2543
|
+
return () => {
|
|
2544
|
+
if (typeof abortFn === "function") {
|
|
2545
|
+
abortFn();
|
|
2546
|
+
}
|
|
2547
|
+
};
|
|
2548
|
+
},
|
|
2549
|
+
});
|
|
2550
|
+
return promise;
|
|
2481
2551
|
}
|
|
2482
2552
|
/**
|
|
2483
2553
|
* DELETE 请求
|
|
2484
2554
|
*/
|
|
2485
|
-
async delete(...args
|
|
2486
|
-
|
|
2555
|
+
async delete(...args // @ts-ignore
|
|
2556
|
+
) {
|
|
2487
2557
|
let details = this.HttpxRequestDetails.handleBeforeRequestDetails(...args);
|
|
2488
|
-
|
|
2489
|
-
|
|
2558
|
+
let abortFn = null;
|
|
2559
|
+
const promise = new Promise((resolve, reject) => {
|
|
2560
|
+
let requestDetails = this.HttpxRequestDetails.getDetails("DELETE", details, resolve, reject);
|
|
2490
2561
|
Reflect.deleteProperty(requestDetails, "onprogress");
|
|
2491
|
-
|
|
2492
|
-
|
|
2562
|
+
// @ts-ignore
|
|
2563
|
+
requestDetails = this.HttpxRequestDetails.handle(requestDetails);
|
|
2564
|
+
const requestResult = this.HttpxRequest.request(requestDetails);
|
|
2565
|
+
if (requestResult != null &&
|
|
2566
|
+
typeof requestResult.abort === "function") {
|
|
2567
|
+
abortFn = requestResult.abort;
|
|
2568
|
+
}
|
|
2569
|
+
});
|
|
2570
|
+
Object.defineProperty(promise, "abort", {
|
|
2571
|
+
value: () => {
|
|
2572
|
+
return () => {
|
|
2573
|
+
if (typeof abortFn === "function") {
|
|
2574
|
+
abortFn();
|
|
2575
|
+
}
|
|
2576
|
+
};
|
|
2577
|
+
},
|
|
2493
2578
|
});
|
|
2579
|
+
return promise;
|
|
2494
2580
|
}
|
|
2495
2581
|
/**
|
|
2496
2582
|
* PUT 请求
|
|
2497
2583
|
*/
|
|
2498
|
-
async put(...args
|
|
2499
|
-
|
|
2584
|
+
async put(...args // @ts-ignore
|
|
2585
|
+
) {
|
|
2500
2586
|
let details = this.HttpxRequestDetails.handleBeforeRequestDetails(...args);
|
|
2501
|
-
|
|
2502
|
-
|
|
2503
|
-
requestDetails =
|
|
2504
|
-
|
|
2587
|
+
let abortFn = null;
|
|
2588
|
+
const promise = new Promise((resolve, reject) => {
|
|
2589
|
+
let requestDetails = this.HttpxRequestDetails.getDetails("PUT", details, resolve, reject);
|
|
2590
|
+
// @ts-ignore
|
|
2591
|
+
requestDetails = this.HttpxRequestDetails.handle(requestDetails);
|
|
2592
|
+
const requestResult = this.HttpxRequest.request(requestDetails);
|
|
2593
|
+
if (requestResult != null &&
|
|
2594
|
+
typeof requestResult.abort === "function") {
|
|
2595
|
+
abortFn = requestResult.abort;
|
|
2596
|
+
}
|
|
2505
2597
|
});
|
|
2598
|
+
Object.defineProperty(promise, "abort", {
|
|
2599
|
+
value: () => {
|
|
2600
|
+
return () => {
|
|
2601
|
+
if (typeof abortFn === "function") {
|
|
2602
|
+
abortFn();
|
|
2603
|
+
}
|
|
2604
|
+
};
|
|
2605
|
+
},
|
|
2606
|
+
});
|
|
2607
|
+
return promise;
|
|
2506
2608
|
}
|
|
2507
2609
|
}
|
|
2508
2610
|
|
|
@@ -3551,7 +3653,7 @@ class Utils {
|
|
|
3551
3653
|
this.windowApi = new WindowApi(option);
|
|
3552
3654
|
}
|
|
3553
3655
|
/** 版本号 */
|
|
3554
|
-
version = "2024.
|
|
3656
|
+
version = "2024.9.4";
|
|
3555
3657
|
addStyle(cssText) {
|
|
3556
3658
|
if (typeof cssText !== "string") {
|
|
3557
3659
|
throw new Error("Utils.addStyle 参数cssText 必须为String类型");
|
|
@@ -4935,6 +5037,19 @@ class Utils {
|
|
|
4935
5037
|
return this.windowApi.globalThis.matchMedia("(prefers-color-scheme: dark)")
|
|
4936
5038
|
.matches;
|
|
4937
5039
|
}
|
|
5040
|
+
/**
|
|
5041
|
+
* 判断元素是否在页面中可见
|
|
5042
|
+
* @param element 需要检查的元素,可以是普通元素|数组形式的元素|通过querySelectorAll获取的元素数组
|
|
5043
|
+
* @param inView
|
|
5044
|
+
* + true 在窗口可视区域
|
|
5045
|
+
* + false 不在窗口可视区域
|
|
5046
|
+
* @returns
|
|
5047
|
+
* + true 可见
|
|
5048
|
+
* + false 不可见
|
|
5049
|
+
* @example
|
|
5050
|
+
* Utils.isVisible(document.documentElement)
|
|
5051
|
+
* > true
|
|
5052
|
+
*/
|
|
4938
5053
|
isVisible(element, inView = false) {
|
|
4939
5054
|
let needCheckDomList = [];
|
|
4940
5055
|
if (element instanceof Array || element instanceof NodeList) {
|