@whitesev/utils 2.2.1 → 2.2.3

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.
@@ -2173,11 +2173,11 @@ var Utils = (function () {
2173
2173
  }
2174
2174
  if (details.fetch) {
2175
2175
  const { fetchDetails, fetchRequestInit, abortController } = this.context.HttpxRequestDetails.handleFetchDetail(details);
2176
- this.fetch(fetchDetails, fetchRequestInit, abortController);
2176
+ return this.fetch(fetchDetails, fetchRequestInit, abortController);
2177
2177
  }
2178
2178
  else {
2179
2179
  Reflect.deleteProperty(details, "fetchInit");
2180
- this.xmlHttpRequest(details);
2180
+ return this.xmlHttpRequest(details);
2181
2181
  }
2182
2182
  },
2183
2183
  /**
@@ -2185,7 +2185,7 @@ var Utils = (function () {
2185
2185
  * @param details
2186
2186
  */
2187
2187
  xmlHttpRequest(details) {
2188
- this.context.GM_Api.xmlHttpRequest(details);
2188
+ return this.context.GM_Api.xmlHttpRequest(details);
2189
2189
  },
2190
2190
  /**
2191
2191
  * 使用fetch发送请求
@@ -2336,6 +2336,10 @@ var Utils = (function () {
2336
2336
  * 当前使用请求时,输出请求的配置
2337
2337
  */
2338
2338
  #LOG_DETAILS = false;
