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