WhiteSevsUtils

一个好用的工具类

此脚本不应直接安装。它是供其他脚本使用的外部库,要使用该库请加入元指令 // @require https://update.cn-greasyfork.org/scripts/455186/1603078/WhiteSevsUtils.js

  1. (function (global, factory) {
  2. typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
  3. typeof define === 'function' && define.amd ? define(factory) :
  4. (global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.Utils = factory());
  5. })(this, (function () { 'use strict';
  6.  
  7. class ColorConversion {
  8. /**
  9. * 判断是否是16进制颜色
  10. * @param str
  11. */
  12. isHex(str) {
  13. if (typeof str !== "string") {
  14. return false;
  15. }
  16. if (!str.match(/^(\#|)[0-9a-fA-F]{6}$/)) {
  17. return false;
  18. }
  19. return true;
  20. }
  21. /**
  22. * 16进制颜色转rgba
  23. *
  24. * #ff0000 转 rgba(123,123,123, 0.4)
  25. * @param hex
  26. * @param opacity
  27. */
  28. hexToRgba(hex, opacity) {
  29. if (!this.isHex(hex)) {
  30. // @ts-ignore
  31. throw new TypeError("输入错误的hex", hex);
  32. }
  33. return hex && hex.replace(/\s+/g, "").length === 7
  34. ? "rgba(" +
  35. parseInt("0x" + hex.slice(1, 3)) +
  36. "," +
  37. parseInt("0x" + hex.slice(3, 5)) +
  38. "," +
  39. parseInt("0x" + hex.slice(5, 7)) +
  40. "," +
  41. opacity +
  42. ")"
  43. : "";
  44. }
  45. /**
  46. * hex转rgb
  47. * @param str
  48. * @returns
  49. */
  50. hexToRgb(str) {
  51. if (!this.isHex(str)) {
  52. // @ts-ignore
  53. throw new TypeError("输入错误的hex", str);
  54. }
  55. /* replace替换查找的到的字符串 */
  56. str = str.replace("#", "");
  57. /* match得到查询数组 */
  58. let hxs = str.match(/../g);
  59. for (let index = 0; index < 3; index++) {
  60. // @ts-ignore
  61. hxs[index] = parseInt(hxs[index], 16);
  62. }
  63. return hxs;
  64. }
  65. /**
  66. * rgb转hex
  67. * @param redValue
  68. * @param greenValue
  69. * @param blueValue
  70. * @returns
  71. */
  72. rgbToHex(redValue, greenValue, blueValue) {
  73. /* 验证输入的rgb值是否合法 */
  74. let validPattern = /^\d{1,3}$/;
  75. if (!validPattern.test(redValue.toString()) ||
  76. !validPattern.test(greenValue.toString()) ||
  77. !validPattern.test(blueValue.toString()))
  78. throw new TypeError("输入错误的rgb颜色值");
  79. let hexs = [
  80. redValue.toString(16),
  81. greenValue.toString(16),
  82. blueValue.toString(16),
  83. ];
  84. for (let index = 0; index < 3; index++)
  85. if (hexs[index].length == 1)
  86. hexs[index] = "0" + hexs[index];
  87. return "#" + hexs.join("");
  88. }
  89. /**
  90. * 获取颜色变暗或亮
  91. * @param color 颜色
  92. * @param level 0~1.0
  93. * @returns
  94. */
  95. getDarkColor(color, level) {
  96. if (!this.isHex(color)) {
  97. // @ts-ignore
  98. throw new TypeError("输入错误的hex", color);
  99. }
  100. let rgbc = this.hexToRgb(color);
  101. for (let index = 0; index < 3; index++) {
  102. // @ts-ignore
  103. rgbc[index] = Math.floor(rgbc[index] * (1 - level));
  104. }
  105. // @ts-ignore
  106. return this.rgbToHex(rgbc[0], rgbc[1], rgbc[2]);
  107. }
  108. /**
  109. * 获取颜色变亮
  110. * @param color 颜色
  111. * @param level 0~1.0
  112. * @returns
  113. */
  114. getLightColor(color, level) {
  115. if (!this.isHex(color)) {
  116. // @ts-ignore
  117. throw new TypeError("输入错误的hex", color);
  118. }
  119. let rgbc = this.hexToRgb(color);
  120. for (let index = 0; index < 3; index++) {
  121. // @ts-ignore
  122. rgbc[index] = Math.floor((255 - rgbc[index]) * level + rgbc[index]);
  123. }
  124. // @ts-ignore
  125. return this.rgbToHex(rgbc[0], rgbc[1], rgbc[2]);
  126. }
  127. }
  128.  
  129. class GBKEncoder {
  130. #data = [];
  131. #U2Ghash = {};
  132. #G2Uhash = {};
  133. constructor() {
  134. let dataText = this.handleText("4e:020405060f12171f20212326292e2f313335373c40414244464a5155575a5b6263646567686a6b6c6d6e6f727475767778797a7b7c7d7f808182838485878a#909697999c9d9ea3aaafb0b1b4b6b7b8b9bcbdbec8cccfd0d2dadbdce0e2e6e7e9edeeeff1f4f8f9fafcfe,4f:00020304050607080b0c12131415161c1d212328292c2d2e31333537393b3e3f40414244454748494a4b4c525456616266686a6b6d6e7172757778797a7d8081828586878a8c8e909293959698999a9c9e9fa1a2a4abadb0b1b2b3b4b6b7b8b9babbbcbdbec0c1c2c6c7c8c9cbcccdd2d3d4d5d6d9dbe0e2e4e5e7ebecf0f2f4f5f6f7f9fbfcfdff,50:000102030405060708090a#0b0e1011131516171b1d1e20222324272b2f303132333435363738393b3d3f404142444546494a4b4d5051525354565758595b5d5e5f6061626364666768696a6b6d6e6f70717273747578797a7c7d818283848687898a8b8c8e8f909192939495969798999a9b9c9d9e9fa0a1a2a4a6aaabadaeafb0b1b3b4b5b6b7b8b9bcbdbebfc0c1c2c3c4c5c6c7c8c9cacbcccdced0d1d2d3d4d5d7d8d9dbdcdddedfe0e1e2e3e4e5e8e9eaebeff0f1f2f4f6f7f8f9fafcfdfeff,51:00010203040508#090a0c0d0e0f1011131415161718191a1b1c1d1e1f2022232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e42474a4c4e4f5052535758595b5d5e5f606163646667696a6f727a7e7f838486878a8b8e8f90919394989a9d9e9fa1a3a6a7a8a9aaadaeb4b8b9babebfc1c2c3c5c8cacdced0d2d3d4d5d6d7d8d9dadcdedfe2e3e5e6e7e8e9eaeceef1f2f4f7fe,52:0405090b0c0f101314151c1e1f2122232526272a2c2f313234353c3e4445464748494b4e4f5253555758#595a5b5d5f6062636466686b6c6d6e7071737475767778797a7b7c7e808384858687898a8b8c8d8e8f91929495969798999a9ca4a5a6a7aeafb0b4b5b6b7b8b9babbbcbdc0c1c2c4c5c6c8cacccdcecfd1d3d4d5d7d9dadbdcdddee0e1e2e3e5e6e7e8e9eaebecedeeeff1f2f3f4f5f6f7f8fbfcfd,53:0102030407090a0b0c0e11121314181b1c1e1f2224252728292b2c2d2f3031323334353637383c3d404244464b4c4d505458595b5d65686a6c6d7276797b7c7d7e80818387888a8e8f#90919293949697999b9c9ea0a1a4a7aaabacadafb0b1b2b3b4b5b7b8b9babcbdbec0c3c4c5c6c7cecfd0d2d3d5dadcdddee1e2e7f4fafeff,54:000205070b1418191a1c2224252a303336373a3d3f4142444547494c4d4e4f515a5d5e5f6061636567696a6b6c6d6e6f7074797a7e7f8183858788898a8d919397989c9e9fa0a1a2a5aeb0b2b5b6b7b9babcbec3c5cacbd6d8dbe0e1e2e3e4ebeceff0f1f4f5f6f7f8f9fbfe,55:0002030405080a0b0c0d0e121315161718191a1c1d1e1f212526#28292b2d3234353638393a3b3d40424547484b4c4d4e4f515253545758595a5b5d5e5f60626368696b6f7071727374797a7d7f85868c8d8e9092939596979a9b9ea0a1a2a3a4a5a6a8a9aaabacadaeafb0b2b4b6b8babcbfc0c1c2c3c6c7c8cacbcecfd0d5d7d8d9dadbdee0e2e7e9edeef0f1f4f6f8f9fafbfcff,56:0203040506070a0b0d1011121314151617191a1c1d202122252628292a2b2e2f30333537383a3c3d3e404142434445464748494a4b4f5051525355565a5b5d5e5f6061#636566676d6e6f70727374757778797a7d7e7f80818283848788898a8b8c8d9091929495969798999a9b9c9d9e9fa0a1a2a4a5a6a7a8a9aaabacadaeb0b1b2b3b4b5b6b8b9babbbdbebfc0c1c2c3c4c5c6c7c8c9cbcccdcecfd0d1d2d3d5d6d8d9dce3e5e6e7e8e9eaeceeeff2f3f6f7f8fbfc,57:00010205070b0c0d0e0f101112131415161718191a1b1d1e202122242526272b313234353637383c3d3f414344454648494b52535455565859626365676c6e707172747578797a7d7e7f80#818788898a8d8e8f90919495969798999a9c9d9e9fa5a8aaacafb0b1b3b5b6b7b9babbbcbdbebfc0c1c4c5c6c7c8c9cacccdd0d1d3d6d7dbdcdee1e2e3e5e6e7e8e9eaebeceef0f1f2f3f5f6f7fbfcfeff,58:0103040508090a0c0e0f101213141617181a1b1c1d1f222325262728292b2c2d2e2f31323334363738393a3b3c3d3e3f4041424345464748494a4b4e4f505253555657595a5b5c5d5f6061626364666768696a6d6e6f707172737475767778797a7b7c7d7f82848687888a8b8c#8d8e8f909194959697989b9c9da0a1a2a3a4a5a6a7aaabacadaeafb0b1b2b3b4b5b6b7b8b9babbbdbebfc0c2c3c4c6c7c8c9cacbcccdcecfd0d2d3d4d6d7d8d9dadbdcdddedfe0e1e2e3e5e6e7e8e9eaedeff1f2f4f5f7f8fafbfcfdfeff,59:000103050608090a0b0c0e1011121317181b1d1e2021222326282c30323335363b3d3e3f404345464a4c4d505253595b5c5d5e5f616364666768696a6b6c6d6e6f70717275777a7b7c7e7f8085898b8c8e8f90919495989a9b9c9d9fa0a1a2a6#a7acadb0b1b3b4b5b6b7b8babcbdbfc0c1c2c3c4c5c7c8c9cccdcecfd5d6d9dbdedfe0e1e2e4e6e7e9eaebedeeeff0f1f2f3f4f5f6f7f8fafcfdfe,5a:00020a0b0d0e0f101214151617191a1b1d1e2122242627282a2b2c2d2e2f3033353738393a3b3d3e3f414243444547484b4c4d4e4f5051525354565758595b5c5d5e5f60616364656668696b6c6d6e6f7071727378797b7c7d7e808182838485868788898a8b8c8d8e8f9091939495969798999c9d9e9fa0a1a2a3a4a5a6a7a8a9abac#adaeafb0b1b4b6b7b9babbbcbdbfc0c3c4c5c6c7c8cacbcdcecfd0d1d3d5d7d9dadbdddedfe2e4e5e7e8eaecedeeeff0f2f3f4f5f6f7f8f9fafbfcfdfeff,5b:0001020304050607080a0b0c0d0e0f10111213141518191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303133353638393a3b3c3d3e3f4142434445464748494a4b4c4d4e4f52565e606167686b6d6e6f7274767778797b7c7e7f82868a8d8e90919294969fa7a8a9acadaeafb1b2b7babbbcc0c1c3c8c9cacbcdcecf#d1d4d5d6d7d8d9dadbdce0e2e3e6e7e9eaebecedeff1f2f3f4f5f6f7fdfe,5c:0002030507080b0c0d0e10121317191b1e1f2021232628292a2b2d2e2f303233353637434446474c4d5253545657585a5b5c5d5f62646768696a6b6c6d70727374757677787b7c7d7e808384858687898a8b8e8f9293959d9e9fa0a1a4a5a6a7a8aaaeafb0b2b4b6b9babbbcbec0c2c3c5c6c7c8c9cacccdcecfd0d1d3d4d5d6d7d8dadbdcdddedfe0e2e3e7e9ebeceeeff1f2f3f4f5f6f7f8f9fafcfdfeff,5d:00#01040508090a0b0c0d0f10111213151718191a1c1d1f2021222325282a2b2c2f3031323335363738393a3b3c3f4041424344454648494d4e4f5051525354555657595a5c5e5f6061626364656667686a6d6e7071727375767778797a7b7c7d7e7f8081838485868788898a8b8c8d8e8f9091929394959697989a9b9c9e9fa0a1a2a3a4a5a6a7a8a9aaabacadaeafb0b1b2b3b4b5b6b8b9babbbcbdbebfc0c1c2c3c4c6c7c8c9cacbcccecfd0d1d2d3d4d5d6d7d8d9dadcdfe0e3e4eaeced#f0f5f6f8f9fafbfcff,5e:000407090a0b0d0e1213171e1f20212223242528292a2b2c2f303233343536393a3e3f404143464748494a4b4d4e4f50515253565758595a5c5d5f60636465666768696a6b6c6d6e6f70717577797e8182838588898c8d8e92989b9da1a2a3a4a8a9aaabacaeafb0b1b2b4babbbcbdbfc0c1c2c3c4c5c6c7c8cbcccdcecfd0d4d5d7d8d9dadcdddedfe0e1e2e3e4e5e6e7e9ebecedeeeff0f1f2f3f5f8f9fbfcfd,5f:050607090c0d0e10121416191a1c1d1e21222324#282b2c2e30323334353637383b3d3e3f4142434445464748494a4b4c4d4e4f5154595a5b5c5e5f60636567686b6e6f72747576787a7d7e7f83868d8e8f919394969a9b9d9e9fa0a2a3a4a5a6a7a9abacafb0b1b2b3b4b6b8b9babbbebfc0c1c2c7c8cacbced3d4d5dadbdcdedfe2e3e5e6e8e9eceff0f2f3f4f6f7f9fafc,60:0708090b0c10111317181a1e1f2223242c2d2e3031323334363738393a3d3e404445464748494a4c4e4f5153545657585b5c5e5f606165666e71727475777e80#8182858687888a8b8e8f909193959798999c9ea1a2a4a5a7a9aaaeb0b3b5b6b7b9babdbebfc0c1c2c3c4c7c8c9cccdcecfd0d2d3d4d6d7d9dbdee1e2e3e4e5eaf1f2f5f7f8fbfcfdfeff,61:02030405070a0b0c1011121314161718191b1c1d1e21222528292a2c2d2e2f303132333435363738393a3b3c3d3e4041424344454647494b4d4f50525354565758595a5b5c5e5f606163646566696a6b6c6d6e6f717273747678797a7b7c7d7e7f808182838485868788898a8c8d8f9091929395#969798999a9b9c9e9fa0a1a2a3a4a5a6aaabadaeafb0b1b2b3b4b5b6b8b9babbbcbdbfc0c1c3c4c5c6c7c9cccdcecfd0d3d5d6d7d8d9dadbdcdddedfe0e1e2e3e4e5e7e8e9eaebecedeeeff0f1f2f3f4f6f7f8f9fafbfcfdfe,62:00010203040507091314191c1d1e2023262728292b2d2f303132353638393a3b3c424445464a4f50555657595a5c5d5e5f6061626465687172747577787a7b7d818283858687888b8c8d8e8f9094999c9d9ea3a6a7a9aaadaeafb0b2b3b4b6b7b8babec0c1#c3cbcfd1d5dddee0e1e4eaebf0f2f5f8f9fafb,63:00030405060a0b0c0d0f10121314151718191c2627292c2d2e30313334353637383b3c3e3f40414447484a51525354565758595a5b5c5d60646566686a6b6c6f707273747578797c7d7e7f81838485868b8d9193949597999a9b9c9d9e9fa1a4a6abafb1b2b5b6b9bbbdbfc0c1c2c3c5c7c8cacbccd1d3d4d5d7d8d9dadbdcdddfe2e4e5e6e7e8ebeceeeff0f1f3f5f7f9fafbfcfe,64:0304060708090a0d0e111215161718191a1d1f222324#252728292b2e2f3031323335363738393b3c3e404243494b4c4d4e4f505153555657595a5b5c5d5f60616263646566686a6b6c6e6f70717273747576777b7c7d7e7f8081838688898a8b8c8d8e8f90939497989a9b9c9d9fa0a1a2a3a5a6a7a8aaabafb1b2b3b4b6b9bbbdbebfc1c3c4c6c7c8c9cacbcccfd1d3d4d5d6d9dadbdcdddfe0e1e3e5e7e8e9eaebecedeeeff0f1f2f3f4f5f6f7f8f9fafbfcfdfeff,65:01020304050607080a0b0c0d0e0f10111314151617191a1b1c1d1e1f2021#222324262728292a2c2d30313233373a3c3d404142434446474a4b4d4e5052535457585a5c5f606164656768696a6d6e6f7173757678797a7b7c7d7e7f8081828384858688898a8d8e8f92949596989a9d9ea0a2a3a6a8aaacaeb1b2b3b4b5b6b7b8babbbebfc0c2c7c8c9cacdd0d1d3d4d5d8d9dadbdcdddedfe1e3e4eaebf2f3f4f5f8f9fbfcfdfeff,66:0104050708090b0d1011121617181a1b1c1e2122232426292a2b2c2e3032333738393a3b3d3f40424445464748494a4d4e505158#595b5c5d5e6062636567696a6b6c6d7172737578797b7c7d7f808183858688898a8b8d8e8f909293949598999a9b9c9e9fa0a1a2a3a4a5a6a9aaabacadafb0b1b2b3b5b6b7b8babbbcbdbfc0c1c2c3c4c5c6c7c8c9cacbcccdcecfd0d1d2d3d4d5d6d7d8dadedfe0e1e2e3e4e5e7e8eaebecedeeeff1f5f6f8fafbfd,67:010203040506070c0e0f1112131618191a1c1e20212223242527292e303233363738393b3c3e3f414445474a4b4d5254555758595a5b5d62636466676b6c6e717476#78797a7b7d8082838586888a8c8d8e8f9192939496999b9fa0a1a4a6a9acaeb1b2b4b9babbbcbdbebfc0c2c5c6c7c8c9cacbcccdced5d6d7dbdfe1e3e4e6e7e8eaebedeef2f5f6f7f8f9fafbfcfe,68:01020304060d1012141518191a1b1c1e1f20222324252627282b2c2d2e2f30313435363a3b3f474b4d4f52565758595a5b5c5d5e5f6a6c6d6e6f707172737578797a7b7c7d7e7f8082848788898a8b8c8d8e90919294959698999a9b9c9d9e9fa0a1a3a4a5a9aaabacaeb1b2b4b6b7b8#b9babbbcbdbebfc1c3c4c5c6c7c8cacccecfd0d1d3d4d6d7d9dbdcdddedfe1e2e4e5e6e7e8e9eaebecedeff2f3f4f6f7f8fbfdfeff,69:00020304060708090a0c0f11131415161718191a1b1c1d1e21222325262728292a2b2c2e2f313233353637383a3b3c3e4041434445464748494a4b4c4d4e4f50515253555658595b5c5f616264656768696a6c6d6f7072737475767a7b7d7e7f8183858a8b8c8e8f909192939697999a9d9e9fa0a1a2a3a4a5a6a9aaacaeafb0b2b3b5b6b8b9babcbd#bebfc0c2c3c4c5c6c7c8c9cbcdcfd1d2d3d5d6d7d8d9dadcdddee1e2e3e4e5e6e7e8e9eaebeceeeff0f1f3f4f5f6f7f8f9fafbfcfe,6a:000102030405060708090b0c0d0e0f10111213141516191a1b1c1d1e20222324252627292b2c2d2e30323334363738393a3b3c3f40414243454648494a4b4c4d4e4f515253545556575a5c5d5e5f60626364666768696a6b6c6d6e6f70727374757677787a7b7d7e7f81828385868788898a8b8c8d8f929394959698999a9b9c9d9e9fa1a2a3a4a5a6#a7a8aaadaeafb0b1b2b3b4b5b6b7b8b9babbbcbdbebfc0c1c2c3c4c5c6c7c8c9cacbcccdcecfd0d1d2d3d4d5d6d7d8d9dadbdcdddedfe0e1e2e3e4e5e6e7e8e9eaebecedeeeff0f1f2f3f4f5f6f7f8f9fafbfcfdfeff,6b:000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f252628292a2b2c2d2e2f303133343536383b3c3d3f4041424445484a4b4d4e4f5051525354555657585a5b5c5d5e5f606168696b6c6d6e6f7071727374757677787a7d7e7f808588#8c8e8f909194959798999c9d9e9fa0a2a3a4a5a6a7a8a9abacadaeafb0b1b2b6b8b9babbbcbdbec0c3c4c6c7c8c9caccced0d1d8dadcdddedfe0e2e3e4e5e6e7e8e9ecedeef0f1f2f4f6f7f8fafbfcfeff,6c:000102030408090a0b0c0e12171c1d1e2023252b2c2d31333637393a3b3c3e3f434445484b4c4d4e4f5152535658595a62636566676b6c6d6e6f71737577787a7b7c7f8084878a8b8d8e9192959697989a9c9d9ea0a2a8acafb0b4b5b6b7bac0c1c2c3c6c7c8cbcdcecfd1d2d8#d9dadcdddfe4e6e7e9ecedf2f4f9ff,6d:000203050608090a0d0f101113141516181c1d1f20212223242628292c2d2f30343637383a3f404244494c50555657585b5d5f6162646567686b6c6d707172737576797a7b7d7e7f8081838486878a8b8d8f9092969798999a9ca2a5acadb0b1b3b4b6b7b9babbbcbdbec1c2c3c8c9cacdcecfd0d2d3d4d5d7dadbdcdfe2e3e5e7e8e9eaedeff0f2f4f5f6f8fafdfeff,6e:0001020304060708090b0f12131518191b1c1e1f222627282a2c2e30313335#3637393b3c3d3e3f40414245464748494a4b4c4f5051525557595a5c5d5e606162636465666768696a6c6d6f707172737475767778797a7b7c7d8081828487888a8b8c8d8e91929394959697999a9b9d9ea0a1a3a4a6a8a9abacadaeb0b3b5b8b9bcbebfc0c3c4c5c6c8c9cacccdced0d2d6d8d9dbdcdde3e7eaebecedeeeff0f1f2f3f5f6f7f8fafbfcfdfeff,6f:000103040507080a0b0c0d0e101112161718191a1b1c1d1e1f212223252627282c2e303234353738393a3b3c3d3f404142#43444548494a4c4e4f5051525354555657595a5b5d5f60616364656768696a6b6c6f707173757677797b7d7e7f808182838586878a8b8f909192939495969798999a9b9d9e9fa0a2a3a4a5a6a8a9aaabacadaeafb0b1b2b4b5b7b8babbbcbdbebfc1c3c4c5c6c7c8cacbcccdcecfd0d3d4d5d6d7d8d9dadbdcdddfe2e3e4e5e6e7e8e9eaebecedf0f1f2f3f4f5f6f7f8f9fafbfcfdfeff,70:000102030405060708090a0b0c0d0e0f1012131415161718191c1d1e1f2021222425262728292a#2b2c2d2e2f30313233343637383a3b3c3d3e3f404142434445464748494a4b4d4e505152535455565758595a5b5c5d5f606162636465666768696a6e7172737477797a7b7d818283848687888b8c8d8f90919397989a9b9e9fa0a1a2a3a4a5a6a7a8a9aab0b2b4b5b6babebfc4c5c6c7c9cbcccdcecfd0d1d2d3d4d5d6d7dadcdddee0e1e2e3e5eaeef0f1f2f3f4f5f6f8fafbfcfeff,71:0001020304050607080b0c0d0e0f111214171b1c1d1e1f2021222324252728292a2b2c2d2e323334#353738393a3b3c3d3e3f4041424344464748494b4d4f505152535455565758595a5b5d5f6061626365696a6b6c6d6f707174757677797b7c7e7f8081828385868788898b8c8d8e909192939596979a9b9c9d9ea1a2a3a4a5a6a7a9aaabadaeafb0b1b2b4b6b7b8babbbcbdbebfc0c1c2c4c5c6c7c8c9cacbcccdcfd0d1d2d3d6d7d8d9dadbdcdddedfe1e2e3e4e6e8e9eaebecedeff0f1f2f3f4f5f6f7f8fafbfcfdfeff,72:0001020304050708090a0b0c0d0e0f101112131415161718191a#1b1c1e1f2021222324252627292b2d2e2f3233343a3c3e40414243444546494a4b4e4f505153545557585a5c5e60636465686a6b6c6d707173747677787b7c7d828385868788898c8e9091939495969798999a9b9c9d9ea0a1a2a3a4a5a6a7a8a9aaabaeb1b2b3b5babbbcbdbebfc0c5c6c7c9cacbcccfd1d3d4d5d6d8dadb#95$,30:000102,00b702:c9c7,00a830:0305,2014ff5e20:162618191c1d,30:141508090a0b0c0d0e0f16171011,00:b1d7f7,22:362728110f2a2908371aa52520,231222:992b2e614c483d1d606e6f64651e3534,26:4240,00b020:3233,2103ff0400a4ff:e0e1,203000a7211626:0605,25:cbcfcec7c6a1a0b3b2,203b21:92909193,30:13#95$,21:70717273747576777879#4$,24:88898a8b8c8d8e8f909192939495969798999a9b7475767778797a7b7c7d7e7f808182838485868760616263646566676869##,32:20212223242526272829##,21:606162636465666768696a6b#97$,ff:010203e505060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f404142434445464748494a4b4c4d4e4f505152535455565758595a5b5c5de3#95$,30:4142434445464748494a4b4c4d4e4f505152535455565758595a5b5c5d5e5f606162636465666768696a6b6c6d6e6f707172737475767778797a7b7c7d7e7f808182838485868788898a8b8c8d8e8f90919293#106$a1a2a3a4a5a6a7a8a9aaabacadaeafb0b1b2b3b4b5b6b7b8b9babbbcbdbebfc0c1c2c3c4c5c6c7c8c9cacbcccdcecfd0d1d2d3d4d5d6d7d8d9dadbdcdddedfe0e1e2e3e4e5e6e7e8e9eaebecedeeeff0f1f2f3f4f5f6#103$,03:9192939495969798999a9b9c9d9e9fa0a1a3a4a5a6a7a8a9#6$b1b2b3b4b5b6b7b8b9babbbcbdbebfc0c1c3c4c5c6c7c8c9#5$,fe:3536393a3f403d3e41424344##3b3c373831#3334#104$,04:10111213141501161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f#13$30313233343551363738393a3b3c3d3e3f404142434445464748494a4b4c4d4e4f#11$,02:cacbd9,20:13152535,21:050996979899,22:151f23526667bf,25:505152535455565758595a5b5c5d5e5f606162636465666768696a6b6c6d6e6f7071727381828384858687#88898a8b8c8d8e8f939495bcbde2e3e4e5,2609229530:121d1e#9$,010100e101ce00e0011300e9011b00e8012b00ed01d000ec014d00f301d200f2016b00fa01d400f901:d6d8dadc,00:fcea,0251e7c701:4448,e7c802:61#2$,31:05060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20212223242526272829#19$,30:212223242526272829,32a333:8e8f9c9d9ea1c4ced1d2d5,fe30ff:e2e4#,212132:31#,20:10#1$,30:fc9b9cfdfe069d9e,fe:494a4b4c4d4e4f50515254555657595a5b5c5d5e5f6061#626364656668696a6b,e7:e7e8e9eaebecedeeeff0f1f2f3,30:07#11$,25:000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f404142434445464748494a4b#13$,72:dcdddfe2e3e4e5e6e7eaebf5f6f9fdfeff,73:00020405060708090b0c0d0f1011121418191a1f2023242627282d2f30323335363a3b3c3d404142434445464748#494a4b4c4e4f515354555658595a5b5c5d5e5f6162636465666768696a6b6e7071#92$72737475767778797a7b7c7d7f808182838586888a8c8d8f90929394959798999a9c9d9ea0a1a3a4a5a6a7a8aaacadb1b4b5b6b8b9bcbdbebfc1c3c4c5c6c7#cbccced2d3d4d5d6d7d8dadbdcdddfe1e2e3e4e6e8eaebeceeeff0f1f3f4f5f6f7#92$f8f9fafbfcfdfeff,74:0001020407080b0c0d0e1112131415161718191c1d1e1f2021232427292b2d2f31323738393a3b3d3e3f4042434445464748494a4b4c4d#4e4f505152535456585d606162636465666768696a6b6c6e6f717273747578797a#92$7b7c7d7f8284858688898a8c8d8f9192939495969798999a9b9d9fa0a1a2a3a4a5a6aaabacadaeafb0b1b2b3b4b5b6b7b8b9bbbcbdbebfc0c1c2c3c4c5c6c7#c8c9cacbcccdcecfd0d1d3d4d5d6d7d8d9dadbdddfe1e5e7e8e9eaebecedf0f1f2#92$f3f5f8f9fafbfcfdfe,75:0001020305060708090a0b0c0e1012141516171b1d1e202122232426272a2e3436393c3d3f414243444647494a4d5051525355565758#5d5e5f60616263646768696b6c6d6e6f7071737576777a7b7c7d7e808182848587#92$88898a8c8d8e909395989b9c9ea2a6a7a8a9aaadb6b7babbbfc0c1c6cbcccecfd0d1d3d7d9dadcdddfe0e1e5e9ecedeeeff2f3f5f6f7f8fafbfdfe,76:02040607#08090b0d0e0f11121314161a1c1d1e212327282c2e2f31323637393a3b3d414244#92$45464748494a4b4e4f50515253555758595a5b5d5f6061626465666768696a6c6d6e7071727374757677797a7c7f80818385898a8c8d8f9092949597989a9b#9c9d9e9fa0a1a2a3a5a6a7a8a9aaabacadafb0b3b5b6b7b8b9babbbcbdbec0c1c3,554a963f57c3632854ce550954c076:914c,853c77ee827e788d72319698978d6c285b894ffa630966975cb880fa684880ae660276ce51f9655671ac7ff1888450b2596561ca6fb382ad634c625253ed54277b06516b75a45df462d48dcb9776628a8019575d97387f627238767d67cf767e64464f708d2562dc7a17659173ed642c6273822c9881677f724862:6ecc,4f3474e3534a529e7eca90a65e2e6886699c81807ed168d278c5868c9551508d8c2482de80de53058912526576:c4c7c9cbccd3d5d9dadcdddee0e1e2e3e4e6e7e8e9eaebecedf0f3f5f6f7fafbfdff,77:00020305060a0c0e0f1011121314151617181b1c1d1e21232425272a2b#2c2e3031323334393b3d3e3f4244454648494a4b4c4d4e4f52535455565758595c,858496f94fdd582199715b9d62:b1a5,66b48c799c8d7206676f789160b253:5117,8f8880cc8d1d94a1500d72c8590760eb711988ab595482ef672c7b285d297ef7752d6cf58e668ff8903c9f3b6bd491197b145f7c78a784d6853d6b:d5d9d6,5e:0187,75f995ed655d5f:0ac5,8f9f58c181c2907f965b97ad8fb97f168d2c62414fbf53:d85e,8f:a8a9ab,904d68075f6a819888689cd6618b522b762a5f6c658c6fd26ee85bbe644851:75b0,67c44e1979c9997c70b377:5d5e5f606467696a6d6e6f7071727374757677787a7b7c818283868788898a8b8f90939495969798999a9b9c9d9ea1a3a4a6a8abadaeafb1b2b4b6b7b8b9ba#bcbec0c1c2c3c4c5c6c7c8c9cacbcccecfd0d1d2d3d4d5d6d8d9dadddedfe0e1e4,75c55e7673bb83e064ad62e894b56ce2535a52c3640f94c27b944f2f5e1b823681:168a,6e246cca9a736355535c54fa886557e04e0d5e036b657c3f90e8601664e6731c88c16750624d8d22776c8e2991c75f6983dc8521991053c286956b8b60:ede8,707f82:cd31,4ed36ca785cf64cd7cd969fd66f9834953957b564fa7518c6d4b5c428e6d63d253c983:2c36,67e578b4643d5bdf5c945dee8be762c667f48c7a640063ba8749998b8c177f2094f24ea7961098a4660c731677:e6e8eaeff0f1f2f4f5f7f9fafbfc,78:0304050607080a0b0e0f101315191b1e20212224282a2b2e2f31323335363d3f414243444648494a4b4d4f51535458595a#5b5c5e5f606162636465666768696f7071727374757678797a7b7d7e7f80818283,573a5c1d5e38957f507f80a05382655e7545553150218d856284949e671d56326f6e5de2543570928f66626f64a463a35f7b6f8890f481e38fb05c1866685ff16c8996488d81886c649179f057ce6a59621054484e587a0b60e96f848bda627f901e9a8b79e4540375f4630153196c608fdf5f1b9a70803b9f7f4f885c3a8d647fc565a570bd51:45b2,866b5d075ba062bd916c75748e0c7a2061017b794ec77ef877854e1181ed521d51fa6a7153a88e87950496cf6ec19664695a78:848586888a8b8f9092949596999d9ea0a2a4a6a8a9aaabacadaeafb5b6b7b8babbbcbdbfc0c2c3c4c6c7c8cccdcecfd1d2d3d6d7d8dadbdcdddedfe0e1e2e3#e4e5e6e7e9eaebedeeeff0f1f3f5f6f8f9fbfcfdfeff,79:00020304060708090a0b0c,784050a877d7641089e6590463e35ddd7a7f693d4f20823955984e3275ae7a975e:628a,95ef521b5439708a6376952457826625693f918755076df37eaf882262337ef075b5832878c196cc8f9e614874f78bcd6b64523a8d506b21806a847156f153064e:ce1b,51d17c97918b7c074fc38e7f7be17a9c64675d1450ac810676017cb96dec7fe067515b:58f8,78cb64:ae13,63:aa2b,9519642d8fbe7b5476296253592754466b7950a362345e266b864ee38d37888b5f85902e79:0d0e0f1011121415161718191a1b1c1d1f2021222325262728292a2b2c2d2e2f3031323335363738393d3f42434445474a4b4c4d4e4f505152545558596163#6466696a6b6c6e70717273747576797b7c7d7e7f8283868788898b8c8d8e909192,6020803d62c54e39535590f863b880c665e66c2e4f4660ee6de18bde5f3986cb5f536321515a83616863520063638e4850125c9b79775bfc52307a3b60bc905376d75f:b797,76848e6c706f767b7b4977aa51f3909358244f4e6ef48fea654c7b1b72c46da47fdf5ae162b55e95573084827b2c5e1d5f1f90127f1498a063826ec7789870b95178975b57ab75354f4375385e9760e659606dc06bbf788953fc96d551cb52016389540a94938c038dcc7239789f87768fed8c0d53e079:939495969798999b9c9d9e9fa0a1a2a3a4a5a6a8a9aaabacadaeafb0b1b2b4b5b6b7b8bcbfc2c4c5c7c8cacccecfd0d3d4d6d7d9dadbdcdddee0e1e2e5e8ea#eceef1f2f3f4f5f6f7f9fafcfeff,7a:0104050708090a0c0f10111213151618191b1c,4e0176ef53ee948998769f0e952d5b9a8ba24e:221c,51ac846361c252a8680b4f97606b51bb6d1e515c6296659796618c46901775d890fd77636bd272:8aec,8bfb583577798d4c675c9540809a5ea66e2159927aef77ed953b6bb565ad7f0e58065151961f5bf958a954288e726566987f56e4949d76fe9041638754c659:1a3a,579b8eb267358dfa8235524160f0581586fe5ce89e454fc4989d8bb95a2560765384627c904f9102997f6069800c513f80335c1499756d314e8c7a:1d1f21222425262728292a2b2c2d2e2f303132343536383a3e4041424344454748494a4b4c4d4e4f50525354555658595a5b5c5d5e5f606162636465666768#696a6b6c6d6e6f717273757b7c7d7e828587898a8b8c8e8f909394999a9b9ea1a2,8d3053d17f5a7b4f4f104e4f96006cd573d085e95e06756a7ffb6a0a77fe94927e4151e170e653cd8fd483038d2972af996d6cdb574a82b365b980aa623f963259a84eff8bbf7eba653e83f2975e556198de80a5532a8bfd542080ba5e9f6cb88d3982ac915a54296c1b52067eb7575f711a6c7e7c89594b4efd5fff61247caa4e305c0167ab87025cf0950b98ce75af70fd902251af7f1d8bbd594951e44f5b5426592b657780a45b7562:76c2,8f905e456c1f7b264f:0fd8,670d7a:a3a4a7a9aaabaeafb0b1b2b4b5b6b7b8b9babbbcbdbec0c1c2c3c4c5c6c7c8c9cacccdcecfd0d1d2d3d4d5d7d8dadbdcdde1e2e4e7e8e9eaebeceef0f1f2f3#f4f5f6f7f8fbfcfe,7b:0001020507090c0d0e1012131617181a1c1d1f21222327292d,6d:6eaa,798f88b15f17752b629a8f854fef91dc65a781:2f51,5e9c81508d74526f89868d4b590d50854ed8961c723681798d1f5bcc8ba3964459877f1a549056:760e,8be565396982949976d66e895e72751867:46d1,7aff809d8d76611f79c665628d635188521a94a27f38809b7eb25c976e2f67607bd9768b9ad8818f7f947cd5641e95507a3f54:4ae5,6b4c640162089e3d80f3759952729769845b683c86e496:0194,94ec4e2a54047ed968398ddf801566f45e9a7fb97b:2f303234353637393b3d3f404142434446484a4d4e535557595c5e5f61636465666768696a6b6c6d6f70737476787a7c7d7f81828384868788898a8b8c8e8f#9192939698999a9b9e9fa0a3a4a5aeafb0b2b3b5b6b7b9babbbcbdbebfc0c2c3c4,57c2803f68975de5653b529f606d9f9a4f9b8eac516c5bab5f135de96c5e62f18d21517194a952fe6c9f82df72d757a267848d2d591f8f9c83c754957b8d4f306cbd5b6459d19f1353e486ca9aa88c3780a16545987e56fa96c7522e74dc52505be1630289024e5662d0602a68fa51735b9851a089c27ba199867f5060ef704c8d2f51495e7f901b747089c4572d78455f529f9f95fa8f689b3c8be17678684267dc8d:ea35,523d8f8a6eda68cd950590ed56fd679c88f98fc754c87b:c5c8c9cacbcdcecfd0d2d4d5d6d7d8dbdcdedfe0e2e3e4e7e8e9ebecedeff0f2f3f4f5f6f8f9fafbfdff,7c:0001020304050608090a0d0e101112131415171819#1a1b1c1d1e20212223242528292b2c2d2e2f3031323334353637393a3b3c3d3e42,9ab85b696d776c264ea55bb39a87916361a890af97e9542b6db55bd251fd558a7f:55f0,64bc634d65f161be608d710a6c:5749,592f676d822a58d5568e8c6a6beb90dd597d801753f76d695475559d83:77cf,683879be548c4f55540876d28c8996026cb36db88d6b89109e648d3a563f9ed175d55f8872e0606854fc4ea86a2a886160528f7054c470d886799e3f6d2a5b8f5f187ea255894faf7334543c539a501954:0e7c,4e4e5ffd745a58f6846b80e1877472d07cca6e567c:434445464748494a4b4c4e4f505152535455565758595a5b5c5d5e5f606162636465666768696a6b6c6d6e6f70717275767778797a7e7f8081828384858687#888a8b8c8d8e8f90939496999a9ba0a1a3a6a7a8a9abacadafb0b4b5b6b7b8babb,5f27864e552c62a44e926caa623782b154d7534e733e6ed1753b521253168bdd69d05f8a60006dee574f6b2273af68538fd87f13636260a3552475ea8c6271156da35ba65e7b8352614c9ec478fa87577c27768751f060f6714c66435e4c604d8c0e707063258f895fbd606286d456de6bc160946167534960e066668d3f79fd4f1a70e96c478b:b3f2,7ed88364660f5a5a9b426d:51f7,8c416d3b4f19706b83b7621660d1970d8d27797851fb57:3efa,673a75787a3d79ef7b957c:bfc0c2c3c4c6c9cbcecfd0d1d2d3d4d8dadbdddee1e2e3e4e5e6e7e9eaebecedeef0f1f2f3f4f5f6f7f9fafcfdfeff,7d:000102030405060708090b0c0d0e0f10#1112131415161718191a1b1c1d1e1f212324252628292a2c2d2e30313233343536,808c99658ff96fc08ba59e2159ec7ee97f095409678168d88f917c4d96c653ca602575be6c7253735ac97ea7632451e0810a5df184df628051805b634f0e796d524260b86d4e5b:c4c2,8b:a1b0,65e25fcc964559937e:e7aa,560967b759394f735bb652a0835a988a8d3e753294be50477a3c4ef767b69a7e5ac16b7c76d1575a5c167b3a95f4714e517c80a9827059787f04832768c067ec78:b177,62e363617b804fed526a51cf835069db92748d:f531,89c1952e7bad4ef67d:3738393a3b3c3d3e3f404142434445464748494a4b4c4d4e4f505152535455565758595a5b5c5d5e5f606162636465666768696a6b6c6d6f70717273747576#78797a7b7c7d7e7f808182838485868788898a8b8c8d8e8f909192939495969798,506582305251996f6e:1085,6da75efa50f559dc5c066d466c5f7586848b686859568bb253209171964d854969127901712680f64ea490ca6d479a845a0756bc640594f077eb4fa5811a72e189d2997a7f347ede527f655991758f:7f83,53eb7a9663:eda5,768679f888579636622a52ab8282685467706377776b7aed6d017ed389e359d0621285c982a5754c501f4ecb75a58beb5c4a5dfe7b4b65a491d14eca6d25895f7d2795264ec58c288fdb9773664b79818fd170ec6d787d:999a9b9c9d9e9fa0a1a2a3a4a5a7a8a9aaabacadafb0b1b2b3b4b5b6b7b8b9babbbcbdbebfc0c1c2c3c4c5c6c7c8c9cacbcccdcecfd0d1d2d3d4d5d6d7d8d9#dadbdcdddedfe0e1e2e3e4e5e6e7e8e9eaebecedeeeff0f1f2f3f4f5f6f7f8f9fa,5c3d52b283465162830e775b66769cb84eac60ca7c:beb3,7ecf4e958b66666f988897595883656c955c5f8475c997567a:dfde,51c070af7a9863ea7a767ea0739697ed4e4570784e5d915253a965:51e7,81fc8205548e5c31759a97a062d872d975bd5c459a7983ca5c40548077e94e3e6cae805a62d2636e5de851778ddd8e1e952f4ff153e560e770ac526763509e435a1f5026773753777ee26485652b628963985014723589c951b38bc07edd574783cc94a7519b541b5cfb7d:fbfcfdfeff,7e:000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f30313233343536373839#3a3c3d3e3f40424344454648494a4b4c4d4e4f505152535455565758595a5b5c5d,4fca7ae36d5a90e19a8f55805496536154af5f0063e9697751ef6168520a582a52d8574e780d770b5eb761777ce062:5b97,4ea27095800362f770e49760577782db67ef68f578d5989779d158f354b353ef6e34514b523b5ba28bfe80af554357a660735751542d7a7a60505b5463a762a053e362635bc767af54ed7a9f82e691775e9388e4593857ae630e8de880ef57577b774fa95feb5bbd6b3e53217b5072c2684677:ff36,65f751b54e8f76d45cbf7aa58475594e9b4150807e:5e5f606162636465666768696a6b6c6d6e6f707172737475767778797a7b7c7d7e7f8081838485868788898a8b8c8d8e8f909192939495969798999a9c9d9e#aeb4bbbcd6e4ecf9,7f:0a101e37393b3c3d3e3f404143464748494a4b4c4d4e4f5253,998861276e8357646606634656f062:ec69,5ed39614578362c955878721814a8fa3556683b167658d5684dd5a6a680f62e67bee961151706f9c8c3063fd89c861d27f0670c26ee57405699472fc5eca90ce67176d6a635e52b3726280014f6c59e5916a70d96d9d52d24e5096f7956d857e78ca7d2f5121579264c2808b7c7b6cea68f1695e51b7539868a872819ece7bf172f879bb6f137406674e91cc9ca4793c83:8954,540f68174e3d538952b1783e5386522950884f:8bd0,7f:56595b5c5d5e6063646566676b6c6d6f7073757677787a7b7c7d7f8082838485868788898b8d8f9091929395969798999b9ca0a2a3a5a6a8a9aaabacadaeb1#b3b4b5b6b7babbbec0c2c3c4c6c7c8c9cbcdcfd0d1d2d3d6d7d9dadbdcdddee2e3,75e27acb7c926ca596b6529b748354e94fe9805483b28fde95705ec9601c6d9f5e18655b813894fe604b70bc7ec37cae51c968817cb1826f4e248f8691cf667e4eae8c0564a9804a50da759771ce5be58fbd6f664e86648295635ed66599521788c270c852a3730e7433679778f797164e3490bb9cde6dcb51db8d41541d62ce73b283f196f69f8494c34f367f9a51cc707596755cad988653e64ee46e9c740969b4786b998f7559521876246d4167f3516d9f99804b54997b3c7abf7f:e4e7e8eaebecedeff2f4f5f6f7f8f9fafdfeff,80:020708090a0e0f11131a1b1d1e1f2123242b2c2d2e2f303234393a3c3e404144454748494e4f505153555657#595b5c5d5e5f6061626364656667686b6c6d6e6f7072737475767778797a7b7c7d,9686578462e29647697c5a0464027bd36f0f964b82a6536298855e90708963b35364864f9c819e93788c97328d:ef42,9e7f6f5e79845f559646622e9a74541594dd4fa365c55c:6561,7f1586516c2f5f8b73876ee47eff5ce6631b5b6a6ee653754e7163a0756562a18f6e4f264ed16ca67eb68bba841d87ba7f57903b95237ba99aa188f8843d6d1b9a867edc59889ebb739b780186829a:6c82,561b541757cb4e709ea653568fc881097792999286ee6ee1851366fc61626f2b80:7e818285888a8d8e8f909192949597999ea3a6a7a8acb0b3b5b6b8b9bbc5c7c8c9cacbcfd0d1d2d3d4d5d8dfe0e2e3e6eef5f7f9fbfeff,81:000103040507080b#0c1517191b1c1d1f202122232425262728292a2b2d2e3033343537393a3b3c3d3f,8c298292832b76f26c135fd983bd732b8305951a6bdb77db94c6536f830251925e3d8c8c8d384e4873ab679a68859176970971646ca177095a9295416bcf7f8e66275bd059b95a9a95:e8f7,4eec84:0c99,6aac76df9530731b68a65b5f772f919a97617cdc8ff78c1c5f257c7379d889c56ccc871c5bc65e4268c977207ef551:954d,52c95a297f05976282d763cf778485d079d26e3a5e9959998511706d6c1162bf76bf654f60af95fd660e879f9e2394ed54:0d7d,8c2c647881:40414243444547494d4e4f525657585b5c5d5e5f6162636466686a6b6c6f727375767778818384858687898b8c8d8e90929394959697999a9e9fa0a1a2a4a5#a7a9abacadaeafb0b1b2b4b5b6b7b8b9bcbdbebfc4c5c7c8c9cbcdcecfd0d1d2d3,647986116a21819c78e864699b5462b9672b83ab58a89ed86cab6f205bde964c8c0b725f67d062c772614ea959c66bcd589366ae5e5552df6155672876ee776672677a4662ff54:ea50,94a090a35a1c7eb36c164e435976801059485357753796be56ca63208111607c95f96dd65462998151855ae980fd59ae9713502a6ce55c3c62df4f60533f817b90066eba852b62c85e7478be64b5637b5ff55a18917f9e1f5c3f634f80425b7d556e95:4a4d,6d8560a867e072de51dd5b8181:d4d5d6d7d8d9dadbdcdddedfe0e1e2e4e5e6e8e9ebeeeff0f1f2f5f6f7f8f9fafdff,82:030708090a0b0e0f111315161718191a1d2024252627292e323a3c3d3f#404142434546484a4c4d4e5051525354555657595b5c5d5e606162636465666769,62e76cde725b626d94ae7ebd81136d53519c5f04597452aa6012597366968650759f632a61e67cef8bfa54e66b279e256bb485d5545550766ca4556a8db4722c5e156015743662cd6392724c5f986e436d3e65006f5876d878d076fc7554522453db4e535e9e65c180:2ad6,629b5486522870ae888d8dd16ce1547880da57f988f48d54966a914d4f696c9b55b776c6783062a870f96f8e5f6d84ec68da787c7bf781a8670b9e4f636778b0576f7812973962:79ab,528874356bd782:6a6b6c6d71757677787b7c808183858687898c90939495969a9b9ea0a2a3a7b2b5b6babbbcbfc0c2c3c5c6c9d0d6d9dadde2e7e8e9eaecedeef0f2f3f5f6f8#fafcfdfeff,83:000a0b0d1012131618191d1e1f20212223242526292a2e3032373b3d,5564813e75b276ae533975de50fb5c418b6c7bc7504f72479a9798d86f0274e27968648777a562fc98918d2b54c180584e52576a82f9840d5e7351ed74f68bc45c4f57616cfc98875a4678349b448feb7c955256625194fa4ec68386846183e984b257d467345703666e6d668c3166dd7011671f6b3a6816621a59bb4e0351c46f0667d26c8f517668cb59476b6775665d0e81109f5065d779:4841,9a918d775c824e5e4f01542f5951780c56686c148fc45f036c:7de3,8bab639083:3e3f41424445484a4b4c4d4e5355565758595d6270717273747576797a7e7f808182838487888a8b8c8d8f909194959697999a9d9fa1a2a3a4a5a6a7acadae#afb5bbbebfc2c3c4c6c8c9cbcdced0d1d2d3d5d7d9dadbdee2e3e4e6e7e8ebeced,60706d3d7275626694:8ec5,53438fc17b7e4edf8c264e7e9ed494:b1b3,524d6f5c90636d458c3458115d4c6b:2049,67aa545b81547f8c589985375f3a62a26a47953965726084686577a74e544fa85de7979864ac7fd85ced4fcf7a8d520783044e14602f7a8394a64fb54eb279e6743452e482b964d279bd5bdd6c8197528f7b6c22503e537f6e0564ce66746c3060c598778bf75e86743c7a7779cb4e1890b174036c4256da914b6cc58d8b533a86c666f28eaf5c489a716e2083:eeeff3f4f5f6f7fafbfcfeff,84:0002050708090a10121314151617191a1b1e1f20212223292a2b2c2d2e2f30323334353637393a3b3e3f404142434445474849#4a4b4c4d4e4f505253545556585d5e5f606264656667686a6e6f70727477797b7c,53d65a369f8b8da353bb570898a76743919b6cc9516875ca62f372ac52:389d,7f3a7094763853749e4a69b7786e96c088d97fa471:36c3,518967d374e458e4651856b78ba9997662707ed560f970ed58ec4e:c1ba,5fcd97e74efb8ba45203598a7eab62544ecd65e5620e833884c98363878d71946eb65bb97ed2519763c967d480898339881551125b7a59828fb14e736c5d516589258f6f962e854a745e95:10f0,6da682e55f3164926d128428816e9cc3585e8d5b4e0953c184:7d7e7f8081838485868a8d8f90919293949596989a9b9d9e9fa0a2a3a4a5a6a7a8a9aaabacadaeb0b1b3b5b6b7bbbcbec0c2c3c5c6c7c8cbcccecfd2d4d5d7#d8d9dadbdcdee1e2e4e7e8e9eaebedeeeff1f2f3f4f5f6f7f8f9fafbfdfe,85:000102,4f1e6563685155d34e2764149a9a626b5ac2745f82726da968ee50e7838e7802674052396c997eb150bb5565715e7b5b665273ca82eb67495c715220717d886b95ea965564c58d6181b355846c5562477f2e58924f2455468d4f664c4e0a5c1a88f368a2634e7a0d70e7828d52fa97f65c1154e890b57ecd59628d4a86c782:0c0d,8d6664445c0461516d89793e8bbe78377533547b4f388eab6df15a207ec5795e6c885ba15a76751a80be614e6e1758f075:1f25,727253477ef385:030405060708090a0b0d0e0f101214151618191b1c1d1e2022232425262728292a2d2e2f303132333435363e3f404142444546474b4c4d4e4f505152535455#57585a5b5c5d5f60616263656667696a6b6c6d6e6f707173757677787c7d7f8081,770176db526980dc57235e08593172ee65bd6e7f8bd75c388671534177f362fe65f64ec098df86805b9e8bc653f277e24f7f5c4e9a7659cb5f0f793a58eb4e1667ff4e8b62ed8a93901d52bf662f55dc566c90024ed54f8d91ca99706c0f5e0260435ba489c68bd56536624b99965b:88ff,6388552e53d77626517d852c67a268b36b8a62928f9353d482126dd1758f4e668d4e5b70719f85af66:91d9,7f7287009ecd9f205c5e672f8ff06811675f620d7ad658855eb665706f3185:82838688898a8b8c8d8e909192939495969798999a9d9e9fa0a1a2a3a5a6a7a9abacadb1b2b3b4b5b6b8babbbcbdbebfc0c2c3c4c5c6c7c8cacbcccdced1d2#d4d6d7d8d9dadbdddedfe0e1e2e3e5e6e7e8eaebecedeeeff0f1f2f3f4f5f6f7f8,60555237800d6454887075295e05681362f4971c53cc723d8c016c3477617a0e542e77ac987a821c8bf47855671470c165af64955636601d79c153f84e1d6b7b80865bfa55e356db4f:3a3c,99725df3677e80386002988290015b8b8b:bcf5,641c825864de55fd82cf91654fd77d20901f7c9f50f358516eaf5bbf8bc980839178849c7b97867d96:8b8f,7ee59ad3788e5c817a57904296a7795f5b59635f7b0b84d168ad55067f2974107d2295016240584c4ed65b835979585485:f9fafcfdfe,86:0001020304060708090a0b0c0d0e0f10121314151718191a1b1c1d1e1f20212223242526282a2b2c2d2e2f3031323334353637393a3b3d3e3f40#4142434445464748494a4b4c525355565758595b5c5d5f6061636465666768696a,736d631e8e:4b0f,80ce82d462ac53f06cf0915e592a60016c70574d644a8d2a762b6ee9575b6a8075f06f6d8c:2d08,57666bef889278b363a253f970ad6c645858642a580268e0819b55107cd650188eba6dcc8d9f70eb638f6d9b6ed47ee68404684390036dd896768ba85957727985e4817e75bc8a8a68af52548e22951163d098988e44557c4f5366ff568f60d56d9552435c4959296dfb586b75:301c,606c82148146631167618fe2773a8d:f334,94c15e165385542c70c386:6d6f7072737475767778838485868788898e8f90919294969798999a9b9e9fa0a1a2a5a6abadaeb2b3b7b8b9bbbcbdbebfc1c2c3c5c8cccdd2d3d5d6d7dadc#dde0e1e2e3e5e6e7e8eaebeceff5f6f7fafbfcfdff,87:010405060b0c0e0f10111416,6c405ef7505c4ead5ead633a8247901a6850916e77b3540c94dc5f647ae5687663457b527edf75db507762955934900f51f879c37a8156fe5f9290146d825c60571f541051546e4d56e263a89893817f8715892a9000541e5c6f81c062:d658,81319e3596409a:6e7c,692d59a562d3553e631654c786d96d3c5a0374e6889c6b6a59168c4c5f2f6e7e73a9987d4e3870f75b8c7897633d665a769660cb5b9b5a494e0781556c6a738b4ea167897f515f8065fa671b5fd859845a0187:191b1d1f20242627282a2b2c2d2f303233353638393a3c3d404142434445464a4b4d4f505152545556585a5b5c5d5e5f6162666768696a6b6c6d6f71727375#7778797a7f8081848687898a8c8e8f90919294959698999a9b9c9d9ea0a1a2a3a4,5dcd5fae537197e68fdd684556f4552f60df4e3a6f4d7ef482c7840e59d44f:1f2a,5c3e7eac672a851a5473754f80c355829b4f4f4d6e2d8c135c096170536b761f6e29868a658795fb7eb9543b7a337d0a95ee55e17fc174ee631d87176da17a9d621165a1536763e16c835deb545c94a84e4c6c618bec5c4b65e0829c68a754:3e34,6b:cb66,4e9463425348821e4f:0dae,575e620a96fe6664726952:ffa1,609f8bef661471996790897f785277fd6670563b54389521727a87:a5a6a7a9aaaeb0b1b2b4b6b7b8b9bbbcbebfc1c2c3c4c5c7c8c9cccdcecfd0d4d5d6d7d8d9dadcdddedfe1e2e3e4e6e7e8e9ebecedeff0f1f2f3f4f5f6f7f8#fafbfcfdff,88:0001020405060708090b0c0d0e0f101112141718191a1c1d1e1f2023,7a00606f5e0c6089819d591560dc718470ef6eaa6c5072806a8488ad5e2d4e605ab3559c94e36d177cfb9699620f7ec6778e867e5323971e8f9666875ce14fa072ed4e0b53a6590f54136380952851484ed99c9c7ea454b88d248854823795f26d8e5f265acc663e966973:b02e,53bf817a99857fa15baa96:7750,7ebf76f853a2957699997bb189446e584e617fd479658be660f354cd4eab98795df76a6150cf54118c618427785d9704524a54ee56a395006d885bb56dc6665388:2425262728292a2b2c2d2e2f30313334353637383a3b3d3e3f414243464748494a4b4e4f505152535556585a5b5c5d5e5f6066676a6d6f717374757678797a#7b7c80838687898a8c8e8f90919394959798999a9b9d9e9fa0a1a3a5a6a7a8a9aa,5c0f5b5d6821809655787b11654869544e9b6b47874e978b534f631f643a90aa659c80c18c10519968b0537887f961c86c:c4fb,8c225c5185aa82af950c6b238f9b65b05f:fbc3,4fe18845661f8165732960fa51745211578b5f6290a2884c91925e78674f602759d351:44f6,80f853086c7996c4718a4f:11ee,7f9e673d55c5950879c088967ee3589f620c9700865a5618987b5f908bb884c4915753d965ed5e8f755c60647d6e5a7f7e:eaed,8f6955a75ba360ac65cb738488:acaeafb0b2b3b4b5b6b8b9babbbdbebfc0c3c4c7c8cacbcccdcfd0d1d3d6d7dadbdcdddee0e1e6e7e9eaebecedeeeff2f5f6f7fafbfdff,89:0001030405060708#090b0c0d0e0f1114151617181c1d1e1f20222324262728292c2d2e2f3132333537,9009766377297eda9774859b5b667a7496ea884052cb718f5faa65ec8be25bfb9a6f5de16b896c5b8b:adaf,900a8fc5538b62bc9e:262d,54404e2b82bd7259869c5d1688596daf96c554d14e9a8bb6710954bd960970df6df976d04e25781487125ca95ef68a00989c960e708e6cbf594463a9773c884d6f148273583071d5538c781a96c155015f6671305bb48c1a9a8c6b83592e9e2f79e76768626c4f6f75a17f8a6d0b96336c274ef075d2517b68376f3e908081705996747689:38393a3b3c3d3e3f40424345464748494a4b4c4d4e4f505152535455565758595a5b5c5d6061626364656768696a6b6c6d6e6f707172737475767778797a7c#7d7e808284858788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9fa0a1,64475c2790657a918c2359da54ac8200836f898180006930564e8036723791ce51b64e5f987563964e1a53f666f3814b591c6db24e0058f9533b63d694f14f:9d0a,886398905937905779fb4eea80f075916c825b9c59e85f5d69058681501a5df24e5977e34ee5827a6291661390915c794ebf5f7981c69038808475ab4ea688d4610f6bc55fc64e4976ca6ea28b:e3ae,8c0a8bd15f027f:fccc,7ece83:356b,56e06bb797f3963459fb541f94f66deb5bc5996e5c395f15969089:a2a3a4a5a6a7a8a9aaabacadaeafb0b1b2b3b4b5b6b7b8b9babbbcbdbebfc0c3cdd3d4d5d7d8d9dbdddfe0e1e2e4e7e8e9eaecedeef0f1f2f4f5f6f7f8f9fa#fbfcfdfeff,8a:01020304050608090a0b0c0d0e0f101112131415161718191a1b1c1d,537082f16a315a749e705e947f2883b984:2425,836787478fce8d6276c85f719896786c662054df62e54f6381c375c85eb896cd8e0a86f9548f6cf36d8c6c38607f52c775285e7d4f1860a05fe75c24753190ae94c072b96cb96e389149670953:cbf3,4f5191c98bf153c85e7c8fc26de44e8e76c26986865e611a82064f:59de,903e9c7c61096e:1d14,96854e885a3196e84e0e5c7f79b95b878bed7fbd738957df828b90c15401904755bb5cea5fa161086b3272f180b28a:891e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3f4041424344454647494a4b4c4d4e4f505152535455565758595a5b5c5d5e#5f606162636465666768696a6b6c6d6e6f7071727374757677787a7b7c7d7e7f80,6d745bd388d598848c6b9a6d9e336e0a51:a443,57a38881539f63f48f9556ed54585706733f6e907f188fdc82d1613f6028966266f07ea68d:8ac3,94a55cb37ca4670860a6960580184e9190e75300966851418fd08574915d665597f55b55531d78386742683d54c9707e5bb08f7d518d572854b1651266828d:5e43,810f846c906d7cdf51ff85fb67a365e96fa186a48e81566a90207682707671e58d2362e952196cfd8d3c600e589e618e66fe8d60624e55b36e23672d8f678a:81828384858687888b8c8d8e8f9091929495969798999a9b9c9d9e9fa0a1a2a3a4a5a6a7a8a9aaabacadaeafb0b1b2b3b4b5b6b7b8b9babbbcbdbebfc0c1c2#c3c4c5c6c7c8c9cacbcccdcecfd0d1d2d3d4d5d6d7d8d9dadbdcdddedfe0e1e2e3,94e195f87728680569a8548b4e4d70b88bc86458658b5b857a84503a5be877bb6be18a797c986cbe76cf65a98f975d2d5c5586386808536062187ad96e5b7efd6a1f7ae05f706f335f20638c6da867564e085e108d264ed780c07634969c62db662d627e6cbc8d7571677f695146808753ec906e629854f286f08f998005951785178fd96d5973cd659f771f7504782781fb8d1e94884fa6679575b98bca9707632f9547963584b8632377415f8172f04e896014657462ef6b63653f8a:e4e5e6e7e8e9eaebecedeeeff0f1f2f3f4f5f6f7f8f9fafbfcfdfeff,8b:0001020304050608090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20212223#24252728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f404142434445,5e2775c790d18bc1829d679d652f5431871877e580a281026c414e4b7ec7804c76f4690d6b966267503c4f84574063076b628dbe53ea65e87eb85fd763:1ab7,81:f3f4,7f6e5e1c5cd95236667a79e97a1a8d28709975d46ede6cbb7a924e2d76c55fe0949f88777ec879cd80bf91cd4ef24f17821f54685dde6d328bcc7ca58f7480985e1a549276b15b99663c9aa473e0682a86db6731732a8b:f8db,90107af970db716e62c477a956314e3b845767f152a986c08d2e94f87b518b:464748494a4b4c4d4e4f505152535455565758595a5b5c5d5e5f6061626364656768696a6b6d6e6f707172737475767778797a7b7c7d7e7f80818283848586#8788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9facb1bbc7d0ea,8c:091e,4f4f6ce8795d9a7b6293722a62fd4e1378168f6c64b08d5a7bc668695e8488c55986649e58ee72b6690e95258ffd8d5857607f008c0651c6634962d95353684c74228301914c55447740707c6d4a517954a88d4459ff6ecb6dc45b5c7d2b4ed47c7d6ed35b5081ea6e0d5b579b0368d58e2a5b977efc603b7eb590b98d70594f63cd79df8db3535265cf79568bc5963b7ec494bb7e825634918967007f6a5c0a907566285de64f5067de505a4f5c57505e:a7#3$,8c:38393a3b3c3d3e3f4042434445484a4b4d4e4f5051525354565758595b5c5d5e5f60636465666768696c6d6e6f707172747576777b7c7d7e7f808183848687#888b8d8e8f90919293959697999a9b9c9d9e9fa0a1a2a3a4a5a6a7a8a9aaabacad,4e:8d0c,51404e105eff53454e:15981e,9b325b6c56694e2879ba4e3f53154e47592d723b536e6c1056df80e499976bd3777e9f174e:369f,9f104e:5c6993,82885b5b556c560f4ec453:8d9da3a5ae,97658d5d53:1af5262e3e,8d5c53:6663,52:02080e2d333f404c5e615c,84af52:7d82819093,51827f544e:bbc3c9c2e8e1ebde,4f1b4ef34f:2264,4ef54f:2527092b5e67,65384f:5a5d,8c:aeafb0b1b2b3b4b5b6b7b8b9babbbcbdbebfc0c1c2c3c4c5c6c7c8c9cacbcccdcecfd0d1d2d3d4d5d6d7d8d9dadbdcdddedfe0e1e2e3e4e5e6e7e8e9eaebec#edeeeff0f1f2f3f4f5f6f7f8f9fafbfcfdfeff,8d:000102030405060708090a0b0c0d,4f:5f57323d76749189838f7e7baa7cac94e6e8eac5dae3dcd1dff8,50:294c,4ff350:2c0f2e2d,4ffe50:1c0c25287e4355484e6c7ba5a7a9bad6,510650:edece6ee,51:070b,4edd6c3d4f:5865ce,9fa06c467c74516e5dfd9ec999985181591452f9530d8a07531051eb591951554ea051564eb388:6ea4,4eb5811488d279805b3488037fb851:abb1bdbc,8d:0e0f101112131415161718191a1b1c205152575f6568696a6c6e6f717278797a7b7c7d7e7f808283868788898c8d8e8f90929395969798999a9b9c9d9ea0a1#a2a4a5a6a7a8a9aaabacadaeafb0b2b6b7b9bbbdc0c1c2c5c7c8c9cacdd0d2d3d4,51:c796a2a5,8b:a0a6a7aab4b5b7c2c3cbcfced2d3d4d6d8d9dcdfe0e4e8e9eef0f3f6f9fcff,8c:000204070c0f1112141516191b181d1f202125272a2b2e2f32333536,53:697a,96:1d2221312a3d3c4249545f676c7274888d97b0,90:979b9d99aca1b4b3b6ba,8d:d5d8d9dce0e1e2e5e6e7e9edeef0f1f2f4f6fcfeff,8e:00010203040607080b0d0e1011121315161718191a1b1c202124252627282b2d303233343637383b3c3e#3f4345464c4d4e4f505354555657585a5b5c5d5e5f60616263646567686a6b6e71,90:b8b0cfc5bed0c4c7d3e6e2dcd7dbebeffe,91:04221e23312f394346,520d594252:a2acadbe,54ff52:d0d6f0,53df71ee77cd5ef451:f5fc,9b2f53b65f01755a5def57:4ca9a1,58:7ebcc5d1,57:292c2a33392e2f5c3b4269856b867c7b686d7673ada48cb2cfa7b493a0d5d8dad9d2b8f4eff8e4dd,8e:73757778797a7b7d7e808283848688898a8b8c8d8e91929395969798999a9b9d9fa0a1a2a3a4a5a6a7a8a9aaadaeb0b1b3b4b5b6b7b8b9bbbcbdbebfc0c1c2#c3c4c5c6c7c8c9cacbcccdcfd0d1d2d3d4d5d6d7d8d9dadbdcdddedfe0e1e2e3e4,58:0b0d,57:fded,58:001e194420656c81899a80,99a89f1961ff82:797d7f8f8aa8848e919799abb8beb0c8cae398b7aecbccc1a9b4a1aa9fc4cea4e1,830982:f7e4,83:0f07,82:dcf4d2d8,830c82:fbd3,83:111a061415,82:e0d5,83:1c515b5c08923c34319b5e2f4f47435f4017602d3a336665,8e:e5e6e7e8e9eaebecedeeeff0f1f2f3f4f5f6f7f8f9fafbfcfdfeff,8f:000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20212223#2425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f4041424344,83:681b696c6a6d6eb078b3b4a0aa939c857cb6a97db87b989ea8babcc1,840183:e5d8,580784:180b,83:ddfdd6,84:1c381106,83:d4df,84:0f03,83:f8f9eac5c0,842683:f0e1,84:5c515a597387887a89783c4669768c8e316dc1cdd0e6bdd3cabfbae0a1b9b497e5e3,850c750d853884f085:391f3a,8f:45464748494a4b4c4d4e4f505152535455565758595a5b5c5d5e5f6061626364656a808c929da0a1a2a4a5a6a7aaacadaeafb2b3b4b5b7b8babbbcbfc0c3c6#c9cacbcccdcfd2d6d7dae0e1e3e7eceff1f2f4f5f6fafbfcfeff,90:07080c0e131518,85:563b,84:fffc,85:594868645e7a,77a285:43727ba4a8878f79ae9c85b9b7b0d3c1dcff,86:270529163c,5efe5f0859:3c41,803759:555a58,530f5c:22252c34,62:4c6a9fbbcadad7ee,632262f663:394b43adf6717a8eb46dac8a69aebcf2f8e0ffc4dece,645263:c6be,64:45410b1b200c26215e846d96,90:191c2324252728292a2b2c303132333437393a3d3f4043454648494a4b4c4e545556595a5c5d5e5f6061646667696a6b6c6f70717273767778797a7b7c7e81#84858687898a8c8d8e8f90929496989a9c9e9fa0a4a5a7a8a9abadb2b7bcbdbfc0,64:7ab7b899bac0d0d7e4e2,65:09252e,5f:0bd2,75195f1153:5ff1fde9e8fb,54:1216064b5253545643215759233282947771649a9b8476669dd0adc2b4d2a7a6d3d472a3d5bbbfccd9dadca9aaa4ddcfde,551b54e7552054fd551454f355:22230f11272a678fb5496d41553f503c,90:c2c3c6c8c9cbcccdd2d4d5d6d8d9dadedfe0e3e4e5e9eaeceef0f1f2f3f5f6f7f9fafbfcff,91:00010305060708090a0b0c0d0e0f1011121314151617181a1b1c#1d1f20212425262728292a2b2c2d2e30323334353637383a3b3c3d3e3f40414244,55:375675767733305c8bd283b1b988819f7ed6917bdfbdbe9499eaf7c9,561f55:d1ebecd4e6ddc4efe5f2f3cccde8f5e4,8f9456:1e080c012423,55fe56:00272d5839572c4d62595c4c548664716b7b7c8593afd4d7dde1f5ebf9ff,57:040a091c,5e:0f191411313b3c,91:454748515354555658595b5c5f606667686b6d737a7b7c808182838486888a8e8f939495969798999c9d9e9fa0a1a4a5a6a7a8a9abacb0b1b2b3b6b7b8b9bb#bcbdbebfc0c1c2c3c4c5c6c8cbd0d2d3d4d5d6d7d8d9dadbdddedfe0e1e2e3e4e5,5e:3744545b5e61,5c:8c7a8d9096889899919a9cb5a2bdacabb1a3c1b7c4d2e4cbe5,5d:020327262e241e061b583e343d6c5b6f5d6b4b4a697482999d,8c735d:b7c5,5f:73778287898c95999ca8adb5bc,88625f6172:adb0b4b7b8c3c1cecdd2e8efe9f2f4f7,730172f3730372fa91:e6e7e8e9eaebecedeeeff0f1f2f3f4f5f6f7f8f9fafbfcfdfeff,92:000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f2021222324#25262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f404142434445,72fb73:1713210a1e1d152239252c3831504d57606c6f7e,821b592598e759:2402,99:636768696a6b6c74777d8084878a8d9091939495,5e:80918b96a5a0b9b5beb3,8d535e:d2d1dbe8ea,81ba5f:c4c9d6cf,60035fee60045f:e1e4fe,60:0506,5f:eaedf8,60:1935261b0f0d292b0a3f2178797b7a42,92:464748494a4b4c4d4e4f505152535455565758595a5b5c5d5e5f606162636465666768696a6b6c6d6e6f7071727375767778797a7b7c7d7e7f808182838485#868788898a8b8c8d8f909192939495969798999a9b9c9d9e9fa0a1a2a3a4a5a6a7,60:6a7d969aad9d83928c9becbbb1ddd8c6dab4,61:20261523,60f461:000e2b4a75ac94a7b7d4f5,5fdd96b395:e9ebf1f3f5f6fcfe,96:030406080a0b0c0d0f12151617191a,4e2c723f62156c:35545c4aa38590948c6869747686a9d0d4adf7f8f1d7b2e0d6faebeeb1d3effe,92:a8a9aaabacadafb0b1b2b3b4b5b6b7b8b9babbbcbdbebfc0c1c2c3c4c5c6c7c9cacbcccdcecfd0d1d2d3d4d5d6d7d8d9dadbdcdddedfe0e1e2e3e4e5e6e7e8#e9eaebecedeeeff0f1f2f3f4f5f6f7f8f9fafbfcfdfeff,93:00010203040506070809,6d:39270c43480704190e2b4d2e351a4f525433916f9ea05e93945c607c63,6e1a6d:c7c5de,6e0e6d:bfe0,6e116d:e6ddd9,6e166dab6e0c6dae6e:2b6e4e6bb25f865354322544dfb198e0,6f2d6e:e2a5a7bdbbb7d7b4cf8fc29f,6f:6246472415,6ef96f:2f364b742a0929898d8c78727c7ad1,93:0a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3f40414243444546474849#4a4b4c4d4e4f505152535455565758595a5b5c5d5e5f606162636465666768696b,6f:c9a7b9b6c2e1eedee0ef,70:1a231b39354f5e,5b:80849593a5b8,752f9a9e64345b:e4ee,89305bf08e478b078f:b6d3d5e5eee4e9e6f3e8,90:05040b26110d162135362d2f445152506858625b,66b990:747d8288838b,5f:50575658,5c3b54ab5c:5059,5b715c:6366,7fbc5f:2a292d,82745f3c9b3b5c6e59:81838da9aaa3,93:6c6d6e6f707172737475767778797a7b7c7d7e7f808182838485868788898a8b8c8d8e909192939495969798999a9b9c9d9e9fa0a1a2a3a4a5a6a7a8a9aaab#acadaeafb0b1b2b3b4b5b6b7b8b9babbbcbdbebfc0c1c2c3c4c5c6c7c8c9cbcccd,59:97caab9ea4d2b2afd7be,5a:0506,59dd5a0859:e3d8f9,5a:0c09323411231340674a553c6275,80ec5a:aa9b777abeebb2d2d4b8e0e3f1d6e6d8dc,5b:091716323740,5c:151c,5b:5a6573515362,9a:7577787a7f7d808185888a90929396989b9c9d9fa0a2a3a5a7,7e:9fa1a3a5a8a9,93:cecfd0d1d2d3d4d5d7d8d9dadbdcdddedfe0e1e2e3e4e5e6e7e8e9eaebecedeeeff0f1f2f3f4f5f6f7f8f9fafbfcfdfeff,94:000102030405060708090a0b0c0d#0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e,7e:adb0bec0c1c2c9cbccd0d4d7dbe0e1e8ebeeeff1f2,7f0d7e:f6fafbfe,7f:01020307080b0c0f111217191c1b1f212223242526272a2b2c2d2f3031323335,5e7a757f5ddb753e909573:8e91aea29fcfc2d1b7b3c0c9c8e5d9,987c740a73:e9e7debaf2,74:0f2a5b262528302e2c,94:2f303132333435363738393a3b3c3d3f404142434445464748494a4b4c4d4e4f505152535455565758595a5b5c5d5e5f606162636465666768696a6c6d6e6f#707172737475767778797a7b7c7d7e7f8081828384919698c7cfd3d4dae6fb,95:1c20,74:1b1a415c575559776d7e9c8e8081878b9ea8a990a7d2ba,97:eaebec,67:4c535e4869a5876a7398a775a89ead8b777cf0,680967d8680a67:e9b0,680c67:d9b5dab3dd,680067:c3b8e2,680e67:c1fd,68:323360614e624464831d55664167403e4a4929b58f7477936bc2,696e68fc69:1f20,68f995:27333d43484b555a606e74757778797a7b7c7d7e808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9fa0a1a2a3a4a5a6a7a8a9aa#abacadaeafb0b1b2b3b4b5b6b7b8b9babbbcbdbebfc0c1c2c3c4c5c6c7c8c9cacb,692468f069:0b0157,68e369:10713960425d846b80987834cc8788ce896663799ba7bbabadd4b1c1cadf95e08dff,6a2f69ed6a:171865,69f26a:443ea0505b358e793d28587c9190a997ab,73:3752,6b:8182878492938d9a9ba1aa,8f:6b6d71727375767877797a7c7e818284878b,95:cccdcecfd0d1d2d3d4d5d6d7d8d9dadbdcdddedfe0e1e2e3e4e5e6e7ecff,96:0713181b1e20232425262728292b2c2d2f303738393a3e41434a4e4f5152535657#58595a5c5d5e606365666b6d6e6f70717378797a7b7c7d7e7f808182838487898a,8f:8d8e8f989a,8ece62:0b171b1f222125242c,81e774:eff4ff,75:0f1113,65:34eeeff0,66:0a19,677266:031500,708566:f71d34313635,800666:5f54414f56615777848ca79dbedbdce6e9,8d:3233363b3d4045464849474d5559,89:c7cacbcccecfd0d1,72:6e9f5d666f7e7f848b8d8f92,63:0832b0,96:8c8e91929395969a9b9d9e9fa0a1a2a3a4a5a6a8a9aaabacadaeafb1b2b4b5b7b8babbbfc2c3c8cacbd0d1d3d4d6d7d8d9dadbdcdddedfe1e2e3e4e5e6e7eb#ecedeef0f1f2f4f5f8fafbfcfdff,97:0203050a0b0c10111214151718191a1b1d1f20,64:3fd8,80046b:eaf3fdf5f9,6c:0507060d1518191a2129242a32,65:35556b,72:4d525630,8662521680:9f9c93bc,670a80:bdb1abadb4b7e7e8e9eadbc2c4d9cdd7,671080:ddebf1f4ed,81:0d0e,80:f2fc,671581128c5a81:361e2c1832484c5374595a7160697c7d6d67,584d5ab581:888291,6ed581:a3aacc,672681:cabb,97:2122232425262728292b2c2e2f3133343536373a3b3c3d3f404142434445464748494a4b4c4d4e4f5051545557585a5c5d5f63646667686a6b6c6d6e6f7071#72757778797a7b7d7e7f8081828384868788898a8c8e8f9093959697999a9b9c9d,81:c1a6,6b:243739434659,98:d1d2d3d5d9da,6bb35f406bc289f365909f5165:93bcc6c4c3ccced2d6,70:809c969dbbc0b7abb1e8ca,71:1013162f31735c6845724a787a98b3b5a8a0e0d4e7f9,72:1d28,706c71:1866b9,62:3e3d434849,79:3b4046495b5c535a6257606f677a858a9aa7b3,5f:d1d0,97:9e9fa1a2a4a5a6a7a8a9aaacaeb0b1b3b5b6b7b8b9babbbcbdbebfc0c1c2c3c4c5c6c7c8c9cacbcccdcecfd0d1d2d3d4d5d6d7d8d9dadbdcdddedfe0e1e2e3#e4e5e8eeeff0f1f2f4f7f8f9fafbfcfdfeff,98:000102030405060708090a0b0c0d0e,60:3c5d5a67415963ab,61:060d5da99dcbd1,620680:807f,6c:93f6,6dfc77:f6f8,78:0009171811,65ab78:2d1c1d393a3b1f3c252c23294e6d56572650474c6a9b939a879ca1a3b2b9a5d4d9c9ecf2,790578f479:13241e34,9f9b9e:f9fbfc,76f177:040d,76f977:07081a22192d263538505147435a68,98:0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f404142434445464748494a4b4c4d#4e4f505152535455565758595a5b5c5d5e5f606162636465666768696a6b6c6d6e,77:62657f8d7d808c919fa0b0b5bd,75:3a404e4b485b727983,7f:58615f,8a487f:68747179817e,76:cde5,883294:8586878b8a8c8d8f909497959a9b9ca3a4abaaadacafb0b2b4b6b7b8b9babcbdbfc4c8c9cacbcccdced0d1d2d5d6d7d9d8dbdedfe0e2e4e5e7e8ea,98:6f70717273748b8e929599a3a8a9aaabacadaeafb0b1b2b3b4b5b6b7b8b9babbbcbdbebfc0c1c2c3c4c5c6c7c8c9cacbcccdcfd0d4d6d7dbdcdde0e1e2e3e4#e5e6e9eaebecedeeeff0f1f2f3f4f5f6f7f8f9fafbfcfdfeff,99:0001020304050607,94:e9ebeeeff3f4f5f7f9fcfdff,95:03020607090a0d0e0f1213141516181b1d1e1f222a2b292c3132343637383c3e3f4235444546494c4e4f525354565758595b5e5f5d61626465666768696a6b6c6f7172733a,77:e7ec,96c979:d5ede3eb,7a065d477a:03021e14,99:08090a0b0c0e0f1112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2f303132333435363738393a3b3c3d3e3f40414243444546474849#4a4b4c4d4e4f50515253565758595a5b5c5d5e5f60616264667378797b7e828389,7a:393751,9ecf99a57a7076:888e9399a4,74:dee0,752c9e:202228292a2b2c3231363837393a3e414244464748494b4c4e5155575a5b5c5e63666768696a6b6c716d73,75:929496a09daca3b3b4b8c4b1b0c3c2d6cde3e8e6e4ebe7,760375:f1fcff,76:1000050c170a25181519,99:8c8e9a9b9c9d9e9fa0a1a2a3a4a6a7a9aaabacadaeafb0b1b2b3b4b5b6b7b8b9babbbcbdbebfc0c1c2c3c4c5c6c7c8c9cacbcccdcecfd0d1d2d3d4d5d6d7d8#d9dadbdcdddedfe0e1e2e3e4e5e6e7e8e9eaebecedeeeff0f1f2f3f4f5f6f7f8f9,76:1b3c2220402d303f35433e334d5e545c566b6f,7fca7a:e6787980868895a6a0aca8adb3,88:6469727d7f82a2c6b7bcc9e2cee3e5f1,891a88:fce8fef0,89:2119131b0a342b3641667b,758b80e576:b2b4,77dc80:1214161c20222526272928310b3543464d526971,898398:788083,99:fafbfcfdfeff,9a:000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738#393a3b3c3d3e3f404142434445464748494a4b4c4d4e4f50515253545556575859,98:898c8d8f949a9b9e9fa1a2a5a6,86:4d546c6e7f7a7c7ba88d8bac9da7a3aa93a9b6c4b5ceb0bab1afc9cfb4e9f1f2edf3d0,871386:def4dfd8d1,87:0307,86f887:080a0d09233b1e252e1a3e48343129373f82227d7e7b60704c6e8b53637c64596593afa8d2,9a:5a5b5c5d5e5f606162636465666768696a6b7283898d8e949599a6a9aaabacadaeafb2b3b4b5b9bbbdbebfc3c4c6c7c8c9cacdcecfd0d2d4d5d6d7d9dadbdc#dddee0e2e3e4e5e7e8e9eaeceef0f1f2f3f4f5f6f7f8fafcfdfeff,9b:000102040506,87:c68885ad9783abe5acb5b3cbd3bdd1c0cadbeae0ee,88:1613,87fe88:0a1b21393c,7f:36424445,82107a:fafd,7b:080304150a2b0f47382a192e31202524333e1e585a45754c5d606e7b62727190a6a7b8ac9da885aa9ca2abb4d1c1ccdddae5e6ea,7c0c7b:fefc,7c:0f160b,9b:07090a0b0c0d0e1011121415161718191a1b1c1d1e2021222425262728292a2b2c2d2e3031333435363738393a3d3e3f40464a4b4c4e50525355565758595a#5b5c5d5e5f606162636465666768696a6b6c6d6e6f707172737475767778797a7b,7c:1f2a26384140,81fe82:010204,81ec884482:2122232d2f282b383b33343e44494b4f5a5f68,88:7e8588d8df,895e7f:9d9fa7afb0b2,7c7c65497c:919d9c9ea2b2bcbdc1c7cccdc8c5d7e8,826e66a87f:bfced5e5e1e6e9eef3,7cf87d:77a6ae,7e:479b,9e:b8b4,8d:73849491b1676d,8c:4749,91:4a504e4f64,9b:7c7d7e7f808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9fa0a1a2a3a4a5a6a7a8a9aaabacadaeafb0b1b2b3b4b5b6b7b8b9ba#bbbcbdbebfc0c1c2c3c4c5c6c7c8c9cacbcccdcecfd0d1d2d3d4d5d6d7d8d9dadb,91:626170696f7d7e7274798c85908d91a2a3aaadaeafb5b4ba,8c559e7e8d:b8eb,8e:055969,8d:b5bfbcbac4d6d7dadececfdbc6ecf7f8e3f9fbe4,8e098dfd8e:141d1f2c2e232f3a4039353d3149414251524a70767c6f74858f94909c9e,8c:78828a859894,659b89:d6dedadc,9b:dcdddedfe0e1e2e3e4e5e6e7e8e9eaebecedeeeff0f1f2f3f4f5f6f7f8f9fafbfcfdfeff,9c:000102030405060708090a0b0c0d0e0f101112131415161718191a#1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b,89:e5ebef,8a3e8b26975396:e9f3ef,97:0601080f0e2a2d303e,9f:808385868788898a8c,9efe9f:0b0d,96:b9bcbdced2,77bf96e092:8eaec8,93:3e6aca8f,94:3e6b,9c:7f8285868788,7a239c:8b8e90919294959a9b9e9fa0a1a2a3a5a6a7a8a9abadaeb0b1b2b3b4b5b6b7babbbcbdc4c5c6c7cacb3c3d3e3f404142434445464748494a4b4c4d4e4f505152535455565758595a5b5c5d5e5f606162636465666768696a6b6c6d6e6f707172737475767778797a#7b7d7e808384898a8c8f93969798999daaacafb9bebfc0c1c2c8c9d1d2dadbe0e1cccdcecfd0d3d4d5d7d8d9dcdddfe2,97:7c85919294afaba3b2b4,9a:b1b0b7,9e589a:b6babcc1c0c5c2cbccd1,9b:45434749484d51,98e899:0d2e5554,9a:dfe1e6efebfbedf9,9b:080f131f23,9e:bdbe,7e3b9e:8287888b92,93d69e:9d9fdbdcdde0dfe2e9e7e5eaef,9f:222c2f39373d3e44,9c:e3e4e5e6e7e8e9eaebecedeeeff0f1f2f3f4f5f6f7f8f9fafbfcfdfeff,9d:000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f2021#22232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f404142#92$434445464748494a4b4c4d4e4f505152535455565758595a5b5c5d5e5f606162636465666768696a6b6c6d6e6f707172737475767778797a7b7c7d7e7f8081#82838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9fa0a1a2#92$a3a4a5a6a7a8a9aaabacadaeafb0b1b2b3b4b5b6b7b8b9babbbcbdbebfc0c1c2c3c4c5c6c7c8c9cacbcccdcecfd0d1d2d3d4d5d6d7d8d9dadbdcdddedfe0e1#e2e3e4e5e6e7e8e9eaebecedeeeff0f1f2f3f4f5f6f7f8f9fafbfcfdfeff,9e:000102#92$030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e24272e30343b3c404d5052535456595d5f606162656e6f727475767778797a7b7c7d80#8183848586898a8c8d8e8f90919495969798999a9b9c9ea0a1a2a3a4a5a7a8a9aa#92$abacadaeafb0b1b2b3b5b6b7b9babcbfc0c1c2c3c5c6c7c8cacbccd0d2d3d5d6d7d9dadee1e3e4e6e8ebecedeef0f1f2f3f4f5f6f7f8fafdff,9f:000102030405#060708090a0c0f1112141516181a1b1c1d1e1f21232425262728292a2b2d2e3031#92$3233343536383a3c3f4041424345464748494a4b4c4d4e4f52535455565758595a5b5c5d5e5f606162636465666768696a6b6c6d6e6f707172737475767778#797a7b7c7d7e81828d8e8f9091929394959697989c9d9ea1a2a3a4a5,f9:2c7995e7f1#92$,fa:0c0d0e0f111314181f20212324272829,e8:15161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f40414243#4445464748494a4b4c4d4e4f505152535455565758595a5b5c5d5e5f6061626364");
  135. let index = 0;
  136. this.#data = dataText.match(/..../g);
  137. for (let i = 0x81; i <= 0xfe; i++) {
  138. for (let j = 0x40; j <= 0xfe; j++) {
  139. this.#U2Ghash[this.#data[index++]] = ("%" +
  140. i.toString(16) +
  141. "%" +
  142. j.toString(16)).toUpperCase();
  143. }
  144. }
  145. for (let key in this.#U2Ghash) {
  146. this.#G2Uhash[this.#U2Ghash[key]] = key;
  147. }
  148. }
  149. handleText(text) {
  150. text = text
  151. .replace(/#(\d+)\$/g, function (a, b) {
  152. return Array(+b + 3).join("#");
  153. })
  154. .replace(/#/g, "####")
  155. .replace(/(\w\w):([\w#]+)(?:,|$)/g, function (a, hd, dt) {
  156. return dt.replace(/../g, function (a) {
  157. if (a != "##") {
  158. return hd + a;
  159. }
  160. else {
  161. return a;
  162. }
  163. });
  164. });
  165. return text;
  166. }
  167. isAscii(unicode) {
  168. return unicode <= 0x007f && unicode >= 0x0000;
  169. }
  170. /**
  171. * 编码
  172. * @param str
  173. */
  174. encode(str) {
  175. let that = this;
  176. return [...str].reduce((result, val, i) => {
  177. return result + toGBK(val);
  178. }, "");
  179. function toGBK(val) {
  180. let result = "";
  181. for (let i = 0; i < val.length; i++) {
  182. const codePoint = val.codePointAt(i);
  183. const code = String.fromCodePoint(codePoint);
  184. let key = codePoint.toString(16);
  185. key.length != 4 && (key = ("000" + key).match(/....$/)?.[0]);
  186. /* Add up i by code.length */
  187. i += code.length - 1;
  188. /* If code is in ascii range */
  189. if (that.isAscii(codePoint)) {
  190. result += encodeURIComponent(code);
  191. continue;
  192. }
  193. /* If Got encoded string from U2Ghash */
  194. if (that.#U2Ghash[key]) {
  195. result += that.#U2Ghash[key];
  196. continue;
  197. }
  198. /*
  199. If 2 or more char combines to one visible code,
  200. or just this code is not in GBK
  201. */
  202. result += toGBK(`&#${codePoint};`);
  203. }
  204. return result;
  205. }
  206. }
  207. /**
  208. * 解码
  209. * @param str
  210. */
  211. decode(str) {
  212. var GBKMatcher = /%[0-9A-F]{2}%[0-9A-F]{2}/;
  213. var UTFMatcher = /%[0-9A-F]{2}/;
  214. // @ts-ignore
  215. var utf = true;
  216. let that = this;
  217. while (utf) {
  218. let gbkMatch = str.match(GBKMatcher);
  219. let utfMatch = str.match(UTFMatcher);
  220. utf = Boolean(utfMatch);
  221. if (gbkMatch && gbkMatch in that.#G2Uhash) {
  222. str = str.replace(gbkMatch, String.fromCharCode(("0x" + that.#G2Uhash[gbkMatch])));
  223. }
  224. else {
  225. // @ts-ignore
  226. str = str.replace(utfMatch, decodeURIComponent(utfMatch));
  227. }
  228. }
  229. return str;
  230. }
  231. }
  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.  
  500. class UtilsGMCookie {
  501. windowApi = {
  502. window: window,
  503. document: document,
  504. };
  505. constructor(windowApiOption) {
  506. if (windowApiOption) {
  507. this.windowApi = Object.assign({}, windowApiOption);
  508. }
  509. }
  510. /**
  511. * 获取Cookie分组
  512. */
  513. getCookiesList() {
  514. if (this.windowApi.document.cookie.trim() === "") {
  515. return [];
  516. }
  517. return this.windowApi.document.cookie.split(";");
  518. }
  519. /**
  520. * 获取单个cookie
  521. * @param cookieName cookie名
  522. */
  523. get(cookieName) {
  524. if (typeof cookieName !== "string") {
  525. throw new TypeError("Utils.GMCookie.get 参数cookieName 必须为字符串");
  526. }
  527. let cookies = this.getCookiesList();
  528. let findValue = undefined;
  529. for (const cookieItem of cookies) {
  530. let item = cookieItem.trim();
  531. let itemSplit = item.split("=");
  532. let itemName = itemSplit[0];
  533. itemSplit.splice(0, 1);
  534. let itemValue = decodeURIComponent(itemSplit.join(""));
  535. if (itemName === cookieName) {
  536. findValue = {
  537. domain: this.windowApi.window.location.hostname,
  538. expirationDate: null,
  539. hostOnly: true,
  540. httpOnly: false,
  541. name: cookieName,
  542. path: "/",
  543. sameSite: "unspecified",
  544. secure: true,
  545. session: false,
  546. value: itemValue,
  547. };
  548. break;
  549. }
  550. }
  551. return findValue;
  552. }
  553. /**
  554. * 获取多组Cookie
  555. * @param option 配置
  556. * @param callback 获取操作后的回调
  557. * + cookies object[]
  558. * + error string|undefined
  559. **/
  560. list(option, callback) {
  561. if (option == null) {
  562. throw new Error("Utils.GMCookie.list 参数不能为空");
  563. }
  564. let resultData = [];
  565. try {
  566. let defaultOption = {
  567. url: this.windowApi.window.location.href,
  568. domain: this.windowApi.window.location.hostname,
  569. name: "",
  570. path: "/",
  571. };
  572. defaultOption = commonUtil.assign(defaultOption, option);
  573. let cookies = this.getCookiesList();
  574. cookies.forEach((item) => {
  575. item = item.trim();
  576. let itemSplit = item.split("=");
  577. let itemName = itemSplit[0];
  578. itemSplit.splice(0, 1);
  579. let itemValue = decodeURIComponent(itemSplit.join(""));
  580. let nameRegexp = defaultOption.name instanceof RegExp
  581. ? defaultOption.name
  582. : new RegExp("^" + defaultOption.name, "g");
  583. if (itemName.match(nameRegexp)) {
  584. resultData.push({
  585. domain: this.windowApi.window.location.hostname,
  586. expirationDate: null,
  587. hostOnly: true,
  588. httpOnly: false,
  589. name: itemName,
  590. path: "/",
  591. sameSite: "unspecified",
  592. secure: true,
  593. session: false,
  594. value: itemValue,
  595. });
  596. }
  597. });
  598. if (typeof callback === "function") {
  599. callback(resultData);
  600. }
  601. }
  602. catch (error) {
  603. if (typeof callback === "function") {
  604. callback(resultData, error);
  605. }
  606. }
  607. }
  608. /**
  609. * 获取多组Cookie
  610. * @param option 配置
  611. **/
  612. getList(option) {
  613. if (option == null) {
  614. throw new Error("Utils.GMCookie.list 参数不能为空");
  615. }
  616. let resultData = [];
  617. let defaultOption = {
  618. url: this.windowApi.window.location.href,
  619. domain: this.windowApi.window.location.hostname,
  620. name: "",
  621. path: "/",
  622. };
  623. defaultOption = commonUtil.assign(defaultOption, option);
  624. let cookies = this.getCookiesList();
  625. cookies.forEach((item) => {
  626. item = item.trim();
  627. let itemSplit = item.split("=");
  628. let itemName = itemSplit[0];
  629. itemSplit.splice(0, 1);
  630. let itemValue = decodeURIComponent(itemSplit.join(""));
  631. let nameRegexp = defaultOption.name instanceof RegExp
  632. ? defaultOption.name
  633. : new RegExp("^" + defaultOption.name, "g");
  634. if (itemName.match(nameRegexp)) {
  635. resultData.push({
  636. domain: this.windowApi.window.location.hostname,
  637. expirationDate: null,
  638. hostOnly: true,
  639. httpOnly: false,
  640. name: itemName,
  641. path: "/",
  642. sameSite: "unspecified",
  643. secure: true,
  644. session: false,
  645. value: itemValue,
  646. });
  647. }
  648. });
  649. return resultData;
  650. }
  651. /**
  652. * 设置Cookie
  653. * @param option 配置
  654. * @param callback 设置操作后的回调(成功/失败)
  655. */
  656. set(option, callback) {
  657. let errorInfo;
  658. try {
  659. let defaultOption = {
  660. url: this.windowApi.window.location.href,
  661. name: "",
  662. value: "",
  663. domain: "",
  664. path: "/",
  665. secure: true,
  666. httpOnly: false,
  667. /**
  668. * Expires in 30 days
  669. */
  670. expirationDate: Math.floor(Date.now()) + 60 * 60 * 24 * 30,
  671. };
  672. defaultOption = commonUtil.assign(defaultOption, option);
  673. let life = defaultOption.expirationDate
  674. ? defaultOption.expirationDate
  675. : Math.floor(Date.now()) + 60 * 60 * 24 * 30;
  676. let cookieStr = defaultOption.name +
  677. "=" +
  678. decodeURIComponent(defaultOption.value) +
  679. ";expires=" +
  680. new Date(life).toGMTString() +
  681. "; path=/";
  682. if (commonUtil.isNull(defaultOption.domain)) {
  683. cookieStr += "; domain=" + defaultOption.domain;
  684. }
  685. this.windowApi.document.cookie = cookieStr;
  686. }
  687. catch (error) {
  688. errorInfo = error;
  689. }
  690. finally {
  691. if (typeof callback === "function") {
  692. callback(errorInfo);
  693. }
  694. }
  695. }
  696. /**
  697. * 删除Cookie
  698. * @param option 配置
  699. * @param callback 删除操作后的回调(成功/失败)
  700. */
  701. delete(option, callback) {
  702. let errorInfo;
  703. try {
  704. let defaultOption = {
  705. url: this.windowApi.window.location.href,
  706. name: "",
  707. path: "/",
  708. firstPartyDomain: "",
  709. };
  710. defaultOption = commonUtil.assign(defaultOption, option);
  711. let cookieStr = `${defaultOption.name}=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=${defaultOption.path}`;
  712. if (commonUtil.isNull(defaultOption.firstPartyDomain)) {
  713. cookieStr += `; domain=${defaultOption.firstPartyDomain};`;
  714. }
  715. this.windowApi.document.cookie = cookieStr;
  716. }
  717. catch (error) {
  718. errorInfo = error;
  719. }
  720. finally {
  721. if (typeof callback === "function") {
  722. callback(errorInfo);
  723. }
  724. }
  725. }
  726. /**
  727. * 解析cookie字符串
  728. * 例如:document.cookie
  729. * @param cookieStr
  730. */
  731. parseCookie(cookieStr) {
  732. if (cookieStr.trim() === "") {
  733. return [];
  734. }
  735. let cookies = cookieStr.split(";");
  736. let result = [];
  737. for (const cookieItem of cookies) {
  738. let item = cookieItem.trim();
  739. let itemSplit = item.split("=");
  740. let itemName = itemSplit[0];
  741. itemSplit.splice(0, 1);
  742. let itemValue = decodeURIComponent(itemSplit.join(""));
  743. result.push({
  744. key: itemName,
  745. value: itemValue,
  746. });
  747. }
  748. return result;
  749. }
  750. }
  751.  
  752. // @name ajaxHooker
  753. // @author cxxjackie
  754. // @version 1.4.4
  755. // @updateLog 修复头条、抖音部分站点下this引用错误的问题。
  756. // @supportURL https://bbs.tampermonkey.net.cn/thread-3284-1-1.html
  757.  
  758. const AjaxHooker = function () {
  759. return (function () {
  760. const version = "1.4.4";
  761. const hookInst = {
  762. hookFns: [],
  763. filters: [],
  764. };
  765. const win = window.unsafeWindow || document.defaultView || window;
  766. let winAh = win.__ajaxHooker;
  767. const resProto = win.Response.prototype;
  768. const xhrResponses = ["response", "responseText", "responseXML"];
  769. const fetchResponses = ["arrayBuffer", "blob", "formData", "json", "text"];
  770. const fetchInitProps = [
  771. "method",
  772. "headers",
  773. "body",
  774. "mode",
  775. "credentials",
  776. "cache",
  777. "redirect",
  778. "referrer",
  779. "referrerPolicy",
  780. "integrity",
  781. "keepalive",
  782. "signal",
  783. "priority",
  784. ];
  785. const xhrAsyncEvents = ["readystatechange", "load", "loadend"];
  786. const getType = {}.toString.call.bind({}.toString);
  787. const getDescriptor = Object.getOwnPropertyDescriptor.bind(Object);
  788. const emptyFn = () => {};
  789. const errorFn = (e) => console.error(e);
  790. function isThenable(obj) {
  791. return (
  792. obj &&
  793. ["object", "function"].includes(typeof obj) &&
  794. typeof obj.then === "function"
  795. );
  796. }
  797. function catchError(fn, ...args) {
  798. try {
  799. const result = fn(...args);
  800. if (isThenable(result)) return result.then(null, errorFn);
  801. return result;
  802. } catch (err) {
  803. console.error(err);
  804. }
  805. }
  806. function defineProp(obj, prop, getter, setter) {
  807. Object.defineProperty(obj, prop, {
  808. configurable: true,
  809. enumerable: true,
  810. get: getter,
  811. set: setter,
  812. });
  813. }
  814. function readonly(obj, prop, value = obj[prop]) {
  815. defineProp(obj, prop, () => value, emptyFn);
  816. }
  817. function writable(obj, prop, value = obj[prop]) {
  818. Object.defineProperty(obj, prop, {
  819. configurable: true,
  820. enumerable: true,
  821. writable: true,
  822. value: value,
  823. });
  824. }
  825. function parseHeaders(obj) {
  826. const headers = {};
  827. switch (getType(obj)) {
  828. case "[object String]":
  829. for (const line of obj.trim().split(/[\r\n]+/)) {
  830. const [header, value] = line.split(/\s*:\s*/);
  831. if (!header) break;
  832. const lheader = header.toLowerCase();
  833. headers[lheader] =
  834. lheader in headers ? `${headers[lheader]}, ${value}` : value;
  835. }
  836. break;
  837. case "[object Headers]":
  838. for (const [key, val] of obj) {
  839. headers[key] = val;
  840. }
  841. break;
  842. case "[object Object]":
  843. return { ...obj };
  844. }
  845. return headers;
  846. }
  847. function stopImmediatePropagation() {
  848. this.ajaxHooker_isStopped = true;
  849. }
  850. class SyncThenable {
  851. then(fn) {
  852. fn && fn();
  853. return new SyncThenable();
  854. }
  855. }
  856. class AHRequest {
  857. constructor(request) {
  858. this.request = request;
  859. this.requestClone = { ...this.request };
  860. }
  861. shouldFilter(filters) {
  862. const { type, url, method, async } = this.request;
  863. return (
  864. filters.length &&
  865. !filters.find((obj) => {
  866. switch (true) {
  867. case obj.type && obj.type !== type:
  868. case getType(obj.url) === "[object String]" &&
  869. !url.includes(obj.url):
  870. case getType(obj.url) === "[object RegExp]" && !obj.url.test(url):
  871. case obj.method &&
  872. obj.method.toUpperCase() !== method.toUpperCase():
  873. case "async" in obj && obj.async !== async:
  874. return false;
  875. }
  876. return true;
  877. })
  878. );
  879. }
  880. waitForRequestKeys() {
  881. const requestKeys = ["url", "method", "abort", "headers", "data"];
  882. if (!this.request.async) {
  883. win.__ajaxHooker.hookInsts.forEach(({ hookFns, filters }) => {
  884. if (this.shouldFilter(filters)) return;
  885. hookFns.forEach((fn) => {
  886. if (getType(fn) === "[object Function]")
  887. catchError(fn, this.request);
  888. });
  889. requestKeys.forEach((key) => {
  890. if (isThenable(this.request[key]))
  891. this.request[key] = this.requestClone[key];
  892. });
  893. });
  894. return new SyncThenable();
  895. }
  896. const promises = [];
  897. win.__ajaxHooker.hookInsts.forEach(({ hookFns, filters }) => {
  898. if (this.shouldFilter(filters)) return;
  899. promises.push(
  900. Promise.all(hookFns.map((fn) => catchError(fn, this.request))).then(
  901. () =>
  902. Promise.all(
  903. requestKeys.map((key) =>
  904. Promise.resolve(this.request[key]).then(
  905. (val) => (this.request[key] = val),
  906. () => (this.request[key] = this.requestClone[key])
  907. )
  908. )
  909. )
  910. )
  911. );
  912. });
  913. return Promise.all(promises);
  914. }
  915. waitForResponseKeys(response) {
  916. const responseKeys =
  917. this.request.type === "xhr" ? xhrResponses : fetchResponses;
  918. if (!this.request.async) {
  919. if (getType(this.request.response) === "[object Function]") {
  920. catchError(this.request.response, response);
  921. responseKeys.forEach((key) => {
  922. if (
  923. "get" in getDescriptor(response, key) ||
  924. isThenable(response[key])
  925. ) {
  926. delete response[key];
  927. }
  928. });
  929. }
  930. return new SyncThenable();
  931. }
  932. return Promise.resolve(
  933. catchError(this.request.response, response)
  934. ).then(() =>
  935. Promise.all(
  936. responseKeys.map((key) => {
  937. const descriptor = getDescriptor(response, key);
  938. if (descriptor && "value" in descriptor) {
  939. return Promise.resolve(descriptor.value).then(
  940. (val) => (response[key] = val),
  941. () => delete response[key]
  942. );
  943. } else {
  944. delete response[key];
  945. }
  946. })
  947. )
  948. );
  949. }
  950. }
  951. const proxyHandler = {
  952. get(target, prop) {
  953. const descriptor = getDescriptor(target, prop);
  954. if (
  955. descriptor &&
  956. !descriptor.configurable &&
  957. !descriptor.writable &&
  958. !descriptor.get
  959. )
  960. return target[prop];
  961. const ah = target.__ajaxHooker;
  962. if (ah && ah.proxyProps) {
  963. if (prop in ah.proxyProps) {
  964. const pDescriptor = ah.proxyProps[prop];
  965. if ("get" in pDescriptor) return pDescriptor.get();
  966. if (typeof pDescriptor.value === "function")
  967. return pDescriptor.value.bind(ah);
  968. return pDescriptor.value;
  969. }
  970. if (typeof target[prop] === "function")
  971. return target[prop].bind(target);
  972. }
  973. return target[prop];
  974. },
  975. set(target, prop, value) {
  976. const descriptor = getDescriptor(target, prop);
  977. if (
  978. descriptor &&
  979. !descriptor.configurable &&
  980. !descriptor.writable &&
  981. !descriptor.set
  982. )
  983. return true;
  984. const ah = target.__ajaxHooker;
  985. if (ah && ah.proxyProps && prop in ah.proxyProps) {
  986. const pDescriptor = ah.proxyProps[prop];
  987. pDescriptor.set
  988. ? pDescriptor.set(value)
  989. : (pDescriptor.value = value);
  990. } else {
  991. target[prop] = value;
  992. }
  993. return true;
  994. },
  995. };
  996. class XhrHooker {
  997. constructor(xhr) {
  998. const ah = this;
  999. Object.assign(ah, {
  1000. originalXhr: xhr,
  1001. proxyXhr: new Proxy(xhr, proxyHandler),
  1002. resThenable: new SyncThenable(),
  1003. proxyProps: {},
  1004. proxyEvents: {},
  1005. });
  1006. xhr.addEventListener("readystatechange", (e) => {
  1007. if (
  1008. ah.proxyXhr.readyState === 4 &&
  1009. ah.request &&
  1010. typeof ah.request.response === "function"
  1011. ) {
  1012. const response = {
  1013. finalUrl: ah.proxyXhr.responseURL,
  1014. status: ah.proxyXhr.status,
  1015. responseHeaders: parseHeaders(
  1016. ah.proxyXhr.getAllResponseHeaders()
  1017. ),
  1018. };
  1019. const tempValues = {};
  1020. for (const key of xhrResponses) {
  1021. try {
  1022. tempValues[key] = ah.originalXhr[key];
  1023. } catch (err) {}
  1024. defineProp(
  1025. response,
  1026. key,
  1027. () => {
  1028. return (response[key] = tempValues[key]);
  1029. },
  1030. (val) => {
  1031. delete response[key];
  1032. response[key] = val;
  1033. }
  1034. );
  1035. }
  1036. ah.resThenable = new AHRequest(ah.request)
  1037. .waitForResponseKeys(response)
  1038. .then(() => {
  1039. for (const key of xhrResponses) {
  1040. ah.proxyProps[key] = {
  1041. get: () => {
  1042. if (!(key in response)) response[key] = tempValues[key];
  1043. return response[key];
  1044. },
  1045. };
  1046. }
  1047. });
  1048. }
  1049. ah.dispatchEvent(e);
  1050. });
  1051. xhr.addEventListener("load", (e) => ah.dispatchEvent(e));
  1052. xhr.addEventListener("loadend", (e) => ah.dispatchEvent(e));
  1053. for (const evt of xhrAsyncEvents) {
  1054. const onEvt = "on" + evt;
  1055. ah.proxyProps[onEvt] = {
  1056. get: () => ah.proxyEvents[onEvt] || null,
  1057. set: (val) => ah.addEvent(onEvt, val),
  1058. };
  1059. }
  1060. for (const method of [
  1061. "setRequestHeader",
  1062. "addEventListener",
  1063. "removeEventListener",
  1064. "open",
  1065. "send",
  1066. ]) {
  1067. ah.proxyProps[method] = { value: ah[method] };
  1068. }
  1069. }
  1070. toJSON() {} // Converting circular structure to JSON
  1071. addEvent(type, event) {
  1072. if (type.startsWith("on")) {
  1073. this.proxyEvents[type] = typeof event === "function" ? event : null;
  1074. } else {
  1075. if (typeof event === "object" && event !== null)
  1076. event = event.handleEvent;
  1077. if (typeof event !== "function") return;
  1078. this.proxyEvents[type] = this.proxyEvents[type] || new Set();
  1079. this.proxyEvents[type].add(event);
  1080. }
  1081. }
  1082. removeEvent(type, event) {
  1083. if (type.startsWith("on")) {
  1084. this.proxyEvents[type] = null;
  1085. } else {
  1086. if (typeof event === "object" && event !== null)
  1087. event = event.handleEvent;
  1088. this.proxyEvents[type] && this.proxyEvents[type].delete(event);
  1089. }
  1090. }
  1091. dispatchEvent(e) {
  1092. e.stopImmediatePropagation = stopImmediatePropagation;
  1093. defineProp(e, "target", () => this.proxyXhr);
  1094. defineProp(e, "currentTarget", () => this.proxyXhr);
  1095. this.proxyEvents[e.type] &&
  1096. this.proxyEvents[e.type].forEach((fn) => {
  1097. this.resThenable.then(
  1098. () => !e.ajaxHooker_isStopped && fn.call(this.proxyXhr, e)
  1099. );
  1100. });
  1101. if (e.ajaxHooker_isStopped) return;
  1102. const onEvent = this.proxyEvents["on" + e.type];
  1103. onEvent && this.resThenable.then(onEvent.bind(this.proxyXhr, e));
  1104. }
  1105. setRequestHeader(header, value) {
  1106. this.originalXhr.setRequestHeader(header, value);
  1107. if (!this.request) return;
  1108. const headers = this.request.headers;
  1109. headers[header] =
  1110. header in headers ? `${headers[header]}, ${value}` : value;
  1111. }
  1112. addEventListener(...args) {
  1113. if (xhrAsyncEvents.includes(args[0])) {
  1114. this.addEvent(args[0], args[1]);
  1115. } else {
  1116. this.originalXhr.addEventListener(...args);
  1117. }
  1118. }
  1119. removeEventListener(...args) {
  1120. if (xhrAsyncEvents.includes(args[0])) {
  1121. this.removeEvent(args[0], args[1]);
  1122. } else {
  1123. this.originalXhr.removeEventListener(...args);
  1124. }
  1125. }
  1126. open(method, url, async = true, ...args) {
  1127. this.request = {
  1128. type: "xhr",
  1129. url: url.toString(),
  1130. method: method.toUpperCase(),
  1131. abort: false,
  1132. headers: {},
  1133. data: null,
  1134. response: null,
  1135. async: !!async,
  1136. };
  1137. this.openArgs = args;
  1138. this.resThenable = new SyncThenable();
  1139. [
  1140. "responseURL",
  1141. "readyState",
  1142. "status",
  1143. "statusText",
  1144. ...xhrResponses,
  1145. ].forEach((key) => {
  1146. delete this.proxyProps[key];
  1147. });
  1148. return this.originalXhr.open(method, url, async, ...args);
  1149. }
  1150. send(data) {
  1151. const ah = this;
  1152. const xhr = ah.originalXhr;
  1153. const request = ah.request;
  1154. if (!request) return xhr.send(data);
  1155. request.data = data;
  1156. new AHRequest(request).waitForRequestKeys().then(() => {
  1157. if (request.abort) {
  1158. if (typeof request.response === "function") {
  1159. Object.assign(ah.proxyProps, {
  1160. responseURL: { value: request.url },
  1161. readyState: { value: 4 },
  1162. status: { value: 200 },
  1163. statusText: { value: "OK" },
  1164. });
  1165. xhrAsyncEvents.forEach((evt) =>
  1166. xhr.dispatchEvent(new Event(evt))
  1167. );
  1168. }
  1169. } else {
  1170. xhr.open(
  1171. request.method,
  1172. request.url,
  1173. request.async,
  1174. ...ah.openArgs
  1175. );
  1176. for (const header in request.headers) {
  1177. xhr.setRequestHeader(header, request.headers[header]);
  1178. }
  1179. xhr.send(request.data);
  1180. }
  1181. });
  1182. }
  1183. }
  1184. function fakeXHR() {
  1185. const xhr = new winAh.realXHR();
  1186. if ("__ajaxHooker" in xhr)
  1187. console.warn("检测到不同版本的ajaxHooker,可能发生冲突!");
  1188. xhr.__ajaxHooker = new XhrHooker(xhr);
  1189. return xhr.__ajaxHooker.proxyXhr;
  1190. }
  1191. fakeXHR.prototype = win.XMLHttpRequest.prototype;
  1192. Object.keys(win.XMLHttpRequest).forEach(
  1193. (key) => (fakeXHR[key] = win.XMLHttpRequest[key])
  1194. );
  1195. function fakeFetch(url, options = {}) {
  1196. if (!url) return winAh.realFetch.call(win, url, options);
  1197. return new Promise(async (resolve, reject) => {
  1198. const init = {};
  1199. if (getType(url) === "[object Request]") {
  1200. for (const prop of fetchInitProps) init[prop] = url[prop];
  1201. if (url.body) init.body = await url.arrayBuffer();
  1202. url = url.url;
  1203. }
  1204. url = url.toString();
  1205. Object.assign(init, options);
  1206. init.method = init.method || "GET";
  1207. init.headers = init.headers || {};
  1208. const request = {
  1209. type: "fetch",
  1210. url: url,
  1211. method: init.method.toUpperCase(),
  1212. abort: false,
  1213. headers: parseHeaders(init.headers),
  1214. data: init.body,
  1215. response: null,
  1216. async: true,
  1217. };
  1218. const req = new AHRequest(request);
  1219. await req.waitForRequestKeys();
  1220. if (request.abort) {
  1221. if (typeof request.response === "function") {
  1222. const response = {
  1223. finalUrl: request.url,
  1224. status: 200,
  1225. responseHeaders: {},
  1226. };
  1227. await req.waitForResponseKeys(response);
  1228. const key = fetchResponses.find((k) => k in response);
  1229. let val = response[key];
  1230. if (key === "json" && typeof val === "object") {
  1231. val = catchError(JSON.stringify.bind(JSON), val);
  1232. }
  1233. const res = new Response(val, {
  1234. status: 200,
  1235. statusText: "OK",
  1236. });
  1237. defineProp(res, "type", () => "basic");
  1238. defineProp(res, "url", () => request.url);
  1239. resolve(res);
  1240. } else {
  1241. reject(new DOMException("aborted", "AbortError"));
  1242. }
  1243. return;
  1244. }
  1245. init.method = request.method;
  1246. init.headers = request.headers;
  1247. init.body = request.data;
  1248. winAh.realFetch.call(win, request.url, init).then((res) => {
  1249. if (typeof request.response === "function") {
  1250. const response = {
  1251. finalUrl: res.url,
  1252. status: res.status,
  1253. responseHeaders: parseHeaders(res.headers),
  1254. };
  1255. fetchResponses.forEach(
  1256. (key) =>
  1257. (res[key] = function () {
  1258. if (key in response) return Promise.resolve(response[key]);
  1259. return resProto[key].call(this).then((val) => {
  1260. response[key] = val;
  1261. return req
  1262. .waitForResponseKeys(response)
  1263. .then(() => (key in response ? response[key] : val));
  1264. });
  1265. })
  1266. );
  1267. }
  1268. resolve(res);
  1269. }, reject);
  1270. });
  1271. }
  1272. function fakeFetchClone() {
  1273. const descriptors = Object.getOwnPropertyDescriptors(this);
  1274. const res = winAh.realFetchClone.call(this);
  1275. Object.defineProperties(res, descriptors);
  1276. return res;
  1277. }
  1278. winAh = win.__ajaxHooker = winAh || {
  1279. version,
  1280. fakeXHR,
  1281. fakeFetch,
  1282. fakeFetchClone,
  1283. realXHR: win.XMLHttpRequest,
  1284. realFetch: win.fetch,
  1285. realFetchClone: resProto.clone,
  1286. hookInsts: new Set(),
  1287. };
  1288. if (winAh.version !== version)
  1289. console.warn("检测到不同版本的ajaxHooker,可能发生冲突!");
  1290. win.XMLHttpRequest = winAh.fakeXHR;
  1291. win.fetch = winAh.fakeFetch;
  1292. resProto.clone = winAh.fakeFetchClone;
  1293. winAh.hookInsts.add(hookInst);
  1294. // 针对头条、抖音 secsdk.umd.js 的兼容性处理
  1295. class AHFunction {
  1296. call(thisArg, ...args) {
  1297. if (
  1298. thisArg &&
  1299. thisArg.__ajaxHooker &&
  1300. thisArg.__ajaxHooker.proxyXhr === thisArg
  1301. ) {
  1302. thisArg = thisArg.__ajaxHooker.originalXhr;
  1303. }
  1304. return Reflect.apply(this, thisArg, args);
  1305. }
  1306. apply(thisArg, args) {
  1307. if (
  1308. thisArg &&
  1309. thisArg.__ajaxHooker &&
  1310. thisArg.__ajaxHooker.proxyXhr === thisArg
  1311. ) {
  1312. thisArg = thisArg.__ajaxHooker.originalXhr;
  1313. }
  1314. return Reflect.apply(this, thisArg, args || []);
  1315. }
  1316. }
  1317. function hookSecsdk(csrf) {
  1318. Object.setPrototypeOf(
  1319. csrf.nativeXMLHttpRequestSetRequestHeader,
  1320. AHFunction.prototype
  1321. );
  1322. Object.setPrototypeOf(
  1323. csrf.nativeXMLHttpRequestOpen,
  1324. AHFunction.prototype
  1325. );
  1326. Object.setPrototypeOf(
  1327. csrf.nativeXMLHttpRequestSend,
  1328. AHFunction.prototype
  1329. );
  1330. }
  1331. if (win.secsdk) {
  1332. if (win.secsdk.csrf && win.secsdk.csrf.nativeXMLHttpRequestOpen)
  1333. hookSecsdk(win.secsdk.csrf);
  1334. } else {
  1335. defineProp(win, "secsdk", emptyFn, (secsdk) => {
  1336. delete win.secsdk;
  1337. win.secsdk = secsdk;
  1338. defineProp(secsdk, "csrf", emptyFn, (csrf) => {
  1339. delete secsdk.csrf;
  1340. secsdk.csrf = csrf;
  1341. if (csrf.nativeXMLHttpRequestOpen) hookSecsdk(csrf);
  1342. });
  1343. });
  1344. }
  1345. return {
  1346. hook: (fn) => hookInst.hookFns.push(fn),
  1347. filter: (arr) => {
  1348. if (Array.isArray(arr)) hookInst.filters = arr;
  1349. },
  1350. protect: () => {
  1351. readonly(win, "XMLHttpRequest", winAh.fakeXHR);
  1352. readonly(win, "fetch", winAh.fakeFetch);
  1353. readonly(resProto, "clone", winAh.fakeFetchClone);
  1354. },
  1355. unhook: () => {
  1356. winAh.hookInsts.delete(hookInst);
  1357. if (!winAh.hookInsts.size) {
  1358. writable(win, "XMLHttpRequest", winAh.realXHR);
  1359. writable(win, "fetch", winAh.realFetch);
  1360. writable(resProto, "clone", winAh.realFetchClone);
  1361. delete win.__ajaxHooker;
  1362. }
  1363. },
  1364. };
  1365. })();
  1366. };
  1367.  
  1368. // ==UserScript==
  1369. // @name ajaxHooker
  1370. // @author cxxjackie
  1371. // @version 1.2.4
  1372. // @supportURL https://bbs.tampermonkey.net.cn/thread-3284-1-1.html
  1373. // ==/UserScript==
  1374.  
  1375. const AjaxHooker1_2_4 = function () {
  1376. return (function () {
  1377. const win = window.unsafeWindow || document.defaultView || window;
  1378. const hookFns = [];
  1379. const realXhr = win.XMLHttpRequest;
  1380. const resProto = win.Response.prototype;
  1381. const toString = Object.prototype.toString;
  1382. const realFetch = win.fetch;
  1383. const xhrResponses = ["response", "responseText", "responseXML"];
  1384. const fetchResponses = ["arrayBuffer", "blob", "formData", "json", "text"];
  1385. const xhrAsyncEvents = ["readystatechange", "load", "loadend"];
  1386. let filter;
  1387. function emptyFn() {}
  1388. function errorFn(err) {
  1389. console.error(err);
  1390. }
  1391. function defineProp(obj, prop, getter, setter) {
  1392. Object.defineProperty(obj, prop, {
  1393. configurable: true,
  1394. enumerable: true,
  1395. get: getter,
  1396. set: setter,
  1397. });
  1398. }
  1399. function readonly(obj, prop, value = obj[prop]) {
  1400. defineProp(obj, prop, () => value, emptyFn);
  1401. }
  1402. function writable(obj, prop, value = obj[prop]) {
  1403. Object.defineProperty(obj, prop, {
  1404. configurable: true,
  1405. enumerable: true,
  1406. writable: true,
  1407. value: value,
  1408. });
  1409. }
  1410. function toFilterObj(obj) {
  1411. return {
  1412. type: obj.type,
  1413. url: obj.url,
  1414. method: obj.method && obj.method.toUpperCase(),
  1415. };
  1416. }
  1417. function shouldFilter(type, url, method) {
  1418. return (
  1419. filter &&
  1420. !filter.find(
  1421. (obj) =>
  1422. (!obj.type || obj.type === type) &&
  1423. (!obj.url ||
  1424. (toString.call(obj.url) === "[object String]"
  1425. ? url.includes(obj.url)
  1426. : obj.url.test(url))) &&
  1427. (!obj.method || obj.method === method.toUpperCase())
  1428. )
  1429. );
  1430. }
  1431. function lookupGetter(obj, prop) {
  1432. let getter;
  1433. let proto = obj;
  1434. while (proto) {
  1435. const descriptor = Object.getOwnPropertyDescriptor(proto, prop);
  1436. getter = descriptor && descriptor.get;
  1437. if (getter) break;
  1438. proto = Object.getPrototypeOf(proto);
  1439. }
  1440. return getter ? getter.bind(obj) : emptyFn;
  1441. }
  1442. function waitForHookFns(request) {
  1443. return Promise.all(
  1444. hookFns.map((fn) => Promise.resolve(fn(request)).then(emptyFn, errorFn))
  1445. );
  1446. }
  1447. function waitForRequestKeys(request, requestClone) {
  1448. return Promise.all(
  1449. ["url", "method", "abort", "headers", "data"].map((key) => {
  1450. return Promise.resolve(request[key]).then(
  1451. (val) => (request[key] = val),
  1452. () => (request[key] = requestClone[key])
  1453. );
  1454. })
  1455. );
  1456. }
  1457. function fakeEventSIP() {
  1458. this.ajaxHooker_stopped = true;
  1459. }
  1460. function xhrDelegateEvent(e) {
  1461. const xhr = e.target;
  1462. e.stopImmediatePropagation = fakeEventSIP;
  1463. xhr.__ajaxHooker.hookedEvents[e.type].forEach(
  1464. (fn) => !e.ajaxHooker_stopped && fn.call(xhr, e)
  1465. );
  1466. const onEvent = xhr.__ajaxHooker.hookedEvents["on" + e.type];
  1467. typeof onEvent === "function" && onEvent.call(xhr, e);
  1468. }
  1469. function xhrReadyStateChange(e) {
  1470. if (e.target.readyState === 4) {
  1471. e.target.dispatchEvent(
  1472. new CustomEvent("ajaxHooker_responseReady", { detail: e })
  1473. );
  1474. } else {
  1475. e.target.__ajaxHooker.delegateEvent(e);
  1476. }
  1477. }
  1478. function xhrLoadAndLoadend(e) {
  1479. e.target.__ajaxHooker.delegateEvent(e);
  1480. }
  1481. function fakeXhrOpen(method, url, ...args) {
  1482. const ah = this.__ajaxHooker;
  1483. ah.url = url.toString();
  1484. ah.method = method.toUpperCase();
  1485. ah.openArgs = args;
  1486. ah.headers = {};
  1487. return ah.originalMethods.open(method, url, ...args);
  1488. }
  1489. function fakeXhr() {
  1490. const xhr = new realXhr();
  1491. let ah = xhr.__ajaxHooker;
  1492. if (!ah) {
  1493. ah = xhr.__ajaxHooker = {
  1494. headers: {},
  1495. hookedEvents: {
  1496. readystatechange: new Set(),
  1497. load: new Set(),
  1498. loadend: new Set(),
  1499. },
  1500. delegateEvent: xhrDelegateEvent,
  1501. originalGetters: {},
  1502. originalMethods: {},
  1503. };
  1504. xhr.addEventListener("readystatechange", xhrReadyStateChange);
  1505. xhr.addEventListener("load", xhrLoadAndLoadend);
  1506. xhr.addEventListener("loadend", xhrLoadAndLoadend);
  1507. for (const key of xhrResponses) {
  1508. ah.originalGetters[key] = lookupGetter(xhr, key);
  1509. }
  1510. for (const method of [
  1511. "open",
  1512. "setRequestHeader",
  1513. "addEventListener",
  1514. "removeEventListener",
  1515. ]) {
  1516. ah.originalMethods[method] = xhr[method].bind(xhr);
  1517. }
  1518. xhr.open = fakeXhrOpen;
  1519. xhr.setRequestHeader = (header, value) => {
  1520. ah.originalMethods.setRequestHeader(header, value);
  1521. if (xhr.readyState === 1) {
  1522. if (ah.headers[header]) {
  1523. ah.headers[header] += ", " + value;
  1524. } else {
  1525. ah.headers[header] = value;
  1526. }
  1527. }
  1528. };
  1529. xhr.addEventListener = function (...args) {
  1530. if (xhrAsyncEvents.includes(args[0])) {
  1531. ah.hookedEvents[args[0]].add(args[1]);
  1532. } else {
  1533. ah.originalMethods.addEventListener(...args);
  1534. }
  1535. };
  1536. xhr.removeEventListener = function (...args) {
  1537. if (xhrAsyncEvents.includes(args[0])) {
  1538. ah.hookedEvents[args[0]].delete(args[1]);
  1539. } else {
  1540. ah.originalMethods.removeEventListener(...args);
  1541. }
  1542. };
  1543. xhrAsyncEvents.forEach((evt) => {
  1544. const onEvt = "on" + evt;
  1545. defineProp(
  1546. xhr,
  1547. onEvt,
  1548. () => {
  1549. return ah.hookedEvents[onEvt] || null;
  1550. },
  1551. (val) => {
  1552. ah.hookedEvents[onEvt] = typeof val === "function" ? val : null;
  1553. }
  1554. );
  1555. });
  1556. }
  1557. const realSend = xhr.send.bind(xhr);
  1558. xhr.send = function (data) {
  1559. if (xhr.readyState !== 1) return realSend(data);
  1560. ah.delegateEvent = xhrDelegateEvent;
  1561. xhrResponses.forEach((prop) => {
  1562. delete xhr[prop]; // delete descriptor
  1563. });
  1564. if (shouldFilter("xhr", ah.url, ah.method)) {
  1565. xhr.addEventListener("ajaxHooker_responseReady", (e) => {
  1566. ah.delegateEvent(e.detail);
  1567. });
  1568. return realSend(data);
  1569. }
  1570. try {
  1571. const request = {
  1572. type: "xhr",
  1573. url: ah.url,
  1574. method: ah.method,
  1575. abort: false,
  1576. headers: ah.headers,
  1577. data: data,
  1578. response: null,
  1579. };
  1580. const requestClone = { ...request };
  1581. waitForHookFns(request).then(() => {
  1582. waitForRequestKeys(request, requestClone).then(() => {
  1583. if (request.abort) return;
  1584. ah.originalMethods.open(
  1585. request.method,
  1586. request.url,
  1587. ...ah.openArgs
  1588. );
  1589. for (const header in request.headers) {
  1590. ah.originalMethods.setRequestHeader(
  1591. header,
  1592. request.headers[header]
  1593. );
  1594. }
  1595. data = request.data;
  1596. xhr.addEventListener("ajaxHooker_responseReady", (e) => {
  1597. try {
  1598. if (typeof request.response === "function") {
  1599. const arg = {
  1600. finalUrl: xhr.responseURL,
  1601. status: xhr.status,
  1602. responseHeaders: {},
  1603. };
  1604. for (const line of xhr
  1605. .getAllResponseHeaders()
  1606. .trim()
  1607. .split(/[\r\n]+/)) {
  1608. const parts = line.split(/:\s*/);
  1609. if (parts.length === 2) {
  1610. const lheader = parts[0].toLowerCase();
  1611. if (arg.responseHeaders[lheader]) {
  1612. arg.responseHeaders[lheader] += ", " + parts[1];
  1613. } else {
  1614. arg.responseHeaders[lheader] = parts[1];
  1615. }
  1616. }
  1617. }
  1618. xhrResponses.forEach((prop) => {
  1619. defineProp(
  1620. arg,
  1621. prop,
  1622. () => {
  1623. return (arg[prop] = ah.originalGetters[prop]());
  1624. },
  1625. (val) => {
  1626. delete arg[prop];
  1627. arg[prop] = val;
  1628. }
  1629. );
  1630. defineProp(xhr, prop, () => {
  1631. const val = ah.originalGetters[prop]();
  1632. xhr.dispatchEvent(
  1633. new CustomEvent("ajaxHooker_readResponse", {
  1634. detail: { prop, val },
  1635. })
  1636. );
  1637. return val;
  1638. });
  1639. });
  1640. xhr.addEventListener("ajaxHooker_readResponse", (e) => {
  1641. arg[e.detail.prop] = e.detail.val;
  1642. });
  1643. const resPromise = Promise.resolve(
  1644. request.response(arg)
  1645. ).then(() => {
  1646. const task = [];
  1647. xhrResponses.forEach((prop) => {
  1648. const descriptor = Object.getOwnPropertyDescriptor(
  1649. arg,
  1650. prop
  1651. );
  1652. if (descriptor && "value" in descriptor) {
  1653. task.push(
  1654. Promise.resolve(descriptor.value).then((val) => {
  1655. arg[prop] = val;
  1656. defineProp(xhr, prop, () => {
  1657. xhr.dispatchEvent(
  1658. new CustomEvent("ajaxHooker_readResponse", {
  1659. detail: { prop, val },
  1660. })
  1661. );
  1662. return val;
  1663. });
  1664. }, emptyFn)
  1665. );
  1666. }
  1667. });
  1668. return Promise.all(task);
  1669. }, errorFn);
  1670. const eventsClone = {};
  1671. xhrAsyncEvents.forEach((type) => {
  1672. eventsClone[type] = new Set([...ah.hookedEvents[type]]);
  1673. eventsClone["on" + type] = ah.hookedEvents["on" + type];
  1674. });
  1675. ah.delegateEvent = (event) =>
  1676. resPromise.then(() => {
  1677. event.stopImmediatePropagation = fakeEventSIP;
  1678. eventsClone[event.type].forEach(
  1679. (fn) =>
  1680. !event.ajaxHooker_stopped && fn.call(xhr, event)
  1681. );
  1682. const onEvent = eventsClone["on" + event.type];
  1683. typeof onEvent === "function" &&
  1684. onEvent.call(xhr, event);
  1685. });
  1686. }
  1687. } catch (err) {
  1688. console.error(err);
  1689. }
  1690. ah.delegateEvent(e.detail);
  1691. });
  1692. realSend(data);
  1693. });
  1694. });
  1695. } catch (err) {
  1696. console.error(err);
  1697. realSend(data);
  1698. }
  1699. };
  1700. return xhr;
  1701. }
  1702. function hookFetchResponse(response, arg, callback) {
  1703. fetchResponses.forEach((prop) => {
  1704. response[prop] = () =>
  1705. new Promise((resolve, reject) => {
  1706. resProto[prop].call(response).then((res) => {
  1707. if (prop in arg) {
  1708. resolve(arg[prop]);
  1709. } else {
  1710. try {
  1711. arg[prop] = res;
  1712. Promise.resolve(callback(arg)).then(() => {
  1713. if (prop in arg) {
  1714. Promise.resolve(arg[prop]).then(
  1715. (val) => resolve((arg[prop] = val)),
  1716. () => resolve(res)
  1717. );
  1718. } else {
  1719. resolve(res);
  1720. }
  1721. }, errorFn);
  1722. } catch (err) {
  1723. console.error(err);
  1724. resolve(res);
  1725. }
  1726. }
  1727. }, reject);
  1728. });
  1729. });
  1730. }
  1731. function fakeFetch(url, init) {
  1732. if (url && typeof url.toString === "function") {
  1733. url = url.toString();
  1734. init = init || {};
  1735. init.method = init.method || "GET";
  1736. init.headers = init.headers || {};
  1737. if (shouldFilter("fetch", url, init.method))
  1738. return realFetch.call(win, url, init);
  1739. const request = {
  1740. type: "fetch",
  1741. url: url,
  1742. method: init.method.toUpperCase(),
  1743. abort: false,
  1744. headers: {},
  1745. data: init.body,
  1746. response: null,
  1747. };
  1748. if (toString.call(init.headers) === "[object Headers]") {
  1749. for (const [key, val] of init.headers) {
  1750. request.headers[key] = val;
  1751. }
  1752. } else {
  1753. request.headers = { ...init.headers };
  1754. }
  1755. const requestClone = { ...request };
  1756. return new Promise((resolve, reject) => {
  1757. try {
  1758. waitForHookFns(request).then(() => {
  1759. waitForRequestKeys(request, requestClone).then(() => {
  1760. if (request.abort) return reject("aborted");
  1761. url = request.url;
  1762. init.method = request.method;
  1763. init.headers = request.headers;
  1764. init.body = request.data;
  1765. realFetch.call(win, url, init).then((response) => {
  1766. if (typeof request.response === "function") {
  1767. const arg = {
  1768. finalUrl: response.url,
  1769. status: response.status,
  1770. responseHeaders: {},
  1771. };
  1772. for (const [key, val] of response.headers) {
  1773. arg.responseHeaders[key] = val;
  1774. }
  1775. hookFetchResponse(response, arg, request.response);
  1776. response.clone = () => {
  1777. const resClone = resProto.clone.call(response);
  1778. hookFetchResponse(resClone, arg, request.response);
  1779. return resClone;
  1780. };
  1781. }
  1782. resolve(response);
  1783. }, reject);
  1784. });
  1785. });
  1786. } catch (err) {
  1787. console.error(err);
  1788. return realFetch.call(win, url, init);
  1789. }
  1790. });
  1791. } else {
  1792. return realFetch.call(win, url, init);
  1793. }
  1794. }
  1795. win.XMLHttpRequest = fakeXhr;
  1796. Object.keys(realXhr).forEach((key) => (fakeXhr[key] = realXhr[key]));
  1797. fakeXhr.prototype = realXhr.prototype;
  1798. win.fetch = fakeFetch;
  1799. return {
  1800. hook: (fn) => hookFns.push(fn),
  1801. filter: (arr) => {
  1802. filter = Array.isArray(arr) && arr.map(toFilterObj);
  1803. },
  1804. protect: () => {
  1805. readonly(win, "XMLHttpRequest", fakeXhr);
  1806. readonly(win, "fetch", fakeFetch);
  1807. },
  1808. unhook: () => {
  1809. writable(win, "XMLHttpRequest", realXhr);
  1810. writable(win, "fetch", realFetch);
  1811. },
  1812. };
  1813. })();
  1814. };
  1815.  
  1816. class GMMenu {
  1817. GM_Api = {
  1818. /**
  1819. * 获取存储的数据
  1820. */
  1821. getValue: null,
  1822. /**
  1823. * 设置数据到存储
  1824. */
  1825. setValue: null,
  1826. /**
  1827. * 注册菜单
  1828. */
  1829. registerMenuCommand: null,
  1830. /**
  1831. * 卸载菜单
  1832. */
  1833. unregisterMenuCommand: null,
  1834. };
  1835. MenuHandle = {
  1836. context: this,
  1837. $data: {
  1838. /**
  1839. * 菜单数据
  1840. */
  1841. data: [],
  1842. /**
  1843. * 本地存储的键名
  1844. */
  1845. key: "GM_Menu_Local_Map",
  1846. },
  1847. $default: {
  1848. /** 自动刷新网页,默认为true */
  1849. autoReload: true,
  1850. /**
  1851. * 菜单isStoreValue的默认值
  1852. */
  1853. isStoreValue: true,
  1854. },
  1855. $emoji: {
  1856. /**
  1857. * 菜单enable为true的emoji
  1858. */
  1859. success: "✅",
  1860. /**
  1861. * 菜单enable为false的emoji
  1862. */
  1863. error: "❌",
  1864. },
  1865. /**
  1866. * 初始化数据
  1867. */
  1868. init() {
  1869. for (let index = 0; index < this.$data.data.length; index++) {
  1870. let menuOption = this.$data.data[index]["data"];
  1871. menuOption.enable = Boolean(this.getLocalMenuData(menuOption.key, menuOption.enable));
  1872. if (typeof menuOption.showText !== "function") {
  1873. menuOption.showText = (menuText, menuEnable) => {
  1874. if (menuEnable) {
  1875. return this.$emoji.success + " " + menuText;
  1876. }
  1877. else {
  1878. return this.$emoji.error + " " + menuText;
  1879. }
  1880. };
  1881. }
  1882. }
  1883. },
  1884. /**
  1885. * 注册油猴菜单
  1886. * @param menuOptions 如果存在,使用它
  1887. */
  1888. register(menuOptions) {
  1889. let that = this;
  1890. if (menuOptions == null) {
  1891. throw new TypeError("register菜单数据不能为空");
  1892. }
  1893. if (!Array.isArray(menuOptions)) {
  1894. menuOptions = [menuOptions];
  1895. }
  1896. for (let index = 0; index < menuOptions.length; index++) {
  1897. let cloneMenuOptionData = commonUtil.deepClone(menuOptions[index].data);
  1898. const { showText, clickCallBack } = this.handleMenuData(cloneMenuOptionData);
  1899. let menuId = that.context.GM_Api.registerMenuCommand(showText, clickCallBack);
  1900. menuOptions[index].id = menuId;
  1901. cloneMenuOptionData.deleteMenu = function () {
  1902. that.context.GM_Api.unregisterMenuCommand(menuId);
  1903. };
  1904. Reflect.deleteProperty(menuOptions[index], "handleData");
  1905. menuOptions[index].handleData = cloneMenuOptionData;
  1906. }
  1907. },
  1908. /**
  1909. * 获取本地存储菜单键值
  1910. * @param {string} key 键
  1911. */
  1912. getLocalMenuData(key, defaultValue) {
  1913. let localData = this.context.GM_Api.getValue(this.$data.key, {});
  1914. if (key in localData) {
  1915. return localData[key];
  1916. }
  1917. else {
  1918. return defaultValue;
  1919. }
  1920. },
  1921. /**
  1922. * 设置本地存储菜单键值
  1923. * @param key 键
  1924. * @param value 值
  1925. */
  1926. setLocalMenuData(key, value) {
  1927. let localData = this.context.GM_Api.getValue(this.$data.key, {});
  1928. localData[key] = value;
  1929. this.context.GM_Api.setValue(this.$data.key, localData);
  1930. },
  1931. /**
  1932. * 处理初始化配置
  1933. * @param menuOption
  1934. */
  1935. handleInitDetail(menuOption) {
  1936. menuOption.enable = Boolean(this.getLocalMenuData(menuOption.key, menuOption.enable));
  1937. if (typeof menuOption.showText !== "function") {
  1938. menuOption.showText = (menuText, menuEnable) => {
  1939. if (menuEnable) {
  1940. return this.$emoji.success + " " + menuText;
  1941. }
  1942. else {
  1943. return this.$emoji.error + " " + menuText;
  1944. }
  1945. };
  1946. }
  1947. return menuOption;
  1948. },
  1949. /**
  1950. * 对菜单数据进行处理
  1951. * @param menuOption
  1952. */
  1953. handleMenuData(menuOption) {
  1954. let that = this;
  1955. let menuLocalDataItemKey = menuOption.key;
  1956. /* 菜单默认开启的状态 */
  1957. let defaultEnable = Boolean(this.getLocalMenuData(menuLocalDataItemKey, menuOption.enable));
  1958. /** 油猴菜单上显示的文本 */
  1959. let showText = menuOption.showText(menuOption.text, defaultEnable);
  1960. // @ts-ignore
  1961. ({
  1962. /**
  1963. * 菜单的id
  1964. */
  1965. id: menuOption.id,
  1966. /**
  1967. * 点击菜单项后是否应关闭弹出菜单
  1968. */
  1969. autoClose: menuOption.autoClose,
  1970. /**
  1971. * 菜单项的可选访问键
  1972. */
  1973. accessKey: menuOption.accessKey,
  1974. /**
  1975. * 菜单项的鼠标悬浮上的工具提示
  1976. */
  1977. title: menuOption.title,
  1978. });
  1979. /* 点击菜单后触发callback后的网页是否刷新 */
  1980. menuOption.autoReload =
  1981. typeof menuOption.autoReload !== "boolean"
  1982. ? this.$default.autoReload
  1983. : menuOption.autoReload;
  1984. /* 点击菜单后触发callback后的网页是否存储值 */
  1985. menuOption.isStoreValue =
  1986. typeof menuOption.isStoreValue !== "boolean"
  1987. ? this.$default.isStoreValue
  1988. : menuOption.isStoreValue;
  1989. /**
  1990. * 用户点击菜单后的回调函数
  1991. * @param event
  1992. */
  1993. function clickCallBack(event) {
  1994. let localEnable = Boolean(that.getLocalMenuData(menuLocalDataItemKey, defaultEnable));
  1995. if (menuOption.isStoreValue) {
  1996. that.setLocalMenuData(menuLocalDataItemKey, !localEnable);
  1997. }
  1998. if (typeof menuOption.callback === "function") {
  1999. menuOption.callback({
  2000. key: menuLocalDataItemKey,
  2001. enable: !localEnable,
  2002. oldEnable: localEnable,
  2003. event: event,
  2004. storeValue(value) {
  2005. that.setLocalMenuData(menuLocalDataItemKey, value);
  2006. },
  2007. });
  2008. }
  2009. /* 不刷新网页就刷新菜单 */
  2010. if (menuOption.autoReload) {
  2011. window.location.reload();
  2012. }
  2013. else {
  2014. that.context.update();
  2015. }
  2016. }
  2017. return {
  2018. showText,
  2019. clickCallBack,
  2020. };
  2021. },
  2022. /**
  2023. * 获取目标菜单配置数据
  2024. * @param menuKey 菜单-键key
  2025. */
  2026. getMenuData(menuKey) {
  2027. return this.$data.data.find((item) => item.data.key === menuKey);
  2028. },
  2029. /**
  2030. * 获取目标菜单配置
  2031. * @param menuKey 菜单-键key
  2032. */
  2033. getMenuOption(menuKey) {
  2034. return this.$data.data.find((item) => item.data.key === menuKey)?.data;
  2035. },
  2036. /**
  2037. * 获取目标菜单处理后的配置
  2038. * @param menuKey 菜单-键key
  2039. */
  2040. getMenuHandledOption(menuKey) {
  2041. return this.$data.data.find((item) => item.handleData.key === menuKey)
  2042. ?.handleData;
  2043. },
  2044. };
  2045. constructor(details) {
  2046. this.GM_Api.getValue = details.GM_getValue;
  2047. this.GM_Api.setValue = details.GM_setValue;
  2048. this.GM_Api.registerMenuCommand = details.GM_registerMenuCommand;
  2049. this.GM_Api.unregisterMenuCommand = details.GM_unregisterMenuCommand;
  2050. this.MenuHandle.$default.autoReload =
  2051. typeof details.autoReload === "boolean" ? details.autoReload : true;
  2052. for (const keyName of Object.keys(this.GM_Api)) {
  2053. if (typeof this.GM_Api[keyName] !== "function") {
  2054. throw new Error(`Utils.GM_Menu 请在脚本开头加上 @grant ${keyName},且传入该对象`);
  2055. }
  2056. }
  2057. this.add(details?.data || []);
  2058. }
  2059. /**
  2060. * 新增菜单数据
  2061. * @param menuOption
  2062. */
  2063. __add(menuOption) {
  2064. if (Array.isArray(menuOption)) {
  2065. for (let index = 0; index < menuOption.length; index++) {
  2066. const option = menuOption[index];
  2067. this.MenuHandle.$data.data.push({
  2068. data: option,
  2069. id: undefined,
  2070. });
  2071. }
  2072. }
  2073. else {
  2074. this.MenuHandle.$data.data.push({
  2075. data: menuOption,
  2076. id: undefined,
  2077. });
  2078. }
  2079. }
  2080. /**
  2081. * 新增菜单数据
  2082. *
  2083. * 自动调用.update()
  2084. * @param menuOption
  2085. */
  2086. add(menuOption) {
  2087. this.__add(menuOption);
  2088. this.update();
  2089. }
  2090. /**
  2091. * 更新菜单数据
  2092. *
  2093. * 实现方式:先取消注册所有已注册的菜单、再依次注册所有菜单项
  2094. *
  2095. * 如果菜单不存在,新增菜单项
  2096. *
  2097. * 如果菜单已存在,新菜单项覆盖旧的菜单项
  2098. * @param options 数据
  2099. */
  2100. update(options) {
  2101. let menuOptionList = [];
  2102. if (Array.isArray(options)) {
  2103. menuOptionList = [...menuOptionList, ...options];
  2104. }
  2105. else if (options != null) {
  2106. menuOptionList = [...menuOptionList, options];
  2107. }
  2108. menuOptionList.forEach((menuOption) => {
  2109. let oldMenuOption = this.MenuHandle.getMenuOption(menuOption.key);
  2110. if (oldMenuOption) {
  2111. // 覆盖
  2112. Object.assign(oldMenuOption, menuOption);
  2113. }
  2114. else {
  2115. this.__add(menuOption);
  2116. }
  2117. });
  2118. this.MenuHandle.$data.data.forEach((value) => {
  2119. if (value.handleData) {
  2120. value.handleData.deleteMenu();
  2121. }
  2122. });
  2123. this.MenuHandle.init();
  2124. this.MenuHandle.register(this.MenuHandle.$data.data);
  2125. }
  2126. /**
  2127. * 卸载菜单
  2128. * @param menuId 已注册的菜单id
  2129. */
  2130. delete(menuId) {
  2131. this.GM_Api.unregisterMenuCommand(menuId);
  2132. }
  2133. /**
  2134. * 根据键值获取enable值
  2135. * @param menuKey 菜单-键key
  2136. * @deprecated
  2137. */
  2138. get(menuKey) {
  2139. return this.getEnable(menuKey);
  2140. }
  2141. /**
  2142. * 根据键值获取enable值
  2143. * @param menuKey 菜单-键key
  2144. */
  2145. getEnable(menuKey) {
  2146. return this.MenuHandle.getMenuHandledOption(menuKey).enable;
  2147. }
  2148. /**
  2149. * 根据键值获取text值
  2150. * @param menuKey 菜单-键key
  2151. */
  2152. getText(menuKey) {
  2153. return this.MenuHandle.getMenuHandledOption(menuKey).text;
  2154. }
  2155. /**
  2156. * 根据键值获取showText函数的值
  2157. * @param menuKey 菜单-键key
  2158. */
  2159. getShowTextValue(menuKey) {
  2160. return this.MenuHandle.getMenuHandledOption(menuKey).showText(this.getText(menuKey), this.getEnable(menuKey));
  2161. }
  2162. /**
  2163. * 获取当前已注册菜单的id
  2164. * @param menuKey
  2165. */
  2166. getMenuId(menuKey) {
  2167. let result = null;
  2168. for (let index = 0; index < this.MenuHandle.$data.data.length; index++) {
  2169. const optionData = this.MenuHandle.$data.data[index];
  2170. if (optionData.handleData.key === menuKey) {
  2171. result = optionData.id;
  2172. break;
  2173. }
  2174. }
  2175. return result;
  2176. }
  2177. /**
  2178. * 根据键值获取accessKey值
  2179. * @param menuKey 菜单-键key
  2180. */
  2181. getAccessKey(menuKey) {
  2182. return this.MenuHandle.getMenuHandledOption(menuKey)?.accessKey;
  2183. }
  2184. /**
  2185. * 根据键值获取autoClose值
  2186. * @param menuKey 菜单-键key
  2187. */
  2188. getAutoClose(menuKey) {
  2189. return this.MenuHandle.getMenuHandledOption(menuKey)?.autoClose;
  2190. }
  2191. /**
  2192. * 根据键值获取autoReload值
  2193. * @param menuKey 菜单-键key
  2194. */
  2195. getAutoReload(menuKey) {
  2196. return this.MenuHandle.getMenuHandledOption(menuKey)?.autoReload;
  2197. }
  2198. /**
  2199. * 根据键值获取callback函数
  2200. * @param menuKey 菜单-键key
  2201. */
  2202. getCallBack(menuKey) {
  2203. return this.MenuHandle.getMenuHandledOption(menuKey)?.callback;
  2204. }
  2205. /**
  2206. * 获取当enable为true时默认显示在菜单中前面的emoji图标
  2207. */
  2208. getEnableTrueEmoji() {
  2209. return this.MenuHandle.$emoji.success;
  2210. }
  2211. /**
  2212. * 获取当enable为false时默认显示在菜单中前面的emoji图标
  2213. */
  2214. getEnableFalseEmoji() {
  2215. return this.MenuHandle.$emoji.error;
  2216. }
  2217. /**
  2218. * 获取本地存储的菜单外部的键名
  2219. * @param keyName
  2220. */
  2221. getLocalStorageKeyName() {
  2222. return this.MenuHandle.$data.key;
  2223. }
  2224. /**
  2225. * 设置菜单的值
  2226. * @param menuKey 菜单-键key
  2227. * @param value 需要设置的值
  2228. */
  2229. setValue(menuKey, value) {
  2230. this.MenuHandle.setLocalMenuData(menuKey, value);
  2231. }
  2232. /**
  2233. * 设置菜单的值
  2234. * @param menuKey 菜单-键key
  2235. * @param value 需要设置的值
  2236. */
  2237. setEnable(menuKey, value) {
  2238. this.setValue(menuKey, Boolean(value));
  2239. }
  2240. /**
  2241. * 设置当enable为true时默认显示在菜单中前面的emoji图标
  2242. * @param emojiString
  2243. */
  2244. setEnableTrueEmoji(emojiString) {
  2245. if (typeof emojiString !== "string") {
  2246. throw new Error("参数emojiString必须是string类型");
  2247. }
  2248. this.MenuHandle.$emoji.success = emojiString;
  2249. }
  2250. /**
  2251. * 设置当enable为false时默认显示在菜单中前面的emoji图标
  2252. * @param emojiString
  2253. */
  2254. setEnableFalseEmoji(emojiString) {
  2255. if (typeof emojiString !== "string") {
  2256. throw new Error("参数emojiString必须是string类型");
  2257. }
  2258. this.MenuHandle.$emoji.error = emojiString;
  2259. }
  2260. /**
  2261. * 设置本地存储的菜单外部的键名
  2262. * @param keyName
  2263. */
  2264. setLocalStorageKeyName(keyName) {
  2265. if (typeof keyName !== "string") {
  2266. throw new Error("参数keyName必须是string类型");
  2267. }
  2268. this.MenuHandle.$data.key = keyName;
  2269. }
  2270. }
  2271.  
  2272. class Hooks {
  2273. /**
  2274. * 在Function原型上添加自定义方法.hook和.unhook
  2275. */
  2276. initEnv() {
  2277. Function.prototype.hook = function (realFunc, hookFunc, context) {
  2278. let _context = null; //函数上下文
  2279. let _funcName = null; //函数名
  2280. _context = context || window;
  2281. _funcName = getFuncName(this);
  2282. _context["realFunc_" + _funcName] = this;
  2283. if (_context[_funcName].prototype &&
  2284. _context[_funcName].prototype.isHooked) {
  2285. console.log("Already has been hooked,unhook first");
  2286. return false;
  2287. }
  2288. function getFuncName(fn) {
  2289. // 获取函数名
  2290. let strFunc = fn.toString();
  2291. let _regex = /function\s+(\w+)\s*\(/;
  2292. let patten = strFunc.match(_regex);
  2293. if (patten) {
  2294. return patten[1];
  2295. }
  2296. return "";
  2297. }
  2298. try {
  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);
  2305. _context[_funcName].prototype.isHooked = true;
  2306. return true;
  2307. }
  2308. catch (e) {
  2309. console.log("Hook failed,check the params.");
  2310. return false;
  2311. }
  2312. };
  2313. Function.prototype.unhook = function (realFunc, funcName, context) {
  2314. let _context = null;
  2315. let _funcName = null;
  2316. _context = context || window;
  2317. _funcName = funcName;
  2318. if (!_context[_funcName].prototype.isHooked) {
  2319. console.log("No function is hooked on");
  2320. return false;
  2321. }
  2322. _context[_funcName] = _context["realFunc" + _funcName];
  2323. Reflect.deleteProperty(_context, "realFunc_" + _funcName);
  2324. return true;
  2325. };
  2326. }
  2327. /**
  2328. * 删除在Function原型上添加的自定义方法.hook和.unhook
  2329. */
  2330. cleanEnv() {
  2331. if (Function.prototype.hasOwnProperty("hook")) {
  2332. Reflect.deleteProperty(Function.prototype, "hook");
  2333. }
  2334. if (Function.prototype.hasOwnProperty("unhook")) {
  2335. Reflect.deleteProperty(Function.prototype, "unhook");
  2336. }
  2337. return true;
  2338. }
  2339. }
  2340.  
  2341. /**
  2342. * 生成uuid
  2343. */
  2344. const GenerateUUID = function () {
  2345. if (typeof window?.crypto?.randomUUID === "function") {
  2346. return window.crypto.randomUUID();
  2347. }
  2348. else {
  2349. return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function (charStr) {
  2350. var randomValue = (Math.random() * 16) | 0, randomCharValue = charStr === "x" ? randomValue : (randomValue & 0x3) | 0x8;
  2351. return randomCharValue.toString(16);
  2352. });
  2353. }
  2354. };
  2355.  
  2356. class Httpx {
  2357. GM_Api = {
  2358. xmlHttpRequest: null,
  2359. };
  2360. HttpxRequestHook = {
  2361. /**
  2362. * @private
  2363. */
  2364. $config: {
  2365. configList: [],
  2366. },
  2367. /**
  2368. * 发送请求前的回调
  2369. * 如果返回false则阻止本次返回
  2370. * @param details 当前的请求配置
  2371. * @private
  2372. */
  2373. async beforeRequestCallBack(details) {
  2374. if (typeof details.allowInterceptConfig === "boolean") {
  2375. if (!details.allowInterceptConfig) {
  2376. // 不允许拦截
  2377. return details;
  2378. }
  2379. }
  2380. else {
  2381. if (details.allowInterceptConfig != null) {
  2382. // 配置存在
  2383. // 细分处理是否拦截
  2384. if (typeof details.allowInterceptConfig.beforeRequest === "boolean" &&
  2385. !details.allowInterceptConfig.beforeRequest) {
  2386. // 设置了禁止拦截
  2387. return details;
  2388. }
  2389. }
  2390. }
  2391. for (let index = 0; index < this.$config.configList.length; index++) {
  2392. let item = this.$config.configList[index];
  2393. if (typeof item.fn === "function") {
  2394. let result = await item.fn(details);
  2395. if (result == null) {
  2396. return;
  2397. }
  2398. }
  2399. }
  2400. return details;
  2401. },
  2402. /**
  2403. * 添加请求前的回调处理配置
  2404. */
  2405. add(fn) {
  2406. if (typeof fn === "function") {
  2407. let uuid = GenerateUUID();
  2408. this.$config.configList.push({
  2409. id: uuid,
  2410. fn: fn,
  2411. });
  2412. return uuid;
  2413. }
  2414. else {
  2415. console.warn("[Httpx-HttpxRequestHook.addBeforeRequestCallBack] fn is not a function");
  2416. }
  2417. },
  2418. /**
  2419. * 删除请求前的回调处理配置
  2420. * @param id
  2421. */
  2422. delete(id) {
  2423. if (typeof id === "string") {
  2424. let findIndex = this.$config.configList.findIndex((item) => item.id === id);
  2425. if (findIndex !== -1) {
  2426. this.$config.configList.splice(findIndex, 1);
  2427. return true;
  2428. }
  2429. }
  2430. return false;
  2431. },
  2432. /**
  2433. * 清空设置的请求前的回调处理配置
  2434. */
  2435. clearAll() {
  2436. this.$config.configList = [];
  2437. },
  2438. };
  2439. HttpxResponseHook = {
  2440. /**
  2441. * @private
  2442. */
  2443. $config: {
  2444. configList: [],
  2445. },
  2446. /**
  2447. * 成功的回调
  2448. * @param response 响应
  2449. * @param details 请求的配置
  2450. */
  2451. async successResponseCallBack(response, details) {
  2452. if (typeof details.allowInterceptConfig === "boolean") {
  2453. if (!details.allowInterceptConfig) {
  2454. // 不允许拦截
  2455. return details;
  2456. }
  2457. }
  2458. else {
  2459. if (details.allowInterceptConfig != null) {
  2460. // 配置存在
  2461. // 细分处理是否拦截
  2462. if (typeof details.allowInterceptConfig.afterResponseSuccess ===
  2463. "boolean" &&
  2464. !details.allowInterceptConfig.afterResponseSuccess) {
  2465. // 设置了禁止拦截
  2466. return details;
  2467. }
  2468. }
  2469. }
  2470. for (let index = 0; index < this.$config.configList.length; index++) {
  2471. let item = this.$config.configList[index];
  2472. if (typeof item.successFn === "function") {
  2473. let result = await item.successFn(response, details);
  2474. if (result == null) {
  2475. return;
  2476. }
  2477. }
  2478. }
  2479. return response;
  2480. },
  2481. /**
  2482. * 失败的回调
  2483. * @param data 配置
  2484. * @returns
  2485. * 返回null|undefined就是拦截掉了
  2486. */
  2487. async errorResponseCallBack(data) {
  2488. if (typeof data.details.allowInterceptConfig === "boolean") {
  2489. if (!data.details.allowInterceptConfig) {
  2490. // 不允许拦截
  2491. return data;
  2492. }
  2493. }
  2494. else {
  2495. if (data.details.allowInterceptConfig != null) {
  2496. // 配置存在
  2497. // 细分处理是否拦截
  2498. if (typeof data.details.allowInterceptConfig.afterResponseError ===
  2499. "boolean" &&
  2500. !data.details.allowInterceptConfig.afterResponseError) {
  2501. // 设置了禁止拦截
  2502. return data;
  2503. }
  2504. }
  2505. }
  2506. for (let index = 0; index < this.$config.configList.length; index++) {
  2507. let item = this.$config.configList[index];
  2508. if (typeof item.errorFn === "function") {
  2509. let result = await item.errorFn(data);
  2510. if (result == null) {
  2511. return;
  2512. }
  2513. }
  2514. }
  2515. return data;
  2516. },
  2517. /**
  2518. * 添加请求前的回调处理配置
  2519. */
  2520. add(successFn, errorFn) {
  2521. let id = GenerateUUID();
  2522. this.$config.configList.push({
  2523. id: id,
  2524. successFn: successFn,
  2525. errorFn: errorFn,
  2526. });
  2527. return id;
  2528. },
  2529. /**
  2530. * 删除请求前的回调处理配置
  2531. * @param id
  2532. */
  2533. delete(id) {
  2534. if (typeof id === "string") {
  2535. let findIndex = this.$config.configList.findIndex((item) => item.id === id);
  2536. if (findIndex !== -1) {
  2537. this.$config.configList.splice(findIndex, 1);
  2538. return true;
  2539. }
  2540. }
  2541. return false;
  2542. },
  2543. /**
  2544. * 清空设置的请求前的回调处理配置
  2545. */
  2546. clearAll() {
  2547. this.$config.configList = [];
  2548. },
  2549. };
  2550. HttpxRequestOption = {
  2551. context: this,
  2552. /**
  2553. * 对请求的参数进行合并处理
  2554. */
  2555. handleBeforeRequestOptionArgs(...args) {
  2556. let option = {};
  2557. if (typeof args[0] === "string") {
  2558. /* 传入的是url,转为配置 */
  2559. let url = args[0];
  2560. option.url = url;
  2561. if (typeof args[1] === "object") {
  2562. /* 处理第二个参数details */
  2563. let optionArg = args[1];
  2564. commonUtil.assign(option, optionArg, true);
  2565. option.url = url;
  2566. }
  2567. }
  2568. else {
  2569. /* 传入的是配置 */
  2570. let optionArg = args[0];
  2571. commonUtil.assign(option, optionArg, true);
  2572. }
  2573. return option;
  2574. },
  2575. /**
  2576. * 获取请求配置
  2577. * @param method 当前请求方法,默认get
  2578. * @param userRequestOption 用户的请求配置
  2579. * @param resolve promise回调
  2580. * @param reject 抛出错误回调
  2581. */
  2582. getRequestOption(method, userRequestOption, resolve, reject) {
  2583. let that = this;
  2584. let url = userRequestOption.url || this.context.#defaultRequestOption.url;
  2585. if (typeof url === "string") {
  2586. // 去除左右空格
  2587. url = url.trim();
  2588. if (url.startsWith("http://") || url.startsWith("https://")) ;
  2589. else {
  2590. if (typeof this.context.#defaultInitOption.baseURL === "string") {
  2591. // 设置了基础域
  2592. url = this.context.#defaultInitOption.baseURL + url;
  2593. }
  2594. }
  2595. }
  2596. let requestOption = {
  2597. url: url,
  2598. method: (method || "GET").toString().toUpperCase().trim(),
  2599. timeout: userRequestOption.timeout ||
  2600. this.context.#defaultRequestOption.timeout,
  2601. responseType: userRequestOption.responseType ||
  2602. this.context.#defaultRequestOption.responseType,
  2603. /* 对象使用深拷贝 */
  2604. headers: commonUtil.deepClone(this.context.#defaultRequestOption.headers),
  2605. data: userRequestOption.data || this.context.#defaultRequestOption.data,
  2606. redirect: userRequestOption.redirect ||
  2607. this.context.#defaultRequestOption.redirect,
  2608. cookie: userRequestOption.cookie || this.context.#defaultRequestOption.cookie,
  2609. cookiePartition: userRequestOption.cookiePartition ||
  2610. this.context.#defaultRequestOption.cookiePartition,
  2611. binary: userRequestOption.binary || this.context.#defaultRequestOption.binary,
  2612. nocache: userRequestOption.nocache ||
  2613. this.context.#defaultRequestOption.nocache,
  2614. revalidate: userRequestOption.revalidate ||
  2615. this.context.#defaultRequestOption.revalidate,
  2616. /* 对象使用深拷贝 */
  2617. context: commonUtil.deepClone(userRequestOption.context ||
  2618. this.context.#defaultRequestOption.context),
  2619. overrideMimeType: userRequestOption.overrideMimeType ||
  2620. this.context.#defaultRequestOption.overrideMimeType,
  2621. anonymous: userRequestOption.anonymous ||
  2622. this.context.#defaultRequestOption.anonymous,
  2623. fetch: userRequestOption.fetch || this.context.#defaultRequestOption.fetch,
  2624. /* 对象使用深拷贝 */
  2625. fetchInit: commonUtil.deepClone(this.context.#defaultRequestOption.fetchInit),
  2626. allowInterceptConfig: {
  2627. beforeRequest: this.context.#defaultRequestOption
  2628. .allowInterceptConfig.beforeRequest,
  2629. afterResponseSuccess: this.context.#defaultRequestOption
  2630. .allowInterceptConfig.afterResponseSuccess,
  2631. afterResponseError: this.context.#defaultRequestOption
  2632. .allowInterceptConfig.afterResponseError,
  2633. },
  2634. user: userRequestOption.user || this.context.#defaultRequestOption.user,
  2635. password: userRequestOption.password ||
  2636. this.context.#defaultRequestOption.password,
  2637. onabort(...args) {
  2638. that.context.HttpxCallBack.onAbort(userRequestOption, resolve, reject, args);
  2639. },
  2640. onerror(...args) {
  2641. that.context.HttpxCallBack.onError(userRequestOption, resolve, reject, args);
  2642. },
  2643. onloadstart(...args) {
  2644. that.context.HttpxCallBack.onLoadStart(userRequestOption, args);
  2645. },
  2646. onprogress(...args) {
  2647. that.context.HttpxCallBack.onProgress(userRequestOption, args);
  2648. },
  2649. onreadystatechange(...args) {
  2650. that.context.HttpxCallBack.onReadyStateChange(userRequestOption, args);
  2651. },
  2652. ontimeout(...args) {
  2653. that.context.HttpxCallBack.onTimeout(userRequestOption, resolve, reject, args);
  2654. },
  2655. onload(...args) {
  2656. that.context.HttpxCallBack.onLoad(userRequestOption, resolve, reject, args);
  2657. },
  2658. };
  2659. // 补全allowInterceptConfig参数
  2660. if (typeof userRequestOption.allowInterceptConfig === "boolean") {
  2661. Object.keys(requestOption.allowInterceptConfig).forEach((keyName) => {
  2662. Reflect.set(requestOption.allowInterceptConfig, keyName, userRequestOption.allowInterceptConfig);
  2663. });
  2664. }
  2665. else {
  2666. if (typeof userRequestOption.allowInterceptConfig === "object" &&
  2667. userRequestOption.allowInterceptConfig != null) {
  2668. Object.keys(userRequestOption.allowInterceptConfig).forEach((keyName) => {
  2669. let value = Reflect.get(userRequestOption.allowInterceptConfig, keyName);
  2670. if (typeof value === "boolean" &&
  2671. Reflect.has(requestOption.allowInterceptConfig, keyName)) {
  2672. Reflect.set(requestOption.allowInterceptConfig, keyName, value);
  2673. }
  2674. });
  2675. }
  2676. }
  2677. if (typeof this.context.GM_Api.xmlHttpRequest !== "function") {
  2678. // GM函数不存在,强制使用fetch
  2679. requestOption.fetch = true;
  2680. }
  2681. if (typeof requestOption.headers === "object") {
  2682. if (typeof userRequestOption.headers === "object") {
  2683. Object.keys(userRequestOption.headers).forEach((keyName, index) => {
  2684. if (keyName in requestOption.headers &&
  2685. userRequestOption.headers?.[keyName] == null) {
  2686. /* 在默认的header中存在,且设置它新的值为空,那么就是默认的值 */
  2687. Reflect.deleteProperty(requestOption.headers, keyName);
  2688. }
  2689. else {
  2690. requestOption.headers[keyName] =
  2691. userRequestOption?.headers?.[keyName];
  2692. }
  2693. });
  2694. }
  2695. }
  2696. else {
  2697. /* 默认的headers不是对象,那么就直接使用新的 */
  2698. Reflect.set(requestOption, "headers", userRequestOption.headers);
  2699. }
  2700. if (typeof requestOption.fetchInit === "object") {
  2701. /* 使用assign替换且添加 */
  2702. if (typeof userRequestOption.fetchInit === "object") {
  2703. Object.keys(userRequestOption.fetchInit).forEach((keyName, index) => {
  2704. if (keyName in requestOption.fetchInit &&
  2705. userRequestOption.fetchInit[keyName] == null) {
  2706. /* 在默认的fetchInit中存在,且设置它新的值为空,那么就是默认的值 */
  2707. Reflect.deleteProperty(requestOption.fetchInit, keyName);
  2708. }
  2709. else {
  2710. Reflect.set(requestOption.fetchInit, keyName, Reflect.get(userRequestOption.fetchInit, keyName));
  2711. }
  2712. });
  2713. }
  2714. }
  2715. else {
  2716. Reflect.set(requestOption, "fetchInit", userRequestOption.fetchInit);
  2717. }
  2718. // 处理新的cookiePartition
  2719. if (typeof requestOption.cookiePartition === "object" &&
  2720. requestOption.cookiePartition != null) {
  2721. if (Reflect.has(requestOption.cookiePartition, "topLevelSite") &&
  2722. typeof requestOption.cookiePartition.topLevelSite !== "string") {
  2723. // topLevelSite必须是字符串
  2724. Reflect.deleteProperty(requestOption.cookiePartition, "topLevelSite");
  2725. }
  2726. }
  2727. /* 完善请求的url */
  2728. try {
  2729. new URL(requestOption.url);
  2730. }
  2731. catch (error) {
  2732. if (requestOption.url.startsWith("//")) {
  2733. // 补充https:
  2734. requestOption.url = globalThis.location.protocol + requestOption.url;
  2735. }
  2736. else if (requestOption.url.startsWith("/")) {
  2737. // 补充origin
  2738. requestOption.url = globalThis.location.origin + requestOption.url;
  2739. }
  2740. else {
  2741. // 补充origin+/
  2742. requestOption.url =
  2743. globalThis.location.origin + "/" + requestOption.url;
  2744. }
  2745. }
  2746. if (requestOption.fetchInit && !requestOption.fetch) {
  2747. // 清空fetchInit
  2748. Reflect.deleteProperty(requestOption, "fetchInit");
  2749. }
  2750. // 转换data类型
  2751. try {
  2752. /** 是否对数据进行处理 */
  2753. let processData = userRequestOption.processData ?? true;
  2754. if (requestOption.data != null && processData) {
  2755. let method = requestOption.method;
  2756. if (method === "GET" || method === "HEAD") {
  2757. // GET类型,data如果有,那么需要转为searchParams
  2758. let urlObj = new URL(requestOption.url);
  2759. let urlSearch = "";
  2760. let isHandler = false;
  2761. if (typeof requestOption.data === "string") {
  2762. isHandler = true;
  2763. urlSearch = requestOption.data;
  2764. }
  2765. else if (typeof requestOption.data === "object") {
  2766. isHandler = true;
  2767. // URLSearchParams参数可以转普通的string:string,包括FormData
  2768. // @ts-ignore
  2769. let searchParams = new URLSearchParams(requestOption.data);
  2770. urlSearch = searchParams.toString();
  2771. }
  2772. if (isHandler) {
  2773. // GET/HEAD请求不支持data参数
  2774. // 对data进行处理了才可以删除
  2775. Reflect.deleteProperty(requestOption, "data");
  2776. }
  2777. if (urlSearch != "") {
  2778. if (urlObj.search === "") {
  2779. // url没有search参数,直接覆盖
  2780. urlObj.search = urlSearch;
  2781. }
  2782. else {
  2783. // 有search参数
  2784. if (urlObj.search.endsWith("&")) {
  2785. // xxx=xxx&
  2786. urlObj.search = urlObj.search + urlSearch;
  2787. }
  2788. else {
  2789. // xxx=xxx&xxx=
  2790. urlObj.search = urlObj.search + "&" + urlSearch;
  2791. }
  2792. }
  2793. }
  2794. requestOption.url = urlObj.toString();
  2795. }
  2796. else if (method === "POST" && requestOption.headers != null) {
  2797. // POST类型,data如果是FormData,那么需要转为string
  2798. let headersKeyList = Object.keys(requestOption.headers);
  2799. let ContentTypeIndex = headersKeyList.findIndex((headerKey) => {
  2800. return (headerKey.trim().toLowerCase() === "content-type" &&
  2801. typeof requestOption.headers[headerKey] === "string");
  2802. });
  2803. if (ContentTypeIndex !== -1) {
  2804. let ContentTypeKey = headersKeyList[ContentTypeIndex];
  2805. let ContentType = requestOption.headers[ContentTypeKey];
  2806. // 设置了Content-Type
  2807. if (ContentType.includes("application/json")) {
  2808. // application/json
  2809. if (requestOption.data instanceof FormData) {
  2810. const entries = {};
  2811. requestOption.data.forEach((value, key) => {
  2812. entries[key] = value;
  2813. });
  2814. requestOption.data = JSON.stringify(entries);
  2815. }
  2816. else if (typeof requestOption.data === "object") {
  2817. requestOption.data = JSON.stringify(requestOption.data);
  2818. }
  2819. }
  2820. else if (ContentType.includes("application/x-www-form-urlencoded")) {
  2821. // application/x-www-form-urlencoded
  2822. if (typeof requestOption.data === "object") {
  2823. requestOption.data = new URLSearchParams(
  2824. // @ts-ignore
  2825. requestOption.data).toString();
  2826. }
  2827. }
  2828. else if (ContentType.includes("multipart/form-data")) {
  2829. // multipart/form-data
  2830. if (requestOption.data instanceof FormData) {
  2831. Reflect.deleteProperty(requestOption.headers, ContentTypeKey);
  2832. }
  2833. }
  2834. }
  2835. }
  2836. }
  2837. }
  2838. catch (error) {
  2839. console.warn("Httpx ==> 转换data参数错误", error);
  2840. }
  2841. return requestOption;
  2842. },
  2843. /**
  2844. * 处理发送请求的配置,去除值为undefined、空function的值
  2845. * @param option
  2846. */
  2847. removeRequestNullOption(option) {
  2848. Object.keys(option).forEach((keyName) => {
  2849. if (option[keyName] == null ||
  2850. (option[keyName] instanceof Function &&
  2851. commonUtil.isNull(option[keyName]))) {
  2852. Reflect.deleteProperty(option, keyName);
  2853. return;
  2854. }
  2855. });
  2856. if (commonUtil.isNull(option.url)) {
  2857. throw new TypeError(`Utils.Httpx 参数 url不符合要求: ${option.url}`);
  2858. }
  2859. return option;
  2860. },
  2861. /**
  2862. * 处理fetch的配置
  2863. * @param option
  2864. */
  2865. handleFetchOption(option) {
  2866. /**
  2867. * fetch的请求配置
  2868. **/
  2869. let fetchRequestOption = {};
  2870. if ((option.method === "GET" || option.method === "HEAD") &&
  2871. option.data != null) {
  2872. /* GET 或 HEAD 方法的请求不能包含 body 信息 */
  2873. Reflect.deleteProperty(option, "data");
  2874. }
  2875. /* 中止信号控制器 */
  2876. let abortController = new AbortController();
  2877. let signal = abortController.signal;
  2878. signal.onabort = () => {
  2879. option.onabort({
  2880. isFetch: true,
  2881. responseText: "",
  2882. response: null,
  2883. readyState: 4,
  2884. responseHeaders: "",
  2885. status: 0,
  2886. statusText: "",
  2887. error: "aborted",
  2888. });
  2889. };
  2890. // 设置请求
  2891. fetchRequestOption.method = option.method ?? "GET";
  2892. // 设置请求头
  2893. fetchRequestOption.headers = option.headers;
  2894. // 设置请求体
  2895. fetchRequestOption.body = option.data;
  2896. // 设置跨域
  2897. fetchRequestOption.mode = "cors";
  2898. // 设置包含
  2899. fetchRequestOption.credentials = "include";
  2900. // 设置不缓存
  2901. fetchRequestOption.cache = "no-cache";
  2902. // 设置始终重定向
  2903. fetchRequestOption.redirect = "follow";
  2904. // 设置referer跨域
  2905. fetchRequestOption.referrerPolicy = "origin-when-cross-origin";
  2906. // 设置信号中断
  2907. fetchRequestOption.signal = signal;
  2908. Object.assign(fetchRequestOption, option.fetchInit || {});
  2909. return {
  2910. fetchOption: option,
  2911. fetchRequestOption: fetchRequestOption,
  2912. abortController: abortController,
  2913. };
  2914. },
  2915. };
  2916. HttpxCallBack = {
  2917. context: this,
  2918. /**
  2919. * onabort请求被取消-触发
  2920. * @param details 配置
  2921. * @param resolve 回调
  2922. * @param reject 抛出错误
  2923. * @param argsResult 返回的参数列表
  2924. */
  2925. async onAbort(details, resolve, reject, argsResult) {
  2926. // console.log(argsResult);
  2927. if ("onabort" in details) {
  2928. details.onabort.apply(this, argsResult);
  2929. }
  2930. else if ("onabort" in this.context.#defaultRequestOption) {
  2931. this.context.#defaultRequestOption.onabort.apply(this, argsResult);
  2932. }
  2933. let response = argsResult;
  2934. if (response.length) {
  2935. response = response[0];
  2936. }
  2937. if ((await this.context.HttpxResponseHook.errorResponseCallBack({
  2938. type: "onabort",
  2939. error: new TypeError("request canceled"),
  2940. response: null,
  2941. details: details,
  2942. })) == null) {
  2943. // reject(new TypeError("response is intercept with onabort"));
  2944. return;
  2945. }
  2946. resolve({
  2947. data: response,
  2948. details: details,
  2949. msg: "请求被取消",
  2950. status: false,
  2951. statusCode: -1,
  2952. type: "onabort",
  2953. });
  2954. },
  2955. /**
  2956. * onerror请求异常-触发
  2957. * @param details 配置
  2958. * @param resolve 回调
  2959. * @param reject 抛出错误
  2960. * @param argsResult 返回的参数列表
  2961. */
  2962. async onError(details, resolve, reject, argsResult) {
  2963. // console.log(argsResult);
  2964. if ("onerror" in details) {
  2965. details.onerror.apply(this, argsResult);
  2966. }
  2967. else if ("onerror" in this.context.#defaultRequestOption) {
  2968. this.context.#defaultRequestOption.onerror.apply(this, argsResult);
  2969. }
  2970. let response = argsResult;
  2971. if (response.length) {
  2972. response = response[0];
  2973. }
  2974. if ((await this.context.HttpxResponseHook.errorResponseCallBack({
  2975. type: "onerror",
  2976. error: new TypeError("request error"),
  2977. response: response,
  2978. details: details,
  2979. })) == null) {
  2980. // reject(new TypeError("response is intercept with onerror"));
  2981. return;
  2982. }
  2983. resolve({
  2984. data: response,
  2985. details: details,
  2986. msg: "请求异常",
  2987. status: false,
  2988. statusCode: response["status"],
  2989. type: "onerror",
  2990. });
  2991. },
  2992. /**
  2993. * ontimeout请求超时-触发
  2994. * @param details 配置
  2995. * @param resolve 回调
  2996. * @param reject 抛出错误
  2997. * @param argsResult 返回的参数列表
  2998. */
  2999. async onTimeout(details, resolve, reject, argsResult) {
  3000. // console.log(argsResult);
  3001. if ("ontimeout" in details) {
  3002. details.ontimeout.apply(this, argsResult);
  3003. }
  3004. else if ("ontimeout" in this.context.#defaultRequestOption) {
  3005. this.context.#defaultRequestOption.ontimeout.apply(this, argsResult);
  3006. }
  3007. let response = argsResult;
  3008. if (response.length) {
  3009. response = response[0];
  3010. }
  3011. if ((await this.context.HttpxResponseHook.errorResponseCallBack({
  3012. type: "ontimeout",
  3013. error: new TypeError("request timeout"),
  3014. response: (argsResult || [null])[0],
  3015. details: details,
  3016. })) == null) {
  3017. // reject(new TypeError("response is intercept with ontimeout"));
  3018. return;
  3019. }
  3020. resolve({
  3021. data: response,
  3022. details: details,
  3023. msg: "请求超时",
  3024. status: false,
  3025. statusCode: 0,
  3026. type: "ontimeout",
  3027. });
  3028. },
  3029. /**
  3030. * onloadstart请求开始-触发
  3031. * @param details 配置
  3032. * @param argsResult 返回的参数列表
  3033. */
  3034. onLoadStart(details, argsResult) {
  3035. // console.log(argsResult);
  3036. if ("onloadstart" in details) {
  3037. details.onloadstart.apply(this, argsResult);
  3038. }
  3039. else if ("onloadstart" in this.context.#defaultRequestOption) {
  3040. this.context.#defaultRequestOption.onloadstart.apply(this, argsResult);
  3041. }
  3042. },
  3043. /**
  3044. * onload加载完毕-触发
  3045. * @param details 请求的配置
  3046. * @param resolve 回调
  3047. * @param reject 抛出错误
  3048. * @param argsResult 返回的参数列表
  3049. */
  3050. async onLoad(details, resolve, reject, argsResult) {
  3051. // console.log(argsResult);
  3052. /* X浏览器会因为设置了responseType导致不返回responseText */
  3053. let originResponse = argsResult[0];
  3054. /* responseText为空,response不为空的情况 */
  3055. if (commonUtil.isNull(originResponse["responseText"]) &&
  3056. commonUtil.isNotNull(originResponse["response"])) {
  3057. if (typeof originResponse["response"] === "object") {
  3058. TryCatch().run(() => {
  3059. originResponse["responseText"] = JSON.stringify(originResponse["response"]);
  3060. });
  3061. }
  3062. else {
  3063. originResponse["responseText"] = originResponse["response"];
  3064. }
  3065. }
  3066. /* response为空,responseText不为空的情况 */
  3067. if (originResponse["response"] == null &&
  3068. typeof originResponse["responseText"] === "string" &&
  3069. originResponse["responseText"].trim() !== "") {
  3070. /** 原始的请求text */
  3071. let httpxResponseText = originResponse.responseText;
  3072. // 自定义个新的response
  3073. let httpxResponse = httpxResponseText;
  3074. if (details.responseType === "json") {
  3075. httpxResponse = commonUtil.toJSON(httpxResponseText);
  3076. }
  3077. else if (details.responseType === "document") {
  3078. let parser = new DOMParser();
  3079. httpxResponse = parser.parseFromString(httpxResponseText, "text/html");
  3080. }
  3081. else if (details.responseType === "arraybuffer") {
  3082. let encoder = new TextEncoder();
  3083. let arrayBuffer = encoder.encode(httpxResponseText);
  3084. httpxResponse = arrayBuffer;
  3085. }
  3086. else if (details.responseType === "blob") {
  3087. let encoder = new TextEncoder();
  3088. let arrayBuffer = encoder.encode(httpxResponseText);
  3089. httpxResponse = new Blob([arrayBuffer]);
  3090. }
  3091. // 尝试覆盖原response
  3092. try {
  3093. let setStatus = Reflect.set(originResponse, "response", httpxResponse);
  3094. if (!setStatus) {
  3095. console.warn("[Httpx-HttpxCallBack.oonLoad] 覆盖原始 response 失败,尝试添加新的httpxResponse");
  3096. try {
  3097. Reflect.set(originResponse, "httpxResponse", httpxResponse);
  3098. }
  3099. catch (error) {
  3100. console.warn("[Httpx-HttpxCallBack.oonLoad] httpxResponse 无法被覆盖");
  3101. }
  3102. }
  3103. }
  3104. catch (error) {
  3105. console.warn("[Httpx-HttpxCallBack.oonLoad] 原始 response 无法被覆盖,尝试添加新的httpxResponse");
  3106. try {
  3107. Reflect.set(originResponse, "httpxResponse", httpxResponse);
  3108. }
  3109. catch (error) {
  3110. console.warn("[Httpx-HttpxCallBack.oonLoad] httpxResponse 无法被覆盖");
  3111. }
  3112. }
  3113. }
  3114. /* Stay扩展中没有finalUrl,对应的是responseURL */
  3115. let originResponseURL = Reflect.get(originResponse, "responseURL");
  3116. if (originResponse["finalUrl"] == null && originResponseURL != null) {
  3117. Reflect.set(originResponse, "finalUrl", originResponseURL);
  3118. }
  3119. /* 状态码2xx都是成功的 */
  3120. if (Math.floor(originResponse.status / 100) === 2) {
  3121. if ((await this.context.HttpxResponseHook.successResponseCallBack(originResponse, details)) == null) {
  3122. // reject(new TypeError("response is intercept with onloada"));
  3123. return;
  3124. }
  3125. resolve({
  3126. data: originResponse,
  3127. details: details,
  3128. msg: "请求成功",
  3129. status: true,
  3130. statusCode: originResponse.status,
  3131. type: "onload",
  3132. });
  3133. }
  3134. else {
  3135. this.context.HttpxCallBack.onError(details, resolve, reject, argsResult);
  3136. }
  3137. },
  3138. /**
  3139. * onprogress上传进度-触发
  3140. * @param details 配置
  3141. * @param argsResult 返回的参数列表
  3142. */
  3143. onProgress(details, argsResult) {
  3144. // console.log(argsResult);
  3145. if ("onprogress" in details) {
  3146. details.onprogress.apply(this, argsResult);
  3147. }
  3148. else if ("onprogress" in this.context.#defaultRequestOption) {
  3149. this.context.#defaultRequestOption.onprogress.apply(this, argsResult);
  3150. }
  3151. },
  3152. /**
  3153. * onreadystatechange准备状态改变-触发
  3154. * @param details 配置
  3155. * @param argsResult 返回的参数列表
  3156. */
  3157. onReadyStateChange(details, argsResult) {
  3158. // console.log(argsResult);
  3159. if ("onreadystatechange" in details) {
  3160. details.onreadystatechange.apply(this, argsResult);
  3161. }
  3162. else if ("onreadystatechange" in this.context.#defaultRequestOption) {
  3163. this.context.#defaultRequestOption.onreadystatechange.apply(this, argsResult);
  3164. }
  3165. },
  3166. };
  3167. HttpxRequest = {
  3168. context: this,
  3169. /**
  3170. * 发送请求
  3171. * @param details
  3172. */
  3173. async request(details) {
  3174. if (this.context.#defaultInitOption.logDetails) {
  3175. console.log("[Httpx-HttpxRequest.request] 请求前的配置👇", details);
  3176. }
  3177. if (typeof this.context.HttpxRequestHook.beforeRequestCallBack ===
  3178. "function") {
  3179. let hookResult = await this.context.HttpxRequestHook.beforeRequestCallBack(details);
  3180. if (hookResult == null) {
  3181. return;
  3182. }
  3183. }
  3184. if (details.fetch) {
  3185. // 使用fetch请求
  3186. const { fetchOption: fetchOption, fetchRequestOption: fetchRequestOption, abortController, } = this.context.HttpxRequestOption.handleFetchOption(details);
  3187. return this.fetch(fetchOption, fetchRequestOption, abortController);
  3188. }
  3189. else {
  3190. // 使用GM_xmlHttpRequest请求
  3191. return this.xmlHttpRequest(details);
  3192. }
  3193. },
  3194. /**
  3195. * 使用油猴函数GM_xmlhttpRequest发送请求
  3196. * @param details
  3197. */
  3198. xmlHttpRequest(details) {
  3199. return this.context.GM_Api.xmlHttpRequest(details);
  3200. },
  3201. /**
  3202. * 使用fetch发送请求
  3203. * @param option
  3204. * @param fetchRequestOption
  3205. * @param abortController
  3206. */
  3207. fetch(option, fetchRequestOption, abortController) {
  3208. fetch(option.url, fetchRequestOption)
  3209. .then(async (fetchResponse) => {
  3210. /** 自定义的response */
  3211. let httpxResponse = {
  3212. isFetch: true,
  3213. finalUrl: fetchResponse.url,
  3214. readyState: 4,
  3215. // @ts-ignore
  3216. status: fetchResponse.status,
  3217. statusText: fetchResponse.statusText,
  3218. // @ts-ignore
  3219. response: undefined,
  3220. responseFetchHeaders: fetchResponse.headers,
  3221. responseHeaders: "",
  3222. // @ts-ignore
  3223. responseText: undefined,
  3224. responseType: option.responseType,
  3225. responseXML: undefined,
  3226. };
  3227. Object.assign(httpxResponse, option.context || {});
  3228. // 把headers转为字符串
  3229. for (const [key, value] of fetchResponse.headers.entries()) {
  3230. httpxResponse.responseHeaders += `${key}: ${value}\n`;
  3231. }
  3232. /** 请求返回的类型 */
  3233. const fetchResponseType = fetchResponse.headers.get("Content-Type");
  3234. /* 如果需要stream,且获取到的是stream,那直接返回 */
  3235. if (option.responseType === "stream" ||
  3236. (fetchResponse.headers.has("Content-Type") &&
  3237. fetchResponse.headers
  3238. .get("Content-Type")
  3239. .includes("text/event-stream"))) {
  3240. Reflect.set(httpxResponse, "isStream", true);
  3241. Reflect.set(httpxResponse, "response", fetchResponse.body);
  3242. Reflect.deleteProperty(httpxResponse, "responseText");
  3243. Reflect.deleteProperty(httpxResponse, "responseXML");
  3244. option.onload(httpxResponse);
  3245. return;
  3246. }
  3247. /** 响应 */
  3248. let response = "";
  3249. /** 响应字符串 */
  3250. let responseText = "";
  3251. /** 响应xml文档 */
  3252. let responseXML = "";
  3253. /** 先获取二进制数据 */
  3254. let arrayBuffer = await fetchResponse.arrayBuffer();
  3255. /** 数据编码 */
  3256. let encoding = "utf-8";
  3257. if (fetchResponse.headers.has("Content-Type")) {
  3258. let charsetMatched = fetchResponse.headers
  3259. .get("Content-Type")
  3260. ?.match(/charset=(.+)/);
  3261. if (charsetMatched) {
  3262. encoding = charsetMatched[1];
  3263. encoding = encoding.toLowerCase();
  3264. }
  3265. }
  3266. // Failed to construct 'TextDecoder': The encoding label provided ('"UTF-8"') is invalid.
  3267. // 去除引号
  3268. encoding = encoding.replace(/('|")/gi, "");
  3269. // 编码
  3270. let textDecoder = new TextDecoder(encoding);
  3271. responseText = textDecoder.decode(arrayBuffer);
  3272. response = responseText;
  3273. if (option.responseType === "arraybuffer") {
  3274. // response返回格式是二进制流
  3275. response = arrayBuffer;
  3276. }
  3277. else if (option.responseType === "blob") {
  3278. // response返回格式是blob
  3279. response = new Blob([arrayBuffer]);
  3280. }
  3281. else if (option.responseType === "json" ||
  3282. (typeof fetchResponseType === "string" &&
  3283. fetchResponseType.includes("application/json"))) {
  3284. // response返回格式是JSON格式
  3285. response = commonUtil.toJSON(responseText);
  3286. }
  3287. else if (option.responseType === "document" ||
  3288. option.responseType == null) {
  3289. // response返回格式是文档格式
  3290. let parser = new DOMParser();
  3291. response = parser.parseFromString(responseText, "text/html");
  3292. }
  3293. // 转为XML结构
  3294. let parser = new DOMParser();
  3295. responseXML = parser.parseFromString(responseText, "text/xml");
  3296. Reflect.set(httpxResponse, "response", response);
  3297. Reflect.set(httpxResponse, "responseText", responseText);
  3298. Reflect.set(httpxResponse, "responseXML", responseXML);
  3299. // 执行回调
  3300. option.onload(httpxResponse);
  3301. })
  3302. .catch((error) => {
  3303. if (error.name === "AbortError") {
  3304. return;
  3305. }
  3306. option.onerror({
  3307. isFetch: true,
  3308. finalUrl: option.url,
  3309. readyState: 4,
  3310. status: 0,
  3311. statusText: "",
  3312. responseHeaders: "",
  3313. responseText: "",
  3314. error: error,
  3315. });
  3316. });
  3317. option.onloadstart({
  3318. isFetch: true,
  3319. finalUrl: option.url,
  3320. readyState: 1,
  3321. responseHeaders: "",
  3322. responseText: "",
  3323. status: 0,
  3324. statusText: "",
  3325. });
  3326. return {
  3327. abort() {
  3328. abortController.abort();
  3329. },
  3330. };
  3331. },
  3332. };
  3333. /**
  3334. * 默认配置
  3335. */
  3336. #defaultRequestOption = {
  3337. url: undefined,
  3338. timeout: 5000,
  3339. async: false,
  3340. responseType: undefined,
  3341. headers: undefined,
  3342. data: undefined,
  3343. redirect: undefined,
  3344. cookie: undefined,
  3345. cookiePartition: undefined,
  3346. binary: undefined,
  3347. nocache: undefined,
  3348. revalidate: undefined,
  3349. context: undefined,
  3350. overrideMimeType: undefined,
  3351. anonymous: undefined,
  3352. fetch: undefined,
  3353. fetchInit: undefined,
  3354. allowInterceptConfig: {
  3355. beforeRequest: true,
  3356. afterResponseSuccess: true,
  3357. afterResponseError: true,
  3358. },
  3359. user: undefined,
  3360. password: undefined,
  3361. onabort() { },
  3362. onerror() { },
  3363. ontimeout() { },
  3364. onloadstart() { },
  3365. onreadystatechange() { },
  3366. onprogress() { },
  3367. };
  3368. #defaultInitOption = {
  3369. /**
  3370. * `baseURL` 将自动加在 `url` 前面,除非 `url` 是一个绝对 URL。
  3371. */
  3372. baseURL: undefined,
  3373. /**
  3374. * 当前使用请求时,输出请求的配置,一般用于DEBUG|DEV
  3375. */
  3376. logDetails: false,
  3377. };
  3378. /**
  3379. * 实例化
  3380. * @param option 初始化配置
  3381. */
  3382. constructor(option = {}) {
  3383. if (typeof option.xmlHttpRequest !== "function") {
  3384. console.warn("[Httpx-constructor] 未传入GM_xmlhttpRequest函数或传入的GM_xmlhttpRequest不是Function,将默认使用window.fetch");
  3385. }
  3386. commonUtil.coverObjectFunctionThis(this);
  3387. this.interceptors.request.context = this;
  3388. this.interceptors.response.context = this;
  3389. this.config(option);
  3390. }
  3391. /**
  3392. * 覆盖当前配置
  3393. * @param option
  3394. */
  3395. config(option = {}) {
  3396. if (typeof option.xmlHttpRequest === "function") {
  3397. this.GM_Api.xmlHttpRequest = option.xmlHttpRequest;
  3398. }
  3399. this.#defaultRequestOption = commonUtil.assign(this.#defaultRequestOption, option);
  3400. this.#defaultInitOption = commonUtil.assign(this.#defaultInitOption, option);
  3401. }
  3402. /**
  3403. * 拦截器
  3404. */
  3405. interceptors = {
  3406. /**
  3407. * 请求拦截器
  3408. */
  3409. request: {
  3410. context: null,
  3411. /**
  3412. * 添加拦截器
  3413. * @param fn 设置的请求前回调函数,如果返回配置,则使用返回的配置,如果返回null|undefined,则阻止请求
  3414. */
  3415. use(fn) {
  3416. if (typeof fn !== "function") {
  3417. console.warn("[Httpx-interceptors-request] 请传入拦截器函数");
  3418. return;
  3419. }
  3420. return this.context.HttpxRequestHook.add(fn);
  3421. },
  3422. /**
  3423. * 移除拦截器
  3424. * @param id 通过use返回的id
  3425. */
  3426. eject(id) {
  3427. return this.context.HttpxRequestHook.delete(id);
  3428. },
  3429. /**
  3430. * 移除所有拦截器
  3431. */
  3432. ejectAll() {
  3433. this.context.HttpxRequestHook.clearAll();
  3434. },
  3435. },
  3436. /**
  3437. * 响应拦截器
  3438. */
  3439. response: {
  3440. context: null,
  3441. /**
  3442. * 添加拦截器
  3443. * @param successFn 设置的响应后回调函数,如果返回响应,则使用返回的响应,如果返回null|undefined,则阻止响应
  3444. * + 2xx 范围内的状态码都会触发该函数
  3445. * @param errorFn 设置的响应后回调函数,如果返回响应,则使用返回的响应,如果返回null|undefined,则阻止响应
  3446. * + 超出 2xx 范围的状态码都会触发该函数
  3447. */
  3448. use(successFn, errorFn) {
  3449. if (typeof successFn !== "function" && typeof errorFn !== "function") {
  3450. console.warn("[Httpx-interceptors-response] 必须传入一个拦截器函数");
  3451. return;
  3452. }
  3453. return this.context.HttpxResponseHook.add(successFn, errorFn);
  3454. },
  3455. /**
  3456. * 移除拦截器
  3457. * @param id 通过use返回的id
  3458. */
  3459. eject(id) {
  3460. return this.context.HttpxResponseHook.delete(id);
  3461. },
  3462. /**
  3463. * 移除所有拦截器
  3464. */
  3465. ejectAll() {
  3466. this.context.HttpxResponseHook.clearAll();
  3467. },
  3468. },
  3469. };
  3470. /**
  3471. * 修改xmlHttpRequest
  3472. * @param httpRequest 网络请求函数
  3473. */
  3474. setXMLHttpRequest(httpRequest) {
  3475. this.GM_Api.xmlHttpRequest = httpRequest;
  3476. }
  3477. /**
  3478. * GET 请求
  3479. * @param url 网址
  3480. * @param details 配置
  3481. */
  3482. get(...args) {
  3483. let useRequestOption = this.HttpxRequestOption.handleBeforeRequestOptionArgs(...args);
  3484. useRequestOption.method = "GET";
  3485. return this.request(useRequestOption, (option) => {
  3486. Reflect.deleteProperty(option, "onprogress");
  3487. });
  3488. }
  3489. /**
  3490. * POST 请求
  3491. */
  3492. post(...args) {
  3493. let useRequestOption = this.HttpxRequestOption.handleBeforeRequestOptionArgs(...args);
  3494. useRequestOption.method = "POST";
  3495. return this.request(useRequestOption);
  3496. }
  3497. /**
  3498. * HEAD 请求
  3499. */
  3500. head(...args) {
  3501. let useRequestOption = this.HttpxRequestOption.handleBeforeRequestOptionArgs(...args);
  3502. useRequestOption.method = "HEAD";
  3503. return this.request(useRequestOption, (option) => {
  3504. Reflect.deleteProperty(option, "onprogress");
  3505. });
  3506. }
  3507. /**
  3508. * OPTIONS 请求
  3509. */
  3510. options(...args) {
  3511. let useRequestOption = this.HttpxRequestOption.handleBeforeRequestOptionArgs(...args);
  3512. useRequestOption.method = "OPTIONS";
  3513. return this.request(useRequestOption, (option) => {
  3514. Reflect.deleteProperty(option, "onprogress");
  3515. });
  3516. }
  3517. /**
  3518. * DELETE 请求
  3519. */
  3520. delete(...args) {
  3521. let useRequestOption = this.HttpxRequestOption.handleBeforeRequestOptionArgs(...args);
  3522. useRequestOption.method = "DELETE";
  3523. return this.request(useRequestOption, (option) => {
  3524. Reflect.deleteProperty(option, "onprogress");
  3525. });
  3526. }
  3527. /**
  3528. * PUT 请求
  3529. */
  3530. put(...args) {
  3531. let userRequestOption = this.HttpxRequestOption.handleBeforeRequestOptionArgs(...args);
  3532. userRequestOption.method = "PUT";
  3533. return this.request(userRequestOption);
  3534. }
  3535. /**
  3536. * 发送请求
  3537. * @param details 配置
  3538. * @param beforeRequestOption 处理请求前的配置
  3539. */
  3540. request(details, beforeRequestOption) {
  3541. let useRequestOption = this.HttpxRequestOption.handleBeforeRequestOptionArgs(details);
  3542. /** 取消请求 */
  3543. let abortFn = null;
  3544. let promise = new globalThis.Promise(async (resolve, reject) => {
  3545. let requestOption = this.HttpxRequestOption.getRequestOption(useRequestOption.method, useRequestOption, resolve, reject);
  3546. if (typeof beforeRequestOption === "function") {
  3547. // @ts-ignore
  3548. beforeRequestOption(requestOption);
  3549. }
  3550. // @ts-ignore
  3551. requestOption =
  3552. this.HttpxRequestOption.removeRequestNullOption(requestOption);
  3553. const requestResult = await this.HttpxRequest.request(requestOption);
  3554. if (requestResult != null &&
  3555. typeof requestResult.abort === "function") {
  3556. abortFn = requestResult.abort;
  3557. }
  3558. });
  3559. // @ts-ignore
  3560. promise.abort = () => {
  3561. if (typeof abortFn === "function") {
  3562. abortFn();
  3563. }
  3564. };
  3565. // @ts-ignore
  3566. return promise;
  3567. }
  3568. }
  3569.  
  3570. class indexedDB {
  3571. #dbName;
  3572. #storeName;
  3573. #dbVersion;
  3574. /* websql的版本号,由于ios的问题,版本号的写法不一样 */
  3575. // @ts-ignore
  3576. #slqVersion = "1";
  3577. /* 监听IndexDB */
  3578. #indexedDB = window.indexedDB ||
  3579. window.mozIndexedDB ||
  3580. window.webkitIndexedDB ||
  3581. window.msIndexedDB;
  3582. /* 缓存数据库,避免同一个页面重复创建和销毁 */
  3583. #db = {};
  3584. // @ts-ignore
  3585. #store = null;
  3586. /** 状态码 */
  3587. #statusCode = {
  3588. operationSuccess: {
  3589. code: 200,
  3590. msg: "操作成功",
  3591. },
  3592. operationFailed: {
  3593. code: 401,
  3594. msg: "操作失败",
  3595. },
  3596. empty: {
  3597. code: 201,
  3598. msg: "操作成功,但是没有数据",
  3599. },
  3600. openFailed: { code: 91001, msg: "打开数据库失败" },
  3601. saveFailed: { code: 91002, msg: "保存数据失败" },
  3602. getFailed: { code: 91003, msg: "获取数据失败" },
  3603. deleteFailed: { code: 91004, msg: "删除数据失败" },
  3604. deleteAllFailed: { code: 91005, msg: "清空数据库失败" },
  3605. regexpGetFailed: { code: 91006, msg: "正则获取数据失败" },
  3606. };
  3607. /**
  3608. * @param dbName 数据存储名,默认为:default_db
  3609. * @param storeName 表名,默认为:default_form
  3610. * @param dbVersion indexDB的版本号,默认为:1
  3611. */
  3612. constructor(dbName = "default_db", storeName = "default_form", dbVersion = 1) {
  3613. this.#dbName = dbName;
  3614. this.#storeName = storeName;
  3615. this.#dbVersion = dbVersion;
  3616. if (!this.#indexedDB) {
  3617. alert("很抱歉,您的浏览器不支持indexedDB");
  3618. throw new TypeError("很抱歉,您的浏览器不支持indexedDB");
  3619. }
  3620. }
  3621. /**
  3622. * 创建 “表”
  3623. * @param dbName 表名
  3624. */
  3625. createStore(dbName) {
  3626. let txn, store;
  3627. txn = this.#db[dbName].transaction(this.#storeName, "readwrite");
  3628. /* IndexDB的读写权限 */
  3629. store = txn.objectStore(this.#storeName);
  3630. this.#store = store;
  3631. return store;
  3632. }
  3633. /**
  3634. * 打开数据库
  3635. * @param callback 回调
  3636. * @param dbName 数据库名
  3637. */
  3638. open(callback, dbName) {
  3639. let that = this;
  3640. /* 打开数据库 */
  3641. /* 如果支持IndexDB */
  3642. if (!that.#db[dbName]) {
  3643. /* 如果缓存中没有,则进行数据库的创建或打开,提高效率 */
  3644. let request = that.#indexedDB.open(dbName, that.#dbVersion);
  3645. request.onerror = function (event) {
  3646. callback(null, {
  3647. code: that.#statusCode.openFailed.code,
  3648. msg: that.#statusCode.openFailed.msg,
  3649. event: event,
  3650. });
  3651. };
  3652. request.onsuccess = function (event) {
  3653. if (!that.#db[dbName]) {
  3654. let target = event.target;
  3655. that.#db[dbName] = target.result;
  3656. }
  3657. let store = that.createStore(dbName);
  3658. callback(store);
  3659. };
  3660. request.onupgradeneeded = function (event) {
  3661. let target = event.target;
  3662. that.#db[dbName] = target.result;
  3663. let store = that.#db[dbName].createObjectStore(that.#storeName, {
  3664. keyPath: "key",
  3665. });
  3666. store.transaction.oncomplete = function (event) {
  3667. callback(store);
  3668. };
  3669. };
  3670. }
  3671. else {
  3672. /* 如果缓存中已经打开了数据库,就直接使用 */
  3673. let store = this.createStore(dbName);
  3674. callback(store);
  3675. }
  3676. }
  3677. /**
  3678. * 保存数据到数据库
  3679. * @param key 数据key
  3680. * @param value 数据值
  3681. */
  3682. async save(key, value) {
  3683. let that = this;
  3684. return new Promise((resolve) => {
  3685. let dbName = this.#dbName;
  3686. let inData = {
  3687. key: key,
  3688. value: value,
  3689. };
  3690. this.open(function (idbStore) {
  3691. if (idbStore == null) {
  3692. resolve({
  3693. success: false,
  3694. code: that.#statusCode.saveFailed.code,
  3695. msg: that.#statusCode.saveFailed.msg,
  3696. });
  3697. }
  3698. else {
  3699. let request = idbStore.put(inData);
  3700. request.onsuccess = function (event) {
  3701. /* 保存成功有success 字段 */
  3702. resolve({
  3703. success: true,
  3704. code: that.#statusCode.operationSuccess.code,
  3705. msg: that.#statusCode.operationSuccess.msg,
  3706. event: event,
  3707. });
  3708. };
  3709. request.onerror = function (event) {
  3710. resolve({
  3711. success: false,
  3712. code: that.#statusCode.saveFailed.code,
  3713. msg: that.#statusCode.saveFailed.msg,
  3714. event: event,
  3715. });
  3716. };
  3717. }
  3718. }, dbName);
  3719. });
  3720. }
  3721. /**
  3722. * 判断是否存在该数据
  3723. * @param key 数据key
  3724. */
  3725. async has(key) {
  3726. let that = this;
  3727. return new Promise((resolve) => {
  3728. let dbName = this.#dbName;
  3729. this.open(function (idbStore) {
  3730. /* 判断返回的数据中是否有error字段 */
  3731. if (idbStore == null) {
  3732. resolve({
  3733. success: false,
  3734. code: that.#statusCode.getFailed.code,
  3735. msg: that.#statusCode.getFailed.msg,
  3736. });
  3737. }
  3738. else {
  3739. let request = idbStore.get(key);
  3740. request.onsuccess = function (event) {
  3741. /* result 返回的是 {key: string, value: any} */
  3742. /* 键值对存储 */
  3743. resolve({
  3744. success: true,
  3745. code: that.#statusCode.operationSuccess.code,
  3746. msg: that.#statusCode.operationSuccess.msg,
  3747. event: event,
  3748. });
  3749. };
  3750. request.onerror = function (event) {
  3751. resolve({
  3752. success: false,
  3753. code: that.#statusCode.getFailed.code,
  3754. msg: that.#statusCode.getFailed.msg,
  3755. event: event,
  3756. });
  3757. };
  3758. }
  3759. }, dbName);
  3760. });
  3761. }
  3762. /**
  3763. * 根据key获取值
  3764. * @param key 数据key
  3765. */
  3766. async get(key) {
  3767. let that = this;
  3768. return new Promise((resolve) => {
  3769. let dbName = this.#dbName;
  3770. this.open(function (idbStore) {
  3771. /* 判断返回的数据中是否有error字段 */
  3772. if (idbStore == null) {
  3773. resolve({
  3774. success: false,
  3775. code: that.#statusCode.getFailed.code,
  3776. msg: that.#statusCode.getFailed.msg,
  3777. data: undefined,
  3778. });
  3779. }
  3780. else {
  3781. let request = idbStore.get(key);
  3782. request.onsuccess = function (event) {
  3783. let target = event.target;
  3784. let result = target.result;
  3785. /* result 返回的是 {key: string, value: any} */
  3786. /* 键值对存储 */
  3787. let data = result ? result.value : undefined;
  3788. if (data == null) {
  3789. resolve({
  3790. success: true,
  3791. code: that.#statusCode.empty.code,
  3792. msg: that.#statusCode.empty.msg,
  3793. data: data,
  3794. event: event,
  3795. result: result,
  3796. });
  3797. }
  3798. else {
  3799. resolve({
  3800. success: true,
  3801. code: that.#statusCode.operationSuccess.code,
  3802. msg: that.#statusCode.operationSuccess.msg,
  3803. data: data,
  3804. event: event,
  3805. result: result,
  3806. });
  3807. }
  3808. };
  3809. request.onerror = function (event) {
  3810. resolve({
  3811. success: false,
  3812. code: that.#statusCode.getFailed.code,
  3813. msg: that.#statusCode.getFailed.msg,
  3814. data: undefined,
  3815. event: event,
  3816. });
  3817. };
  3818. }
  3819. }, dbName);
  3820. });
  3821. }
  3822. /**
  3823. * 正则获取数据
  3824. * @param key 数据key,可以是正则
  3825. */
  3826. async regexpGet(key) {
  3827. let list = [];
  3828. let that = this;
  3829. return new Promise((resolve) => {
  3830. /* 正则查询 */
  3831. let dbName = that.#dbName;
  3832. this.open(function (idbStore) {
  3833. /* 判断返回的数据中是否有error字段 */
  3834. if (idbStore == null) {
  3835. resolve({
  3836. success: false,
  3837. code: that.#statusCode.regexpGetFailed.code,
  3838. msg: that.#statusCode.regexpGetFailed.msg,
  3839. data: [],
  3840. });
  3841. }
  3842. else {
  3843. let request = idbStore.getAll();
  3844. request.onsuccess = function (event) {
  3845. let target = event.target;
  3846. let result = target.result;
  3847. if (result.length !== 0) {
  3848. result.forEach((dataItem, index) => {
  3849. // 当前项的key
  3850. let __key = dataItem["key"];
  3851. // 当前项的value
  3852. let __value = dataItem["value"];
  3853. if (__key.match(key)) {
  3854. list = list.concat(__value);
  3855. }
  3856. });
  3857. }
  3858. resolve({
  3859. success: true,
  3860. code: that.#statusCode.operationSuccess.code,
  3861. msg: that.#statusCode.operationSuccess.msg,
  3862. data: list,
  3863. event: event,
  3864. });
  3865. };
  3866. request.onerror = function (event) {
  3867. resolve({
  3868. success: false,
  3869. code: that.#statusCode.getFailed.code,
  3870. msg: that.#statusCode.getFailed.msg,
  3871. data: [],
  3872. event: event,
  3873. });
  3874. };
  3875. }
  3876. }, dbName);
  3877. });
  3878. }
  3879. /**
  3880. * 删除数据
  3881. * @param key 数据key
  3882. */
  3883. async delete(key) {
  3884. let that = this;
  3885. return new Promise((resolve) => {
  3886. /* 根据key删除某条数据 */
  3887. let dbName = that.#dbName;
  3888. this.open(function (idbStore) {
  3889. if (idbStore == null) {
  3890. resolve({
  3891. success: false,
  3892. code: that.#statusCode.deleteFailed.code,
  3893. msg: that.#statusCode.deleteFailed.msg,
  3894. });
  3895. }
  3896. else {
  3897. // 删除键
  3898. let request = idbStore.delete(key);
  3899. request.onsuccess = function (event) {
  3900. resolve({
  3901. success: true,
  3902. code: that.#statusCode.operationSuccess.code,
  3903. msg: that.#statusCode.operationSuccess.msg,
  3904. event: event,
  3905. });
  3906. };
  3907. request.onerror = function (event) {
  3908. resolve({
  3909. success: false,
  3910. code: that.#statusCode.deleteFailed.code,
  3911. msg: that.#statusCode.deleteFailed.msg,
  3912. event: event,
  3913. });
  3914. };
  3915. }
  3916. }, dbName);
  3917. });
  3918. }
  3919. /**
  3920. * 删除所有数据
  3921. */
  3922. async deleteAll() {
  3923. let that = this;
  3924. return new Promise((resolve) => {
  3925. /* 清空数据库 */
  3926. let dbName = that.#dbName;
  3927. this.open(function (idbStore) {
  3928. if (idbStore == null) {
  3929. resolve({
  3930. success: false,
  3931. code: that.#statusCode.deleteAllFailed.code,
  3932. msg: that.#statusCode.deleteAllFailed.msg,
  3933. });
  3934. }
  3935. else {
  3936. // 清空
  3937. let operateResult = idbStore.clear();
  3938. operateResult.onsuccess = function (event) {
  3939. resolve({
  3940. success: true,
  3941. code: that.#statusCode.operationSuccess.code,
  3942. msg: that.#statusCode.operationSuccess.msg,
  3943. event: event,
  3944. });
  3945. };
  3946. operateResult.onerror = function (event) {
  3947. resolve({
  3948. success: false,
  3949. code: that.#statusCode.deleteAllFailed.code,
  3950. msg: that.#statusCode.deleteAllFailed.msg,
  3951. event: event,
  3952. });
  3953. };
  3954. }
  3955. }, dbName);
  3956. });
  3957. }
  3958. }
  3959.  
  3960. class LockFunction {
  3961. #flag = false;
  3962. #delayTime = 0;
  3963. #callback;
  3964. #timeId = undefined;
  3965. lock;
  3966. unlock;
  3967. run;
  3968. isLock;
  3969. constructor(callback, context, delayTime) {
  3970. let that = this;
  3971. this.#callback = callback;
  3972. if (typeof context === "number") {
  3973. this.#delayTime = context;
  3974. }
  3975. else {
  3976. this.#delayTime = delayTime;
  3977. }
  3978. /**
  3979. * 锁
  3980. */
  3981. this.lock = function () {
  3982. that.#flag = true;
  3983. clearTimeout(that.#timeId);
  3984. };
  3985. /**
  3986. * 解锁
  3987. */
  3988. this.unlock = function () {
  3989. that.#timeId = setTimeout(() => {
  3990. that.#flag = false;
  3991. }, that.#delayTime);
  3992. };
  3993. /**
  3994. * 判断是否被锁
  3995. */
  3996. this.isLock = function () {
  3997. return that.#flag;
  3998. };
  3999. /**
  4000. * 执行
  4001. */
  4002. this.run = async function (...args) {
  4003. if (that.isLock()) {
  4004. return;
  4005. }
  4006. that.lock();
  4007. await that.#callback.apply(this, args);
  4008. that.unlock();
  4009. };
  4010. }
  4011. }
  4012.  
  4013. class Log {
  4014. /** 是否禁用输出的flag */
  4015. #disable = false;
  4016. /** 前面的TAG标志 */
  4017. tag = "Utils.Log";
  4018. /* 使用的console函数 */
  4019. #console = null;
  4020. /* 当前输出的数量 */
  4021. #logCount = 0;
  4022. /* 配置 */
  4023. #details = {
  4024. tag: true,
  4025. successColor: "#0000FF",
  4026. errorColor: "#FF0000",
  4027. infoColor: "0",
  4028. warnColor: "0",
  4029. debug: false,
  4030. autoClearConsole: false,
  4031. logMaxCount: 999,
  4032. };
  4033. /**
  4034. * 颜色配置
  4035. */
  4036. #msgColorDetails = [
  4037. "font-weight: bold; color: cornflowerblue",
  4038. "font-weight: bold; color: cornflowerblue",
  4039. "font-weight: bold; color: darkorange",
  4040. "font-weight: bold; color: cornflowerblue",
  4041. ];
  4042. /**
  4043. * @param __GM_info 油猴管理器的API GM_info,或者是一个对象,如{"script":{name:"Utils.Log"}},或者直接是一个字符串,用作tag名
  4044. * @param console 可指定console对象为unsafeWindow下的console或者是油猴window下的console
  4045. */
  4046. constructor(__GM_info, console = window.console) {
  4047. if (typeof __GM_info === "string") {
  4048. this.tag = __GM_info;
  4049. }
  4050. else if (typeof __GM_info === "object" &&
  4051. typeof __GM_info?.script?.name === "string") {
  4052. this.tag = __GM_info.script.name;
  4053. }
  4054. this.#console = console;
  4055. }
  4056. /**
  4057. * 解析Error的堆栈获取实际调用者的函数名及函数所在的位置
  4058. * @param stack
  4059. */
  4060. parseErrorStack(stack) {
  4061. let result = {
  4062. name: "",
  4063. position: "",
  4064. };
  4065. for (let stackString of stack) {
  4066. stackString = stackString.trim();
  4067. let stackFunctionNameMatch = stackString.match(/^at[\s]+(.+?)[\s]+/i);
  4068. let stackFunctionNamePositionMatch = stackString.match(/^at[\s]+.+[\s]+\((.+?)\)/i);
  4069. if (stackFunctionNameMatch == null) {
  4070. continue;
  4071. }
  4072. if (stackFunctionNamePositionMatch == null) {
  4073. continue;
  4074. }
  4075. /* 获取最后一个,因为第一个是包含了at */
  4076. let stackFunctionName = stackFunctionNameMatch[stackFunctionNameMatch.length - 1];
  4077. let stackFunctionNamePosition = stackFunctionNamePositionMatch[stackFunctionNamePositionMatch.length - 1];
  4078. if (stackFunctionName === "" ||
  4079. stackFunctionName.match(/^(Utils\.|)Log(\.|)|.<anonymous>$|^Function.each|^NodeList.forEach|^k.fn.init.each/g)) {
  4080. continue;
  4081. }
  4082. else {
  4083. result.name = stackFunctionName;
  4084. result.position = stackFunctionNamePosition;
  4085. break;
  4086. }
  4087. }
  4088. if (result.position === "") {
  4089. let lastStackString = stack[stack.length - 1].trim();
  4090. if (lastStackString.startsWith("at chrome-extension://")) {
  4091. let lastStackMatch = lastStackString.match(/^at[\s]+(.+)/);
  4092. if (lastStackMatch) {
  4093. result.position = lastStackMatch[lastStackMatch.length - 1];
  4094. }
  4095. }
  4096. }
  4097. if (result.position === "") {
  4098. result.position = stack[stack.length - 1].trim().replace(/^at[\s]*/g, "");
  4099. }
  4100. return result;
  4101. }
  4102. /**
  4103. * 检测清理控制台
  4104. */
  4105. checkClearConsole() {
  4106. this.#logCount++;
  4107. if (this.#details.autoClearConsole &&
  4108. this.#logCount > this.#details.logMaxCount) {
  4109. this.#console.clear();
  4110. this.#logCount = 0;
  4111. }
  4112. }
  4113. /**
  4114. * 输出内容
  4115. * @param msg 需要输出的内容
  4116. * @param color 颜色
  4117. * @param otherStyle 其它CSS
  4118. */
  4119. printContent(msg, color, otherStyle) {
  4120. this.checkClearConsole();
  4121. otherStyle = otherStyle || "";
  4122. let stackSplit = new Error().stack.split("\n");
  4123. stackSplit.splice(0, 2);
  4124. let { name: callerName, position: callerPosition } = this.parseErrorStack(stackSplit);
  4125. let tagName = this.tag;
  4126. let that = this;
  4127. /** tag的html输出格式 */
  4128. let tagNameHTML = `%c[${tagName}%c`;
  4129. /** 调用的函数名的html输出格式 */
  4130. let callerNameHTML = `%c${callerName}%c]%c`;
  4131. callerName.trim() !== "" && (callerNameHTML = "-" + callerNameHTML);
  4132. /**
  4133. * 输出消息到控制台
  4134. * @param message
  4135. */
  4136. function consoleMsg(message) {
  4137. if (typeof message === "string") {
  4138. that.#console.log(`${tagNameHTML}${callerNameHTML} %s`, ...that.#msgColorDetails, `color: ${color};${otherStyle}`, message);
  4139. }
  4140. else if (typeof message === "number") {
  4141. that.#console.log(`${tagNameHTML}${callerNameHTML} %d`, ...that.#msgColorDetails, `color: ${color};${otherStyle}`, message);
  4142. }
  4143. else if (typeof message === "object") {
  4144. that.#console.log(`${tagNameHTML}${callerNameHTML} %o`, ...that.#msgColorDetails, `color: ${color};${otherStyle}`, message);
  4145. }
  4146. else {
  4147. that.#console.log(message);
  4148. }
  4149. }
  4150. if (Array.isArray(msg)) {
  4151. for (let index = 0; index < msg.length; index++) {
  4152. consoleMsg(msg[index]);
  4153. }
  4154. }
  4155. else {
  4156. consoleMsg(msg);
  4157. }
  4158. if (this.#details.debug) {
  4159. /* 如果开启调试模式,输出堆栈位置 */
  4160. this.#console.log(callerPosition);
  4161. }
  4162. }
  4163. /**
  4164. * 控制台-普通输出
  4165. * @param args 需要输出的内容
  4166. * @example
  4167. * log.info("输出信息","输出信息2","输出信息3","输出")
  4168. */
  4169. info(...args) {
  4170. if (this.#disable)
  4171. return;
  4172. this.printContent(args, this.#details.infoColor);
  4173. }
  4174. /**
  4175. * 控制台-警告输出
  4176. * @param args 需要输出的内容
  4177. * @example
  4178. * log.warn("输出警告","输出警告2","输出警告3","输出警告4")
  4179. */
  4180. warn(...args) {
  4181. if (this.#disable)
  4182. return;
  4183. this.printContent(args, this.#details.warnColor, "background: #FEF6D5;padding: 4px 6px 4px 0px;");
  4184. }
  4185. /**
  4186. * 控制台-错误输出
  4187. * @param args 需要输出的内容
  4188. * @example
  4189. * log.error("输出错误","输出错误2","输出错误3","输出错误4")
  4190. */
  4191. error(...args) {
  4192. if (this.#disable)
  4193. return;
  4194. this.printContent(args, this.#details.errorColor);
  4195. }
  4196. /**
  4197. * 控制台-成功输出
  4198. * @param args 需要输出的内容
  4199. * @example
  4200. * log.success("输出成功")
  4201. */
  4202. success(...args) {
  4203. if (this.#disable)
  4204. return;
  4205. this.printContent(args, this.#details.successColor);
  4206. }
  4207. /**
  4208. * 控制台-输出表格
  4209. * @param msg 需要输出的内容
  4210. * @example
  4211. * log.table([{"名字":"example","值":"123"},{"名字":"example2","值":"345"}])
  4212. */
  4213. table(msg) {
  4214. if (this.#disable)
  4215. return;
  4216. this.checkClearConsole();
  4217. let stack = new Error().stack.split("\n");
  4218. stack.splice(0, 1);
  4219. let errorStackParse = this.parseErrorStack(stack);
  4220. /** 堆栈函数名 */
  4221. let stackFunctionName = errorStackParse.name;
  4222. /** 堆栈位置 */
  4223. let stackFunctionNamePosition = errorStackParse.position;
  4224. let callerName = stackFunctionName;
  4225. this.#console.log(`%c[${this.tag}%c-%c${callerName}%c]%c`, ...this.#msgColorDetails, `color: ${this.#details.infoColor};`);
  4226. this.#console.table(msg);
  4227. if (this.#details.debug) {
  4228. this.#console.log(stackFunctionNamePosition);
  4229. }
  4230. }
  4231. /**
  4232. * 配置Log对象的颜色
  4233. * @param paramDetails 配置信息
  4234. */
  4235. config(paramDetails) {
  4236. this.#details = Object.assign(this.#details, paramDetails);
  4237. }
  4238. /** 禁用输出 */
  4239. disable() {
  4240. this.#disable = true;
  4241. }
  4242. /** 恢复输出 */
  4243. recovery() {
  4244. this.#disable = false;
  4245. }
  4246. }
  4247.  
  4248. class Progress {
  4249. #config = {
  4250. /**
  4251. * canvas元素节点
  4252. */
  4253. canvasNode: null,
  4254. /**
  4255. * 绘制角度
  4256. */
  4257. deg: 95,
  4258. /**
  4259. * 进度
  4260. */
  4261. progress: 0,
  4262. /**
  4263. * 绘制的线宽度
  4264. */
  4265. lineWidth: 10,
  4266. /**
  4267. * 绘制的背景颜色
  4268. */
  4269. lineBgColor: "#1e637c",
  4270. /**
  4271. * 绘制的线的颜色
  4272. */
  4273. lineColor: "#25deff",
  4274. /**
  4275. * 绘制的字体颜色
  4276. */
  4277. textColor: "#000000",
  4278. /**
  4279. * 绘制的字体大小(px)
  4280. */
  4281. fontSize: 22,
  4282. /**
  4283. * 绘制的圆的半径
  4284. */
  4285. circleRadius: 50,
  4286. };
  4287. #ctx = null;
  4288. #width = null;
  4289. #height = null;
  4290. /**
  4291. *
  4292. * @param paramConfig 配置信息
  4293. */
  4294. constructor(paramConfig) {
  4295. this.#config = commonUtil.assign(this.#config, paramConfig);
  4296. if (!(this.#config.canvasNode instanceof HTMLCanvasElement)) {
  4297. throw new Error("Utils.Progress 参数 canvasNode 必须是 HTMLCanvasElement");
  4298. }
  4299. this.init();
  4300. }
  4301. /**
  4302. * 初始化
  4303. */
  4304. init() {
  4305. /* 获取画笔 */
  4306. let ctx = this.#config.canvasNode.getContext("2d");
  4307. if (ctx == null) {
  4308. throw new Error("Utils.Progress 获取画笔失败");
  4309. }
  4310. this.#ctx = ctx;
  4311. /* 元素宽度 */
  4312. this.#width = this.#config.canvasNode.width;
  4313. /* 元素高度 */
  4314. this.#height = this.#config.canvasNode.height;
  4315. /* 清除锯齿 */
  4316. if (window.devicePixelRatio) {
  4317. this.#config.canvasNode.style.width = this.#width + "px";
  4318. this.#config.canvasNode.style.height = this.#height + "px";
  4319. this.#config.canvasNode.height = this.#height * window.devicePixelRatio;
  4320. this.#config.canvasNode.width = this.#width * window.devicePixelRatio;
  4321. this.#ctx.scale(window.devicePixelRatio, window.devicePixelRatio);
  4322. }
  4323. /* 设置线宽 */
  4324. this.#ctx.lineWidth = this.#config.lineWidth;
  4325. }
  4326. /**
  4327. * 绘制
  4328. */
  4329. draw() {
  4330. let degActive = (this.#config.progress * 360) / 100;
  4331. /* 清除画布 */
  4332. this.#ctx.clearRect(0, 0, this.#width, this.#height);
  4333. /* 开始绘制底圆 */
  4334. this.#ctx.beginPath();
  4335. this.#ctx.arc(this.#width / 2, this.#height / 2, this.#config.circleRadius, 1, 8);
  4336. this.#ctx.strokeStyle = this.#config.lineBgColor;
  4337. this.#ctx.stroke();
  4338. /* 开始绘制动态圆 */
  4339. this.#ctx.beginPath();
  4340. this.#ctx.arc(this.#width / 2, this.#height / 2, this.#config.circleRadius, -Math.PI / 2, (degActive * Math.PI) / 180 - Math.PI / 2);
  4341. this.#ctx.strokeStyle = this.#config.lineColor;
  4342. this.#ctx.stroke();
  4343. /* 获取百分比 */
  4344. let txt = parseInt(this.#config.progress.toString()) + "%";
  4345. this.#ctx.font = this.#config.fontSize + "px SimHei";
  4346. /* 获取文本宽度 */
  4347. let w = this.#ctx.measureText(txt).width;
  4348. let h = this.#config.fontSize / 2;
  4349. this.#ctx.fillStyle = this.#config.textColor;
  4350. this.#ctx.fillText(txt, this.#width / 2 - w / 2, this.#height / 2 + h / 2);
  4351. }
  4352. }
  4353.  
  4354. class UtilsDictionary {
  4355. items = {};
  4356. constructor(key, value) {
  4357. if (key != null) {
  4358. this.set(key, value);
  4359. }
  4360. }
  4361. /**
  4362. * 检查是否有某一个键
  4363. * @param key 键
  4364. */
  4365. has(key) {
  4366. return Reflect.has(this.items, key);
  4367. }
  4368. /**
  4369. * 检查已有的键中是否以xx开头
  4370. * @param key 需要匹配的键
  4371. */
  4372. startsWith(key) {
  4373. let allKeys = this.keys();
  4374. for (const keyName of allKeys) {
  4375. if (String(keyName).startsWith(String(key))) {
  4376. return true;
  4377. }
  4378. }
  4379. return false;
  4380. }
  4381. /**
  4382. * 获取以xx开头的键的值
  4383. * @param key 需要匹配的键
  4384. */
  4385. getStartsWith(key) {
  4386. let allKeys = this.keys();
  4387. let result = undefined;
  4388. for (const keyName of allKeys) {
  4389. if (String(keyName).startsWith(String(key))) {
  4390. result = this.get(keyName);
  4391. break;
  4392. }
  4393. }
  4394. return result;
  4395. }
  4396. /**
  4397. * 为字典添加某一个值
  4398. * @param key 键
  4399. * @param val 值,默认为""
  4400. */
  4401. set(key, val) {
  4402. if (key === undefined) {
  4403. throw new Error("Utils.Dictionary().set 参数 key 不能为空");
  4404. }
  4405. Reflect.set(this.items, key, val);
  4406. }
  4407. /**
  4408. * 删除某一个键
  4409. * @param key 键
  4410. */
  4411. delete(key) {
  4412. if (this.has(key)) {
  4413. return Reflect.deleteProperty(this.items, key);
  4414. }
  4415. return false;
  4416. }
  4417. /**
  4418. * 获取某个键的值
  4419. * https://github.com/microsoft/TypeScript/issues/9619
  4420. * 微软到现在都没有修复has和get的联动
  4421. * @param key 键
  4422. */
  4423. get(key) {
  4424. return Reflect.get(this.items, key);
  4425. }
  4426. /**
  4427. * 返回字典中的所有值
  4428. */
  4429. values() {
  4430. let resultList = [];
  4431. for (let prop in this.getItems()) {
  4432. if (this.has(prop)) {
  4433. resultList.push(this.get(prop));
  4434. }
  4435. }
  4436. return resultList;
  4437. }
  4438. /**
  4439. * 清空字典
  4440. */
  4441. clear() {
  4442. this.items = null;
  4443. this.items = {};
  4444. }
  4445. /**
  4446. * 获取字典的长度
  4447. */
  4448. size() {
  4449. return Object.keys(this.getItems()).length;
  4450. }
  4451. /**
  4452. * 获取字典所有的键
  4453. */
  4454. keys() {
  4455. return Reflect.ownKeys(this.items);
  4456. }
  4457. /**
  4458. * 返回字典本身
  4459. */
  4460. getItems() {
  4461. // @ts-ignore
  4462. return this.items;
  4463. }
  4464. /**
  4465. * 合并另一个字典
  4466. * @param data 需要合并的字典
  4467. */
  4468. concat(data) {
  4469. this.items = commonUtil.assign(this.items, data.getItems());
  4470. }
  4471. forEach(callbackfn) {
  4472. for (const key in this.getItems()) {
  4473. callbackfn(this.get(key), key, this.getItems());
  4474. }
  4475. }
  4476. /**
  4477. * 获取字典的长度,同this.size
  4478. */
  4479. get length() {
  4480. return this.size();
  4481. }
  4482. /**
  4483. * 迭代器
  4484. */
  4485. get entries() {
  4486. let that = this;
  4487. return function* () {
  4488. let itemKeys = Object.keys(that.getItems());
  4489. for (const keyName of itemKeys) {
  4490. yield [keyName, that.get(keyName)];
  4491. }
  4492. };
  4493. }
  4494. /**
  4495. * 是否可遍历
  4496. */
  4497. get [Symbol.iterator]() {
  4498. let that = this;
  4499. return function () {
  4500. return that.entries();
  4501. };
  4502. }
  4503. }
  4504.  
  4505. class WindowApi {
  4506. /** 默认的配置 */
  4507. defaultApi = {
  4508. document: document,
  4509. window: window,
  4510. globalThis: globalThis,
  4511. self: self,
  4512. top: top,
  4513. };
  4514. /** 使用的配置 */
  4515. api;
  4516. constructor(option) {
  4517. if (option) {
  4518. if (option.globalThis == null) {
  4519. option.globalThis = option.window;
  4520. }
  4521. if (option.self == null) {
  4522. option.self = option.window;
  4523. }
  4524. }
  4525. if (!option) {
  4526. option = Object.assign({}, this.defaultApi);
  4527. }
  4528. // @ts-ignore
  4529. this.api = Object.assign({}, option);
  4530. }
  4531. get document() {
  4532. return this.api.document;
  4533. }
  4534. get window() {
  4535. return this.api.window;
  4536. }
  4537. get globalThis() {
  4538. return this.api.globalThis;
  4539. }
  4540. get self() {
  4541. return this.api.self;
  4542. }
  4543. get top() {
  4544. return this.api.top;
  4545. }
  4546. }
  4547.  
  4548. const VueUtils = {
  4549. /** 标签 */
  4550. ReactiveFlags: {
  4551. IS_REACTIVE: Symbol("isReactive"),
  4552. },
  4553. /**
  4554. * 判断是否是对象
  4555. * @param value
  4556. */
  4557. isObject(value) {
  4558. return typeof value === "object" && value !== null;
  4559. },
  4560. /**
  4561. * 判断是否是函数
  4562. * @param val
  4563. */
  4564. isFunction(val) {
  4565. return typeof val === "function";
  4566. },
  4567. /**
  4568. * 处理对象再次代理,可以直接返回
  4569. * @param value
  4570. */
  4571. isReactive(value) {
  4572. return !!(value && value[VueUtils.ReactiveFlags.IS_REACTIVE]);
  4573. },
  4574. /**
  4575. * 判断是否是数组
  4576. * @param value
  4577. */
  4578. isArray(value) {
  4579. return Array.isArray(value);
  4580. },
  4581. };
  4582. class ReactiveEffect {
  4583. deps = [];
  4584. active = true;
  4585. fn;
  4586. // @ts-ignore
  4587. scheduler;
  4588. constructor(fn, scheduler) {
  4589. this.fn = fn;
  4590. this.scheduler = scheduler;
  4591. }
  4592. run(cb) {
  4593. if (!this.active) {
  4594. this.fn();
  4595. }
  4596. try {
  4597. if (typeof cb === "function") {
  4598. cb(this);
  4599. }
  4600. return this.fn();
  4601. }
  4602. finally {
  4603. if (typeof cb === "function") {
  4604. cb(undefined);
  4605. }
  4606. }
  4607. }
  4608. }
  4609. class RefImpl {
  4610. _value;
  4611. _isRef = true;
  4612. _rawValue;
  4613. _vue;
  4614. constructor(vueIns, rawValue) {
  4615. this._vue = vueIns;
  4616. this._rawValue = rawValue;
  4617. this._value = this._vue.toReactive(rawValue);
  4618. }
  4619. get value() {
  4620. return this._value;
  4621. }
  4622. set value(newValue) {
  4623. if (newValue !== this._rawValue) {
  4624. this._value = this._vue.toReactive(newValue);
  4625. this._rawValue = newValue;
  4626. }
  4627. }
  4628. }
  4629. class ObjectRefImpl {
  4630. object;
  4631. key;
  4632. constructor(object, key) {
  4633. this.object = object;
  4634. this.key = key;
  4635. }
  4636. get value() {
  4637. return this.object[this.key];
  4638. }
  4639. set value(newValue) {
  4640. this.object[this.key] = newValue;
  4641. }
  4642. }
  4643. class Vue {
  4644. reactMap = new WeakMap();
  4645. targetMap = new WeakMap();
  4646. activeEffect = undefined;
  4647. constructor() {
  4648. // 将数据转化成响应式的数据,只能做对象的代理
  4649. }
  4650. /**
  4651. * 生成一个被代理的对象
  4652. * @param target 需要代理的对象
  4653. */
  4654. reactive(target) {
  4655. const that = this;
  4656. if (!(typeof target === "object" && target !== null)) {
  4657. // @ts-ignore
  4658. return;
  4659. }
  4660. if (VueUtils.isReactive(target)) {
  4661. return target;
  4662. }
  4663. let exisProxy = this.reactMap.get(target);
  4664. if (exisProxy) {
  4665. return exisProxy;
  4666. }
  4667. const proxy = new Proxy(target, {
  4668. get(target, key, receiver) {
  4669. if (key === VueUtils.ReactiveFlags.IS_REACTIVE) {
  4670. return true;
  4671. }
  4672. that.track(target, "get", key);
  4673. return Reflect.get(target, key, receiver);
  4674. },
  4675. set(target, key, value, receiver) {
  4676. let oldValue = target[key];
  4677. let result = Reflect.set(target, key, value, receiver);
  4678. if (oldValue !== value) {
  4679. that.trigger(target, "set", key, oldValue, value);
  4680. }
  4681. return result;
  4682. },
  4683. });
  4684. that.reactMap.set(target, proxy);
  4685. return proxy;
  4686. }
  4687. /**
  4688. * 观察被reactive的对象值改变
  4689. * @param source 被观察的对象,这里采用函数返回对象
  4690. * @param changeCallBack 值改变的回调
  4691. */
  4692. watch(source, changeCallBack) {
  4693. let getter;
  4694. if (VueUtils.isReactive(source)) {
  4695. getter = () => this.traversal(source);
  4696. }
  4697. else if (VueUtils.isFunction(source)) {
  4698. getter = source;
  4699. }
  4700. else {
  4701. return;
  4702. }
  4703. let oldValue;
  4704. const job = () => {
  4705. const newValue = effect.run((activeEffect) => {
  4706. this.activeEffect = activeEffect;
  4707. });
  4708. changeCallBack(newValue, oldValue);
  4709. oldValue = newValue;
  4710. };
  4711. const effect = new ReactiveEffect(getter, job);
  4712. oldValue = effect.run((activeEffect) => {
  4713. this.activeEffect = activeEffect;
  4714. });
  4715. }
  4716. toReactive(value) {
  4717. return VueUtils.isObject(value) ? this.reactive(value) : value;
  4718. }
  4719. ref(value) {
  4720. return new RefImpl(this, value);
  4721. }
  4722. toRef(object, key) {
  4723. return new ObjectRefImpl(object, key);
  4724. }
  4725. toRefs(object) {
  4726. const result = VueUtils.isArray(object) ? new Array(object.length) : {};
  4727. for (let key in object) {
  4728. // @ts-ignore
  4729. result[key] = this.toRef(object, key);
  4730. }
  4731. return result;
  4732. }
  4733. trigger(target, type, key, oldValue, value) {
  4734. const depsMap = this.targetMap.get(target);
  4735. if (!depsMap)
  4736. return;
  4737. const effects = depsMap.get(key);
  4738. this.triggerEffect(effects, "effects");
  4739. }
  4740. triggerEffect(effects, name) {
  4741. effects &&
  4742. effects.forEach((effect) => {
  4743. if (effect.scheduler) {
  4744. effect.scheduler();
  4745. }
  4746. else {
  4747. effect.run();
  4748. }
  4749. });
  4750. }
  4751. track(target, type, key) {
  4752. if (!this.activeEffect)
  4753. return;
  4754. let depsMap = this.targetMap.get(target);
  4755. if (!depsMap) {
  4756. this.targetMap.set(target, (depsMap = new Map()));
  4757. }
  4758. let dep = depsMap.get(key);
  4759. if (!dep) {
  4760. depsMap.set(key, (dep = new Set()));
  4761. }
  4762. this.trackEffect(dep);
  4763. }
  4764. trackEffect(dep) {
  4765. if (this.activeEffect) {
  4766. let shouldTrack = !dep.has(this.activeEffect);
  4767. if (shouldTrack) {
  4768. dep.add(this.activeEffect);
  4769. this.activeEffect.deps.push(dep);
  4770. }
  4771. }
  4772. }
  4773. traversal(value, set = new Set()) {
  4774. if (!VueUtils.isObject(value))
  4775. return value;
  4776. if (set.has(value)) {
  4777. return value;
  4778. }
  4779. set.add(value);
  4780. for (let key in value) {
  4781. this.traversal(value[key], set);
  4782. }
  4783. return value;
  4784. }
  4785. }
  4786.  
  4787. const createCache = (lastNumberWeakMap) => {
  4788. return (collection, nextNumber) => {
  4789. lastNumberWeakMap.set(collection, nextNumber);
  4790. return nextNumber;
  4791. };
  4792. };
  4793.  
  4794. /*
  4795. * The value of the constant Number.MAX_SAFE_INTEGER equals (2 ** 53 - 1) but it
  4796. * is fairly new.
  4797. */
  4798. const MAX_SAFE_INTEGER = Number.MAX_SAFE_INTEGER === undefined ? 9007199254740991 : Number.MAX_SAFE_INTEGER;
  4799. const TWO_TO_THE_POWER_OF_TWENTY_NINE = 536870912;
  4800. const TWO_TO_THE_POWER_OF_THIRTY = TWO_TO_THE_POWER_OF_TWENTY_NINE * 2;
  4801. const createGenerateUniqueNumber = (cache, lastNumberWeakMap) => {
  4802. return (collection) => {
  4803. const lastNumber = lastNumberWeakMap.get(collection);
  4804. /*
  4805. * Let's try the cheapest algorithm first. It might fail to produce a new
  4806. * number, but it is so cheap that it is okay to take the risk. Just
  4807. * increase the last number by one or reset it to 0 if we reached the upper
  4808. * bound of SMIs (which stands for small integers). When the last number is
  4809. * unknown it is assumed that the collection contains zero based consecutive
  4810. * numbers.
  4811. */
  4812. let nextNumber = lastNumber === undefined ? collection.size : lastNumber < TWO_TO_THE_POWER_OF_THIRTY ? lastNumber + 1 : 0;
  4813. if (!collection.has(nextNumber)) {
  4814. return cache(collection, nextNumber);
  4815. }
  4816. /*
  4817. * If there are less than half of 2 ** 30 numbers stored in the collection,
  4818. * the chance to generate a new random number in the range from 0 to 2 ** 30
  4819. * is at least 50%. It's benifitial to use only SMIs because they perform
  4820. * much better in any environment based on V8.
  4821. */
  4822. if (collection.size < TWO_TO_THE_POWER_OF_TWENTY_NINE) {
  4823. while (collection.has(nextNumber)) {
  4824. nextNumber = Math.floor(Math.random() * TWO_TO_THE_POWER_OF_THIRTY);
  4825. }
  4826. return cache(collection, nextNumber);
  4827. }
  4828. // Quickly check if there is a theoretical chance to generate a new number.
  4829. if (collection.size > MAX_SAFE_INTEGER) {
  4830. throw new Error('Congratulations, you created a collection of unique numbers which uses all available integers!');
  4831. }
  4832. // Otherwise use the full scale of safely usable integers.
  4833. while (collection.has(nextNumber)) {
  4834. nextNumber = Math.floor(Math.random() * MAX_SAFE_INTEGER);
  4835. }
  4836. return cache(collection, nextNumber);
  4837. };
  4838. };
  4839.  
  4840. const LAST_NUMBER_WEAK_MAP = new WeakMap();
  4841. const cache = createCache(LAST_NUMBER_WEAK_MAP);
  4842. const generateUniqueNumber = createGenerateUniqueNumber(cache, LAST_NUMBER_WEAK_MAP);
  4843.  
  4844. const isMessagePort = (sender) => {
  4845. return typeof sender.start === 'function';
  4846. };
  4847.  
  4848. const PORT_MAP = new WeakMap();
  4849.  
  4850. const extendBrokerImplementation = (partialBrokerImplementation) => ({
  4851. ...partialBrokerImplementation,
  4852. connect: ({ call }) => {
  4853. return async () => {
  4854. const { port1, port2 } = new MessageChannel();
  4855. const portId = await call('connect', { port: port1 }, [port1]);
  4856. PORT_MAP.set(port2, portId);
  4857. return port2;
  4858. };
  4859. },
  4860. disconnect: ({ call }) => {
  4861. return async (port) => {
  4862. const portId = PORT_MAP.get(port);
  4863. if (portId === undefined) {
  4864. throw new Error('The given port is not connected.');
  4865. }
  4866. await call('disconnect', { portId });
  4867. };
  4868. },
  4869. isSupported: ({ call }) => {
  4870. return () => call('isSupported');
  4871. }
  4872. });
  4873.  
  4874. const ONGOING_REQUESTS = new WeakMap();
  4875. const createOrGetOngoingRequests = (sender) => {
  4876. if (ONGOING_REQUESTS.has(sender)) {
  4877. // @todo TypeScript needs to be convinced that has() works as expected.
  4878. return ONGOING_REQUESTS.get(sender);
  4879. }
  4880. const ongoingRequests = new Map();
  4881. ONGOING_REQUESTS.set(sender, ongoingRequests);
  4882. return ongoingRequests;
  4883. };
  4884. const createBroker = (brokerImplementation) => {
  4885. const fullBrokerImplementation = extendBrokerImplementation(brokerImplementation);
  4886. return (sender) => {
  4887. const ongoingRequests = createOrGetOngoingRequests(sender);
  4888. sender.addEventListener('message', (({ data: message }) => {
  4889. const { id } = message;
  4890. if (id !== null && ongoingRequests.has(id)) {
  4891. const { reject, resolve } = ongoingRequests.get(id);
  4892. ongoingRequests.delete(id);
  4893. if (message.error === undefined) {
  4894. resolve(message.result);
  4895. }
  4896. else {
  4897. reject(new Error(message.error.message));
  4898. }
  4899. }
  4900. }));
  4901. if (isMessagePort(sender)) {
  4902. sender.start();
  4903. }
  4904. const call = (method, params = null, transferables = []) => {
  4905. return new Promise((resolve, reject) => {
  4906. const id = generateUniqueNumber(ongoingRequests);
  4907. ongoingRequests.set(id, { reject, resolve });
  4908. if (params === null) {
  4909. sender.postMessage({ id, method }, transferables);
  4910. }
  4911. else {
  4912. sender.postMessage({ id, method, params }, transferables);
  4913. }
  4914. });
  4915. };
  4916. const notify = (method, params, transferables = []) => {
  4917. sender.postMessage({ id: null, method, params }, transferables);
  4918. };
  4919. let functions = {};
  4920. for (const [key, handler] of Object.entries(fullBrokerImplementation)) {
  4921. functions = { ...functions, [key]: handler({ call, notify }) };
  4922. }
  4923. return { ...functions };
  4924. };
  4925. };
  4926.  
  4927. // Prefilling the Maps with a function indexed by zero is necessary to be compliant with the specification.
  4928. const scheduledIntervalsState = new Map([[0, null]]); // tslint:disable-line no-empty
  4929. const scheduledTimeoutsState = new Map([[0, null]]); // tslint:disable-line no-empty
  4930. const wrap = createBroker({
  4931. clearInterval: ({ call }) => {
  4932. return (timerId) => {
  4933. if (typeof scheduledIntervalsState.get(timerId) === 'symbol') {
  4934. scheduledIntervalsState.set(timerId, null);
  4935. call('clear', { timerId, timerType: 'interval' }).then(() => {
  4936. scheduledIntervalsState.delete(timerId);
  4937. });
  4938. }
  4939. };
  4940. },
  4941. clearTimeout: ({ call }) => {
  4942. return (timerId) => {
  4943. if (typeof scheduledTimeoutsState.get(timerId) === 'symbol') {
  4944. scheduledTimeoutsState.set(timerId, null);
  4945. call('clear', { timerId, timerType: 'timeout' }).then(() => {
  4946. scheduledTimeoutsState.delete(timerId);
  4947. });
  4948. }
  4949. };
  4950. },
  4951. setInterval: ({ call }) => {
  4952. return (func, delay = 0, ...args) => {
  4953. const symbol = Symbol();
  4954. const timerId = generateUniqueNumber(scheduledIntervalsState);
  4955. scheduledIntervalsState.set(timerId, symbol);
  4956. const schedule = () => call('set', {
  4957. delay,
  4958. now: performance.timeOrigin + performance.now(),
  4959. timerId,
  4960. timerType: 'interval'
  4961. }).then(() => {
  4962. const state = scheduledIntervalsState.get(timerId);
  4963. if (state === undefined) {
  4964. throw new Error('The timer is in an undefined state.');
  4965. }
  4966. if (state === symbol) {
  4967. func(...args);
  4968. // Doublecheck if the interval should still be rescheduled because it could have been cleared inside of func().
  4969. if (scheduledIntervalsState.get(timerId) === symbol) {
  4970. schedule();
  4971. }
  4972. }
  4973. });
  4974. schedule();
  4975. return timerId;
  4976. };
  4977. },
  4978. setTimeout: ({ call }) => {
  4979. return (func, delay = 0, ...args) => {
  4980. const symbol = Symbol();
  4981. const timerId = generateUniqueNumber(scheduledTimeoutsState);
  4982. scheduledTimeoutsState.set(timerId, symbol);
  4983. call('set', {
  4984. delay,
  4985. now: performance.timeOrigin + performance.now(),
  4986. timerId,
  4987. timerType: 'timeout'
  4988. }).then(() => {
  4989. const state = scheduledTimeoutsState.get(timerId);
  4990. if (state === undefined) {
  4991. throw new Error('The timer is in an undefined state.');
  4992. }
  4993. if (state === symbol) {
  4994. // A timeout can be savely deleted because it is only called once.
  4995. scheduledTimeoutsState.delete(timerId);
  4996. func(...args);
  4997. }
  4998. });
  4999. return timerId;
  5000. };
  5001. }
  5002. });
  5003. const load = (url) => {
  5004. const worker = new Worker(url);
  5005. return wrap(worker);
  5006. };
  5007.  
  5008. const createLoadOrReturnBroker = (loadBroker, worker) => {
  5009. let broker = null;
  5010. return () => {
  5011. if (broker !== null) {
  5012. return broker;
  5013. }
  5014. const blob = new Blob([worker], { type: 'application/javascript; charset=utf-8' });
  5015. const url = URL.createObjectURL(blob);
  5016. broker = loadBroker(url);
  5017. // Bug #1: Edge up until v18 didn't like the URL to be revoked directly.
  5018. setTimeout(() => URL.revokeObjectURL(url));
  5019. return broker;
  5020. };
  5021. };
  5022.  
  5023. // This is the minified and stringified code of the worker-timers-worker package.
  5024. const worker = `(()=>{var e={455:function(e,t){!function(e){"use strict";var t=function(e){return function(t){var r=e(t);return t.add(r),r}},r=function(e){return function(t,r){return e.set(t,r),r}},n=void 0===Number.MAX_SAFE_INTEGER?9007199254740991:Number.MAX_SAFE_INTEGER,o=536870912,s=2*o,a=function(e,t){return function(r){var a=t.get(r),i=void 0===a?r.size:a<s?a+1:0;if(!r.has(i))return e(r,i);if(r.size<o){for(;r.has(i);)i=Math.floor(Math.random()*s);return e(r,i)}if(r.size>n)throw new Error("Congratulations, you created a collection of unique numbers which uses all available integers!");for(;r.has(i);)i=Math.floor(Math.random()*n);return e(r,i)}},i=new WeakMap,u=r(i),c=a(u,i),d=t(c);e.addUniqueNumber=d,e.generateUniqueNumber=c}(t)}},t={};function r(n){var o=t[n];if(void 0!==o)return o.exports;var s=t[n]={exports:{}};return e[n].call(s.exports,s,s.exports,r),s.exports}(()=>{"use strict";const e=-32603,t=-32602,n=-32601,o=(e,t)=>Object.assign(new Error(e),{status:t}),s=t=>o('The handler of the method called "'.concat(t,'" returned an unexpected result.'),e),a=(t,r)=>async({data:{id:a,method:i,params:u}})=>{const c=r[i];try{if(void 0===c)throw(e=>o('The requested method called "'.concat(e,'" is not supported.'),n))(i);const r=void 0===u?c():c(u);if(void 0===r)throw(t=>o('The handler of the method called "'.concat(t,'" returned no required result.'),e))(i);const d=r instanceof Promise?await r:r;if(null===a){if(void 0!==d.result)throw s(i)}else{if(void 0===d.result)throw s(i);const{result:e,transferables:r=[]}=d;t.postMessage({id:a,result:e},r)}}catch(e){const{message:r,status:n=-32603}=e;t.postMessage({error:{code:n,message:r},id:a})}};var i=r(455);const u=new Map,c=(e,r,n)=>({...r,connect:({port:t})=>{t.start();const n=e(t,r),o=(0,i.generateUniqueNumber)(u);return u.set(o,(()=>{n(),t.close(),u.delete(o)})),{result:o}},disconnect:({portId:e})=>{const r=u.get(e);if(void 0===r)throw(e=>o('The specified parameter called "portId" with the given value "'.concat(e,'" does not identify a port connected to this worker.'),t))(e);return r(),{result:null}},isSupported:async()=>{if(await new Promise((e=>{const t=new ArrayBuffer(0),{port1:r,port2:n}=new MessageChannel;r.onmessage=({data:t})=>e(null!==t),n.postMessage(t,[t])}))){const e=n();return{result:e instanceof Promise?await e:e}}return{result:!1}}}),d=(e,t,r=()=>!0)=>{const n=c(d,t,r),o=a(e,n);return e.addEventListener("message",o),()=>e.removeEventListener("message",o)},l=e=>t=>{const r=e.get(t);if(void 0===r)return Promise.resolve(!1);const[n,o]=r;return clearTimeout(n),e.delete(t),o(!1),Promise.resolve(!0)},f=(e,t,r)=>(n,o,s)=>{const{expected:a,remainingDelay:i}=e(n,o);return new Promise((e=>{t.set(s,[setTimeout(r,i,a,t,e,s),e])}))},m=(e,t)=>{const r=performance.now(),n=e+t-r-performance.timeOrigin;return{expected:r+n,remainingDelay:n}},p=(e,t,r,n)=>{const o=e-performance.now();o>0?t.set(n,[setTimeout(p,o,e,t,r,n),r]):(t.delete(n),r(!0))},h=new Map,v=l(h),w=new Map,g=l(w),M=f(m,h,p),y=f(m,w,p);d(self,{clear:async({timerId:e,timerType:t})=>({result:await("interval"===t?v(e):g(e))}),set:async({delay:e,now:t,timerId:r,timerType:n})=>({result:await("interval"===n?M:y)(e,t,r)})})})()})();`; // tslint:disable-line:max-line-length
  5025.  
  5026. const loadOrReturnBroker = createLoadOrReturnBroker(load, worker);
  5027. const clearInterval = (timerId) => loadOrReturnBroker().clearInterval(timerId);
  5028. const clearTimeout$1 = (timerId) => loadOrReturnBroker().clearTimeout(timerId);
  5029. const setInterval = (...args) => loadOrReturnBroker().setInterval(...args);
  5030. const setTimeout$1 = (...args) => loadOrReturnBroker().setTimeout(...args);
  5031.  
  5032. // ==UserScript==
  5033. // @name ModuleRaid.js
  5034. // @namespace http://tampermonkey.net/
  5035. // @version 6.2.0
  5036. // @description 检索调用webpackJsonp模块,可指定检索的window
  5037. // @author empyrealtear
  5038. // @license MIT
  5039. // @original-script https://github.com/pixeldesu/moduleRaid
  5040. // ==/UserScript==
  5041.  
  5042.  
  5043. /**
  5044. * Main moduleRaid class
  5045. * @link https://scriptcat.org/zh-CN/script-show-page/2628
  5046. */
  5047. class ModuleRaid {
  5048. /**
  5049. * moduleRaid constructor
  5050. *
  5051. * @example
  5052. * Constructing an instance without any arguments:
  5053. * ```ts
  5054. * const mR = new ModuleRaid()
  5055. * ```
  5056. *
  5057. * Constructing an instance with the optional `opts` object:
  5058. * ```ts
  5059. * const mR = new ModuleRaid({ entrypoint: 'webpackChunk_custom_name' })
  5060. * ```
  5061. *
  5062. * @param opts a object containing options to initialize moduleRaid with
  5063. * - **opts:**
  5064. * - _target_: the window object being searched for
  5065. * - _entrypoint_: the Webpack entrypoint present on the global window object
  5066. * - _debug_: whether debug mode is enabled or not
  5067. * - _strict_: whether strict mode is enabled or not
  5068. */
  5069. constructor(opts) {
  5070. /**
  5071. * A random generated module ID we use for injecting into Webpack
  5072. */
  5073. this.moduleID = Math.random().toString(36).substring(7);
  5074. /**
  5075. * An array containing different argument injection methods for
  5076. * Webpack (before version 4), and subsequently pulling out methods and modules
  5077. * @internal
  5078. */
  5079. this.functionArguments = [
  5080. [
  5081. [0],
  5082. [
  5083. (_e, _t, i) => {
  5084. this.modules = i.c;
  5085. this.constructors = i.m;
  5086. this.get = i;
  5087. },
  5088. ],
  5089. ],
  5090. [
  5091. [1e3],
  5092. {
  5093. [this.moduleID]: (_e, _t, i) => {
  5094. this.modules = i.c;
  5095. this.constructors = i.m;
  5096. this.get = i;
  5097. },
  5098. },
  5099. [[this.moduleID]],
  5100. ],
  5101. ];
  5102. /**
  5103. * An array containing different argument injection methods for
  5104. * Webpack (after version 4), and subsequently pulling out methods and modules
  5105. * @internal
  5106. */
  5107. this.arrayArguments = [
  5108. [
  5109. [this.moduleID],
  5110. {},
  5111. (e) => {
  5112. const mCac = e.m;
  5113. Object.keys(mCac).forEach((mod) => {
  5114. try {
  5115. this.modules[mod] = e(mod);
  5116. }
  5117. catch (err) {
  5118. this.log(`[arrayArguments/1] Failed to require(${mod}) with error:\n${err}\n${err.stack}`);
  5119. }
  5120. });
  5121. this.get = e;
  5122. },
  5123. ],
  5124. this.functionArguments[1],
  5125. ];
  5126. /**
  5127. * Storage for the modules we extracted from Webpack
  5128. */
  5129. this.modules = {};
  5130. /**
  5131. * Storage for the constructors we extracted from Webpack
  5132. */
  5133. this.constructors = [];
  5134. let options = {
  5135. target: window,
  5136. entrypoint: 'webpackJsonp',
  5137. debug: false,
  5138. strict: false,
  5139. };
  5140. if (typeof opts === 'object') {
  5141. options = Object.assign(Object.assign({}, options), opts);
  5142. }
  5143. this.target = options.target;
  5144. this.entrypoint = options.entrypoint;
  5145. this.debug = options.debug;
  5146. this.strict = options.strict;
  5147. this.detectEntrypoint();
  5148. this.fillModules();
  5149. this.replaceGet();
  5150. this.setupPushEvent();
  5151. }
  5152. /**
  5153. * Debug logging method, outputs to the console when {@link ModuleRaid.debug} is true
  5154. *
  5155. * @param {*} message The message to be logged
  5156. * @internal
  5157. */
  5158. log(message) {
  5159. if (this.debug) {
  5160. console.warn(`[moduleRaid] ${message}`);
  5161. }
  5162. }
  5163. /**
  5164. * Method to set an alternative getter if we weren't able to extract __webpack_require__
  5165. * from Webpack
  5166. * @internal
  5167. */
  5168. replaceGet() {
  5169. if (this.get === null) {
  5170. this.get = (key) => this.modules[key];
  5171. }
  5172. }
  5173. /**
  5174. * Method that will try to inject a module into Webpack or get modules
  5175. * depending on it's success it might be more or less brute about it
  5176. * @internal
  5177. */
  5178. fillModules() {
  5179. if (typeof this.target[this.entrypoint] === 'function') {
  5180. this.functionArguments.forEach((argument, index) => {
  5181. try {
  5182. if (this.modules && Object.keys(this.modules).length > 0)
  5183. return;
  5184. this.target[this.entrypoint](...argument);
  5185. }
  5186. catch (err) {
  5187. this.log(`moduleRaid.functionArguments[${index}] failed:\n${err}\n${err.stack}`);
  5188. }
  5189. });
  5190. }
  5191. else {
  5192. this.arrayArguments.forEach((argument, index) => {
  5193. try {
  5194. if (this.modules && Object.keys(this.modules).length > 0)
  5195. return;
  5196. this.target[this.entrypoint].push(argument);
  5197. }
  5198. catch (err) {
  5199. this.log(`Pushing moduleRaid.arrayArguments[${index}] into ${this.entrypoint} failed:\n${err}\n${err.stack}`);
  5200. }
  5201. });
  5202. }
  5203. if (this.modules && Object.keys(this.modules).length == 0) {
  5204. let moduleEnd = false;
  5205. let moduleIterator = 0;
  5206. if (typeof this.target[this.entrypoint] != 'function' || !this.target[this.entrypoint]([], [], [moduleIterator])) {
  5207. throw Error('Unknown Webpack structure');
  5208. }
  5209. while (!moduleEnd) {
  5210. try {
  5211. this.modules[moduleIterator] = this.target[this.entrypoint]([], [], [moduleIterator]);
  5212. moduleIterator++;
  5213. }
  5214. catch (err) {
  5215. moduleEnd = true;
  5216. }
  5217. }
  5218. }
  5219. }
  5220. /**
  5221. * Method to hook into `window[this.entrypoint].push` adding a listener for new
  5222. * chunks being pushed into Webpack
  5223. *
  5224. * @example
  5225. * You can listen for newly pushed packages using the `moduleraid:webpack-push` event
  5226. * on `document`
  5227. *
  5228. * ```ts
  5229. * document.addEventListener('moduleraid:webpack-push', (e) => {
  5230. * // e.detail contains the arguments push() was called with
  5231. * console.log(e.detail)
  5232. * })
  5233. * ```
  5234. * @internal
  5235. */
  5236. setupPushEvent() {
  5237. const originalPush = this.target[this.entrypoint].push;
  5238. this.target[this.entrypoint].push = (...args) => {
  5239. const result = Reflect.apply(originalPush, this.target[this.entrypoint], args);
  5240. document.dispatchEvent(new CustomEvent('moduleraid:webpack-push', { detail: args }));
  5241. return result;
  5242. };
  5243. }
  5244. /**
  5245. * Method to try autodetecting a Webpack JSONP entrypoint based on common naming
  5246. *
  5247. * If the default entrypoint, or the entrypoint that's passed to the moduleRaid constructor
  5248. * already matches, the method exits early
  5249. *
  5250. * If `options.strict` has been set in the constructor and the initial entrypoint cannot
  5251. * be found, this method will error, demanding a strictly set entrypoint
  5252. * @internal
  5253. */
  5254. detectEntrypoint() {
  5255. if (this.target[this.entrypoint] != undefined) {
  5256. return;
  5257. }
  5258. if (this.strict) {
  5259. throw Error(`Strict mode is enabled and entrypoint at window.${this.entrypoint} couldn't be found. Please specify the correct one!`);
  5260. }
  5261. let windowObjects = Object.keys(this.target);
  5262. windowObjects = windowObjects
  5263. .filter((object) => object.toLowerCase().includes('chunk') || object.toLowerCase().includes('webpack'))
  5264. .filter((object) => typeof this.target[object] === 'function' || Array.isArray(this.target[object]));
  5265. if (windowObjects.length > 1) {
  5266. throw Error(`Multiple possible endpoints have been detected, please create a new moduleRaid instance with a specific one:\n${windowObjects.join(', ')}`);
  5267. }
  5268. if (windowObjects.length === 0) {
  5269. throw Error('No Webpack JSONP entrypoints could be detected');
  5270. }
  5271. this.log(`Entrypoint has been detected at window.${windowObjects[0]} and set for injection`);
  5272. this.entrypoint = windowObjects[0];
  5273. }
  5274. /**
  5275. * Recursive object-search function for modules
  5276. *
  5277. * @param object the object to search through
  5278. * @param query the query the object keys/values are searched for
  5279. * @returns boolean state of `object` containing `query` somewhere in it
  5280. * @internal
  5281. */
  5282. searchObject(object, query) {
  5283. for (const key in object) {
  5284. const value = object[key];
  5285. const lowerCaseQuery = query.toLowerCase();
  5286. if (typeof value != 'object') {
  5287. const lowerCaseKey = key.toString().toLowerCase();
  5288. if (lowerCaseKey.includes(lowerCaseQuery))
  5289. return true;
  5290. if (typeof value != 'object') {
  5291. const lowerCaseValue = value.toString().toLowerCase();
  5292. if (lowerCaseValue.includes(lowerCaseQuery))
  5293. return true;
  5294. }
  5295. else {
  5296. if (this.searchObject(value, query))
  5297. return true;
  5298. }
  5299. }
  5300. }
  5301. return false;
  5302. }
  5303. /**
  5304. * Method to search through the module object, searching for the fitting content
  5305. * if a string is supplied
  5306. *
  5307. * If query is supplied as a function, everything that returns true when passed
  5308. * to the query function will be returned
  5309. *
  5310. * @example
  5311. * With a string as query argument:
  5312. * ```ts
  5313. * const results = mR.findModule('feature')
  5314. * // => Array of module results
  5315. * ```
  5316. *
  5317. * With a function as query argument:
  5318. * ```ts
  5319. * const results = mR.findModule((module) => { typeof module === 'function' })
  5320. * // => Array of module results
  5321. * ```
  5322. *
  5323. * @param query query to search the module list for
  5324. * @return a list of modules fitting the query
  5325. */
  5326. findModule(query) {
  5327. const results = [];
  5328. const modules = Object.keys(this.modules);
  5329. if (modules.length === 0) {
  5330. throw new Error('There are no modules to search through!');
  5331. }
  5332. modules.forEach((key) => {
  5333. const module = this.modules[key.toString()];
  5334. if (module === undefined)
  5335. return;
  5336. try {
  5337. if (typeof query === 'string') {
  5338. query = query.toLowerCase();
  5339. switch (typeof module) {
  5340. case 'string':
  5341. if (module.toLowerCase().includes(query))
  5342. results.push(module);
  5343. break;
  5344. case 'function':
  5345. if (module.toString().toLowerCase().includes(query))
  5346. results.push(module);
  5347. break;
  5348. case 'object':
  5349. if (this.searchObject(module, query))
  5350. results.push(module);
  5351. break;
  5352. }
  5353. }
  5354. else if (typeof query === 'function') {
  5355. if (query(module))
  5356. results.push(module);
  5357. }
  5358. else {
  5359. throw new TypeError(`findModule can only find via string and function, ${typeof query} was passed`);
  5360. }
  5361. }
  5362. catch (err) {
  5363. this.log(`There was an error while searching through module '${key}':\n${err}\n${err.stack}`);
  5364. }
  5365. });
  5366. return results;
  5367. }
  5368. /**
  5369. * Method to search through the constructor array, searching for the fitting content
  5370. * if a string is supplied
  5371. *
  5372. * If query is supplied as a function, everything that returns true when passed
  5373. * to the query function will be returned
  5374. *
  5375. * @example
  5376. * With a string as query argument:
  5377. * ```ts
  5378. * const results = mR.findConstructor('feature')
  5379. * // => Array of constructor/module tuples
  5380. * ```
  5381. *
  5382. * With a function as query argument:
  5383. * ```ts
  5384. * const results = mR.findConstructor((constructor) => { constructor.prototype.value !== undefined })
  5385. * // => Array of constructor/module tuples
  5386. * ```
  5387. *
  5388. * Accessing the resulting data:
  5389. * ```ts
  5390. * // With array destructuring (ES6)
  5391. * const [constructor, module] = results[0]
  5392. *
  5393. * // ...or...
  5394. *
  5395. * // regular access
  5396. * const constructor = results[0][0]
  5397. * const module = results[0][1]
  5398. * ```
  5399. *
  5400. * @param query query to search the constructor list for
  5401. * @returns a list of constructor/module tuples fitting the query
  5402. */
  5403. findConstructor(query) {
  5404. const results = [];
  5405. const constructors = Object.keys(this.constructors);
  5406. if (constructors.length === 0) {
  5407. throw new Error('There are no constructors to search through!');
  5408. }
  5409. constructors.forEach((key) => {
  5410. const constructor = this.constructors[key];
  5411. try {
  5412. if (typeof query === 'string') {
  5413. query = query.toLowerCase();
  5414. if (constructor.toString().toLowerCase().includes(query))
  5415. results.push([this.constructors[key], this.modules[key]]);
  5416. }
  5417. else if (typeof query === 'function') {
  5418. if (query(constructor))
  5419. results.push([this.constructors[key], this.modules[key]]);
  5420. }
  5421. }
  5422. catch (err) {
  5423. this.log(`There was an error while searching through constructor '${key}':\n${err}\n${err.stack}`);
  5424. }
  5425. });
  5426. return results;
  5427. }
  5428. }
  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.  
  5611. class Utils {
  5612. windowApi;
  5613. constructor(option) {
  5614. this.windowApi = new WindowApi(option);
  5615. }
  5616. /** 版本号 */
  5617. version = "2025.6.7";
  5618. addStyle(cssText) {
  5619. if (typeof cssText !== "string") {
  5620. throw new Error("Utils.addStyle 参数cssText 必须为String类型");
  5621. }
  5622. let cssNode = this.windowApi.document.createElement("style");
  5623. cssNode.setAttribute("type", "text/css");
  5624. cssNode.innerHTML = cssText;
  5625. if (this.windowApi.document.head) {
  5626. /* 插入head最后 */
  5627. this.windowApi.document.head.appendChild(cssNode);
  5628. }
  5629. else if (this.windowApi.document.body) {
  5630. /* 插入body后 */
  5631. this.windowApi.document.body.appendChild(cssNode);
  5632. }
  5633. else if (this.windowApi.document.documentElement.childNodes.length === 0) {
  5634. /* 插入#html第一个元素后 */
  5635. this.windowApi.document.documentElement.appendChild(cssNode);
  5636. }
  5637. else {
  5638. /* 插入head前面 */
  5639. this.windowApi.document.documentElement.insertBefore(cssNode, this.windowApi.document.documentElement.childNodes[0]);
  5640. }
  5641. return cssNode;
  5642. }
  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
  5655. }
  5656. }
  5657. */
  5658. assign = commonUtil.assign.bind(commonUtil);
  5659. async asyncReplaceAll(string, pattern, asyncFn) {
  5660. let UtilsContext = this;
  5661. if (typeof string !== "string") {
  5662. throw new TypeError("string必须是字符串");
  5663. }
  5664. if (typeof asyncFn !== "function") {
  5665. throw new TypeError("asyncFn必须是函数");
  5666. }
  5667. let reg;
  5668. if (typeof pattern === "string") {
  5669. reg = new RegExp(UtilsContext.parseStringToRegExpString(pattern), "g");
  5670. }
  5671. else if (pattern instanceof RegExp) {
  5672. if (!pattern.global) {
  5673. throw new TypeError("pattern必须是全局匹配");
  5674. }
  5675. reg = new RegExp(pattern);
  5676. }
  5677. else {
  5678. throw new TypeError("pattern必须是正则对象");
  5679. }
  5680. let result = [];
  5681. let match;
  5682. let lastIndex = 0;
  5683. while ((match = reg.exec(string)) !== null) {
  5684. /* 异步获取匹配对应的字符串 */
  5685. const item = asyncFn(match[0]);
  5686. /* 获取该匹配项和上一个匹配项的中间的字符串 */
  5687. const prefix = string.slice(lastIndex, match.index);
  5688. lastIndex = match.index + match[0].length;
  5689. result.push(item);
  5690. result.push(prefix);
  5691. }
  5692. result.push(string.slice(lastIndex));
  5693. /* 等待所有异步完成 */
  5694. result = await Promise.all(result);
  5695. return result.join("");
  5696. }
  5697. /**
  5698. * ajax劫持库,支持xhr和fetch劫持。
  5699. * + 来源:https://bbs.tampermonkey.net.cn/thread-3284-1-1.html
  5700. * + 作者:cxxjackie
  5701. * + 版本:1.4.4
  5702. * + 旧版本:1.2.4
  5703. * + 文档:https://scriptcat.org/zh-CN/script-show-page/637/
  5704. * @param useOldVersion 是否使用旧版本,默认false
  5705. */
  5706. ajaxHooker = (useOldVersion = false) => {
  5707. if (useOldVersion) {
  5708. return AjaxHooker1_2_4();
  5709. }
  5710. else {
  5711. return AjaxHooker();
  5712. }
  5713. };
  5714. canvasClickByPosition(canvasElement, clientX = 0, clientY = 0, view = globalThis) {
  5715. if (!(canvasElement instanceof HTMLCanvasElement)) {
  5716. throw new Error("Utils.canvasClickByPosition 参数canvasElement必须是canvas元素");
  5717. }
  5718. clientX = parseInt(clientX.toString());
  5719. clientY = parseInt(clientY.toString());
  5720. const eventInit = {
  5721. cancelBubble: true,
  5722. cancelable: true,
  5723. clientX: clientX,
  5724. clientY: clientY,
  5725. // @ts-ignore
  5726. view: view,
  5727. detail: 1,
  5728. };
  5729. canvasElement.dispatchEvent(new MouseEvent("mousedown", eventInit));
  5730. canvasElement.dispatchEvent(new MouseEvent("mouseup", eventInit));
  5731. }
  5732. checkUserClickInNode(element) {
  5733. let UtilsContext = this;
  5734. if (!UtilsContext.isDOM(element)) {
  5735. throw new Error("Utils.checkUserClickInNode 参数 targetNode 必须为 Element|Node 类型");
  5736. }
  5737. let clickEvent = UtilsContext.windowApi.window.event;
  5738. let touchEvent = UtilsContext.windowApi.window.event;
  5739. let $click = clickEvent?.composedPath()?.[0];
  5740. // 点击的x坐标
  5741. let clickPosX = clickEvent?.clientX != null
  5742. ? clickEvent.clientX
  5743. : touchEvent.touches[0].clientX;
  5744. // 点击的y坐标
  5745. let clickPosY = clickEvent?.clientY != null
  5746. ? clickEvent.clientY
  5747. : touchEvent.touches[0].clientY;
  5748. let {
  5749. /* 要检测的元素的相对屏幕的横坐标最左边 */
  5750. left: elementPosXLeft,
  5751. /* 要检测的元素的相对屏幕的横坐标最右边 */
  5752. right: elementPosXRight,
  5753. /* 要检测的元素的相对屏幕的纵坐标最上边 */
  5754. top: elementPosYTop,
  5755. /* 要检测的元素的相对屏幕的纵坐标最下边 */
  5756. bottom: elementPosYBottom, } = element.getBoundingClientRect();
  5757. if (clickPosX >= elementPosXLeft &&
  5758. clickPosX <= elementPosXRight &&
  5759. clickPosY >= elementPosYTop &&
  5760. clickPosY <= elementPosYBottom) {
  5761. return true;
  5762. }
  5763. else if (($click && element.contains($click)) || $click == element) {
  5764. /* 这种情况是应对在界面中隐藏的元素,getBoundingClientRect获取的都是0 */
  5765. return true;
  5766. }
  5767. else {
  5768. return false;
  5769. }
  5770. }
  5771. /**
  5772. * 复制formData数据
  5773. * @param formData 需要clone的数据
  5774. */
  5775. cloneFormData(formData, filterFn) {
  5776. let clonedFormData = new FormData();
  5777. for (let [key, value] of formData.entries()) {
  5778. let isFilter = typeof filterFn === "function" ? filterFn(key, value) : false;
  5779. if (typeof isFilter === "boolean" && isFilter) {
  5780. continue;
  5781. }
  5782. clonedFormData.append(key, value);
  5783. }
  5784. return clonedFormData;
  5785. }
  5786. createOverload() {
  5787. let fnMap = new Map();
  5788. function overload(...args) {
  5789. let key = args.map((it) => typeof it).join(",");
  5790. let fn = fnMap.get(key);
  5791. if (!fn) {
  5792. throw new TypeError("没有找到对应的实现");
  5793. }
  5794. return fn.apply(this, args);
  5795. }
  5796. overload.addImpl = function (...args) {
  5797. let fn = args.pop();
  5798. if (typeof fn !== "function") {
  5799. throw new TypeError("最后一个参数必须是函数");
  5800. }
  5801. let key = args.join(",");
  5802. fnMap.set(key, fn);
  5803. };
  5804. return overload;
  5805. }
  5806. /**
  5807. * 颜色转换
  5808. * @returns
  5809. */
  5810. ColorConversion = ColorConversion;
  5811. /**
  5812. * 深拷贝
  5813. * @param obj 对象
  5814. */
  5815. deepClone = commonUtil.deepClone.bind(commonUtil);
  5816. debounce(fn, delay = 0) {
  5817. let timer = null;
  5818. let UtilsContext = this;
  5819. return function (...args) {
  5820. UtilsContext.workerClearTimeout(timer);
  5821. timer = UtilsContext.workerSetTimeout(function () {
  5822. fn.apply(UtilsContext, args);
  5823. }, delay);
  5824. };
  5825. }
  5826. deleteParentNode(element, targetSelector) {
  5827. let UtilsContext = this;
  5828. if (element == null) {
  5829. return;
  5830. }
  5831. if (!UtilsContext.isDOM(element)) {
  5832. throw new Error("Utils.deleteParentNode 参数 target 必须为 Node|HTMLElement 类型");
  5833. }
  5834. if (typeof targetSelector !== "string") {
  5835. throw new Error("Utils.deleteParentNode 参数 targetSelector 必须为 string 类型");
  5836. }
  5837. let result = false;
  5838. let needRemoveDOM = domUtils.closest(element, targetSelector);
  5839. if (needRemoveDOM) {
  5840. needRemoveDOM.remove();
  5841. result = true;
  5842. }
  5843. return result;
  5844. }
  5845. /**
  5846. * 字典
  5847. * @example
  5848. * let dictionary = new Utils.Dictionary();
  5849. * let dictionary2 = new Utils.Dictionary();
  5850. * dictionary.set("test","111");
  5851. * dictionary.get("test");
  5852. * > '111'
  5853. * dictionary.has("test");
  5854. * > true
  5855. * dictionary.concat(dictionary2);
  5856. **/
  5857. Dictionary = UtilsDictionary;
  5858. dispatchEvent(element, eventName, details) {
  5859. // let UtilsContext = this;
  5860. let eventNameList = [];
  5861. if (typeof eventName === "string") {
  5862. eventNameList = [eventName];
  5863. }
  5864. if (Array.isArray(eventName)) {
  5865. eventNameList = [...eventName];
  5866. }
  5867. eventNameList.forEach((_eventName_) => {
  5868. let event = new Event(_eventName_);
  5869. if (details) {
  5870. Object.assign(event, details);
  5871. }
  5872. element.dispatchEvent(event);
  5873. });
  5874. }
  5875. downloadBase64(base64Data, fileName, isIFrame = false) {
  5876. let UtilsContext = this;
  5877. if (typeof base64Data !== "string") {
  5878. throw new Error("Utils.downloadBase64 参数 base64Data 必须为 string 类型");
  5879. }
  5880. if (typeof fileName !== "string") {
  5881. throw new Error("Utils.downloadBase64 参数 fileName 必须为 string 类型");
  5882. }
  5883. if (isIFrame) {
  5884. /* 使用iframe */
  5885. const iframeElement = this.windowApi.document.createElement("iframe");
  5886. iframeElement.style.display = "none";
  5887. iframeElement.src = base64Data;
  5888. this.windowApi.document.body.appendChild(iframeElement);
  5889. UtilsContext.workerSetTimeout(() => {
  5890. iframeElement.contentWindow.document.execCommand("SaveAs", true, fileName);
  5891. this.windowApi.document.body.removeChild(iframeElement);
  5892. }, 100);
  5893. }
  5894. else {
  5895. /* 使用A标签 */
  5896. const linkElement = this.windowApi.document.createElement("a");
  5897. linkElement.setAttribute("target", "_blank");
  5898. linkElement.download = fileName;
  5899. linkElement.href = base64Data;
  5900. linkElement.click();
  5901. }
  5902. }
  5903. findWebPageVisibleText(str = "", caseSensitive = false) {
  5904. let TRange = null;
  5905. let strFound;
  5906. if (this.windowApi.globalThis.find) {
  5907. /* CODE FOR BROWSERS THAT SUPPORT window.find */
  5908. let windowFind = this.windowApi.self.find;
  5909. strFound = windowFind(str, caseSensitive, true, true, false);
  5910. if (strFound &&
  5911. this.windowApi.self.getSelection &&
  5912. !this.windowApi.self.getSelection().anchorNode) {
  5913. strFound = windowFind(str, caseSensitive, true, true, false);
  5914. }
  5915. if (!strFound) {
  5916. strFound = windowFind(str, 0, 1);
  5917. while (windowFind(str, 0, 1))
  5918. continue;
  5919. }
  5920. }
  5921. else if (navigator.appName.indexOf("Microsoft") != -1) {
  5922. /* EXPLORER-SPECIFIC CODE */
  5923. if (TRange != null) {
  5924. TRange = TRange;
  5925. TRange.collapse(false);
  5926. strFound = TRange.findText(str);
  5927. if (strFound)
  5928. TRange.select();
  5929. }
  5930. if (TRange == null || strFound == 0) {
  5931. TRange = this.windowApi.self.document.body.createTextRange();
  5932. strFound = TRange.findText(str);
  5933. if (strFound)
  5934. TRange.select();
  5935. }
  5936. }
  5937. else if (navigator.appName == "Opera") {
  5938. alert("Opera browsers not supported, sorry...");
  5939. return;
  5940. }
  5941. return strFound ? true : false;
  5942. }
  5943. *findElementsWithText(element, text, filter) {
  5944. let that = this;
  5945. if (element.outerHTML.includes(text)) {
  5946. if (element.children.length === 0) {
  5947. let filterResult = typeof filter === "function" ? filter(element) : false;
  5948. if (!filterResult) {
  5949. yield element;
  5950. }
  5951. }
  5952. else {
  5953. let textElement = Array.from(element.childNodes).filter((ele) => ele.nodeType === Node.TEXT_NODE);
  5954. for (let ele of textElement) {
  5955. if (ele.textContent.includes(text)) {
  5956. let filterResult = typeof filter === "function" ? filter(element) : false;
  5957. if (!filterResult) {
  5958. yield ele;
  5959. }
  5960. }
  5961. }
  5962. }
  5963. }
  5964. for (let index = 0; index < element.children.length; index++) {
  5965. let childElement = element.children[index];
  5966. yield* that.findElementsWithText(childElement, text, filter);
  5967. }
  5968. }
  5969. /**
  5970. * 判断该元素是否可见,如果不可见,向上找它的父元素直至找到可见的元素
  5971. * @param element
  5972. * @example
  5973. * let visibleElement = Utils.findVisibleElement(document.querySelector("a.xx"));
  5974. * > <HTMLElement>
  5975. */
  5976. findVisibleElement(element) {
  5977. let currentElement = element;
  5978. while (currentElement) {
  5979. let elementRect = currentElement.getBoundingClientRect();
  5980. if (Boolean(elementRect.length)) {
  5981. return currentElement;
  5982. }
  5983. currentElement = currentElement.parentElement;
  5984. }
  5985. return null;
  5986. }
  5987. formatByteToSize(byteSize, addType = true) {
  5988. byteSize = parseInt(byteSize.toString());
  5989. if (isNaN(byteSize)) {
  5990. throw new Error("Utils.formatByteToSize 参数 byteSize 格式不正确");
  5991. }
  5992. let result = 0;
  5993. let resultType = "KB";
  5994. let sizeData = {};
  5995. sizeData.B = 1;
  5996. sizeData.KB = 1024;
  5997. sizeData.MB = sizeData.KB * sizeData.KB;
  5998. sizeData.GB = sizeData.MB * sizeData.KB;
  5999. sizeData.TB = sizeData.GB * sizeData.KB;
  6000. sizeData.PB = sizeData.TB * sizeData.KB;
  6001. sizeData.EB = sizeData.PB * sizeData.KB;
  6002. sizeData.ZB = sizeData.EB * sizeData.KB;
  6003. sizeData.YB = sizeData.ZB * sizeData.KB;
  6004. sizeData.BB = sizeData.YB * sizeData.KB;
  6005. sizeData.NB = sizeData.BB * sizeData.KB;
  6006. sizeData.DB = sizeData.NB * sizeData.KB;
  6007. for (let key in sizeData) {
  6008. result = byteSize / sizeData[key];
  6009. resultType = key;
  6010. if (sizeData.KB >= result) {
  6011. break;
  6012. }
  6013. }
  6014. result = result.toFixed(2);
  6015. result = addType
  6016. ? result + resultType.toString()
  6017. : parseFloat(result.toString());
  6018. return result;
  6019. }
  6020. getNodeListValue(...args) {
  6021. let resultArray = [];
  6022. for (let arg of args) {
  6023. let value = arg;
  6024. if (typeof arg === "function") {
  6025. /* 方法 */
  6026. value = arg();
  6027. }
  6028. if (value.length !== 0) {
  6029. resultArray = [...value];
  6030. break;
  6031. }
  6032. }
  6033. return resultArray;
  6034. }
  6035. getNonNullValue(...args) {
  6036. let resultValue = args[args.length - 1];
  6037. let UtilsContext = this;
  6038. for (const argValue of args) {
  6039. if (UtilsContext.isNotNull(argValue)) {
  6040. resultValue = argValue;
  6041. break;
  6042. }
  6043. }
  6044. return resultValue;
  6045. }
  6046. formatTime(text = new Date(), formatType = "yyyy-MM-dd HH:mm:ss") {
  6047. let time = text == null ? new Date() : new Date(text);
  6048. /**
  6049. * 校验时间补0
  6050. * @param timeNum
  6051. * @returns
  6052. */
  6053. function checkTime(timeNum) {
  6054. if (timeNum < 10)
  6055. return "0" + timeNum;
  6056. return timeNum;
  6057. }
  6058. /**
  6059. * 时间制修改 24小时制转12小时制
  6060. * @param hourNum 小时
  6061. * @returns
  6062. */
  6063. function timeSystemChange(hourNum) {
  6064. return hourNum > 12 ? hourNum - 12 : hourNum;
  6065. }
  6066. let timeRegexp = {
  6067. yyyy: time.getFullYear(),
  6068. /* 年 */
  6069. MM: checkTime(time.getMonth() + 1),
  6070. /* 月 */
  6071. dd: checkTime(time.getDate()),
  6072. /* 日 */
  6073. HH: checkTime(time.getHours()),
  6074. /* 时 (24小时制) */
  6075. hh: checkTime(timeSystemChange(time.getHours())),
  6076. /* 时 (12小时制) */
  6077. mm: checkTime(time.getMinutes()),
  6078. /* 分 */
  6079. ss: checkTime(time.getSeconds()),
  6080. /* 秒 */
  6081. };
  6082. Object.keys(timeRegexp).forEach(function (key) {
  6083. let replaecRegexp = new RegExp(key, "g");
  6084. formatType = formatType.replace(replaecRegexp, timeRegexp[key]);
  6085. });
  6086. return formatType;
  6087. }
  6088. formatToTimeStamp(text) {
  6089. /* 把字符串格式的时间(完整,包括日期和时间)格式化成时间 */
  6090. if (typeof text !== "string") {
  6091. throw new Error("Utils.formatToTimeStamp 参数 text 必须为 string 类型");
  6092. }
  6093. if (text.length === 8) {
  6094. /* 该字符串只有时分秒 */
  6095. let today = new Date();
  6096. text =
  6097. today.getFullYear() +
  6098. "-" +
  6099. (today.getMonth() + 1) +
  6100. "-" +
  6101. today.getDate() +
  6102. " " +
  6103. text;
  6104. }
  6105. text = text.substring(0, 19);
  6106. text = text.replace(/-/g, "/");
  6107. let timestamp = new Date(text).getTime();
  6108. return timestamp;
  6109. }
  6110. /**
  6111. * gbk格式的url编码,来自https://greasyfork.org/zh-CN/scripts/427726-gbk-url-js
  6112. * @example
  6113. * let gbkEncoder = new Utils.GBKEncoder();
  6114. * gbkEncoder.encode("测试");
  6115. * > '%B2%E2%CA%D4'
  6116. * gbkEncoder.decode("%B2%E2%CA%D4");
  6117. * > 测试
  6118. */
  6119. GBKEncoder = GBKEncoder;
  6120. /**
  6121. * 获取 transitionend 的在各个浏览器的兼容名
  6122. */
  6123. getTransitionEndNameList() {
  6124. return [
  6125. "webkitTransitionEnd",
  6126. "mozTransitionEnd",
  6127. "MSTransitionEnd",
  6128. "otransitionend",
  6129. "transitionend",
  6130. ];
  6131. }
  6132. /**
  6133. * 获取 animationend 的在各个浏览器的兼容名
  6134. */
  6135. getAnimationEndNameList() {
  6136. return [
  6137. "webkitAnimationEnd",
  6138. "mozAnimationEnd",
  6139. "MSAnimationEnd",
  6140. "oanimationend",
  6141. "animationend",
  6142. ];
  6143. }
  6144. getArrayLastValue(targetObj) {
  6145. return targetObj[targetObj.length - 1];
  6146. }
  6147. getArrayRealValue(...args) {
  6148. let result = null;
  6149. for (let arg of args) {
  6150. if (typeof arg === "function") {
  6151. /* 方法 */
  6152. arg = arg();
  6153. }
  6154. if (arg != null) {
  6155. result = arg;
  6156. break;
  6157. }
  6158. }
  6159. return result;
  6160. }
  6161. getDaysDifference(timestamp1 = Date.now(), timestamp2 = Date.now(), type = "天") {
  6162. type = type.trim();
  6163. if (timestamp1.toString().length === 10) {
  6164. timestamp1 = timestamp1 * 1000;
  6165. }
  6166. if (timestamp2.toString().length === 10) {
  6167. timestamp2 = timestamp2 * 1000;
  6168. }
  6169. let smallTimeStamp = timestamp1 > timestamp2 ? timestamp2 : timestamp1;
  6170. let bigTimeStamp = timestamp1 > timestamp2 ? timestamp1 : timestamp2;
  6171. let oneSecond = 1000; /* 一秒的毫秒数 */
  6172. let oneMinute = 60 * oneSecond; /* 一分钟的毫秒数 */
  6173. let oneHour = 60 * oneMinute; /* 一小时的毫秒数 */
  6174. let oneDay = 24 * oneHour; /* 一天的毫秒数 */
  6175. let oneMonth = 30 * oneDay; /* 一个月的毫秒数(30天) */
  6176. let oneYear = 12 * oneMonth; /* 一年的毫秒数 */
  6177. let bigDate = new Date(bigTimeStamp);
  6178. let smallDate = new Date(smallTimeStamp);
  6179. let remainderValue = 1;
  6180. if (type === "年") {
  6181. remainderValue = oneYear;
  6182. }
  6183. else if (type === "月") {
  6184. remainderValue = oneMonth;
  6185. }
  6186. else if (type === "天") {
  6187. remainderValue = oneDay;
  6188. }
  6189. else if (type === "时") {
  6190. remainderValue = oneHour;
  6191. }
  6192. else if (type === "分") {
  6193. remainderValue = oneMinute;
  6194. }
  6195. else if (type === "秒") {
  6196. remainderValue = oneSecond;
  6197. }
  6198. let diffValue = Math.round(Math.abs((bigDate - smallDate) / remainderValue));
  6199. if (type === "auto") {
  6200. let timeDifference = bigTimeStamp - smallTimeStamp;
  6201. diffValue = Math.floor(timeDifference / (24 * 3600 * 1000));
  6202. if (diffValue > 0) {
  6203. diffValue = diffValue + "天";
  6204. }
  6205. else {
  6206. /* 计算出小时数 */
  6207. let leave1 = timeDifference % (24 * 3600 * 1000); /* 计算天数后剩余的毫秒数 */
  6208. let hours = Math.floor(leave1 / (3600 * 1000));
  6209. if (hours > 0) {
  6210. diffValue = hours + "小时";
  6211. }
  6212. else {
  6213. /* 计算相差分钟数 */
  6214. let leave2 = leave1 % (3600 * 1000); /* 计算小时数后剩余的毫秒数 */
  6215. let minutes = Math.floor(leave2 / (60 * 1000));
  6216. if (minutes > 0) {
  6217. diffValue = minutes + "分钟";
  6218. }
  6219. else {
  6220. /* 计算相差秒数 */
  6221. let leave3 = leave2 % (60 * 1000); /* 计算分钟数后剩余的毫秒数 */
  6222. let seconds = Math.round(leave3 / 1000);
  6223. diffValue = seconds + "秒";
  6224. }
  6225. }
  6226. }
  6227. }
  6228. return diffValue;
  6229. }
  6230. getElementSelector(element) {
  6231. let UtilsContext = this;
  6232. // @ts-ignore
  6233. if (!element)
  6234. return;
  6235. // @ts-ignore
  6236. if (!element.parentElement)
  6237. return;
  6238. /* 如果元素有id属性,则直接返回id选择器 */
  6239. if (element.id)
  6240. return "#" + element.id;
  6241. /* 递归地获取父元素的选择器 */
  6242. let selector = UtilsContext.getElementSelector(element.parentElement);
  6243. if (!selector) {
  6244. return element.tagName.toLowerCase();
  6245. }
  6246. /* 如果有多个相同类型的兄弟元素,则需要添加索引 */
  6247. if (element.parentElement.querySelectorAll(element.tagName).length > 1) {
  6248. let index = Array.prototype.indexOf.call(element.parentElement.children, element) +
  6249. 1;
  6250. selector +=
  6251. " > " + element.tagName.toLowerCase() + ":nth-child(" + index + ")";
  6252. }
  6253. else {
  6254. selector += " > " + element.tagName.toLowerCase();
  6255. }
  6256. return selector;
  6257. }
  6258. /**
  6259. * 获取最大值
  6260. * @example
  6261. * Utils.getMaxValue([{1:123},{2:345},{3:456}],(index,value)=>{return parseInt(index)})
  6262. * > 2
  6263. */
  6264. getMaxValue(...args) {
  6265. let result = [...args];
  6266. let newResult = [];
  6267. if (result.length === 0) {
  6268. // @ts-ignore
  6269. return;
  6270. }
  6271. if (result.length > 1) {
  6272. if (result.length === 2 &&
  6273. typeof result[0] === "object" &&
  6274. typeof result[1] === "function") {
  6275. let data = result[0];
  6276. let handleDataFunc = result[1];
  6277. Object.keys(data).forEach((keyName) => {
  6278. newResult = [...newResult, handleDataFunc(keyName, data[keyName])];
  6279. });
  6280. }
  6281. else {
  6282. result.forEach((item) => {
  6283. if (!isNaN(parseFloat(item))) {
  6284. newResult = [...newResult, parseFloat(item)];
  6285. }
  6286. });
  6287. }
  6288. return Math.max(...newResult);
  6289. }
  6290. else {
  6291. result[0].forEach((item) => {
  6292. if (!isNaN(parseFloat(item))) {
  6293. newResult = [...newResult, parseFloat(item)];
  6294. }
  6295. });
  6296. return Math.max(...newResult);
  6297. }
  6298. }
  6299. getMaxZIndexNodeInfo(deviation = 1, target = this.windowApi.document, ignoreCallBack) {
  6300. deviation = Number.isNaN(deviation) ? 1 : deviation;
  6301. const UtilsContext = this;
  6302. // 最大值 2147483647
  6303. // const maxZIndex = Math.pow(2, 31) - 1;
  6304. // 比较值 2000000000
  6305. const maxZIndexCompare = 2 * Math.pow(10, 9);
  6306. // 当前页面最大的z-index
  6307. let zIndex = 0;
  6308. // 当前的最大z-index的元素,调试使用
  6309. // @ts-ignore
  6310. let maxZIndexNode = null;
  6311. /**
  6312. * 元素是否可见
  6313. * @param $css
  6314. */
  6315. function isVisibleNode($css) {
  6316. return $css.position !== "static" && $css.display !== "none";
  6317. }
  6318. /**
  6319. * 查询元素的z-index
  6320. * 并比较值是否是已获取的最大值
  6321. * @param $ele
  6322. */
  6323. function queryMaxZIndex($ele) {
  6324. if (typeof ignoreCallBack === "function") {
  6325. let ignoreResult = ignoreCallBack($ele);
  6326. if (typeof ignoreResult === "boolean" && !ignoreResult) {
  6327. return;
  6328. }
  6329. }
  6330. /** 元素的样式 */
  6331. const nodeStyle = UtilsContext.windowApi.window.getComputedStyle($ele);
  6332. /* 不对position为static和display为none的元素进行获取它们的z-index */
  6333. if (isVisibleNode(nodeStyle)) {
  6334. let nodeZIndex = parseInt(nodeStyle.zIndex);
  6335. if (!isNaN(nodeZIndex)) {
  6336. if (nodeZIndex > zIndex) {
  6337. // 赋值到全局
  6338. zIndex = nodeZIndex;
  6339. maxZIndexNode = $ele;
  6340. }
  6341. }
  6342. // 判断shadowRoot
  6343. if ($ele.shadowRoot != null && $ele instanceof ShadowRoot) {
  6344. $ele.shadowRoot.querySelectorAll("*").forEach(($shadowEle) => {
  6345. queryMaxZIndex($shadowEle);
  6346. });
  6347. }
  6348. }
  6349. }
  6350. target.querySelectorAll("*").forEach(($ele, index) => {
  6351. queryMaxZIndex($ele);
  6352. });
  6353. zIndex += deviation;
  6354. if (zIndex >= maxZIndexCompare) {
  6355. // 最好不要超过最大值
  6356. zIndex = maxZIndexCompare;
  6357. }
  6358. return {
  6359. node: maxZIndexNode,
  6360. zIndex: zIndex,
  6361. };
  6362. }
  6363. getMaxZIndex(deviation = 1, target = this.windowApi.document, ignoreCallBack) {
  6364. return this.getMaxZIndexNodeInfo(deviation, target, ignoreCallBack).zIndex;
  6365. }
  6366. getMinValue(...args) {
  6367. let result = [...args];
  6368. let newResult = [];
  6369. if (result.length === 0) {
  6370. // @ts-ignore
  6371. return;
  6372. }
  6373. if (result.length > 1) {
  6374. if (result.length === 2 &&
  6375. typeof result[0] === "object" &&
  6376. typeof result[1] === "function") {
  6377. let data = result[0];
  6378. let handleDataFunc = result[1];
  6379. Object.keys(data).forEach((keyName) => {
  6380. newResult = [...newResult, handleDataFunc(keyName, data[keyName])];
  6381. });
  6382. }
  6383. else {
  6384. result.forEach((item) => {
  6385. if (!isNaN(parseFloat(item))) {
  6386. newResult = [...newResult, parseFloat(item)];
  6387. }
  6388. });
  6389. }
  6390. return Math.min(...newResult);
  6391. }
  6392. else {
  6393. result[0].forEach((item) => {
  6394. if (!isNaN(parseFloat(item))) {
  6395. newResult = [...newResult, parseFloat(item)];
  6396. }
  6397. });
  6398. return Math.min(...newResult);
  6399. }
  6400. }
  6401. /**
  6402. * 获取随机的安卓手机User-Agent
  6403. * @example
  6404. * Utils.getRandomAndroidUA();
  6405. * > 'Mozilla/5.0 (Linux; Android 10; MI 13 Build/OPR1.170623.027; wv) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.3490.40 Mobile Safari/537.36'
  6406. **/
  6407. getRandomAndroidUA() {
  6408. let UtilsContext = this;
  6409. let mobileNameList = [
  6410. "LDN-LX3",
  6411. "RNE-L03",
  6412. "ASUS_X00ID Build/NMF26F",
  6413. "WAS-LX3",
  6414. "PRA-LX3",
  6415. "MYA-L03",
  6416. "Moto G Play",
  6417. "Moto C Build/NRD90M.063",
  6418. "Redmi Note 4 Build/NRD90M",
  6419. "HUAWEI VNS-L21 Build/HUAWEIVNS-L21",
  6420. "VTR-L09",
  6421. "TRT-LX3",
  6422. "M2003J15SC Build/RP1A.200720.011; wv",
  6423. "MI 13 Build/OPR1.170623.027; wv",
  6424. ];
  6425. /* 安卓版本 */
  6426. let androidVersion = UtilsContext.getRandomValue(12, 14);
  6427. /* 手机型号 */
  6428. let randomMobile = UtilsContext.getRandomValue(mobileNameList);
  6429. /* chrome大版本号 */
  6430. let chromeVersion1 = UtilsContext.getRandomValue(120, 132);
  6431. let chromeVersion2 = UtilsContext.getRandomValue(0, 0);
  6432. let chromeVersion3 = UtilsContext.getRandomValue(2272, 6099);
  6433. let chromeVersion4 = UtilsContext.getRandomValue(1, 218);
  6434. return `Mozilla/5.0 (Linux; Android ${androidVersion}; ${randomMobile}) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/${chromeVersion1}.${chromeVersion2}.${chromeVersion3}.${chromeVersion4} Mobile Safari/537.36`;
  6435. }
  6436. /**
  6437. * 获取随机的电脑端User-Agent
  6438. * + Mozilla/5.0:以前用于Netscape浏览器,目前大多数浏览器UA都会带有
  6439. * + Windows NT 13:代表Window11系统
  6440. * + Windows NT 10.0:代表Window10系统
  6441. * + Windows NT 6.1:代表windows7系统
  6442. * + WOW64:Windows-on-Windows 64-bit,32位的应用程序运行于此64位处理器上
  6443. * + Win64:64位
  6444. * + AppleWebKit/537.36:浏览器内核
  6445. * + KHTML:HTML排版引擎
  6446. * + like Gecko:这不是Geckeo 浏览器,但是运行起来像Geckeo浏览器
  6447. * + Chrome/106.0.5068.19:Chrome版本号
  6448. * + Safari/537.36:宣称自己是Safari?
  6449. * @returns 返回随机字符串
  6450. * @example
  6451. * Utils.getRandomPCUA();
  6452. * > 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.5068.19 Safari/537.36'
  6453. **/
  6454. getRandomPCUA() {
  6455. let UtilsContext = this;
  6456. /* chrome大版本号 */
  6457. let chromeVersion1 = UtilsContext.getRandomValue(120, 132);
  6458. let chromeVersion2 = UtilsContext.getRandomValue(0, 0);
  6459. let chromeVersion3 = UtilsContext.getRandomValue(2272, 6099);
  6460. let chromeVersion4 = UtilsContext.getRandomValue(1, 218);
  6461. return `Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/${chromeVersion1}.${chromeVersion2}.${chromeVersion3}.${chromeVersion4} Safari/537.36`;
  6462. }
  6463. getRandomValue(...args) {
  6464. let result = [...args];
  6465. if (result.length > 1) {
  6466. if (result.length === 2 &&
  6467. typeof result[0] === "number" &&
  6468. typeof result[1] === "number") {
  6469. let leftNumber = result[0] > result[1] ? result[1] : result[0];
  6470. let rightNumber = result[0] > result[1] ? result[0] : result[1];
  6471. return (Math.round(Math.random() * (rightNumber - leftNumber)) + leftNumber);
  6472. }
  6473. else {
  6474. return result[Math.floor(Math.random() * result.length)];
  6475. }
  6476. }
  6477. else if (result.length === 1) {
  6478. let paramData = result[0];
  6479. if (Array.isArray(paramData)) {
  6480. return paramData[Math.floor(Math.random() * paramData.length)];
  6481. }
  6482. else if (typeof paramData === "object" &&
  6483. Object.keys(paramData).length > 0) {
  6484. let paramObjDataKey = Object.keys(paramData)[Math.floor(Math.random() * Object.keys(paramData).length)];
  6485. return paramData[paramObjDataKey];
  6486. }
  6487. else {
  6488. return paramData;
  6489. }
  6490. }
  6491. }
  6492. /**
  6493. * 获取元素上的使用React框架的实例属性,目前包括reactFiber、reactProps、reactEvents、reactEventHandlers、reactInternalInstance
  6494. * @param element 需要获取的目标元素
  6495. * @returns
  6496. * @example
  6497. * Utils.getReactObj(document.querySelector("input"))?.reactProps?.onChange({target:{value:"123"}});
  6498. */
  6499. getReactObj(element) {
  6500. let result = {};
  6501. Object.keys(element).forEach((domPropsName) => {
  6502. if (domPropsName.startsWith("__react")) {
  6503. let propsName = domPropsName.replace(/__(.+)\$.+/i, "$1");
  6504. if (propsName in result) ;
  6505. else {
  6506. result[propsName] = element[domPropsName];
  6507. }
  6508. }
  6509. });
  6510. return result;
  6511. }
  6512. /**
  6513. * 获取对象上的Symbol属性,如果没设置keyName,那么返回一个对象,对象是所有遍历到的Symbol对象
  6514. * @param target 目标对象
  6515. * @param keyName (可选)Symbol名或者Symbol对象
  6516. */
  6517. getSymbol(target, keyName) {
  6518. if (typeof target !== "object") {
  6519. throw new TypeError("target不是一个对象");
  6520. }
  6521. let objectsSymbols = Object.getOwnPropertySymbols(target);
  6522. if (typeof keyName === "string") {
  6523. let findSymbol = objectsSymbols.find((key) => {
  6524. return key.toString() === keyName;
  6525. });
  6526. if (findSymbol) {
  6527. return target[findSymbol];
  6528. }
  6529. }
  6530. else if (typeof keyName === "symbol") {
  6531. let findSymbol = objectsSymbols.find((key) => {
  6532. return key === keyName;
  6533. });
  6534. if (findSymbol) {
  6535. return target[findSymbol];
  6536. }
  6537. }
  6538. else {
  6539. let result = {};
  6540. objectsSymbols.forEach((item) => {
  6541. result[item] = target[item];
  6542. });
  6543. return result;
  6544. }
  6545. }
  6546. /**
  6547. * 获取文本的字符长度
  6548. * @param text
  6549. * @example
  6550. * Utils.getTextLength("测试文本")
  6551. * > 12
  6552. */
  6553. getTextLength(text) {
  6554. let encoder = new TextEncoder();
  6555. let bytes = encoder.encode(text);
  6556. return bytes.length;
  6557. }
  6558. getTextStorageSize(text, addType = true) {
  6559. let UtilsContext = this;
  6560. return UtilsContext.formatByteToSize(UtilsContext.getTextLength(text), addType);
  6561. }
  6562. getThunderUrl(url) {
  6563. if (url == null) {
  6564. throw new TypeError("url不能为空");
  6565. }
  6566. if (typeof url !== "string") {
  6567. throw new TypeError("url必须是string类型");
  6568. }
  6569. if (url.trim() === "") {
  6570. throw new TypeError("url不能为空字符串或纯空格");
  6571. }
  6572. return `thunder://${this.windowApi.globalThis.btoa("AA" + url + "ZZ")}`;
  6573. }
  6574. /**
  6575. * 对于GM_cookie的兼容写法,当无法使用GM_cookie时可以使用这个,但是并不完全兼容,有些写不出来且限制了httponly是无法访问的
  6576. * @example
  6577. let GM_cookie = new Utils.GM_Cookie();
  6578. GM_cookie.list({name:"xxx_cookie_xxx"},function(cookies,error){
  6579. if (!error) {
  6580. console.log(cookies);
  6581. console.log(cookies.value);
  6582. } else {
  6583. console.error(error);
  6584. }
  6585. });
  6586. GM_cookie.set({name:"xxx_cookie_test_xxx",value:"这是Cookie测试值"},function(error){
  6587. if (error) {
  6588. console.error(error);
  6589. } else {
  6590. console.log('Cookie set successfully.');
  6591. }
  6592. })
  6593. GM_cookie.delete({name:"xxx_cookie_test_xxx"},function(error){
  6594. if (error) {
  6595. console.error(error);
  6596. } else {
  6597. console.log('Cookie set successfully.');
  6598. }
  6599. })
  6600. **/
  6601. GM_Cookie = UtilsGMCookie;
  6602. /**
  6603. * 注册油猴菜单,要求本地存储的键名不能存在其它键名`GM_Menu_Local_Map`会冲突/覆盖
  6604. * @example
  6605. let GM_Menu = new Utils.GM_Menu({
  6606. data: [
  6607. {
  6608. menu_key: "menu_key",
  6609. text: "测试按钮",
  6610. enable: true,
  6611. accessKey: "a",
  6612. autoClose: false,
  6613. showText(text, enable) {
  6614. return "[" + (enable ? "√" : "×") + "]" + text;
  6615. },
  6616. callback(data) {
  6617. console.log("点击菜单,值修改为", data.enable);
  6618. },
  6619. },
  6620. ],
  6621. autoReload: false,
  6622. GM_getValue,
  6623. GM_setValue,
  6624. GM_registerMenuCommand,
  6625. GM_unregisterMenuCommand,
  6626. });
  6627. // 获取某个菜单项的值
  6628. GM_Menu.get("menu_key");
  6629. > true
  6630. // 获取某个菜单项的开启/关闭后显示的文本
  6631. GM_Menu.getShowTextValue("menu_key");
  6632. > √测试按钮
  6633. // 添加键为menu_key2的菜单项
  6634. GM_Menu.add({
  6635. key:"menu_key2",
  6636. text: "测试按钮2",
  6637. enable: false,
  6638. showText(text,enable){
  6639. return "[" + (enable ? "√" : "×") + "]" + text;
  6640. },
  6641. callback(data){
  6642. console.log("点击菜单,值修改为",data.enable);
  6643. }
  6644. });
  6645. // 使用数组的方式添加多个菜单,如menu_key3、menu_key4
  6646. GM_Menu.add([
  6647. {
  6648. key:"menu_key3",
  6649. text: "测试按钮3",
  6650. enable: false,
  6651. showText(text,enable){
  6652. return "[" + (enable ? "√" : "×") + "]" + text;
  6653. },
  6654. callback(data){
  6655. console.log("点击菜单,值修改为",data.enable);
  6656. }
  6657. },
  6658. {
  6659. key:"menu_key4",
  6660. text: "测试按钮4",
  6661. enable: false,
  6662. showText(text,enable){
  6663. return "[" + (enable ? "√" : "×") + "]" + text;
  6664. },
  6665. callback(data){
  6666. console.log("点击菜单,值修改为",data.enable);
  6667. }
  6668. }
  6669. ]);
  6670. // 更新键为menu_key的显示文字和点击回调
  6671. GM_Menu.update({
  6672. menu_key:{
  6673. text: "更新后的测试按钮",
  6674. enable: true,
  6675. showText(text,enable){
  6676. return "[" + (enable ? "√" : "×") + "]" + text;
  6677. },
  6678. callback(data){
  6679. console.log("点击菜单更新后的测试按钮,新值修改为",data.enable);
  6680. }
  6681. }
  6682. });
  6683. // 删除键为menu_key的菜单
  6684. GM_Menu.delete("menu_key");
  6685. **/
  6686. GM_Menu = GMMenu;
  6687. /**
  6688. * 基于Function prototype,能够勾住和释放任何函数
  6689. *
  6690. * .hook
  6691. * + realFunc {string} 用于保存原始函数的函数名称,用于unHook
  6692. * + hookFunc {string} 替换的hook函数
  6693. * + context {object} 目标函数所在对象,用于hook非window对象下的函数,如String.protype.slice,carInstance1
  6694. * + methodName {string} 匿名函数需显式传入目标函数名eg:this.Begin = function(){....};}
  6695. *
  6696. * .unhook
  6697. * + realFunc {string} 用于保存原始函数的函数名称,用于unHook
  6698. * + funcName {string} 被Hook的函数名称
  6699. * + context {object} 目标函数所在对象,用于hook非window对象下的函数,如String.protype.slice,carInstance1
  6700. * @example
  6701. let hook = new Utils.Hooks();
  6702. hook.initEnv();
  6703. function myFunction(){
  6704. console.log("我自己需要执行的函数");
  6705. }
  6706. function testFunction(){
  6707. console.log("正常执行的函数");
  6708. }
  6709. testFunction.hook(testFunction,myFunction,window);
  6710. **/
  6711. Hooks = Hooks;
  6712. /**
  6713. * 为减少代码量和回调,把GM_xmlhttpRequest封装
  6714. * 文档地址: https://www.tampermonkey.net/documentation.php?ext=iikm
  6715. * 其中onloadstart、onprogress、onreadystatechange是回调形式,onabort、ontimeout、onerror可以设置全局回调函数
  6716. * @param _GM_xmlHttpRequest_ 油猴中的GM_xmlhttpRequest
  6717. * @example
  6718. let httpx = new Utils.Httpx(GM_xmlhttpRequest);
  6719. let postResp = await httpx.post({
  6720. url:url,
  6721. data:JSON.stringify({
  6722. test:1
  6723. }),
  6724. timeout: 5000
  6725. });
  6726. console.log(postResp);
  6727. > {
  6728. status: true,
  6729. data: {responseText: "...", response: xxx,...},
  6730. msg: "请求完毕",
  6731. type: "onload",
  6732. }
  6733. if(postResp === "onload" && postResp.status){
  6734. // onload
  6735. }else if(postResp === "ontimeout"){
  6736. // ontimeout
  6737. }
  6738. * @example
  6739. // 也可以先配置全局参数
  6740. let httpx = new Utils.Httpx(GM_xmlhttpRequest);
  6741. httpx.config({
  6742. timeout: 5000,
  6743. async: false,
  6744. responseType: "html",
  6745. redirect: "follow",
  6746. })
  6747. // 优先级为 默认details < 全局details < 单独的details
  6748. */
  6749. Httpx = Httpx;
  6750. /**
  6751. * 浏览器端的indexedDB操作封装
  6752. * @example
  6753. let db = new Utils.indexedDB('web_DB', 'nav_text')
  6754. let data = {name:'管理员', roleId: 1, type: 1};
  6755. db.save('list',data).then((resolve)=>{
  6756. console.log(resolve,'存储成功')
  6757. })
  6758. db.get('list').then((resolve)=>{
  6759. console.log(resolve,'查询成功')
  6760. })
  6761. db.getPaging('list',20,10).then((resolve)=>{
  6762. console.log(resolve,'查询分页偏移第20,一共10行成功');
  6763. })
  6764. db.delete('list').then(resolve=>{
  6765. console.log(resolve,'删除成功---->>>>>>name')
  6766. })
  6767. db.deleteAll().then(resolve=>{
  6768. console.log(resolve,'清除数据库---->>>>>>name')
  6769. })
  6770. **/
  6771. indexedDB = indexedDB;
  6772. isNativeFunc(target) {
  6773. return Boolean(target.toString().match(/^function .*\(\) { \[native code\] }$/));
  6774. }
  6775. isNearBottom(...args) {
  6776. let nearBottomHeight = 50;
  6777. let checkWindow = () => {
  6778. // 已滚动的距离
  6779. let scrollTop = this.windowApi.window.pageYOffset ||
  6780. this.windowApi.document.documentElement.scrollTop;
  6781. // 视窗高度
  6782. let viewportHeight = this.windowApi.window.innerHeight ||
  6783. this.windowApi.document.documentElement.clientHeight;
  6784. // 最大滚动距离
  6785. let maxScrollHeight = this.windowApi.document.documentElement.scrollHeight - nearBottomHeight;
  6786. return scrollTop + viewportHeight >= maxScrollHeight;
  6787. };
  6788. let checkNode = ($ele) => {
  6789. // 已滚动的距离
  6790. let scrollTop = $ele.scrollTop;
  6791. // 视窗高度
  6792. let viewportHeight = $ele.clientHeight;
  6793. // 最大滚动距离
  6794. let maxScrollHeight = $ele.scrollHeight - viewportHeight - nearBottomHeight;
  6795. return scrollTop >= maxScrollHeight;
  6796. };
  6797. let firstArg = args[0];
  6798. if (args.length === 0 || typeof args[0] === "number") {
  6799. // nearBottomHeight
  6800. //
  6801. return checkWindow();
  6802. }
  6803. else if (typeof args[0] === "object" && args[0] instanceof HTMLElement) {
  6804. // target
  6805. // target,nearBottomHeight
  6806. if (typeof args[1] === "number" && !Number.isNaN(args[1])) {
  6807. nearBottomHeight = args[1];
  6808. }
  6809. return checkNode(args[0]);
  6810. }
  6811. else {
  6812. throw new TypeError("参数1类型错误" + typeof firstArg);
  6813. }
  6814. }
  6815. /**
  6816. * 判断对象是否是元素
  6817. * @param target
  6818. * @returns
  6819. * + true 是元素
  6820. * + false 不是元素
  6821. * @example
  6822. * Utils.isDOM(document.querySelector("a"))
  6823. * > true
  6824. */
  6825. isDOM = commonUtil.isDOM.bind(commonUtil);
  6826. isFullscreenEnabled() {
  6827. return !!(this.windowApi.document.fullscreenEnabled ||
  6828. this.windowApi.document.webkitFullScreenEnabled ||
  6829. this.windowApi.document.mozFullScreenEnabled ||
  6830. this.windowApi.document.msFullScreenEnabled);
  6831. }
  6832. isJQuery(target) {
  6833. let result = false;
  6834. // @ts-ignore
  6835. if (typeof jQuery === "object" && target instanceof jQuery) {
  6836. result = true;
  6837. }
  6838. if (target == null) {
  6839. return false;
  6840. }
  6841. if (typeof target === "object") {
  6842. /* 也有种可能,这个jQuery对象是1.8.3版本的,页面中的jQuery是3.4.1版本的 */
  6843. let jQueryProps = [
  6844. "add",
  6845. "addBack",
  6846. "addClass",
  6847. "after",
  6848. "ajaxComplete",
  6849. "ajaxError",
  6850. "ajaxSend",
  6851. "ajaxStart",
  6852. "ajaxStop",
  6853. "ajaxSuccess",
  6854. "animate",
  6855. "append",
  6856. "appendTo",
  6857. "attr",
  6858. "before",
  6859. "bind",
  6860. "blur",
  6861. "change",
  6862. "children",
  6863. "clearQueue",
  6864. "click",
  6865. "clone",
  6866. "closest",
  6867. "constructor",
  6868. "contents",
  6869. "contextmenu",
  6870. "css",
  6871. "data",
  6872. "dblclick",
  6873. "delay",
  6874. "delegate",
  6875. "dequeue",
  6876. "each",
  6877. "empty",
  6878. "end",
  6879. "eq",
  6880. "extend",
  6881. "fadeIn",
  6882. "fadeOut",
  6883. "fadeTo",
  6884. "fadeToggle",
  6885. "filter",
  6886. "find",
  6887. "first",
  6888. "focus",
  6889. "focusin",
  6890. "focusout",
  6891. "get",
  6892. "has",
  6893. "hasClass",
  6894. "height",
  6895. "hide",
  6896. "hover",
  6897. "html",
  6898. "index",
  6899. "init",
  6900. "innerHeight",
  6901. "innerWidth",
  6902. "insertAfter",
  6903. "insertBefore",
  6904. "is",
  6905. "jquery",
  6906. "keydown",
  6907. "keypress",
  6908. "keyup",
  6909. "last",
  6910. "load",
  6911. "map",
  6912. "mousedown",
  6913. "mouseenter",
  6914. "mouseleave",
  6915. "mousemove",
  6916. "mouseout",
  6917. "mouseover",
  6918. "mouseup",
  6919. "next",
  6920. "nextAll",
  6921. "not",
  6922. "off",
  6923. "offset",
  6924. "offsetParent",
  6925. "on",
  6926. "one",
  6927. "outerHeight",
  6928. "outerWidth",
  6929. "parent",
  6930. "parents",
  6931. "position",
  6932. "prepend",
  6933. "prependTo",
  6934. "prev",
  6935. "prevAll",
  6936. "prevUntil",
  6937. "promise",
  6938. "prop",
  6939. "pushStack",
  6940. "queue",
  6941. "ready",
  6942. "remove",
  6943. "removeAttr",
  6944. "removeClass",
  6945. "removeData",
  6946. "removeProp",
  6947. "replaceAll",
  6948. "replaceWith",
  6949. "resize",
  6950. "scroll",
  6951. "scrollLeft",
  6952. "scrollTop",
  6953. "select",
  6954. "show",
  6955. "siblings",
  6956. "slice",
  6957. "slideDown",
  6958. "slideToggle",
  6959. "slideUp",
  6960. "sort",
  6961. "splice",
  6962. "text",
  6963. "toArray",
  6964. "toggle",
  6965. "toggleClass",
  6966. "trigger",
  6967. "triggerHandler",
  6968. "unbind",
  6969. "width",
  6970. "wrap",
  6971. ];
  6972. for (const jQueryPropsName of jQueryProps) {
  6973. if (!(jQueryPropsName in target)) {
  6974. result = false;
  6975. /* console.log(jQueryPropsName); */
  6976. break;
  6977. }
  6978. else {
  6979. result = true;
  6980. }
  6981. }
  6982. }
  6983. return result;
  6984. }
  6985. isPhone(userAgent = navigator.userAgent) {
  6986. return Boolean(/(iPhone|iPad|iPod|iOS|Android|Mobile)/i.test(userAgent));
  6987. }
  6988. isSameChars(targetStr, coefficient = 1) {
  6989. if (typeof targetStr !== "string") {
  6990. throw new TypeError("参数 str 必须是 string 类型");
  6991. }
  6992. if (targetStr.length < 2) {
  6993. return false;
  6994. }
  6995. targetStr = targetStr.toLowerCase();
  6996. const targetCharMap = {};
  6997. let targetStrLength = 0;
  6998. for (const char of targetStr) {
  6999. if (Reflect.has(targetCharMap, char)) {
  7000. targetCharMap[char]++;
  7001. }
  7002. else {
  7003. targetCharMap[char] = 1;
  7004. }
  7005. targetStrLength++;
  7006. }
  7007. let result = false;
  7008. for (const char in targetCharMap) {
  7009. if (targetCharMap[char] / targetStrLength >= coefficient) {
  7010. result = true;
  7011. break;
  7012. }
  7013. }
  7014. return result;
  7015. }
  7016. /**
  7017. * 判断对象是否不为空
  7018. * @returns {boolean}
  7019. * + true 不为空
  7020. * + false 为空
  7021. * @example
  7022. * Utils.isNotNull("123");
  7023. * > true
  7024. */
  7025. isNotNull = commonUtil.isNotNull.bind(commonUtil);
  7026. /**
  7027. * 判断对象或数据是否为空
  7028. * + `String`判空的值,如 ""、"null"、"undefined"、" "
  7029. * + `Number`判空的值,如 0
  7030. * + `Object`判空的值,如 {}、null、undefined
  7031. * + `Array`(存在属性Symbol.iterator)判空的值,如 []
  7032. * + `Boolean`判空的值,如false
  7033. * + `Function`判空的值,如()=>{}、(xxx="")=>{}、function(){}、function(xxx=""){}
  7034. * @returns
  7035. * + true 为空
  7036. * + false 不为空
  7037. * @example
  7038. Utils.isNull({});
  7039. > true
  7040. * @example
  7041. Utils.isNull([]);
  7042. > true
  7043. * @example
  7044. Utils.isNull(" ");
  7045. > true
  7046. * @example
  7047. Utils.isNull(function(){});
  7048. > true
  7049. * @example
  7050. Utils.isNull(()=>{}));
  7051. > true
  7052. * @example
  7053. Utils.isNull("undefined");
  7054. > true
  7055. * @example
  7056. Utils.isNull("null");
  7057. > true
  7058. * @example
  7059. Utils.isNull(" ", false);
  7060. > true
  7061. * @example
  7062. Utils.isNull([1],[]);
  7063. > false
  7064. * @example
  7065. Utils.isNull([],[1]);
  7066. > false
  7067. * @example
  7068. Utils.isNull(false,[123]);
  7069. > false
  7070. **/
  7071. isNull = commonUtil.isNull.bind(commonUtil);
  7072. isThemeDark() {
  7073. return this.windowApi.globalThis.matchMedia("(prefers-color-scheme: dark)")
  7074. .matches;
  7075. }
  7076. /**
  7077. * 判断元素是否在页面中可见
  7078. * @param element 需要检查的元素,可以是普通元素|数组形式的元素|通过querySelectorAll获取的元素数组
  7079. * @param inView
  7080. * + true 在窗口可视区域
  7081. * + false 不在窗口可视区域
  7082. * @returns
  7083. * + true 可见
  7084. * + false 不可见
  7085. * @example
  7086. * Utils.isVisible(document.documentElement)
  7087. * > true
  7088. */
  7089. isVisible(element, inView = false) {
  7090. let needCheckDomList = [];
  7091. if (element instanceof Array || element instanceof NodeList) {
  7092. element = element;
  7093. needCheckDomList = [...element];
  7094. }
  7095. else {
  7096. needCheckDomList = [element];
  7097. }
  7098. let result = true;
  7099. for (const domItem of needCheckDomList) {
  7100. let domDisplay = this.windowApi.window.getComputedStyle(domItem);
  7101. if (domDisplay.display === "none") {
  7102. result = false;
  7103. }
  7104. else {
  7105. let domClientRect = domItem.getBoundingClientRect();
  7106. if (inView) {
  7107. let viewportWidth = this.windowApi.window.innerWidth ||
  7108. this.windowApi.document.documentElement.clientWidth;
  7109. let viewportHeight = this.windowApi.window.innerHeight ||
  7110. this.windowApi.document.documentElement.clientHeight;
  7111. result = !(domClientRect.right < 0 ||
  7112. domClientRect.left > viewportWidth ||
  7113. domClientRect.bottom < 0 ||
  7114. domClientRect.top > viewportHeight);
  7115. }
  7116. else {
  7117. result = Boolean(domItem.getClientRects().length);
  7118. }
  7119. }
  7120. if (!result) {
  7121. /* 有一个不可见就退出循环 */
  7122. break;
  7123. }
  7124. }
  7125. return result;
  7126. }
  7127. isWebView_Via() {
  7128. let result = true;
  7129. let UtilsContext = this;
  7130. if (typeof this.windowApi.top.window.via === "object") {
  7131. for (const key in Object.values(this.windowApi.top.window.via)) {
  7132. if (Reflect.has(this.windowApi.top.window.via, key)) {
  7133. let objValueFunc = this.windowApi.top.window.via[key];
  7134. if (typeof objValueFunc === "function" &&
  7135. UtilsContext.isNativeFunc(objValueFunc)) {
  7136. result = true;
  7137. }
  7138. else {
  7139. result = false;
  7140. break;
  7141. }
  7142. }
  7143. }
  7144. }
  7145. else {
  7146. result = false;
  7147. }
  7148. return result;
  7149. }
  7150. isWebView_X() {
  7151. let result = true;
  7152. let UtilsContext = this;
  7153. if (typeof this.windowApi.top.window.mbrowser === "object") {
  7154. for (const key in Object.values(this.windowApi.top.window.mbrowser)) {
  7155. if (Reflect.has(this.windowApi.top.window.mbrowser, key)) {
  7156. let objValueFunc = this.windowApi.top.window.mbrowser[key];
  7157. if (typeof objValueFunc === "function" &&
  7158. UtilsContext.isNativeFunc(objValueFunc)) {
  7159. result = true;
  7160. }
  7161. else {
  7162. result = false;
  7163. break;
  7164. }
  7165. }
  7166. }
  7167. }
  7168. else {
  7169. result = false;
  7170. }
  7171. return result;
  7172. }
  7173. parseObjectToArray(target) {
  7174. if (typeof target !== "object") {
  7175. throw new Error("Utils.parseObjectToArray 参数 target 必须为 object 类型");
  7176. }
  7177. let result = [];
  7178. Object.keys(target).forEach(function (keyName) {
  7179. result = result.concat(target[keyName]);
  7180. });
  7181. return result;
  7182. }
  7183. /**
  7184. * 自动锁对象,用于循环判断运行的函数,在循环外new后使用,注意,如果函数内部存在异步操作,需要使用await
  7185. * @example
  7186. let lock = new Utils.LockFunction(()=>{console.log(1)}))
  7187. lock.run();
  7188. > 1
  7189. * @example
  7190. let lock = new Utils.LockFunction(()=>{console.log(1)}),true) -- 异步操作
  7191. await lock.run();
  7192. > 1
  7193. **/
  7194. LockFunction = LockFunction;
  7195. /**
  7196. * 日志对象
  7197. * @param _GM_info_ 油猴管理器的API GM_info,或者是一个对象,如{"script":{name:"Utils.Log"}}
  7198. * @example
  7199. let log = new Utils.Log(GM_info);
  7200. log.info("普通输出");
  7201. > 普通输出
  7202. log.success("成功输出");
  7203. > 成功输出
  7204. log.error("错误输出");
  7205. > 错误输出
  7206. log.warn("警告输出");
  7207. > 警告输出
  7208. log.tag = "自定义tag信息";
  7209. log.info("自定义info的颜色","#e0e0e0");
  7210. > 自定义info的颜色
  7211. log.config({
  7212. successColor: "#31dc02",
  7213. errorColor: "#e02d2d",
  7214. infoColor: "black",
  7215. })
  7216. log.success("颜色为#31dc02");
  7217. > 颜色为#31dc02
  7218. */
  7219. Log = Log;
  7220. mergeArrayToString(data, handleFunc) {
  7221. if (!(data instanceof Array)) {
  7222. throw new Error("Utils.mergeArrayToString 参数 data 必须为 Array 类型");
  7223. }
  7224. let content = "";
  7225. if (typeof handleFunc === "function") {
  7226. data.forEach((item) => {
  7227. content += handleFunc(item);
  7228. });
  7229. }
  7230. else if (typeof handleFunc === "string") {
  7231. data.forEach((item) => {
  7232. content += item[handleFunc];
  7233. });
  7234. }
  7235. else {
  7236. data.forEach((item) => {
  7237. Object.values(item)
  7238. .filter((item2) => typeof item2 === "string")
  7239. .forEach((item3) => {
  7240. content += item3;
  7241. });
  7242. });
  7243. }
  7244. return content;
  7245. }
  7246. mutationObserver(target, observer_config) {
  7247. let UtilsContext = this;
  7248. let default_obverser_config = {
  7249. /* 监听到元素有反馈,需执行的函数 */
  7250. callback: () => { },
  7251. config: {
  7252. /**
  7253. * + true 监听以 target 为根节点的整个子树。包括子树中所有节点的属性,而不仅仅是针对 target
  7254. * + false (默认) 不生效
  7255. */
  7256. subtree: undefined,
  7257. /**
  7258. * + true 监听 target 节点中发生的节点的新增与删除(同时,如果 subtree 为 true,会针对整个子树生效)
  7259. * + false (默认) 不生效
  7260. */
  7261. childList: undefined,
  7262. /**
  7263. * + true 观察所有监听的节点属性值的变化。默认值为 true,当声明了 attributeFilter 或 attributeOldValue
  7264. * + false (默认) 不生效
  7265. */
  7266. attributes: undefined,
  7267. /**
  7268. * 一个用于声明哪些属性名会被监听的数组。如果不声明该属性,所有属性的变化都将触发通知
  7269. */
  7270. attributeFilter: undefined,
  7271. /**
  7272. * + true 记录上一次被监听的节点的属性变化;可查阅 MutationObserver 中的 Monitoring attribute values 了解关于观察属性变化和属性值记录的详情
  7273. * + false (默认) 不生效
  7274. */
  7275. attributeOldValue: undefined,
  7276. /**
  7277. * + true 监听声明的 target 节点上所有字符的变化。默认值为 true,如果声明了 characterDataOldValue
  7278. * + false (默认) 不生效
  7279. */
  7280. characterData: undefined,
  7281. /**
  7282. * + true 记录前一个被监听的节点中发生的文本变化
  7283. * + false (默认) 不生效
  7284. */
  7285. characterDataOldValue: undefined,
  7286. },
  7287. immediate: false,
  7288. };
  7289. observer_config = UtilsContext.assign(default_obverser_config, observer_config);
  7290. let windowMutationObserver = this.windowApi.window.MutationObserver ||
  7291. this.windowApi.window.webkitMutationObserver ||
  7292. this.windowApi.window.MozMutationObserver;
  7293. // 观察者对象
  7294. let mutationObserver = new windowMutationObserver(function (mutations, observer) {
  7295. if (typeof observer_config.callback === "function") {
  7296. observer_config.callback(mutations, observer);
  7297. }
  7298. });
  7299. if (Array.isArray(target) || target instanceof NodeList) {
  7300. // 传入的是数组或者元素数组
  7301. target.forEach((item) => {
  7302. mutationObserver.observe(item, observer_config.config);
  7303. });
  7304. }
  7305. else if (UtilsContext.isJQuery(target)) {
  7306. /* 传入的参数是jQuery对象 */
  7307. target.each((index, item) => {
  7308. mutationObserver.observe(item, observer_config.config);
  7309. });
  7310. }
  7311. else {
  7312. mutationObserver.observe(target, observer_config.config);
  7313. }
  7314. if (observer_config.immediate) {
  7315. /* 主动触发一次 */
  7316. if (typeof observer_config.callback === "function") {
  7317. observer_config.callback([], mutationObserver);
  7318. }
  7319. }
  7320. return mutationObserver;
  7321. }
  7322. /**
  7323. * 使用观察器观察元素出现在视图内,出现的话触发回调
  7324. * @param target 目标元素
  7325. * @param callback 触发的回调
  7326. * @param options 观察器配置
  7327. * @example
  7328. * Utils.mutationVisible(document.querySelector("div.xxxx"),(entries,observer)=>{
  7329. * console.log("该元素出现在视图内");
  7330. * }))
  7331. */
  7332. mutationVisible(target, callback, options) {
  7333. if (typeof IntersectionObserver === "undefined") {
  7334. throw new TypeError("IntersectionObserver is not defined");
  7335. }
  7336. if (target == null) {
  7337. throw new TypeError("mutatuinVisible target is null");
  7338. }
  7339. let defaultOptions = {
  7340. root: null,
  7341. rootMargin: "0px 0px 0px 0px",
  7342. threshold: [0.01, 0.99],
  7343. };
  7344. defaultOptions = this.assign(defaultOptions, options || {});
  7345. let intersectionObserver = new IntersectionObserver((entries, observer) => {
  7346. if (entries[0].isIntersecting) {
  7347. if (typeof callback === "function") {
  7348. callback(entries, observer);
  7349. }
  7350. }
  7351. }, defaultOptions);
  7352. if (Array.isArray(target)) {
  7353. target.forEach((item) => {
  7354. intersectionObserver.observe(item);
  7355. });
  7356. }
  7357. else {
  7358. intersectionObserver.observe(target);
  7359. }
  7360. }
  7361. /**
  7362. * 去除全局window下的Utils,返回控制权
  7363. * @example
  7364. * let utils = Utils.noConflict();
  7365. * > ...
  7366. */
  7367. noConflict() {
  7368. if (this.windowApi.window.Utils) {
  7369. Reflect.deleteProperty(this.windowApi.window, "Utils");
  7370. }
  7371. this.windowApi.window.Utils = utils;
  7372. return utils;
  7373. }
  7374. noConflictFunc(needReleaseObject, needReleaseName, functionNameList = [], release = true) {
  7375. let UtilsContext = this;
  7376. if (typeof needReleaseObject !== "object") {
  7377. throw new Error("Utils.noConflictFunc 参数 needReleaseObject 必须为 object 类型");
  7378. }
  7379. if (typeof needReleaseName !== "string") {
  7380. throw new Error("Utils.noConflictFunc 参数 needReleaseName 必须为 string 类型");
  7381. }
  7382. if (!Array.isArray(functionNameList)) {
  7383. throw new Error("Utils.noConflictFunc 参数 functionNameList 必须为 Array 类型");
  7384. }
  7385. let needReleaseKey = "__" + needReleaseName;
  7386. /**
  7387. * 释放所有
  7388. */
  7389. function releaseAll() {
  7390. if (typeof UtilsContext.windowApi.window[needReleaseKey] !==
  7391. "undefined") {
  7392. /* 已存在 */
  7393. return;
  7394. }
  7395. UtilsContext.windowApi.window[needReleaseKey] =
  7396. UtilsContext.deepClone(needReleaseObject);
  7397. Object.values(needReleaseObject).forEach((value) => {
  7398. if (typeof value === "function") {
  7399. needReleaseObject[value.name] = () => { };
  7400. }
  7401. });
  7402. }
  7403. /**
  7404. * 释放单个
  7405. */
  7406. function releaseOne() {
  7407. Array.from(functionNameList).forEach((item) => {
  7408. Object.values(needReleaseObject).forEach((value) => {
  7409. if (typeof value === "function") {
  7410. if (typeof UtilsContext.windowApi.window[needReleaseKey] ===
  7411. "undefined") {
  7412. UtilsContext.windowApi.window[needReleaseKey] = {};
  7413. }
  7414. if (item === value.name) {
  7415. UtilsContext.windowApi.window[needReleaseKey][value.name] = needReleaseObject[value.name];
  7416. needReleaseObject[value.name] = () => { };
  7417. }
  7418. }
  7419. });
  7420. });
  7421. }
  7422. /**
  7423. * 恢复所有
  7424. */
  7425. function recoveryAll() {
  7426. if (typeof UtilsContext.windowApi.window[needReleaseKey] ===
  7427. "undefined") {
  7428. /* 未存在 */
  7429. return;
  7430. }
  7431. Object.assign(needReleaseObject, UtilsContext.windowApi.window[needReleaseKey]);
  7432. Reflect.deleteProperty(UtilsContext.windowApi.window, "needReleaseKey");
  7433. }
  7434. /**
  7435. * 恢复单个
  7436. */
  7437. function recoveryOne() {
  7438. if (typeof UtilsContext.windowApi.window[needReleaseKey] ===
  7439. "undefined") {
  7440. /* 未存在 */
  7441. return;
  7442. }
  7443. Array.from(functionNameList).forEach((item) => {
  7444. if (UtilsContext.windowApi.window[needReleaseKey][item]) {
  7445. needReleaseObject[item] = UtilsContext.windowApi.window[needReleaseKey][item];
  7446. Reflect.deleteProperty(UtilsContext.windowApi.window[needReleaseKey], item);
  7447. if (Object.keys(UtilsContext.windowApi.window[needReleaseKey])
  7448. .length === 0) {
  7449. Reflect.deleteProperty(window, needReleaseKey);
  7450. }
  7451. }
  7452. });
  7453. }
  7454. if (release) {
  7455. /* 释放 */
  7456. if (functionNameList.length === 0) {
  7457. releaseAll();
  7458. }
  7459. else {
  7460. /* 对单个进行操作 */
  7461. releaseOne();
  7462. }
  7463. }
  7464. else {
  7465. /* 恢复 */
  7466. if (functionNameList.length === 0) {
  7467. recoveryAll();
  7468. }
  7469. else {
  7470. /* 对单个进行操作 */
  7471. recoveryOne();
  7472. }
  7473. }
  7474. }
  7475. parseBase64ToBlob(dataUri) {
  7476. if (typeof dataUri !== "string") {
  7477. throw new Error("Utils.parseBase64ToBlob 参数 dataUri 必须为 string 类型");
  7478. }
  7479. let dataUriSplit = dataUri.split(","), dataUriMime = dataUriSplit[0].match(/:(.*?);/)[1], dataUriBase64Str = atob(dataUriSplit[1]), dataUriLength = dataUriBase64Str.length, u8arr = new Uint8Array(dataUriLength);
  7480. while (dataUriLength--) {
  7481. u8arr[dataUriLength] = dataUriBase64Str.charCodeAt(dataUriLength);
  7482. }
  7483. return new Blob([u8arr], {
  7484. type: dataUriMime,
  7485. });
  7486. }
  7487. parseBase64ToFile(dataUri, fileName = "example") {
  7488. if (typeof dataUri !== "string") {
  7489. throw new Error("Utils.parseBase64ToFile 参数 dataUri 必须为 string 类型");
  7490. }
  7491. if (typeof fileName !== "string") {
  7492. throw new Error("Utils.parseBase64ToFile 参数 fileName 必须为 string 类型");
  7493. }
  7494. let dataUriSplit = dataUri.split(","), dataUriMime = dataUriSplit[0].match(/:(.*?);/)[1], dataUriBase64Str = atob(dataUriSplit[1]), dataUriLength = dataUriBase64Str.length, u8arr = new Uint8Array(dataUriLength);
  7495. while (dataUriLength--) {
  7496. u8arr[dataUriLength] = dataUriBase64Str.charCodeAt(dataUriLength);
  7497. }
  7498. return new File([u8arr], fileName, {
  7499. type: dataUriMime,
  7500. });
  7501. }
  7502. parseInt(matchList = [], defaultValue = 0) {
  7503. if (matchList == null) {
  7504. return parseInt(defaultValue.toString());
  7505. }
  7506. let parseValue = parseInt(matchList[matchList.length - 1]);
  7507. if (isNaN(parseValue)) {
  7508. parseValue = parseInt(defaultValue.toString());
  7509. }
  7510. return parseValue;
  7511. }
  7512. async parseBlobToFile(blobUrl, fileName = "example") {
  7513. return new Promise((resolve, reject) => {
  7514. fetch(blobUrl)
  7515. .then((response) => response.blob())
  7516. .then((blob) => {
  7517. const file = new File([blob], fileName, { type: blob.type });
  7518. resolve(file);
  7519. })
  7520. .catch((error) => {
  7521. console.error("Error:", error);
  7522. reject(error);
  7523. });
  7524. });
  7525. }
  7526. parseCDATA(text = "") {
  7527. let result = "";
  7528. let cdataRegexp = /<\!\[CDATA\[([\s\S]*)\]\]>/;
  7529. let cdataMatch = cdataRegexp.exec(text.trim());
  7530. if (cdataMatch && cdataMatch.length > 1) {
  7531. result = cdataMatch[cdataMatch.length - 1];
  7532. }
  7533. return result;
  7534. }
  7535. async parseFileToBase64(fileObj) {
  7536. let reader = new FileReader();
  7537. reader.readAsDataURL(fileObj);
  7538. return new Promise((resolve) => {
  7539. reader.onload = function (event) {
  7540. resolve(event.target.result);
  7541. };
  7542. });
  7543. }
  7544. parseFromString(text, mimeType = "text/html") {
  7545. let parser = new DOMParser();
  7546. return parser.parseFromString(text, mimeType);
  7547. }
  7548. parseStringToRegExpString(text) {
  7549. if (typeof text !== "string") {
  7550. throw new TypeError("string必须是字符串");
  7551. }
  7552. let regString = text.replace(/[.*+\-?^${}()|[\]\\]/g, "\\$&");
  7553. return regString;
  7554. }
  7555. preventEvent(element, eventNameList = [], capture) {
  7556. function stopEvent(event) {
  7557. /* 阻止事件的默认行为发生。例如,当点击一个链接时,浏览器会默认打开链接的URL */
  7558. event?.preventDefault();
  7559. /* 停止事件的传播,阻止它继续向更上层的元素冒泡,事件将不会再传播给其他的元素 */
  7560. event?.stopPropagation();
  7561. /* 阻止事件传播,并且还能阻止元素上的其他事件处理程序被触发 */
  7562. event?.stopImmediatePropagation();
  7563. return false;
  7564. }
  7565. if (arguments.length === 1) {
  7566. /* 直接阻止事件 */
  7567. return stopEvent(arguments[0]);
  7568. }
  7569. else {
  7570. /* 添加对应的事件来阻止触发 */
  7571. if (typeof eventNameList === "string") {
  7572. eventNameList = [eventNameList];
  7573. }
  7574. eventNameList.forEach((eventName) => {
  7575. element.addEventListener(eventName, stopEvent, {
  7576. capture: Boolean(capture),
  7577. });
  7578. });
  7579. }
  7580. }
  7581. /**
  7582. * 在canvas元素节点上绘制进度圆圈
  7583. * @example
  7584. let progress = new Utils.Process({canvasNode:document.querySelector("canvas")});
  7585. progress.draw();
  7586. * **/
  7587. Progress = Progress;
  7588. registerTrustClickEvent(isTrustValue = true, filter) {
  7589. function trustEvent(event) {
  7590. return new Proxy(event, {
  7591. get: function (target, property) {
  7592. if (property === "isTrusted") {
  7593. return isTrustValue;
  7594. }
  7595. else {
  7596. return Reflect.get(target, property);
  7597. }
  7598. },
  7599. });
  7600. }
  7601. if (filter == null) {
  7602. filter = function (typeName) {
  7603. return typeName === "click";
  7604. };
  7605. }
  7606. const originalListener = EventTarget.prototype.addEventListener;
  7607. EventTarget.prototype.addEventListener = function (...args) {
  7608. let type = args[0];
  7609. let callback = args[1];
  7610. // @ts-ignore
  7611. args[2];
  7612. if (filter(type)) {
  7613. if (typeof callback === "function") {
  7614. args[1] = function (event) {
  7615. callback.call(this, trustEvent(event));
  7616. };
  7617. }
  7618. else if (typeof callback === "object" &&
  7619. "handleEvent" in callback) {
  7620. let oldHandleEvent = callback["handleEvent"];
  7621. args[1]["handleEvent"] = function (event) {
  7622. if (event == null) {
  7623. return;
  7624. }
  7625. try {
  7626. /* Proxy对象使用instanceof会报错 */
  7627. event instanceof Proxy;
  7628. oldHandleEvent.call(this, trustEvent(event));
  7629. }
  7630. catch (error) {
  7631. // @ts-ignore
  7632. event["isTrusted"] = isTrustValue;
  7633. }
  7634. };
  7635. }
  7636. }
  7637. return originalListener.apply(this, args);
  7638. };
  7639. }
  7640. reverseNumber(num) {
  7641. let reversedNum = 0;
  7642. let isNegative = false;
  7643. if (num < 0) {
  7644. isNegative = true;
  7645. num = Math.abs(num);
  7646. }
  7647. while (num > 0) {
  7648. reversedNum = reversedNum * 10 + (num % 10);
  7649. num = Math.floor(num / 10);
  7650. }
  7651. return isNegative ? -reversedNum : reversedNum;
  7652. }
  7653. selectElementText(element, childTextNode, startIndex, endIndex) {
  7654. let range = this.windowApi.document.createRange();
  7655. range.selectNodeContents(element);
  7656. if (childTextNode) {
  7657. if (childTextNode.nodeType !== Node.TEXT_NODE) {
  7658. throw new TypeError("childTextNode必须是#text元素");
  7659. }
  7660. if (startIndex != null && endIndex != null) {
  7661. range.setStart(childTextNode, startIndex);
  7662. range.setEnd(childTextNode, endIndex);
  7663. }
  7664. }
  7665. let selection = this.windowApi.globalThis.getSelection();
  7666. if (selection) {
  7667. selection.removeAllRanges();
  7668. selection.addRange(range);
  7669. }
  7670. }
  7671. setClip(data, info = {
  7672. type: "text",
  7673. mimetype: "text/plain",
  7674. }) {
  7675. if (typeof data === "object") {
  7676. if (data instanceof Element) {
  7677. data = data.outerHTML;
  7678. }
  7679. else {
  7680. data = JSON.stringify(data);
  7681. }
  7682. }
  7683. else if (typeof data !== "string") {
  7684. data = data.toString();
  7685. }
  7686. let textType = typeof info === "object" ? info.type : info;
  7687. if (textType.includes("html")) {
  7688. textType = "text/html";
  7689. }
  7690. else {
  7691. textType = "text/plain";
  7692. }
  7693. let UtilsContext = this;
  7694. class UtilsClipboard {
  7695. #resolve;
  7696. #copyData;
  7697. #copyDataType;
  7698. constructor(resolve, copyData, copyDataType) {
  7699. this.#resolve = resolve;
  7700. this.#copyData = copyData;
  7701. this.#copyDataType = copyDataType;
  7702. }
  7703. async init() {
  7704. let copyStatus = false;
  7705. // @ts-ignore
  7706. await this.requestClipboardPermission();
  7707. if (this.hasClipboard() &&
  7708. (this.hasClipboardWrite() || this.hasClipboardWriteText())) {
  7709. try {
  7710. copyStatus = await this.copyDataByClipboard();
  7711. }
  7712. catch (error) {
  7713. console.error("复制失败,使用第二种方式,error👉", error);
  7714. copyStatus = this.copyTextByTextArea();
  7715. }
  7716. }
  7717. else {
  7718. copyStatus = this.copyTextByTextArea();
  7719. }
  7720. this.#resolve(copyStatus);
  7721. this.destroy();
  7722. }
  7723. destroy() {
  7724. // @ts-ignore
  7725. this.#resolve = null;
  7726. // @ts-ignore
  7727. this.#copyData = null;
  7728. // @ts-ignore
  7729. this.#copyDataType = null;
  7730. }
  7731. isText() {
  7732. return this.#copyDataType.includes("text");
  7733. }
  7734. hasClipboard() {
  7735. return navigator?.clipboard != null;
  7736. }
  7737. hasClipboardWrite() {
  7738. return navigator?.clipboard?.write != null;
  7739. }
  7740. hasClipboardWriteText() {
  7741. return navigator?.clipboard?.writeText != null;
  7742. }
  7743. /**
  7744. * 使用textarea和document.execCommand("copy")来复制文字
  7745. */
  7746. copyTextByTextArea() {
  7747. try {
  7748. let copyElement = UtilsContext.windowApi.document.createElement("textarea");
  7749. copyElement.value = this.#copyData;
  7750. copyElement.setAttribute("type", "text");
  7751. copyElement.setAttribute("style", "opacity:0;position:absolute;");
  7752. copyElement.setAttribute("readonly", "readonly");
  7753. UtilsContext.windowApi.document.body.appendChild(copyElement);
  7754. copyElement.select();
  7755. UtilsContext.windowApi.document.execCommand("copy");
  7756. UtilsContext.windowApi.document.body.removeChild(copyElement);
  7757. return true;
  7758. }
  7759. catch (error) {
  7760. console.error("复制失败,error👉", error);
  7761. return false;
  7762. }
  7763. }
  7764. /**
  7765. * 申请剪贴板权限
  7766. */
  7767. requestClipboardPermission() {
  7768. return new Promise((resolve, reject) => {
  7769. if (navigator.permissions && navigator.permissions.query) {
  7770. navigator.permissions
  7771. .query({
  7772. // @ts-ignore
  7773. name: "clipboard-write",
  7774. })
  7775. .then((permissionStatus) => {
  7776. resolve(true);
  7777. })
  7778. .catch((error) => {
  7779. console.error([
  7780. "申请剪贴板权限失败,尝试直接写入👉",
  7781. error.message ?? error.name ?? error.stack,
  7782. ]);
  7783. resolve(false);
  7784. });
  7785. }
  7786. else {
  7787. resolve(false);
  7788. }
  7789. });
  7790. }
  7791. /**
  7792. * 使用clipboard直接写入数据到剪贴板
  7793. */
  7794. copyDataByClipboard() {
  7795. return new Promise((resolve, reject) => {
  7796. if (this.isText()) {
  7797. /* 只复制文字 */
  7798. navigator.clipboard
  7799. .writeText(this.#copyData)
  7800. .then(() => {
  7801. resolve(true);
  7802. })
  7803. .catch((error) => {
  7804. reject(error);
  7805. });
  7806. }
  7807. else {
  7808. /* 可复制对象 */
  7809. let textBlob = new Blob([this.#copyData], {
  7810. type: this.#copyDataType,
  7811. });
  7812. navigator.clipboard
  7813. .write([
  7814. new ClipboardItem({
  7815. [this.#copyDataType]: textBlob,
  7816. }),
  7817. ])
  7818. .then(() => {
  7819. resolve(true);
  7820. })
  7821. .catch((error) => {
  7822. reject(error);
  7823. });
  7824. }
  7825. });
  7826. }
  7827. }
  7828. return new Promise((resolve) => {
  7829. const utilsClipboard = new UtilsClipboard(resolve, data, textType);
  7830. if (UtilsContext.windowApi.document.hasFocus()) {
  7831. utilsClipboard.init();
  7832. }
  7833. else {
  7834. UtilsContext.windowApi.window.addEventListener("focus", () => {
  7835. utilsClipboard.init();
  7836. }, { once: true });
  7837. }
  7838. });
  7839. }
  7840. setTimeout(callback, delayTime = 0) {
  7841. let UtilsContext = this;
  7842. if (typeof callback !== "function" && typeof callback !== "string") {
  7843. throw new TypeError("Utils.setTimeout 参数 callback 必须为 function|string 类型");
  7844. }
  7845. if (typeof delayTime !== "number") {
  7846. throw new TypeError("Utils.setTimeout 参数 delayTime 必须为 number 类型");
  7847. }
  7848. return new Promise((resolve) => {
  7849. UtilsContext.workerSetTimeout(() => {
  7850. resolve(UtilsContext.tryCatch().run(callback));
  7851. }, delayTime);
  7852. });
  7853. }
  7854. sleep(delayTime = 0) {
  7855. let UtilsContext = this;
  7856. if (typeof delayTime !== "number") {
  7857. throw new Error("Utils.sleep 参数 delayTime 必须为 number 类型");
  7858. }
  7859. return new Promise((resolve) => {
  7860. UtilsContext.workerSetTimeout(() => {
  7861. resolve(undefined);
  7862. }, delayTime);
  7863. });
  7864. }
  7865. dragSlider(selector, offsetX = this.windowApi.window.innerWidth) {
  7866. let UtilsContext = this;
  7867. function initMouseEvent(eventName, offSetX, offSetY) {
  7868. // @ts-ignore
  7869. let win = typeof unsafeWindow === "undefined" ? globalThis : unsafeWindow;
  7870. let mouseEvent = UtilsContext.windowApi.document.createEvent("MouseEvents");
  7871. mouseEvent.initMouseEvent(eventName, true, true, win, 0, offSetX, offSetY, offSetX, offSetY, false, false, false, false, 0, null);
  7872. return mouseEvent;
  7873. }
  7874. let sliderElement = typeof selector === "string"
  7875. ? domUtils.selector(selector)
  7876. : selector;
  7877. if (!(sliderElement instanceof Node) ||
  7878. !(sliderElement instanceof Element)) {
  7879. throw new Error("Utils.dragSlider 参数selector 必须为Node/Element类型");
  7880. }
  7881. let rect = sliderElement.getBoundingClientRect(), x0 = rect.x || rect.left, y0 = rect.y || rect.top, x1 = x0 + offsetX, y1 = y0;
  7882. sliderElement.dispatchEvent(initMouseEvent("mousedown", x0, y0));
  7883. sliderElement.dispatchEvent(initMouseEvent("mousemove", x1, y1));
  7884. sliderElement.dispatchEvent(initMouseEvent("mouseleave", x1, y1));
  7885. sliderElement.dispatchEvent(initMouseEvent("mouseout", x1, y1));
  7886. }
  7887. enterFullScreen(element = this.windowApi.document.documentElement, options) {
  7888. try {
  7889. if (element.requestFullscreen) {
  7890. element.requestFullscreen(options);
  7891. }
  7892. else if (element.webkitRequestFullScreen) {
  7893. element.webkitRequestFullScreen();
  7894. }
  7895. else if (element.mozRequestFullScreen) {
  7896. element.mozRequestFullScreen();
  7897. }
  7898. else if (element.msRequestFullscreen) {
  7899. element.msRequestFullscreen();
  7900. }
  7901. else {
  7902. throw new TypeError("该浏览器不支持全屏API");
  7903. }
  7904. }
  7905. catch (err) {
  7906. console.error(err);
  7907. }
  7908. }
  7909. exitFullScreen(element = this.windowApi.document.documentElement) {
  7910. if (this.windowApi.document.exitFullscreen) {
  7911. return this.windowApi.document.exitFullscreen();
  7912. }
  7913. else if (this.windowApi.document.msExitFullscreen) {
  7914. return this.windowApi.document.msExitFullscreen();
  7915. }
  7916. else if (this.windowApi.document.mozCancelFullScreen) {
  7917. return this.windowApi.document.mozCancelFullScreen();
  7918. }
  7919. else if (this.windowApi.document.webkitCancelFullScreen) {
  7920. return this.windowApi.document.webkitCancelFullScreen();
  7921. }
  7922. else {
  7923. return new Promise((resolve, reject) => {
  7924. reject(new TypeError("该浏览器不支持全屏API"));
  7925. });
  7926. }
  7927. }
  7928. sortListByProperty(data, getPropertyValueFunc, sortByDesc = true) {
  7929. let UtilsContext = this;
  7930. if (typeof getPropertyValueFunc !== "function" &&
  7931. typeof getPropertyValueFunc !== "string") {
  7932. throw new Error("Utils.sortListByProperty 参数 getPropertyValueFunc 必须为 function|string 类型");
  7933. }
  7934. if (typeof sortByDesc !== "boolean") {
  7935. throw new Error("Utils.sortListByProperty 参数 sortByDesc 必须为 boolean 类型");
  7936. }
  7937. let getObjValue = function (obj) {
  7938. return typeof getPropertyValueFunc === "string"
  7939. ? obj[getPropertyValueFunc]
  7940. : getPropertyValueFunc(obj);
  7941. };
  7942. /**
  7943. * 排序方法
  7944. * @param {any} after_obj
  7945. * @param {any} before_obj
  7946. * @returns
  7947. */
  7948. let sortFunc = function (after_obj, before_obj) {
  7949. let beforeValue = getObjValue(before_obj); /* 前 */
  7950. let afterValue = getObjValue(after_obj); /* 后 */
  7951. if (sortByDesc) {
  7952. if (afterValue > beforeValue) {
  7953. return -1;
  7954. }
  7955. else if (afterValue < beforeValue) {
  7956. return 1;
  7957. }
  7958. else {
  7959. return 0;
  7960. }
  7961. }
  7962. else {
  7963. if (afterValue < beforeValue) {
  7964. return -1;
  7965. }
  7966. else if (afterValue > beforeValue) {
  7967. return 1;
  7968. }
  7969. else {
  7970. return 0;
  7971. }
  7972. }
  7973. };
  7974. /**
  7975. * 排序元素方法
  7976. * @param nodeList 元素列表
  7977. * @param getNodeListFunc 获取元素列表的函数
  7978. */
  7979. let sortNodeFunc = function (nodeList, getNodeListFunc) {
  7980. let nodeListLength = nodeList.length;
  7981. for (let i = 0; i < nodeListLength - 1; i++) {
  7982. for (let j = 0; j < nodeListLength - 1 - i; j++) {
  7983. let beforeNode = nodeList[j];
  7984. let afterNode = nodeList[j + 1];
  7985. let beforeValue = getObjValue(beforeNode); /* 前 */
  7986. let afterValue = getObjValue(afterNode); /* 后 */
  7987. if ((sortByDesc == true && beforeValue < afterValue) ||
  7988. (sortByDesc == false && beforeValue > afterValue)) {
  7989. /* 升序/降序 */
  7990. /* 相邻元素两两对比 */
  7991. let temp = beforeNode.nextElementSibling;
  7992. afterNode.after(beforeNode);
  7993. if (temp == null) {
  7994. /* 如果为空,那么是最后一个元素,使用append */
  7995. temp.parentNode.appendChild(afterNode);
  7996. }
  7997. else {
  7998. /* 不为空,使用before */
  7999. temp.before(afterNode);
  8000. }
  8001. nodeList = getNodeListFunc();
  8002. }
  8003. }
  8004. }
  8005. };
  8006. let result = data;
  8007. let getDataFunc = null;
  8008. if (data instanceof Function) {
  8009. getDataFunc = data;
  8010. data = data();
  8011. }
  8012. if (Array.isArray(data)) {
  8013. data.sort(sortFunc);
  8014. }
  8015. else if (data instanceof NodeList ||
  8016. UtilsContext.isJQuery(data)) {
  8017. sortNodeFunc(data, getDataFunc);
  8018. result = getDataFunc();
  8019. }
  8020. else {
  8021. throw new Error("Utils.sortListByProperty 参数 data 必须为 Array|NodeList|jQuery 类型");
  8022. }
  8023. return result;
  8024. }
  8025. stringToRegular(targetString, flags = "ig") {
  8026. let reg;
  8027. // @ts-ignore
  8028. flags = flags.toLowerCase();
  8029. if (typeof targetString === "string") {
  8030. reg = new RegExp(targetString.replace(/[.*+\-?^${}()|[\]\\]/g, "\\$&"), flags);
  8031. }
  8032. else if (targetString instanceof RegExp) {
  8033. reg = targetString;
  8034. }
  8035. else {
  8036. throw new Error("Utils.stringToRegular 参数targetString必须是string|Regexp类型");
  8037. }
  8038. return reg;
  8039. }
  8040. stringTitleToUpperCase(targetString, otherStrToLowerCase = false) {
  8041. let newTargetString = targetString.slice(0, 1).toUpperCase();
  8042. if (otherStrToLowerCase) {
  8043. newTargetString = newTargetString + targetString.slice(1).toLowerCase();
  8044. }
  8045. else {
  8046. newTargetString = newTargetString + targetString.slice(1);
  8047. }
  8048. return newTargetString;
  8049. }
  8050. startsWith(target, searchString, position = 0) {
  8051. let UtilsContext = this;
  8052. if (position > target.length) {
  8053. /* 超出目标字符串的长度 */
  8054. return false;
  8055. }
  8056. if (position !== 0) {
  8057. target = target.slice(position, target.length);
  8058. }
  8059. let searchStringRegexp = searchString;
  8060. if (typeof searchString === "string") {
  8061. searchStringRegexp = new RegExp(`^${searchString}`);
  8062. }
  8063. else if (Array.isArray(searchString)) {
  8064. let flag = false;
  8065. for (const searcStr of searchString) {
  8066. if (!UtilsContext.startsWith(target, searcStr, position)) {
  8067. flag = true;
  8068. break;
  8069. }
  8070. }
  8071. return flag;
  8072. }
  8073. return Boolean(target.match(searchStringRegexp));
  8074. }
  8075. stringTitleToLowerCase(targetString, otherStrToUpperCase = false) {
  8076. let newTargetString = targetString.slice(0, 1).toLowerCase();
  8077. if (otherStrToUpperCase) {
  8078. newTargetString = newTargetString + targetString.slice(1).toUpperCase();
  8079. }
  8080. else {
  8081. newTargetString = newTargetString + targetString.slice(1);
  8082. }
  8083. return newTargetString;
  8084. }
  8085. /**
  8086. * 字符串转Object对象,类似'{"test":""}' => {"test":""}
  8087. * @param data
  8088. * @param errorCallBack (可选)错误回调
  8089. * @example
  8090. * Utils.toJSON("{123:123}")
  8091. * > {123:123}
  8092. */
  8093. toJSON = commonUtil.toJSON.bind(commonUtil);
  8094. toSearchParamsStr(obj, addPrefix) {
  8095. let UtilsContext = this;
  8096. let searhParamsStr = "";
  8097. if (Array.isArray(obj)) {
  8098. obj.forEach((item) => {
  8099. if (searhParamsStr === "") {
  8100. searhParamsStr += UtilsContext.toSearchParamsStr(item);
  8101. }
  8102. else {
  8103. searhParamsStr += "&" + UtilsContext.toSearchParamsStr(item);
  8104. }
  8105. });
  8106. }
  8107. else {
  8108. searhParamsStr = new URLSearchParams(Object.entries(obj)).toString();
  8109. }
  8110. if (addPrefix && !searhParamsStr.startsWith("?")) {
  8111. searhParamsStr = "?" + searhParamsStr;
  8112. }
  8113. return searhParamsStr;
  8114. }
  8115. /**
  8116. * 将UrlSearchParams格式的字符串转为对象
  8117. */
  8118. searchParamStrToObj(searhParamsStr) {
  8119. if (typeof searhParamsStr !== "string") {
  8120. // @ts-ignore
  8121. return {};
  8122. }
  8123. // @ts-ignore
  8124. return Object.fromEntries(new URLSearchParams(searhParamsStr));
  8125. }
  8126. /**
  8127. * 提供一个封装了 try-catch 的函数,可以执行传入的函数并捕获其可能抛出的错误,并通过传入的错误处理函数进行处理。
  8128. * @example
  8129. * Utils.tryCatch().error().run(()=>{console.log(1)});
  8130. * > 1
  8131. * @example
  8132. * Utils.tryCatch().config({log:true}).error((error)=>{console.log(error)}).run(()=>{throw new Error('测试错误')});
  8133. * > ()=>{throw new Error('测试错误')}出现错误
  8134. */
  8135. tryCatch = TryCatch;
  8136. uniqueArray(uniqueArrayData = [], compareArrayData, compareFun = (item, item2) => {
  8137. return item === item2;
  8138. }) {
  8139. if (typeof compareArrayData === "function") {
  8140. const compareFn = compareArrayData;
  8141. const seen = new Set();
  8142. const result = [];
  8143. for (const item of uniqueArrayData) {
  8144. // 使用compareFn函数来获取当前对象的唯一标识
  8145. const identfier = compareFn(item);
  8146. // 如果Set中还没有这个标识,则添加到结果数组中,并将其标识存入Set
  8147. if (!seen.has(identfier)) {
  8148. seen.add(identfier);
  8149. result.push(item);
  8150. }
  8151. }
  8152. return result;
  8153. }
  8154. else {
  8155. return Array.from(uniqueArrayData).filter((item) => !Array.from(compareArrayData).some(function (item2) {
  8156. return compareFun(item, item2);
  8157. }));
  8158. }
  8159. }
  8160. waitArrayLoopToEnd(data, handleFunc) {
  8161. let UtilsContext = this;
  8162. if (typeof handleFunc !== "function" && typeof handleFunc !== "string") {
  8163. throw new Error("Utils.waitArrayLoopToEnd 参数 handleDataFunction 必须为 function|string 类型");
  8164. }
  8165. return Promise.all(Array.from(data).map(async (item, index) => {
  8166. await UtilsContext.tryCatch(index, item).run(handleFunc);
  8167. }));
  8168. }
  8169. wait(checkFn, timeout, parent) {
  8170. const UtilsContext = this;
  8171. let __timeout__ = typeof timeout === "number" ? timeout : 0;
  8172. return new Promise((resolve) => {
  8173. let observer = UtilsContext.mutationObserver(parent || UtilsContext.windowApi.document, {
  8174. config: {
  8175. subtree: true,
  8176. childList: true,
  8177. attributes: true,
  8178. },
  8179. immediate: true,
  8180. callback(mutations, __observer__) {
  8181. let result = checkFn();
  8182. if (result.success) {
  8183. // 取消观察器
  8184. if (typeof __observer__?.disconnect === "function") {
  8185. __observer__.disconnect();
  8186. }
  8187. resolve(result.data);
  8188. }
  8189. },
  8190. });
  8191. if (__timeout__ > 0) {
  8192. UtilsContext.workerSetTimeout(() => {
  8193. // 取消观察器
  8194. if (typeof observer?.disconnect === "function") {
  8195. observer.disconnect();
  8196. }
  8197. resolve(null);
  8198. }, __timeout__);
  8199. }
  8200. });
  8201. }
  8202. waitNode(...args) {
  8203. // 过滤掉undefined
  8204. args = args.filter((arg) => arg !== undefined);
  8205. let UtilsContext = this;
  8206. // 选择器
  8207. let selector = args[0];
  8208. // 父元素(监听的元素)
  8209. let parent = UtilsContext.windowApi.document;
  8210. // 超时时间
  8211. let timeout = 0;
  8212. if (typeof args[0] !== "string" &&
  8213. !Array.isArray(args[0]) &&
  8214. typeof args[0] !== "function") {
  8215. throw new TypeError("Utils.waitNode 第一个参数必须是string|string[]|Function");
  8216. }
  8217. if (args.length === 1) ;
  8218. else if (args.length === 2) {
  8219. let secondParam = args[1];
  8220. if (typeof secondParam === "number") {
  8221. // "div",10000
  8222. timeout = secondParam;
  8223. }
  8224. else if (typeof secondParam === "object" &&
  8225. secondParam instanceof Node) {
  8226. // "div",document
  8227. parent = secondParam;
  8228. }
  8229. else {
  8230. throw new TypeError("Utils.waitNode 第二个参数必须是number|Node");
  8231. }
  8232. }
  8233. else if (args.length === 3) {
  8234. // "div",document,10000
  8235. // 第二个参数,parent
  8236. let secondParam = args[1];
  8237. // 第三个参数,timeout
  8238. let thirdParam = args[2];
  8239. if (typeof secondParam === "object" && secondParam instanceof Node) {
  8240. parent = secondParam;
  8241. if (typeof thirdParam === "number") {
  8242. timeout = thirdParam;
  8243. }
  8244. else {
  8245. throw new TypeError("Utils.waitNode 第三个参数必须是number");
  8246. }
  8247. }
  8248. else {
  8249. throw new TypeError("Utils.waitNode 第二个参数必须是Node");
  8250. }
  8251. }
  8252. else {
  8253. throw new TypeError("Utils.waitNode 参数个数错误");
  8254. }
  8255. function getNode() {
  8256. if (Array.isArray(selector)) {
  8257. let result = [];
  8258. for (let index = 0; index < selector.length; index++) {
  8259. let node = domUtils.selector(selector[index]);
  8260. if (node) {
  8261. result.push(node);
  8262. }
  8263. }
  8264. if (result.length === selector.length) {
  8265. return result;
  8266. }
  8267. }
  8268. else if (typeof selector === "function") {
  8269. return selector();
  8270. }
  8271. else {
  8272. return domUtils.selector(selector, parent);
  8273. }
  8274. }
  8275. return UtilsContext.wait(() => {
  8276. let node = getNode();
  8277. if (node) {
  8278. return {
  8279. success: true,
  8280. data: node,
  8281. };
  8282. }
  8283. else {
  8284. return {
  8285. success: false,
  8286. data: node,
  8287. };
  8288. }
  8289. }, timeout, parent);
  8290. }
  8291. waitAnyNode(...args) {
  8292. // 过滤掉undefined
  8293. args = args.filter((arg) => arg !== undefined);
  8294. let UtilsContext = this;
  8295. // 选择器
  8296. let selectorList = args[0];
  8297. // 父元素(监听的元素)
  8298. let parent = UtilsContext.windowApi.document;
  8299. // 超时时间
  8300. let timeout = 0;
  8301. if (typeof args[0] !== "object" && !Array.isArray(args[0])) {
  8302. throw new TypeError("Utils.waitAnyNode 第一个参数必须是string[]");
  8303. }
  8304. if (args.length === 1) ;
  8305. else if (args.length === 2) {
  8306. let secondParam = args[1];
  8307. if (typeof secondParam === "number") {
  8308. // "div",10000
  8309. timeout = secondParam;
  8310. }
  8311. else if (typeof secondParam === "object" &&
  8312. secondParam instanceof Node) {
  8313. // "div",document
  8314. parent = secondParam;
  8315. }
  8316. else {
  8317. throw new TypeError("Utils.waitAnyNode 第二个参数必须是number|Node");
  8318. }
  8319. }
  8320. else if (args.length === 3) {
  8321. // "div",document,10000
  8322. // 第二个参数,parent
  8323. let secondParam = args[1];
  8324. // 第三个参数,timeout
  8325. let thirdParam = args[2];
  8326. if (typeof secondParam === "object" && secondParam instanceof Node) {
  8327. parent = secondParam;
  8328. if (typeof thirdParam === "number") {
  8329. timeout = thirdParam;
  8330. }
  8331. else {
  8332. throw new TypeError("Utils.waitAnyNode 第三个参数必须是number");
  8333. }
  8334. }
  8335. else {
  8336. throw new TypeError("Utils.waitAnyNode 第二个参数必须是Node");
  8337. }
  8338. }
  8339. else {
  8340. throw new TypeError("Utils.waitAnyNode 参数个数错误");
  8341. }
  8342. let promiseList = selectorList.map((selector) => {
  8343. return UtilsContext.waitNode(selector, parent, timeout);
  8344. });
  8345. return Promise.any(promiseList);
  8346. }
  8347. waitNodeList(...args) {
  8348. // 过滤掉undefined
  8349. args = args.filter((arg) => arg !== undefined);
  8350. let UtilsContext = this;
  8351. // 选择器数组
  8352. let selector = args[0];
  8353. // 父元素(监听的元素)
  8354. let parent = UtilsContext.windowApi.document;
  8355. // 超时时间
  8356. let timeout = 0;
  8357. if (typeof args[0] !== "string" && !Array.isArray(args[0])) {
  8358. throw new TypeError("Utils.waitNodeList 第一个参数必须是string|string[]");
  8359. }
  8360. if (args.length === 1) ;
  8361. else if (args.length === 2) {
  8362. let secondParam = args[1];
  8363. if (typeof secondParam === "number") {
  8364. // "div",10000
  8365. timeout = secondParam;
  8366. }
  8367. else if (typeof secondParam === "object" &&
  8368. secondParam instanceof Node) {
  8369. // "div",document
  8370. parent = secondParam;
  8371. }
  8372. else {
  8373. throw new TypeError("Utils.waitNodeList 第二个参数必须是number|Node");
  8374. }
  8375. }
  8376. else if (args.length === 3) {
  8377. // "div",document,10000
  8378. // 第二个参数,parent
  8379. let secondParam = args[1];
  8380. // 第三个参数,timeout
  8381. let thirdParam = args[2];
  8382. if (typeof secondParam === "object" && secondParam instanceof Node) {
  8383. parent = secondParam;
  8384. if (typeof thirdParam === "number") {
  8385. timeout = thirdParam;
  8386. }
  8387. else {
  8388. throw new TypeError("Utils.waitNodeList 第三个参数必须是number");
  8389. }
  8390. }
  8391. else {
  8392. throw new TypeError("Utils.waitNodeList 第二个参数必须是Node");
  8393. }
  8394. }
  8395. else {
  8396. throw new TypeError("Utils.waitNodeList 参数个数错误");
  8397. }
  8398. function getNodeList() {
  8399. if (Array.isArray(selector)) {
  8400. let result = [];
  8401. for (let index = 0; index < selector.length; index++) {
  8402. let nodeList = domUtils.selectorAll(selector[index], parent);
  8403. if (nodeList.length) {
  8404. result.push(nodeList);
  8405. }
  8406. }
  8407. if (result.length === selector.length) {
  8408. return result;
  8409. }
  8410. }
  8411. else {
  8412. let nodeList = domUtils.selectorAll(selector, parent);
  8413. if (nodeList.length) {
  8414. return nodeList;
  8415. }
  8416. }
  8417. }
  8418. return UtilsContext.wait(() => {
  8419. let node = getNodeList();
  8420. if (node) {
  8421. return {
  8422. success: true,
  8423. data: node,
  8424. };
  8425. }
  8426. else {
  8427. return {
  8428. success: false,
  8429. data: node,
  8430. };
  8431. }
  8432. }, timeout, parent);
  8433. }
  8434. waitAnyNodeList(...args) {
  8435. // 过滤掉undefined
  8436. args = args.filter((arg) => arg !== undefined);
  8437. let UtilsContext = this;
  8438. // 选择器数组
  8439. let selectorList = args[0];
  8440. // 父元素(监听的元素)
  8441. let parent = UtilsContext.windowApi.document;
  8442. // 超时时间
  8443. let timeout = 0;
  8444. if (!Array.isArray(args[0])) {
  8445. throw new TypeError("Utils.waitAnyNodeList 第一个参数必须是string[]");
  8446. }
  8447. if (args.length === 1) ;
  8448. else if (args.length === 2) {
  8449. let secondParam = args[1];
  8450. if (typeof secondParam === "number") {
  8451. // "div",10000
  8452. timeout = secondParam;
  8453. }
  8454. else if (typeof secondParam === "object" &&
  8455. secondParam instanceof Node) {
  8456. // "div",document
  8457. parent = secondParam;
  8458. }
  8459. else {
  8460. throw new TypeError("Utils.waitAnyNodeList 第二个参数必须是number|Node");
  8461. }
  8462. }
  8463. else if (args.length === 3) {
  8464. // "div",document,10000
  8465. // 第二个参数,parent
  8466. let secondParam = args[1];
  8467. // 第三个参数,timeout
  8468. let thirdParam = args[2];
  8469. if (typeof secondParam === "object" && secondParam instanceof Node) {
  8470. parent = secondParam;
  8471. if (typeof thirdParam === "number") {
  8472. timeout = thirdParam;
  8473. }
  8474. else {
  8475. throw new TypeError("Utils.waitAnyNodeList 第三个参数必须是number");
  8476. }
  8477. }
  8478. else {
  8479. throw new TypeError("Utils.waitAnyNodeList 第二个参数必须是Node");
  8480. }
  8481. }
  8482. else {
  8483. throw new TypeError("Utils.waitAnyNodeList 参数个数错误");
  8484. }
  8485. let promiseList = selectorList.map((selector) => {
  8486. return UtilsContext.waitNodeList(selector, parent, timeout);
  8487. });
  8488. return Promise.any(promiseList);
  8489. }
  8490. waitProperty(checkObj, checkPropertyName) {
  8491. return new Promise((resolve) => {
  8492. let obj = checkObj;
  8493. if (typeof checkObj === "function") {
  8494. obj = checkObj();
  8495. }
  8496. if (Reflect.has(obj, checkPropertyName)) {
  8497. resolve(obj[checkPropertyName]);
  8498. }
  8499. else {
  8500. Object.defineProperty(obj, checkPropertyName, {
  8501. set: function (value) {
  8502. try {
  8503. resolve(value);
  8504. }
  8505. catch (error) {
  8506. console.error("Error setting property:", error);
  8507. }
  8508. },
  8509. });
  8510. }
  8511. });
  8512. }
  8513. waitPropertyByInterval(checkObj, checkPropertyName, intervalTimer = 250, maxTime = -1) {
  8514. let UtilsContext = this;
  8515. if (checkObj == null) {
  8516. throw new TypeError("checkObj 不能为空对象 ");
  8517. }
  8518. let isResolve = false;
  8519. return new Promise((resolve, reject) => {
  8520. let interval = UtilsContext.workerSetInterval(() => {
  8521. let obj = checkObj;
  8522. if (typeof checkObj === "function") {
  8523. obj = checkObj();
  8524. }
  8525. if (typeof obj !== "object") {
  8526. return;
  8527. }
  8528. if (obj == null) {
  8529. return;
  8530. }
  8531. if ((typeof checkPropertyName === "function" && checkPropertyName(obj)) ||
  8532. Reflect.has(obj, checkPropertyName)) {
  8533. isResolve = true;
  8534. UtilsContext.workerClearInterval(interval);
  8535. resolve(obj[checkPropertyName]);
  8536. }
  8537. }, intervalTimer);
  8538. if (maxTime !== -1) {
  8539. UtilsContext.workerSetTimeout(() => {
  8540. if (!isResolve) {
  8541. UtilsContext.workerClearInterval(interval);
  8542. reject();
  8543. }
  8544. }, maxTime);
  8545. }
  8546. });
  8547. }
  8548. async waitVueByInterval(element, propertyName, timer = 250, maxTime = -1, vueName = "__vue__") {
  8549. if (element == null) {
  8550. throw new Error("Utils.waitVueByInterval 参数element 不能为空");
  8551. }
  8552. let flag = false;
  8553. let UtilsContext = this;
  8554. try {
  8555. await UtilsContext.waitPropertyByInterval(element, function (targetElement) {
  8556. if (targetElement == null) {
  8557. return false;
  8558. }
  8559. if (!(vueName in targetElement)) {
  8560. return false;
  8561. }
  8562. if (propertyName == null) {
  8563. return true;
  8564. }
  8565. let vueObject = targetElement[vueName];
  8566. if (typeof propertyName === "string") {
  8567. if (propertyName in vueObject) {
  8568. flag = true;
  8569. return true;
  8570. }
  8571. }
  8572. else {
  8573. /* Function */
  8574. if (propertyName(vueObject)) {
  8575. flag = true;
  8576. return true;
  8577. }
  8578. }
  8579. return false;
  8580. }, timer, maxTime);
  8581. }
  8582. catch (error) {
  8583. return flag;
  8584. }
  8585. return flag;
  8586. }
  8587. watchObject(target, propertyName, getCallBack, setCallBack) {
  8588. if (typeof getCallBack !== "function" &&
  8589. typeof setCallBack !== "function") {
  8590. return;
  8591. }
  8592. if (typeof getCallBack === "function") {
  8593. Object.defineProperty(target, propertyName, {
  8594. get() {
  8595. if (typeof getCallBack === "function") {
  8596. return getCallBack(target[propertyName]);
  8597. }
  8598. else {
  8599. return target[propertyName];
  8600. }
  8601. },
  8602. });
  8603. }
  8604. else if (typeof setCallBack === "function") {
  8605. Object.defineProperty(target, propertyName, {
  8606. set(value) {
  8607. if (typeof setCallBack === "function") {
  8608. setCallBack(value);
  8609. }
  8610. },
  8611. });
  8612. }
  8613. else {
  8614. Object.defineProperty(target, propertyName, {
  8615. get() {
  8616. if (typeof getCallBack === "function") {
  8617. return getCallBack(target[propertyName]);
  8618. }
  8619. else {
  8620. return target[propertyName];
  8621. }
  8622. },
  8623. set(value) {
  8624. if (typeof setCallBack === "function") {
  8625. setCallBack(value);
  8626. }
  8627. },
  8628. });
  8629. }
  8630. }
  8631. /**
  8632. * 深度获取对象属性
  8633. * @param target 待获取的对象
  8634. * @param handler 获取属性的回调
  8635. */
  8636. queryProperty(target, handler) {
  8637. if (target == null) {
  8638. return;
  8639. }
  8640. let handleResult = handler(target);
  8641. if (handleResult &&
  8642. typeof handleResult.isFind === "boolean" &&
  8643. handleResult.isFind) {
  8644. return handleResult.data;
  8645. }
  8646. return this.queryProperty(handleResult.data, handler);
  8647. }
  8648. /**
  8649. * 创建一个新的Utils实例
  8650. * @param option
  8651. * @returns
  8652. */
  8653. createUtils(option) {
  8654. return new Utils(option);
  8655. }
  8656. /**
  8657. * 将对象转换为FormData
  8658. * @param data 待转换的对象
  8659. * @param isEncode 是否对值为string进行编码转换(encodeURIComponent),默认false
  8660. * @param valueAutoParseToStr 是否对值强制使用JSON.stringify()转换,默认false
  8661. * @example
  8662. * Utils.toFormData({
  8663. * test: "1",
  8664. * 666: 666,
  8665. * })
  8666. */
  8667. toFormData(data, isEncode = false, valueAutoParseToStr = false) {
  8668. const formData = new FormData();
  8669. Object.keys(data).forEach((key) => {
  8670. let value = data[key];
  8671. if (valueAutoParseToStr) {
  8672. value = JSON.stringify(value);
  8673. }
  8674. if (typeof value === "number") {
  8675. value = value.toString();
  8676. }
  8677. if (isEncode && typeof value === "string") {
  8678. value = encodeURIComponent(value);
  8679. }
  8680. if (value instanceof File) {
  8681. formData.append(key, value, value.name);
  8682. }
  8683. else {
  8684. formData.append(key, value);
  8685. }
  8686. });
  8687. return formData;
  8688. }
  8689. /**
  8690. * 将链接转为URL对象,自动补充URL的protocol或者origin
  8691. * @param text 需要转换的链接字符串
  8692. * @example
  8693. * Utils.toUrl("//www.baidu.com/s?word=666");
  8694. * Utils.toUrl("/s?word=666");
  8695. */
  8696. toUrl(text) {
  8697. if (typeof text !== "string") {
  8698. throw new TypeError("toUrl: text must be string");
  8699. }
  8700. text = text.trim();
  8701. if (text === "") {
  8702. throw new TypeError("toUrl: text must not be empty");
  8703. }
  8704. if (text.startsWith("//")) {
  8705. /* //www.baidu.com/xxxxxxx */
  8706. /* 没有protocol,加上 */
  8707. text = this.windowApi.globalThis.location.protocol + text;
  8708. }
  8709. else if (text.startsWith("/")) {
  8710. /* /xxx/info?xxx=xxx */
  8711. /* 没有Origin,加上 */
  8712. text = this.windowApi.globalThis.location.origin + text;
  8713. }
  8714. return new URL(text);
  8715. }
  8716. /**
  8717. * 覆盖对象中的函数this指向
  8718. * @param target 需要覆盖的对象
  8719. * @param [objectThis] 覆盖的this指向,如果为传入,则默认为对象本身
  8720. */
  8721. coverObjectFunctionThis = commonUtil.coverObjectFunctionThis.bind(commonUtil);
  8722. /**
  8723. * 生成uuid
  8724. * @example
  8725. * Utils.generateUUID()
  8726. */
  8727. generateUUID = GenerateUUID;
  8728. /**
  8729. * 自定义的动态响应对象
  8730. * @example
  8731. * let vue = new Utils.Vue();
  8732. * let reactive = new vue.reactive({});
  8733. * vue.watch(()=>reactive["name"], (newValue, oldValue)=>{
  8734. * console.log("newValue ==> " + newValue);
  8735. * console.log("oldValue ==> " + oldValue);
  8736. * })
  8737. * vue["name"] = "测试";
  8738. * > "测试"
  8739. */
  8740. Vue = Vue;
  8741. ModuleRaid = ModuleRaid;
  8742. /**
  8743. * 自动使用 Worker 执行 setTimeout
  8744. * @param callback 回调函数
  8745. * @param [timeout=0] 延迟时间,默认为0
  8746. */
  8747. workerSetTimeout(callback, timeout = 0) {
  8748. try {
  8749. return setTimeout$1(callback, timeout);
  8750. }
  8751. catch (error) {
  8752. return globalThis.setTimeout(callback, timeout);
  8753. }
  8754. }
  8755. /**
  8756. * 配合 .setTimeout 使用
  8757. * @param timeId setTimeout 返回的`id`
  8758. */
  8759. workerClearTimeout(timeId) {
  8760. try {
  8761. if (timeId != null) {
  8762. clearTimeout$1(timeId);
  8763. }
  8764. }
  8765. catch (error) {
  8766. }
  8767. finally {
  8768. globalThis.clearTimeout(timeId);
  8769. }
  8770. }
  8771. /**
  8772. * 自动使用 Worker 执行 setInterval
  8773. * @param callback 回调函数
  8774. * @param timeout 间隔时间,默认为0
  8775. */
  8776. workerSetInterval(callback, timeout = 0) {
  8777. try {
  8778. return setInterval(callback, timeout);
  8779. }
  8780. catch (error) {
  8781. return globalThis.setInterval(callback, timeout);
  8782. }
  8783. }
  8784. /**
  8785. * 配合 .setInterval 使用
  8786. * @param timeId setInterval 返回的`id`
  8787. */
  8788. workerClearInterval(timeId) {
  8789. try {
  8790. if (timeId != null) {
  8791. clearInterval(timeId);
  8792. }
  8793. }
  8794. catch (error) {
  8795. }
  8796. finally {
  8797. globalThis.clearInterval(timeId);
  8798. }
  8799. }
  8800. /**
  8801. * 获取剪贴板信息
  8802. */
  8803. async getClipboardInfo() {
  8804. return new Promise((resolve) => {
  8805. /** 读取剪贴板 */
  8806. function readClipboardText() {
  8807. navigator.clipboard
  8808. .readText()
  8809. .then((clipboardText) => {
  8810. resolve({
  8811. error: null,
  8812. content: clipboardText,
  8813. });
  8814. })
  8815. .catch((error) => {
  8816. resolve({
  8817. error: error,
  8818. content: "",
  8819. });
  8820. });
  8821. }
  8822. /** 申请读取剪贴板的权限 */
  8823. function requestPermissionsWithClipboard() {
  8824. navigator.permissions
  8825. .query({
  8826. // @ts-ignore
  8827. name: "clipboard-read",
  8828. })
  8829. .then((permissionStatus) => {
  8830. readClipboardText();
  8831. })
  8832. .catch((error) => {
  8833. /* 该权限申请Api可能在该环境下不生效,尝试直接读取剪贴板 */
  8834. readClipboardText();
  8835. });
  8836. }
  8837. /**
  8838. * 检查当前环境是否支持读取剪贴板Api
  8839. */
  8840. function checkClipboardApi() {
  8841. if (typeof navigator?.clipboard?.readText !== "function") {
  8842. return false;
  8843. }
  8844. if (typeof navigator?.permissions?.query !== "function") {
  8845. return false;
  8846. }
  8847. return true;
  8848. }
  8849. if (!checkClipboardApi()) {
  8850. resolve({
  8851. error: new Error("当前环境不支持读取剪贴板Api"),
  8852. content: "",
  8853. });
  8854. return;
  8855. }
  8856. if (document.hasFocus()) {
  8857. requestPermissionsWithClipboard();
  8858. }
  8859. else {
  8860. window.addEventListener("focus", () => {
  8861. requestPermissionsWithClipboard();
  8862. }, {
  8863. once: true,
  8864. });
  8865. }
  8866. });
  8867. }
  8868. }
  8869. let utils = new Utils();
  8870.  
  8871. return utils;
  8872.  
  8873. }));
  8874. //# sourceMappingURL=index.umd.js.map