2339
+ /**
2340
+ * 实例化,可传入GM_xmlhttpRequest,未传入则使用window.fetch
2341
+ * @param __xmlHttpRequest__
2342
+ */
2339
2343
  constructor(__xmlHttpRequest__) {
2340
2344
  if (typeof __xmlHttpRequest__ !== "function") {
2341
2345
  console.warn("Httpx未传入GM_xmlhttpRequest函数或传入的GM_xmlhttpRequest不是Function,强制使用window.fetch");
@@ -2431,79 +2435,177 @@ var Utils = (function () {
2431
2435
  }
2432
2436
  /**
2433
2437
  * GET 请求
2438
+ * @param url 网址
2439
+ * @param details 配置
2434
2440
  */
2435
- async get(...args) {
2436
- let that = this;
2441
+ async get(...args // @ts-ignore
2442
+ ) {
2437
2443
  let details = this.HttpxRequestDetails.handleBeforeRequestDetails(...args);
2438
- return new Promise((resolve, reject) => {
2439
- let requestDetails = that.HttpxRequestDetails.getDetails("GET", details, resolve, reject);
2444
+ let abortFn = null;
2445
+ const promise = new Promise((resolve, reject) => {
2446
+ let requestDetails = this.HttpxRequestDetails.getDetails("GET", details, resolve, reject);
2440
2447
  Reflect.deleteProperty(requestDetails, "onprogress");
2441
- requestDetails = that.HttpxRequestDetails.handle(requestDetails);
2442
- that.HttpxRequest.request(requestDetails);
2448
+ // @ts-ignore
2449
+ requestDetails = this.HttpxRequestDetails.handle(requestDetails);
2450
+ const requestResult = this.HttpxRequest.request(requestDetails);
2451
+ if (requestResult != null &&
2452
+ typeof requestResult.abort === "function") {
2453
+ abortFn = requestResult.abort;
2454
+ }
2443
2455
  });
2456
+ Object.defineProperty(promise, "abort", {
2457
+ value: () => {
2458
+ return () => {
2459
+ if (typeof abortFn === "function") {
2460
+ abortFn();
2461
+ }
2462
+ };
2463
+ },
2464
+ });
2465
+ return promise;
2444
2466
  }
2445
2467
  /**
2446
2468
  * POST 请求
2447
2469
  */
2448
- async post(...args) {
2449
- let that = this;
2470
+ async post(...args // @ts-ignore
2471
+ ) {
2450
2472
  let details = this.HttpxRequestDetails.handleBeforeRequestDetails(...args);
2451
- return new Promise((resolve, reject) => {
2452
- let requestDetails = that.HttpxRequestDetails.getDetails("POST", details, resolve, reject);
2453
- requestDetails = that.HttpxRequestDetails.handle(requestDetails);
2454
- that.HttpxRequest.request(requestDetails);
2473
+ let abortFn = null;
2474
+ const promise = new Promise((resolve, reject) => {
2475
+ let requestDetails = this.HttpxRequestDetails.getDetails("POST", details, resolve, reject);
2476
+ // @ts-ignore
2477
+ requestDetails = this.HttpxRequestDetails.handle(requestDetails);
2478
+ const requestResult = this.HttpxRequest.request(requestDetails);
2479
+ if (requestResult != null &&
2480
+ typeof requestResult.abort === "function") {
2481
+ abortFn = requestResult.abort;
2482
+ }
2455
2483
  });
2484
+ Object.defineProperty(promise, "abort", {
2485
+ value: () => {
2486
+ return () => {
2487
+ if (typeof abortFn === "function") {
2488
+ abortFn();
2489
+ }
2490
+ };
2491
+ },
2492
+ });
2493
+ return promise;
2456
2494
  }
2457
2495
  /**
2458
2496
  * HEAD 请求
2459
2497
  */
2460
- async head(...args) {
2461
- let that = this;
2498
+ async head(...args // @ts-ignore
2499
+ ) {
2462
2500
  let details = this.HttpxRequestDetails.handleBeforeRequestDetails(...args);
2463
- return new Promise((resolve, reject) => {
2464
- let requestDetails = that.HttpxRequestDetails.getDetails("HEAD", details, resolve, reject);
2501
+ let abortFn = null;
2502
+ const promise = new Promise((resolve, reject) => {
2503
+ let requestDetails = this.HttpxRequestDetails.getDetails("HEAD", details, resolve, reject);
2465
2504
  Reflect.deleteProperty(requestDetails, "onprogress");
2466
- requestDetails = that.HttpxRequestDetails.handle(requestDetails);
2467
- that.HttpxRequest.request(requestDetails);
2505
+ // @ts-ignore
2506
+ requestDetails = this.HttpxRequestDetails.handle(requestDetails);
2507
+ const requestResult = this.HttpxRequest.request(requestDetails);
2508
+ if (requestResult != null &&
2509
+ typeof requestResult.abort === "function") {
2510
+ abortFn = requestResult.abort;
2511
+ }
2512
+ });
2513
+ Object.defineProperty(promise, "abort", {
2514
+ value: () => {
2515
+ return () => {
2516
+ if (typeof abortFn === "function") {
2517
+ abortFn();
2518
+ }
2519
+ };
2520
+ },
2468
2521
  });
2522
+ return promise;
2469
2523
  }
2470
2524
  /**
2471
2525
  * OPTIONS 请求
2472
2526
  */
2473
- async options(...args) {
2474
- let that = this;
2527
+ async options(...args // @ts-ignore
2528
+ ) {
2475
2529
  let details = this.HttpxRequestDetails.handleBeforeRequestDetails(...args);
2476
- return new Promise((resolve, reject) => {
2477
- let requestDetails = that.HttpxRequestDetails.getDetails("OPTIONS", details, resolve, reject);
2530
+ let abortFn = null;
2531
+ const promise = new Promise((resolve, reject) => {
2532
+ let requestDetails = this.HttpxRequestDetails.getDetails("OPTIONS", details, resolve, reject);
2478
2533
  Reflect.deleteProperty(requestDetails, "onprogress");
2479
- requestDetails = that.HttpxRequestDetails.handle(requestDetails);
2480
- that.HttpxRequest.request(requestDetails);
2534
+ // @ts-ignore
2535
+ requestDetails = this.HttpxRequestDetails.handle(requestDetails);
2536
+ const requestResult = this.HttpxRequest.request(requestDetails);
2537
+ if (requestResult != null &&
2538
+ typeof requestResult.abort === "function") {
2539
+ abortFn = requestResult.abort;
2540
+ }
2481
2541
  });
2542
+ Object.defineProperty(promise, "abort", {
2543
+ value: () => {
2544
+ return () => {
2545
+ if (typeof abortFn === "function") {
2546
+ abortFn();
2547
+ }
2548
+ };
2549
+ },
2550
+ });
2551
+ return promise;
2482
2552
  }
2483
2553
  /**
2484
2554
  * DELETE 请求
2485
2555
  */
2486
- async delete(...args) {
2487
- let that = this;
2556
+ async delete(...args // @ts-ignore
2557
+ ) {
2488
2558
  let details = this.HttpxRequestDetails.handleBeforeRequestDetails(...args);
2489
- return new Promise((resolve, reject) => {
2490
- let requestDetails = that.HttpxRequestDetails.getDetails("DELETE", details, resolve, reject);
2559
+ let abortFn = null;
2560
+ const promise = new Promise((resolve, reject) => {
2561
+ let requestDetails = this.HttpxRequestDetails.getDetails("DELETE", details, resolve, reject);
2491
2562
  Reflect.deleteProperty(requestDetails, "onprogress");
2492
- requestDetails = that.HttpxRequestDetails.handle(requestDetails);
2493
- that.HttpxRequest.request(requestDetails);
2563
+ // @ts-ignore
2564
+ requestDetails = this.HttpxRequestDetails.handle(requestDetails);
2565
+ const requestResult = this.HttpxRequest.request(requestDetails);
2566
+ if (requestResult != null &&
2567
+ typeof requestResult.abort === "function") {
2568
+ abortFn = requestResult.abort;
2569
+ }
2570
+ });
2571
+ Object.defineProperty(promise, "abort", {
2572
+ value: () => {
2573
+ return () => {
2574
+ if (typeof abortFn === "function") {
2575
+ abortFn();
2576
+ }
2577
+ };
2578
+ },
2494
2579
  });
2580
+ return promise;
2495
2581
  }
2496
2582
  /**
2497
2583
  * PUT 请求
2498
2584
  */
2499
- async put(...args) {
2500
- let that = this;
2585
+ async put(...args // @ts-ignore
2586
+ ) {
2501
2587
  let details = this.HttpxRequestDetails.handleBeforeRequestDetails(...args);
2502
- return new Promise((resolve, reject) => {
2503
- let requestDetails = that.HttpxRequestDetails.getDetails("PUT", details, resolve, reject);
2504
- requestDetails = that.HttpxRequestDetails.handle(requestDetails);
2505
- that.HttpxRequest.request(requestDetails);
2588
+ let abortFn = null;
2589
+ const promise = new Promise((resolve, reject) => {
2590
+ let requestDetails = this.HttpxRequestDetails.getDetails("PUT", details, resolve, reject);
2591
+ // @ts-ignore
2592
+ requestDetails = this.HttpxRequestDetails.handle(requestDetails);
2593
+ const requestResult = this.HttpxRequest.request(requestDetails);
2594
+ if (requestResult != null &&
2595
+ typeof requestResult.abort === "function") {
2596
+ abortFn = requestResult.abort;
2597
+ }
2598
+ });
2599
+ Object.defineProperty(promise, "abort", {
2600
+ value: () => {
2601
+ return () => {
2602
+ if (typeof abortFn === "function") {
2603
+ abortFn();
2604
+ }
2605
+ };
2606
+ },
2506
2607
  });
2608
+ return promise;
2507
2609
  }
2508
2610
  }
2509
2611
 
@@ -3552,7 +3654,7 @@ var Utils = (function () {
3552
3654
  this.windowApi = new WindowApi(option);
3553
3655
  }
3554
3656
  /** 版本号 */
3555
- version = "2024.9.1";
3657
+ version = "2024.9.4";
3556
3658
  addStyle(cssText) {
3557
3659
  if (typeof cssText !== "string") {
3558
3660
  throw new Error("Utils.addStyle 参数cssText 必须为String类型");
@@ -5187,6 +5289,41 @@ var Utils = (function () {
5187
5289
  }
5188
5290
  return mutationObserver;
5189
5291
  }
5292
+ /**
5293
+ * 使用观察器观察元素出现在视图内,出现的话触发回调
5294
+ * @param target 目标元素
5295
+ * @param callback 触发的回调
5296
+ * @param options 观察器配置
5297
+ */
5298
+ mutatuinVisible(target, callback, options) {
5299
+ if (typeof IntersectionObserver === "undefined") {
5300
+ throw new TypeError("IntersectionObserver is not defined");
5301
+ }
5302
+ if (target == null) {
5303
+ throw new TypeError("mutatuinVisible target is null");
5304
+ }
5305
+ let defaultOptions = {
5306
+ root: null,
5307
+ rootMargin: "0px 0px 0px 0px",
5308
+ threshold: [0.01, 0.99],
5309
+ };
5310
+ defaultOptions = this.assign(defaultOptions, options || {});
5311
+ let intersectionObserver = new IntersectionObserver((entries, observer) => {
5312
+ if (entries[0].isIntersecting) {
5313
+ if (typeof callback === "function") {
5314
+ callback(entries, observer);
5315
+ }
5316
+ }
5317
+ }, defaultOptions);
5318
+ if (Array.isArray(target)) {
5319
+ target.forEach((item) => {
5320
+ intersectionObserver.observe(item);
5321
+ });
5322
+ }
5323
+ else {
5324
+ intersectionObserver.observe(target);
5325
+ }
5326
+ }
5190
5327
  /**
5191
5328
  * 去除全局window下的Utils,返回控制权
5192
5329
  * @example