@whitesev/utils 2.7.2 → 2.7.4

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.
Files changed (42) hide show
  1. package/dist/index.amd.js +197 -350
  2. package/dist/index.amd.js.map +1 -1
  3. package/dist/index.cjs.js +197 -350
  4. package/dist/index.cjs.js.map +1 -1
  5. package/dist/index.esm.js +197 -350
  6. package/dist/index.esm.js.map +1 -1
  7. package/dist/index.iife.js +197 -350
  8. package/dist/index.iife.js.map +1 -1
  9. package/dist/index.system.js +197 -350
  10. package/dist/index.system.js.map +1 -1
  11. package/dist/index.umd.js +197 -350
  12. package/dist/index.umd.js.map +1 -1
  13. package/dist/types/src/Utils.d.ts +30 -2
  14. package/dist/types/src/WindowApi.d.ts +4 -0
  15. package/dist/types/src/types/Event.d.ts +1 -2
  16. package/dist/types/src/types/Httpx.d.ts +4 -21
  17. package/dist/types/src/types/WindowApi.d.ts +4 -0
  18. package/dist/types/src/types/ajaxHooker.d.ts +1 -5
  19. package/package.json +1 -1
  20. package/src/ColorConversion.ts +5 -18
  21. package/src/CommonUtil.ts +8 -31
  22. package/src/DOMUtils.ts +9 -22
  23. package/src/Dictionary.ts +2 -7
  24. package/src/GBKEncoder.ts +1 -6
  25. package/src/Hooks.ts +1 -4
  26. package/src/Httpx.ts +102 -277
  27. package/src/LockFunction.ts +1 -3
  28. package/src/Log.ts +7 -23
  29. package/src/Progress.ts +2 -10
  30. package/src/TryCatch.ts +3 -11
  31. package/src/Utils.ts +213 -559
  32. package/src/UtilsCommon.ts +5 -9
  33. package/src/UtilsGMCookie.ts +1 -4
  34. package/src/UtilsGMMenu.ts +10 -29
  35. package/src/Vue.ts +2 -11
  36. package/src/WindowApi.ts +16 -0
  37. package/src/ajaxHooker/ajaxHooker.js +78 -142
  38. package/src/indexedDB.ts +3 -12
  39. package/src/types/Event.d.ts +1 -2
  40. package/src/types/Httpx.d.ts +4 -21
  41. package/src/types/WindowApi.d.ts +4 -0
  42. package/src/types/ajaxHooker.d.ts +1 -5
package/src/Httpx.ts CHANGED
@@ -79,9 +79,7 @@ export class Httpx {
79
79
  });
80
80
  return uuid;
81
81
  } else {
82
- console.warn(
83
- "[Httpx-HttpxRequestHook.addBeforeRequestCallBack] fn is not a function"
84
- );
82
+ console.warn("[Httpx-HttpxRequestHook.addBeforeRequestCallBack] fn is not a function");
85
83
  }
86
84
  },
87
85
  /**
@@ -90,9 +88,7 @@ export class Httpx {
90
88
  */
91
89
  delete(id: string) {
92
90
  if (typeof id === "string") {
93
- let findIndex = this.$config.configList.findIndex(
94
- (item) => item.id === id
95
- );
91
+ let findIndex = this.$config.configList.findIndex((item) => item.id === id);
96
92
  if (findIndex !== -1) {
97
93
  this.$config.configList.splice(findIndex, 1);
98
94
  return true;
@@ -139,8 +135,7 @@ export class Httpx {
139
135
  // 配置存在
140
136
  // 细分处理是否拦截
141
137
  if (
142
- typeof details.allowInterceptConfig.afterResponseSuccess ===
143
- "boolean" &&
138
+ typeof details.allowInterceptConfig.afterResponseSuccess === "boolean" &&
144
139
  !details.allowInterceptConfig.afterResponseSuccess
145
140
  ) {
146
141
  // 设置了禁止拦截
@@ -179,8 +174,7 @@ export class Httpx {
179
174
  // 配置存在
180
175
  // 细分处理是否拦截
181
176
  if (
182
- typeof data.details.allowInterceptConfig.afterResponseError ===
183
- "boolean" &&
177
+ typeof data.details.allowInterceptConfig.afterResponseError === "boolean" &&
184
178
  !data.details.allowInterceptConfig.afterResponseError
185
179
  ) {
186
180
  // 设置了禁止拦截
@@ -220,9 +214,7 @@ export class Httpx {
220
214
  */
221
215
  delete(id: string) {
222
216
  if (typeof id === "string") {
223
- let findIndex = this.$config.configList.findIndex(
224
- (item) => item.id === id
225
- );
217
+ let findIndex = this.$config.configList.findIndex((item) => item.id === id);
226
218
  if (findIndex !== -1) {
227
219
  this.$config.configList.splice(findIndex, 1);
228
220
  return true;
@@ -293,68 +285,41 @@ export class Httpx {
293
285
  let requestOption = <Required<HttpxRequestOption>>{
294
286
  url: url,
295
287
  method: (method || "GET").toString().toUpperCase().trim(),
296
- timeout:
297
- userRequestOption.timeout ||
298
- this.context.#defaultRequestOption.timeout,
299
- responseType:
300
- userRequestOption.responseType ||
301
- this.context.#defaultRequestOption.responseType,
288
+ timeout: userRequestOption.timeout || this.context.#defaultRequestOption.timeout,
289
+ responseType: userRequestOption.responseType || this.context.#defaultRequestOption.responseType,
302
290
  /* 对象使用深拷贝 */
303
- headers: CommonUtil.deepClone(
304
- this.context.#defaultRequestOption.headers
305
- ),
291
+ headers: CommonUtil.deepClone(this.context.#defaultRequestOption.headers),
306
292
  data: userRequestOption.data || this.context.#defaultRequestOption.data,
307
- redirect:
308
- userRequestOption.redirect ||
309
- this.context.#defaultRequestOption.redirect,
310
- cookie:
311
- userRequestOption.cookie || this.context.#defaultRequestOption.cookie,
293
+ redirect: userRequestOption.redirect || this.context.#defaultRequestOption.redirect,
294
+ cookie: userRequestOption.cookie || this.context.#defaultRequestOption.cookie,
312
295
  cookiePartition:
313
- userRequestOption.cookiePartition ||
314
- this.context.#defaultRequestOption.cookiePartition,
315
- binary:
316
- userRequestOption.binary || this.context.#defaultRequestOption.binary,
317
- nocache:
318
- userRequestOption.nocache ||
319
- this.context.#defaultRequestOption.nocache,
320
- revalidate:
321
- userRequestOption.revalidate ||
322
- this.context.#defaultRequestOption.revalidate,
296
+ userRequestOption.cookiePartition || this.context.#defaultRequestOption.cookiePartition,
297
+ binary: userRequestOption.binary || this.context.#defaultRequestOption.binary,
298
+ nocache: userRequestOption.nocache || this.context.#defaultRequestOption.nocache,
299
+ revalidate: userRequestOption.revalidate || this.context.#defaultRequestOption.revalidate,
323
300
  /* 对象使用深拷贝 */
324
301
  context: CommonUtil.deepClone(
325
- userRequestOption.context ||
326
- this.context.#defaultRequestOption.context
302
+ userRequestOption.context || this.context.#defaultRequestOption.context
327
303
  ),
328
304
  overrideMimeType:
329
- userRequestOption.overrideMimeType ||
330
- this.context.#defaultRequestOption.overrideMimeType,
331
- anonymous:
332
- userRequestOption.anonymous ||
333
- this.context.#defaultRequestOption.anonymous,
334
- fetch:
335
- userRequestOption.fetch || this.context.#defaultRequestOption.fetch,
305
+ userRequestOption.overrideMimeType || this.context.#defaultRequestOption.overrideMimeType,
306
+ anonymous: userRequestOption.anonymous || this.context.#defaultRequestOption.anonymous,
307
+ fetch: userRequestOption.fetch || this.context.#defaultRequestOption.fetch,
336
308
  /* 对象使用深拷贝 */
337
- fetchInit: CommonUtil.deepClone(
338
- this.context.#defaultRequestOption.fetchInit
339
- ),
309
+ fetchInit: CommonUtil.deepClone(this.context.#defaultRequestOption.fetchInit),
340
310
  allowInterceptConfig: {
341
311
  beforeRequest: (
342
- this.context.#defaultRequestOption
343
- .allowInterceptConfig as HttpxAllowInterceptConfig
312
+ this.context.#defaultRequestOption.allowInterceptConfig as HttpxAllowInterceptConfig
344
313
  ).beforeRequest,
345
314
  afterResponseSuccess: (
346
- this.context.#defaultRequestOption
347
- .allowInterceptConfig as HttpxAllowInterceptConfig
315
+ this.context.#defaultRequestOption.allowInterceptConfig as HttpxAllowInterceptConfig
348
316
  ).afterResponseSuccess,
349
317
  afterResponseError: (
350
- this.context.#defaultRequestOption
351
- .allowInterceptConfig as HttpxAllowInterceptConfig
318
+ this.context.#defaultRequestOption.allowInterceptConfig as HttpxAllowInterceptConfig
352
319
  ).afterResponseError,
353
320
  },
354
321
  user: userRequestOption.user || this.context.#defaultRequestOption.user,
355
- password:
356
- userRequestOption.password ||
357
- this.context.#defaultRequestOption.password,
322
+ password: userRequestOption.password || this.context.#defaultRequestOption.password,
358
323
  onabort(...args) {
359
324
  that.context.HttpxResponseCallBack.onAbort(
360
325
  userRequestOption as Required<HttpxRequestOption>,
@@ -408,9 +373,7 @@ export class Httpx {
408
373
  };
409
374
  // 补全allowInterceptConfig参数
410
375
  if (typeof userRequestOption.allowInterceptConfig === "boolean") {
411
- Object.keys(
412
- requestOption.allowInterceptConfig as HttpxAllowInterceptConfig
413
- ).forEach((keyName) => {
376
+ Object.keys(requestOption.allowInterceptConfig as HttpxAllowInterceptConfig).forEach((keyName) => {
414
377
  Reflect.set(
415
378
  requestOption.allowInterceptConfig as HttpxAllowInterceptConfig,
416
379
  keyName,
@@ -422,27 +385,18 @@ export class Httpx {
422
385
  typeof userRequestOption.allowInterceptConfig === "object" &&
423
386
  userRequestOption.allowInterceptConfig != null
424
387
  ) {
425
- Object.keys(userRequestOption.allowInterceptConfig).forEach(
426
- (keyName) => {
427
- let value = Reflect.get(
428
- userRequestOption.allowInterceptConfig as HttpxAllowInterceptConfig,
429
- keyName
430
- ) as Boolean;
431
- if (
432
- typeof value === "boolean" &&
433
- Reflect.has(
434
- requestOption.allowInterceptConfig as HttpxAllowInterceptConfig,
435
- keyName
436
- )
437
- ) {
438
- Reflect.set(
439
- requestOption.allowInterceptConfig as HttpxAllowInterceptConfig,
440
- keyName,
441
- value
442
- );
443
- }
388
+ Object.keys(userRequestOption.allowInterceptConfig).forEach((keyName) => {
389
+ let value = Reflect.get(
390
+ userRequestOption.allowInterceptConfig as HttpxAllowInterceptConfig,
391
+ keyName
392
+ ) as Boolean;
393
+ if (
394
+ typeof value === "boolean" &&
395
+ Reflect.has(requestOption.allowInterceptConfig as HttpxAllowInterceptConfig, keyName)
396
+ ) {
397
+ Reflect.set(requestOption.allowInterceptConfig as HttpxAllowInterceptConfig, keyName, value);
444
398
  }
445
- );
399
+ });
446
400
  }
447
401
  }
448
402
  if (typeof this.context.GM_Api.xmlHttpRequest !== "function") {
@@ -452,15 +406,11 @@ export class Httpx {
452
406
  if (typeof requestOption.headers === "object") {
453
407
  if (typeof userRequestOption.headers === "object") {
454
408
  Object.keys(userRequestOption.headers).forEach((keyName, index) => {
455
- if (
456
- keyName in requestOption.headers &&
457
- userRequestOption!.headers?.[keyName] == null
458
- ) {
409
+ if (keyName in requestOption.headers && userRequestOption!.headers?.[keyName] == null) {
459
410
  /* 在默认的header中存在,且设置它新的值为空,那么就是默认的值 */
460
411
  Reflect.deleteProperty(requestOption.headers, keyName);
461
412
  } else {
462
- requestOption.headers[keyName] =
463
- userRequestOption?.headers?.[keyName];
413
+ requestOption.headers[keyName] = userRequestOption?.headers?.[keyName];
464
414
  }
465
415
  });
466
416
  } else {
@@ -475,10 +425,7 @@ export class Httpx {
475
425
  /* 使用assign替换且添加 */
476
426
  if (typeof userRequestOption.fetchInit === "object") {
477
427
  Object.keys(userRequestOption.fetchInit).forEach((keyName, index) => {
478
- if (
479
- keyName in requestOption.fetchInit &&
480
- (userRequestOption as any).fetchInit[keyName] == null
481
- ) {
428
+ if (keyName in requestOption.fetchInit && (userRequestOption as any).fetchInit[keyName] == null) {
482
429
  /* 在默认的fetchInit中存在,且设置它新的值为空,那么就是默认的值 */
483
430
  Reflect.deleteProperty(requestOption.fetchInit, keyName);
484
431
  } else {
@@ -495,10 +442,7 @@ export class Httpx {
495
442
  }
496
443
 
497
444
  // 处理新的cookiePartition
498
- if (
499
- typeof requestOption.cookiePartition === "object" &&
500
- requestOption.cookiePartition != null
501
- ) {
445
+ if (typeof requestOption.cookiePartition === "object" && requestOption.cookiePartition != null) {
502
446
  if (
503
447
  Reflect.has(requestOption.cookiePartition, "topLevelSite") &&
504
448
  typeof requestOption.cookiePartition.topLevelSite !== "string"
@@ -520,8 +464,7 @@ export class Httpx {
520
464
  requestOption.url = globalThis.location.origin + requestOption.url;
521
465
  } else {
522
466
  // 补充origin+/
523
- requestOption.url =
524
- globalThis.location.origin + "/" + requestOption.url;
467
+ requestOption.url = globalThis.location.origin + "/" + requestOption.url;
525
468
  }
526
469
  }
527
470
 
@@ -547,9 +490,7 @@ export class Httpx {
547
490
  } else if (typeof requestOption.data === "object") {
548
491
  isHandler = true;
549
492
  // URLSearchParams参数可以转普通的string:string,包括FormData
550
- let searchParams = new URLSearchParams(
551
- requestOption.data as Record<string, string>
552
- );
493
+ let searchParams = new URLSearchParams(requestOption.data as Record<string, string>);
553
494
  urlSearch = searchParams.toString();
554
495
  }
555
496
  if (isHandler) {
@@ -597,9 +538,7 @@ export class Httpx {
597
538
  } else if (typeof requestOption.data === "object") {
598
539
  requestOption.data = JSON.stringify(requestOption.data);
599
540
  }
600
- } else if (
601
- ContentType.includes("application/x-www-form-urlencoded")
602
- ) {
541
+ } else if (ContentType.includes("application/x-www-form-urlencoded")) {
603
542
  // application/x-www-form-urlencoded
604
543
  if (typeof requestOption.data === "object") {
605
544
  requestOption.data = new URLSearchParams(
@@ -625,9 +564,7 @@ export class Httpx {
625
564
  * 处理发送请求的配置,去除值为undefined、空function的值
626
565
  * @param option 请求配置
627
566
  */
628
- removeRequestNullOption(
629
- option: Required<HttpxRequestOption>
630
- ): HttpxRequestOption {
567
+ removeRequestNullOption(option: Required<HttpxRequestOption>): HttpxRequestOption {
631
568
  Object.keys(option).forEach((keyName) => {
632
569
  if (
633
570
  option[keyName as keyof HttpxRequestOption] == null ||
@@ -652,10 +589,7 @@ export class Httpx {
652
589
  * fetch的请求配置
653
590
  **/
654
591
  let fetchRequestOption = <RequestInit>{};
655
- if (
656
- (option.method === "GET" || option.method === "HEAD") &&
657
- option.data != null
658
- ) {
592
+ if ((option.method === "GET" || option.method === "HEAD") && option.data != null) {
659
593
  /* GET 或 HEAD 方法的请求不能包含 body 信息 */
660
594
  Reflect.deleteProperty(option, "data");
661
595
  }
@@ -718,9 +652,7 @@ export class Httpx {
718
652
  // console.log(argsResult);
719
653
  if (typeof details?.onabort === "function") {
720
654
  details.onabort.apply(this, argsResult);
721
- } else if (
722
- typeof this.context.#defaultRequestOption?.onabort === "function"
723
- ) {
655
+ } else if (typeof this.context.#defaultRequestOption?.onabort === "function") {
724
656
  this.context.#defaultRequestOption.onabort.apply(this, argsResult);
725
657
  }
726
658
  let response = argsResult;
@@ -764,9 +696,7 @@ export class Httpx {
764
696
  if (typeof details?.ontimeout === "function") {
765
697
  // 执行配置中的ontime回调
766
698
  details.ontimeout.apply(this, argsResult);
767
- } else if (
768
- typeof this.context.#defaultRequestOption?.ontimeout === "function"
769
- ) {
699
+ } else if (typeof this.context.#defaultRequestOption?.ontimeout === "function") {
770
700
  // 执行默认配置的ontime回调
771
701
  this.context.#defaultRequestOption.ontimeout.apply(this, argsResult);
772
702
  }
@@ -812,9 +742,7 @@ export class Httpx {
812
742
  // console.log(argsResult);
813
743
  if (typeof details?.onerror === "function") {
814
744
  details.onerror.apply(this, argsResult);
815
- } else if (
816
- typeof this.context.#defaultRequestOption?.onerror === "function"
817
- ) {
745
+ } else if (typeof this.context.#defaultRequestOption?.onerror === "function") {
818
746
  this.context.#defaultRequestOption.onerror.apply(this, argsResult);
819
747
  }
820
748
  let response = argsResult;
@@ -864,9 +792,7 @@ export class Httpx {
864
792
  ) {
865
793
  if (typeof originResponse["response"] === "object") {
866
794
  TryCatch().run(() => {
867
- originResponse["responseText"] = JSON.stringify(
868
- originResponse["response"]
869
- );
795
+ originResponse["responseText"] = JSON.stringify(originResponse["response"]);
870
796
  });
871
797
  } else {
872
798
  originResponse["responseText"] = originResponse["response"] as string;
@@ -887,10 +813,7 @@ export class Httpx {
887
813
  httpxResponse = CommonUtil.toJSON(httpxResponseText);
888
814
  } else if (details.responseType === "document") {
889
815
  let parser = new DOMParser();
890
- httpxResponse = parser.parseFromString(
891
- httpxResponseText,
892
- "text/html"
893
- );
816
+ httpxResponse = parser.parseFromString(httpxResponseText, "text/html");
894
817
  } else if (details.responseType === "arraybuffer") {
895
818
  let encoder = new TextEncoder();
896
819
  let arrayBuffer = encoder.encode(httpxResponseText);
@@ -902,33 +825,21 @@ export class Httpx {
902
825
  }
903
826
  // 尝试覆盖原response
904
827
  try {
905
- let setStatus = Reflect.set(
906
- originResponse,
907
- "response",
908
- httpxResponse
909
- );
828
+ let setStatus = Reflect.set(originResponse, "response", httpxResponse);
910
829
  if (!setStatus) {
911
- console.warn(
912
- "[Httpx-HttpxCallBack.oonLoad] 覆盖原始 response 失败,尝试添加新的httpxResponse"
913
- );
830
+ console.warn("[Httpx-HttpxCallBack.oonLoad] 覆盖原始 response 失败,尝试添加新的httpxResponse");
914
831
  try {
915
832
  Reflect.set(originResponse, "httpxResponse", httpxResponse);
916
833
  } catch (error) {
917
- console.warn(
918
- "[Httpx-HttpxCallBack.oonLoad] httpxResponse 无法被覆盖"
919
- );
834
+ console.warn("[Httpx-HttpxCallBack.oonLoad] httpxResponse 无法被覆盖");
920
835
  }
921
836
  }
922
837
  } catch (error) {
923
- console.warn(
924
- "[Httpx-HttpxCallBack.oonLoad] 原始 response 无法被覆盖,尝试添加新的httpxResponse"
925
- );
838
+ console.warn("[Httpx-HttpxCallBack.oonLoad] 原始 response 无法被覆盖,尝试添加新的httpxResponse");
926
839
  try {
927
840
  Reflect.set(originResponse, "httpxResponse", httpxResponse);
928
841
  } catch (error) {
929
- console.warn(
930
- "[Httpx-HttpxCallBack.oonLoad] httpxResponse 无法被覆盖"
931
- );
842
+ console.warn("[Httpx-HttpxCallBack.oonLoad] httpxResponse 无法被覆盖");
932
843
  }
933
844
  }
934
845
  }
@@ -940,12 +851,7 @@ export class Httpx {
940
851
 
941
852
  /* 状态码2xx都是成功的 */
942
853
  if (Math.floor(originResponse.status / 100) === 2) {
943
- if (
944
- (await this.context.HttpxResponseHook.successResponseCallBack(
945
- originResponse,
946
- details
947
- )) == null
948
- ) {
854
+ if ((await this.context.HttpxResponseHook.successResponseCallBack(originResponse, details)) == null) {
949
855
  // reject(new Error("response is intercept with onloada"));
950
856
  return;
951
857
  }
@@ -958,12 +864,7 @@ export class Httpx {
958
864
  type: "onload",
959
865
  });
960
866
  } else {
961
- this.context.HttpxResponseCallBack.onError(
962
- details,
963
- resolve,
964
- reject,
965
- argsResult
966
- );
867
+ this.context.HttpxResponseCallBack.onError(details, resolve, reject, argsResult);
967
868
  }
968
869
  },
969
870
  /**
@@ -975,9 +876,7 @@ export class Httpx {
975
876
  // console.log(argsResult);
976
877
  if (typeof details?.onloadstart === "function") {
977
878
  details.onloadstart.apply(this, argsResult);
978
- } else if (
979
- typeof this.context.#defaultRequestOption?.onloadstart === "function"
980
- ) {
879
+ } else if (typeof this.context.#defaultRequestOption?.onloadstart === "function") {
981
880
  this.context.#defaultRequestOption.onloadstart.apply(this, argsResult);
982
881
  }
983
882
  },
@@ -986,21 +885,12 @@ export class Httpx {
986
885
  * @param details 配置
987
886
  * @param argsResult 返回的参数列表
988
887
  */
989
- onReadyStateChange(
990
- details: Required<HttpxRequestOption>,
991
- argsResult: any[]
992
- ) {
888
+ onReadyStateChange(details: Required<HttpxRequestOption>, argsResult: any[]) {
993
889
  // console.log(argsResult);
994
890
  if (typeof details?.onreadystatechange === "function") {
995
891
  details.onreadystatechange.apply(this, argsResult);
996
- } else if (
997
- typeof this.context.#defaultRequestOption?.onreadystatechange ===
998
- "function"
999
- ) {
1000
- this.context.#defaultRequestOption.onreadystatechange.apply(
1001
- this,
1002
- argsResult
1003
- );
892
+ } else if (typeof this.context.#defaultRequestOption?.onreadystatechange === "function") {
893
+ this.context.#defaultRequestOption.onreadystatechange.apply(this, argsResult);
1004
894
  }
1005
895
  },
1006
896
  /**
@@ -1012,9 +902,7 @@ export class Httpx {
1012
902
  // console.log(argsResult);
1013
903
  if (typeof details?.onprogress === "function") {
1014
904
  details.onprogress.apply(this, argsResult);
1015
- } else if (
1016
- typeof this.context.#defaultRequestOption?.onprogress === "function"
1017
- ) {
905
+ } else if (typeof this.context.#defaultRequestOption?.onprogress === "function") {
1018
906
  this.context.#defaultRequestOption.onprogress.apply(this, argsResult);
1019
907
  }
1020
908
  },
@@ -1029,12 +917,8 @@ export class Httpx {
1029
917
  if (this.context.#defaultInitOption.logDetails) {
1030
918
  console.log("[Httpx-HttpxRequest.request] 请求前的配置👇", details);
1031
919
  }
1032
- if (
1033
- typeof this.context.HttpxRequestHook.beforeRequestCallBack ===
1034
- "function"
1035
- ) {
1036
- let hookResult =
1037
- await this.context.HttpxRequestHook.beforeRequestCallBack(details);
920
+ if (typeof this.context.HttpxRequestHook.beforeRequestCallBack === "function") {
921
+ let hookResult = await this.context.HttpxRequestHook.beforeRequestCallBack(details);
1038
922
  if (hookResult == null) {
1039
923
  return;
1040
924
  }
@@ -1102,9 +986,7 @@ export class Httpx {
1102
986
  if (
1103
987
  option.responseType === "stream" ||
1104
988
  (fetchResponse.headers.has("Content-Type") &&
1105
- fetchResponse.headers
1106
- .get("Content-Type")!
1107
- .includes("text/event-stream"))
989
+ fetchResponse.headers.get("Content-Type")!.includes("text/event-stream"))
1108
990
  ) {
1109
991
  Reflect.set(httpxResponse, "isStream", true);
1110
992
  Reflect.set(httpxResponse, "response", fetchResponse.body);
@@ -1126,9 +1008,7 @@ export class Httpx {
1126
1008
  /** 数据编码 */
1127
1009
  let encoding = "utf-8";
1128
1010
  if (fetchResponse.headers.has("Content-Type")) {
1129
- let charsetMatched = fetchResponse.headers
1130
- .get("Content-Type")
1131
- ?.match(/charset=(.+)/);
1011
+ let charsetMatched = fetchResponse.headers.get("Content-Type")?.match(/charset=(.+)/);
1132
1012
  if (charsetMatched) {
1133
1013
  encoding = charsetMatched[1];
1134
1014
  encoding = encoding.toLowerCase();
@@ -1150,15 +1030,11 @@ export class Httpx {
1150
1030
  response = new Blob([arrayBuffer]);
1151
1031
  } else if (
1152
1032
  option.responseType === "json" ||
1153
- (typeof fetchResponseType === "string" &&
1154
- fetchResponseType.includes("application/json"))
1033
+ (typeof fetchResponseType === "string" && fetchResponseType.includes("application/json"))
1155
1034
  ) {
1156
1035
  // response返回格式是JSON格式
1157
1036
  response = CommonUtil.toJSON(responseText);
1158
- } else if (
1159
- option.responseType === "document" ||
1160
- option.responseType == null
1161
- ) {
1037
+ } else if (option.responseType === "document" || option.responseType == null) {
1162
1038
  // response返回格式是文档格式
1163
1039
  let parser = new DOMParser();
1164
1040
  response = parser.parseFromString(responseText, "text/html");
@@ -1273,14 +1149,8 @@ export class Httpx {
1273
1149
  if (typeof option.xmlHttpRequest === "function") {
1274
1150
  this.GM_Api.xmlHttpRequest = option.xmlHttpRequest;
1275
1151
  }
1276
- this.#defaultRequestOption = CommonUtil.assign(
1277
- this.#defaultRequestOption,
1278
- option
1279
- );
1280
- this.#defaultInitOption = CommonUtil.assign(
1281
- this.#defaultInitOption,
1282
- option
1283
- );
1152
+ this.#defaultRequestOption = CommonUtil.assign(this.#defaultRequestOption, option);
1153
+ this.#defaultInitOption = CommonUtil.assign(this.#defaultInitOption, option);
1284
1154
  }
1285
1155
  /**
1286
1156
  * 拦截器
@@ -1295,11 +1165,7 @@ export class Httpx {
1295
1165
  * 添加拦截器
1296
1166
  * @param fn 设置的请求前回调函数,如果返回配置,则使用返回的配置,如果返回null|undefined,则阻止请求
1297
1167
  */
1298
- use(
1299
- fn: <T extends Required<HttpxRequestOption>>(
1300
- details: T
1301
- ) => void | T | Promise<void | T>
1302
- ) {
1168
+ use(fn: <T extends Required<HttpxRequestOption>>(details: T) => void | T | Promise<void | T>) {
1303
1169
  if (typeof fn !== "function") {
1304
1170
  console.warn("[Httpx-interceptors-request] 请传入拦截器函数");
1305
1171
  return;
@@ -1337,9 +1203,7 @@ export class Httpx {
1337
1203
  response: T,
1338
1204
  details: HttpxRequestOption
1339
1205
  ) => void | T,
1340
- errorFn?: <T extends HttpxHookErrorData>(
1341
- data: T
1342
- ) => void | T | Promise<void | T>
1206
+ errorFn?: <T extends HttpxHookErrorData>(data: T) => void | T | Promise<void | T>
1343
1207
  ) {
1344
1208
  if (typeof successFn !== "function" && typeof errorFn !== "function") {
1345
1209
  console.warn("[Httpx-interceptors-response] 必须传入一个拦截器函数");
@@ -1397,11 +1261,8 @@ export class Httpx {
1397
1261
  * @param url 请求的url
1398
1262
  * @param details 配置
1399
1263
  */
1400
- get(
1401
- ...args: (string | HttpxRequestOption)[]
1402
- ): HttpxPromise<HttpxResponse<HttpxRequestOption>> {
1403
- let useRequestOption =
1404
- this.HttpxRequestOption.handleBeforeRequestOptionArgs(...args);
1264
+ get(...args: (string | HttpxRequestOption)[]): HttpxPromise<HttpxResponse<HttpxRequestOption>> {
1265
+ let useRequestOption = this.HttpxRequestOption.handleBeforeRequestOptionArgs(...args);
1405
1266
  useRequestOption.method = "GET";
1406
1267
  return this.request(useRequestOption, (option) => {
1407
1268
  Reflect.deleteProperty(option, "onprogress");
@@ -1411,9 +1272,7 @@ export class Httpx {
1411
1272
  * POST 请求
1412
1273
  * @param details 配置
1413
1274
  */
1414
- post<T extends HttpxRequestOption>(
1415
- details?: T
1416
- ): HttpxPromise<HttpxResponse<T>>;
1275
+ post<T extends HttpxRequestOption>(details?: T): HttpxPromise<HttpxResponse<T>>;
1417
1276
  /**
1418
1277
  * POST 请求
1419
1278
  * @param url 请求的url
@@ -1435,11 +1294,8 @@ export class Httpx {
1435
1294
  /**
1436
1295
  * POST 请求
1437
1296
  */
1438
- post(
1439
- ...args: (HttpxRequestOption | string)[]
1440
- ): HttpxPromise<HttpxResponse<HttpxRequestOption>> {
1441
- let useRequestOption =
1442
- this.HttpxRequestOption.handleBeforeRequestOptionArgs(...args);
1297
+ post(...args: (HttpxRequestOption | string)[]): HttpxPromise<HttpxResponse<HttpxRequestOption>> {
1298
+ let useRequestOption = this.HttpxRequestOption.handleBeforeRequestOptionArgs(...args);
1443
1299
  useRequestOption.method = "POST";
1444
1300
  return this.request(useRequestOption);
1445
1301
  }
@@ -1447,9 +1303,7 @@ export class Httpx {
1447
1303
  * HEAD 请求
1448
1304
  * @param details 配置
1449
1305
  */
1450
- head<T extends HttpxRequestOption>(
1451
- details: T
1452
- ): HttpxPromise<HttpxResponse<T>>;
1306
+ head<T extends HttpxRequestOption>(details: T): HttpxPromise<HttpxResponse<T>>;
1453
1307
  /**
1454
1308
  * HEAD 请求
1455
1309
  * @param url 请求的url
@@ -1471,11 +1325,8 @@ export class Httpx {
1471
1325
  /**
1472
1326
  * HEAD 请求
1473
1327
  */
1474
- head(
1475
- ...args: (HttpxRequestOption | string)[]
1476
- ): HttpxPromise<HttpxResponse<HttpxRequestOption>> {
1477
- let useRequestOption =
1478
- this.HttpxRequestOption.handleBeforeRequestOptionArgs(...args);
1328
+ head(...args: (HttpxRequestOption | string)[]): HttpxPromise<HttpxResponse<HttpxRequestOption>> {
1329
+ let useRequestOption = this.HttpxRequestOption.handleBeforeRequestOptionArgs(...args);
1479
1330
  useRequestOption.method = "HEAD";
1480
1331
  return this.request(useRequestOption, (option) => {
1481
1332
  Reflect.deleteProperty(option, "onprogress");
@@ -1485,9 +1336,7 @@ export class Httpx {
1485
1336
  * OPTIONS 请求
1486
1337
  * @param details 配置
1487
1338
  */
1488
- options<T extends HttpxRequestOption>(
1489
- details: T
1490
- ): HttpxPromise<HttpxResponse<T>>;
1339
+ options<T extends HttpxRequestOption>(details: T): HttpxPromise<HttpxResponse<T>>;
1491
1340
  /**
1492
1341
  * OPTIONS 请求
1493
1342
  * @param url 请求的url
@@ -1509,11 +1358,8 @@ export class Httpx {
1509
1358
  /**
1510
1359
  * OPTIONS 请求
1511
1360
  */
1512
- options(
1513
- ...args: (HttpxRequestOption | string)[]
1514
- ): HttpxPromise<HttpxResponse<HttpxRequestOption>> {
1515
- let useRequestOption =
1516
- this.HttpxRequestOption.handleBeforeRequestOptionArgs(...args);
1361
+ options(...args: (HttpxRequestOption | string)[]): HttpxPromise<HttpxResponse<HttpxRequestOption>> {
1362
+ let useRequestOption = this.HttpxRequestOption.handleBeforeRequestOptionArgs(...args);
1517
1363
  useRequestOption.method = "OPTIONS";
1518
1364
  return this.request(useRequestOption, (option) => {
1519
1365
  Reflect.deleteProperty(option, "onprogress");
@@ -1523,9 +1369,7 @@ export class Httpx {
1523
1369
  * DELETE 请求
1524
1370
  * @param details 配置
1525
1371
  */
1526
- delete<T extends HttpxRequestOption>(
1527
- details: T
1528
- ): HttpxPromise<HttpxResponse<T>>;
1372
+ delete<T extends HttpxRequestOption>(details: T): HttpxPromise<HttpxResponse<T>>;
1529
1373
  /**
1530
1374
  * DELETE 请求
1531
1375
  * @param url 请求的url
@@ -1547,11 +1391,8 @@ export class Httpx {
1547
1391
  /**
1548
1392
  * DELETE 请求
1549
1393
  */
1550
- delete(
1551
- ...args: (HttpxRequestOption | string)[]
1552
- ): HttpxPromise<HttpxResponse<HttpxRequestOption>> {
1553
- let useRequestOption =
1554
- this.HttpxRequestOption.handleBeforeRequestOptionArgs(...args);
1394
+ delete(...args: (HttpxRequestOption | string)[]): HttpxPromise<HttpxResponse<HttpxRequestOption>> {
1395
+ let useRequestOption = this.HttpxRequestOption.handleBeforeRequestOptionArgs(...args);
1555
1396
  useRequestOption.method = "DELETE";
1556
1397
  return this.request(useRequestOption, (option) => {
1557
1398
  Reflect.deleteProperty(option, "onprogress");
@@ -1583,11 +1424,8 @@ export class Httpx {
1583
1424
  /**
1584
1425
  * PUT 请求
1585
1426
  */
1586
- put(
1587
- ...args: (HttpxRequestOption | string)[]
1588
- ): HttpxPromise<HttpxResponse<HttpxRequestOption>> {
1589
- let userRequestOption =
1590
- this.HttpxRequestOption.handleBeforeRequestOptionArgs(...args);
1427
+ put(...args: (HttpxRequestOption | string)[]): HttpxPromise<HttpxResponse<HttpxRequestOption>> {
1428
+ let userRequestOption = this.HttpxRequestOption.handleBeforeRequestOptionArgs(...args);
1591
1429
  userRequestOption.method = "PUT";
1592
1430
  return this.request(userRequestOption);
1593
1431
  }
@@ -1600,37 +1438,24 @@ export class Httpx {
1600
1438
  details: T,
1601
1439
  beforeRequestOption?: (option: Required<T>) => void
1602
1440
  ): HttpxPromise<HttpxResponse<T>> {
1603
- let useRequestOption =
1604
- this.HttpxRequestOption.handleBeforeRequestOptionArgs(details);
1441
+ let useRequestOption = this.HttpxRequestOption.handleBeforeRequestOptionArgs(details);
1605
1442
  /** 取消请求 */
1606
1443
  let abortFn: Function | null = null;
1607
- let promise = new globalThis.Promise<HttpxResponse<HttpxRequestOption>>(
1608
- async (resolve, reject) => {
1609
- let requestOption = <Required<T>>(
1610
- this.HttpxRequestOption.getRequestOption(
1611
- useRequestOption.method!,
1612
- useRequestOption,
1613
- resolve,
1614
- reject
1615
- )
1616
- );
1617
- if (typeof beforeRequestOption === "function") {
1618
- beforeRequestOption(requestOption);
1619
- }
1620
- requestOption = this.HttpxRequestOption.removeRequestNullOption(
1621
- <Required<HttpxRequestOption>>requestOption
1622
- ) as Required<T>;
1623
- const requestResult = await this.HttpxRequest.request(
1624
- <Required<HttpxRequestOption>>requestOption
1625
- );
1626
- if (
1627
- requestResult != null &&
1628
- typeof requestResult.abort === "function"
1629
- ) {
1630
- abortFn = requestResult.abort;
1631
- }
1444
+ let promise = new globalThis.Promise<HttpxResponse<HttpxRequestOption>>(async (resolve, reject) => {
1445
+ let requestOption = <Required<T>>(
1446
+ this.HttpxRequestOption.getRequestOption(useRequestOption.method!, useRequestOption, resolve, reject)
1447
+ );
1448
+ if (typeof beforeRequestOption === "function") {
1449
+ beforeRequestOption(requestOption);
1450
+ }
1451
+ requestOption = this.HttpxRequestOption.removeRequestNullOption(
1452
+ <Required<HttpxRequestOption>>requestOption
1453
+ ) as Required<T>;
1454
+ const requestResult = await this.HttpxRequest.request(<Required<HttpxRequestOption>>requestOption);
1455
+ if (requestResult != null && typeof requestResult.abort === "function") {
1456
+ abortFn = requestResult.abort;
1632
1457
  }
1633
- ) as HttpxPromise<HttpxResponse<T>>;
1458
+ }) as HttpxPromise<HttpxResponse<T>>;
1634
1459
  promise.abort = () => {
1635
1460
  if (typeof abortFn === "function") {
1636
1461
  abortFn();