@whitesev/utils 1.0.5 → 1.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (59) hide show
  1. package/dist/index.amd.js +2123 -2269
  2. package/dist/index.amd.js.map +1 -1
  3. package/dist/index.cjs.js +2123 -2269
  4. package/dist/index.cjs.js.map +1 -1
  5. package/dist/index.esm.js +2123 -2269
  6. package/dist/index.esm.js.map +1 -1
  7. package/dist/index.iife.js +2123 -2269
  8. package/dist/index.iife.js.map +1 -1
  9. package/dist/index.system.js +2123 -2269
  10. package/dist/index.system.js.map +1 -1
  11. package/dist/index.umd.js +2123 -2269
  12. package/dist/index.umd.js.map +1 -1
  13. package/dist/src/Dictionary.d.ts +82 -0
  14. package/dist/src/Hooks.d.ts +11 -0
  15. package/dist/src/Httpx.d.ts +1201 -0
  16. package/dist/src/LockFunction.d.ts +31 -0
  17. package/dist/src/Log.d.ts +96 -0
  18. package/dist/src/Progress.d.ts +37 -0
  19. package/dist/src/UtilsGMMenu.d.ts +156 -0
  20. package/dist/src/index.d.ts +20 -27
  21. package/dist/src/indexedDB.d.ts +73 -0
  22. package/dist/src/tryCatch.d.ts +31 -0
  23. package/package.json +36 -36
  24. package/src/Dictionary.ts +152 -0
  25. package/src/{Hooks/Hooks.js → Hooks.ts} +31 -17
  26. package/src/{Httpx/index.d.ts → Httpx.ts} +837 -46
  27. package/src/LockFunction.ts +62 -0
  28. package/src/Log.ts +281 -0
  29. package/src/Progress.ts +143 -0
  30. package/src/UtilsGMMenu.ts +681 -0
  31. package/src/index.ts +17 -29
  32. package/src/indexedDB.ts +421 -0
  33. package/src/tryCatch.ts +107 -0
  34. package/tsconfig.json +1 -1
  35. package/dist/src/Dictionary/Dictionary.d.ts +0 -85
  36. package/dist/src/Hooks/Hooks.d.ts +0 -5
  37. package/dist/src/Httpx/Httpx.d.ts +0 -50
  38. package/dist/src/LockFunction/LockFunction.d.ts +0 -16
  39. package/dist/src/Log/Log.d.ts +0 -66
  40. package/dist/src/Progress/Progress.d.ts +0 -6
  41. package/dist/src/UtilsGMMenu/UtilsGMMenu.d.ts +0 -119
  42. package/dist/src/indexedDB/indexedDB.d.ts +0 -165
  43. package/dist/src/tryCatch/tryCatch.d.ts +0 -31
  44. package/src/Dictionary/Dictionary.js +0 -157
  45. package/src/Dictionary/index.d.ts +0 -52
  46. package/src/Hooks/index.d.ts +0 -16
  47. package/src/Httpx/Httpx.js +0 -747
  48. package/src/LockFunction/LockFunction.js +0 -35
  49. package/src/LockFunction/index.d.ts +0 -47
  50. package/src/Log/Log.js +0 -256
  51. package/src/Log/index.d.ts +0 -91
  52. package/src/Progress/Progress.js +0 -98
  53. package/src/Progress/index.d.ts +0 -30
  54. package/src/UtilsGMMenu/UtilsGMMenu.js +0 -464
  55. package/src/UtilsGMMenu/index.d.ts +0 -224
  56. package/src/indexedDB/index.d.ts +0 -128
  57. package/src/indexedDB/indexedDB.js +0 -355
  58. package/src/tryCatch/index.d.ts +0 -6
  59. package/src/tryCatch/tryCatch.js +0 -100
package/dist/index.esm.js CHANGED
@@ -940,2284 +940,2139 @@ const AjaxHooker = function () {
940
940
  })();
941
941
  };
942
942
 
943
- /**
944
- *
945
- * @param {UtilsGMMenuConstructorOptions} details
946
- */
947
- const GMMenu = function (details) {
948
- const GM_Api = {
949
- /**
950
- * 获取存储的数据
951
- * @type {GM_getValue}
952
- */
953
- getValue: details.GM_getValue,
954
- /**
955
- * 设置数据到存储
956
- * @type {GM_setValue}
957
- */
958
- setValue: details.GM_setValue,
959
- /**
960
- * 注册菜单
961
- * @type {GM_registerMenuCommand}
962
- */
963
- registerMenuCommand: details.GM_registerMenuCommand,
964
- /**
965
- * 卸载菜单
966
- * @type {GM_unregisterMenuCommand}
967
- */
968
- unregisterMenuCommand: details.GM_unregisterMenuCommand,
969
- };
970
- for (const keyName of Object.keys(GM_Api)) {
971
- if (typeof GM_Api[keyName] !== "function") {
972
- throw new Error(
973
- `Utils.GM_Menu 请在脚本开头加上 @grant ${keyName},且传入该对象`
974
- );
975
- }
976
- }
977
- /** 上下文 */
978
- const context = this;
979
-
980
- const MenuHandle = {
981
- $data: {
982
- /**
983
- * 菜单数据
984
- * @type {UtilsGMMenuOptionData[]}
985
- */
986
- data: [],
987
- /**
988
- * 本地存储的键名
989
- */
990
- key: "GM_Menu_Local_Map",
991
- },
992
- $default: {
993
- /** 自动刷新网页,默认为true */
994
- autoReload:
995
- typeof details.autoReload === "boolean" ? details.autoReload : true,
996
- /**
997
- * 菜单isStoreValue的默认值
998
- */
999
- isStoreValue: true,
1000
- },
1001
- $emoji: {
1002
- /**
1003
- * 菜单enable为true的emoji
1004
- */
1005
- success: "",
1006
- /**
1007
- * 菜单enable为false的emoji
1008
- */
1009
- error: "❌",
1010
- },
1011
- /**
1012
- * 初始化数据
1013
- */
1014
- init() {
1015
- for (let index = 0; index < this.$data.data.length; index++) {
1016
- let menuOption = this.$data.data[index]["data"];
1017
- menuOption.enable = Boolean(
1018
- this.getLocalMenuData(menuOption.key, menuOption.enable)
1019
- );
1020
- if (typeof menuOption.showText !== "function") {
1021
- menuOption.showText = (menuText, menuEnable) => {
1022
- if (menuEnable) {
1023
- return this.$emoji.success + " " + menuText;
1024
- } else {
1025
- return this.$emoji.error + " " + menuText;
1026
- }
1027
- };
1028
- }
1029
- }
1030
- },
1031
- /**
1032
- * 注册油猴菜单
1033
- * @param { ?UtilsGMMenuOptionData[] } menuOptions 如果存在,使用它
1034
- */
1035
- register(menuOptions) {
1036
- if (menuOptions == null) {
1037
- throw new TypeError("register菜单数据不能为空");
1038
- }
1039
- if (!Array.isArray(menuOptions)) {
1040
- menuOptions = [menuOptions];
1041
- }
1042
- for (let index = 0; index < menuOptions.length; index++) {
1043
- let cloneMenuOptionData = Utils.deepClone(menuOptions[index].data);
1044
- const { showText, clickCallBack } =
1045
- this.handleMenuData(cloneMenuOptionData);
1046
- let menuId = GM_Api.registerMenuCommand(showText, clickCallBack);
1047
- menuOptions[index].id = menuId;
1048
- cloneMenuOptionData.deleteMenu = function () {
1049
- GM_Api.unregisterMenuCommand(menuId);
1050
- };
1051
- Reflect.deleteProperty(menuOptions[index], "handleData");
1052
- menuOptions[index].handleData = cloneMenuOptionData;
1053
- }
1054
- },
1055
- /**
1056
- * 获取本地存储菜单键值
1057
- * @param {string} key 键
1058
- * @returns {boolean}
1059
- */
1060
- getLocalMenuData(key, defaultValue) {
1061
- let localData = GM_Api.getValue(this.$data.key, {});
1062
- if (key in localData) {
1063
- return localData[key];
1064
- } else {
1065
- return defaultValue;
1066
- }
1067
- },
1068
- /**
1069
- * 设置本地存储菜单键值
1070
- * @param {string} key
1071
- * @param {boolean} value 值
1072
- */
1073
- setLocalMenuData(key, value) {
1074
- let localData = GM_Api.getValue(this.$data.key, {});
1075
- localData[key] = value;
1076
- GM_Api.setValue(this.$data.key, localData);
1077
- },
1078
- /**
1079
- * 处理初始化配置
1080
- * @param { UtilsGMMenuOption } menuOption
1081
- */
1082
- handleInitDetail(menuOption) {
1083
- menuOption.enable = Boolean(
1084
- this.getLocalMenuData(menuOption.key, menuOption.enable)
1085
- );
1086
- if (typeof menuOption.showText !== "function") {
1087
- menuOption.showText = (menuText, menuEnable) => {
1088
- if (menuEnable) {
1089
- return this.$emoji.success + " " + menuText;
1090
- } else {
1091
- return this.$emoji.error + " " + menuText;
1092
- }
1093
- };
1094
- }
1095
- return menuOption;
1096
- },
1097
- /**
1098
- * 对菜单数据进行处理
1099
- * @param { UtilsGMMenuOption } menuOption
1100
- */
1101
- handleMenuData(menuOption) {
1102
- let menuLocalDataItemKey = menuOption.key;
1103
- /* 菜单默认开启的状态 */
1104
- let defaultEnable = Boolean(
1105
- this.getLocalMenuData(menuLocalDataItemKey, menuOption.enable)
1106
- );
1107
- /** 油猴菜单上显示的文本 */
1108
- let showText = menuOption.showText(menuOption.text, defaultEnable);
1109
- const that = this;
1110
- ({
1111
- /**
1112
- * 菜单的id
1113
- */
1114
- id: menuOption.id,
1115
- /**
1116
- * 点击菜单项后是否应关闭弹出菜单
1117
- */
1118
- autoClose: menuOption.autoClose,
1119
- /**
1120
- * 菜单项的可选访问键
1121
- */
1122
- accessKey: menuOption.accessKey,
1123
- /**
1124
- * 菜单项的鼠标悬浮上的工具提示
1125
- */
1126
- title: menuOption.title,
1127
- });
1128
- /* 点击菜单后触发callback后的网页是否刷新 */
1129
- menuOption.autoReload =
1130
- typeof menuOption.autoReload !== "boolean"
1131
- ? this.$default.autoReload
1132
- : menuOption.autoReload;
1133
- /* 点击菜单后触发callback后的网页是否存储值 */
1134
- menuOption.isStoreValue =
1135
- typeof menuOption.isStoreValue !== "boolean"
1136
- ? this.$default.isStoreValue
1137
- : menuOption.isStoreValue;
1138
- /**
1139
- * 用户点击菜单后的回调函数
1140
- * @param {MouseEvent|PointerEvent} event
1141
- */
1142
- function clickCallBack(event) {
1143
- let localEnable = Boolean(
1144
- that.getLocalMenuData(menuLocalDataItemKey, defaultEnable)
1145
- );
1146
- if (menuOption.isStoreValue) {
1147
- that.setLocalMenuData(menuLocalDataItemKey, !localEnable);
1148
- }
1149
- if (typeof menuOption.callback === "function") {
1150
- menuOption.callback({
1151
- key: menuLocalDataItemKey,
1152
- enable: !localEnable,
1153
- oldEnable: localEnable,
1154
- event: event,
1155
- storeValue(value) {
1156
- that.setLocalMenuData(menuLocalDataItemKey, value);
1157
- },
1158
- });
1159
- }
1160
- /* 不刷新网页就刷新菜单 */
1161
- if (menuOption.autoReload) {
1162
- window.location.reload();
1163
- } else {
1164
- context.update();
1165
- }
1166
- }
1167
-
1168
- return {
1169
- showText,
1170
- clickCallBack,
1171
- };
1172
- },
1173
- /**
1174
- * 获取目标菜单配置数据
1175
- * @param {string} menuKey 菜单-键key
1176
- */
1177
- getMenuData(menuKey) {
1178
- return this.$data.data.find((item) => item.data.key === menuKey);
1179
- },
1180
- /**
1181
- * 获取目标菜单配置
1182
- * @param {string} menuKey 菜单-键key
1183
- */
1184
- getMenuOption(menuKey) {
1185
- return this.$data.data.find((item) => item.data.key === menuKey)?.data;
1186
- },
1187
- /**
1188
- * 获取目标菜单处理后的配置
1189
- * @param {string} menuKey 菜单-键key
1190
- */
1191
- getMenuHandledOption(menuKey) {
1192
- return this.$data.data.find((item) => item.handleData.key === menuKey)
1193
- ?.handleData;
1194
- },
1195
- };
1196
-
1197
- /**
1198
- * 新增菜单数据
1199
- * @param {UtilsGMMenuOption[]|UtilsGMMenuOption} paramData
1200
- */
1201
- this.add = function (paramData) {
1202
- if (Array.isArray(paramData)) {
1203
- for (const _paramData of paramData) {
1204
- MenuHandle.$data.data.push({
1205
- data: _paramData,
1206
- id: void 0,
1207
- });
1208
- }
1209
- } else {
1210
- MenuHandle.$data.data.push({
1211
- data: paramData,
1212
- id: void 0,
1213
- });
1214
- }
1215
- this.update();
1216
- };
1217
- /**
1218
- * 更新菜单数据
1219
- * @param { ?UtilsGMMenuOption[]|UtilsGMMenuOption } options 数据
1220
- */
1221
- this.update = function (options) {
1222
- /**
1223
- * @type {UtilsGMMenuOption[]}
1224
- */
1225
- let optionsList = [];
1226
- if (Array.isArray(options)) {
1227
- optionsList = [...optionsList, ...options];
1228
- } else if (options != null) {
1229
- optionsList = [...optionsList, options];
1230
- }
1231
- optionsList.forEach((item) => {
1232
- let targetMenu = MenuHandle.getMenuOption(item.key);
1233
- if (targetMenu) {
1234
- Object.assign(targetMenu, item);
1235
- }
1236
- });
1237
- MenuHandle.$data.data.forEach((value) => {
1238
- if (value.handleData) {
1239
- value.handleData.deleteMenu();
1240
- }
1241
- });
1242
- MenuHandle.init();
1243
- MenuHandle.register(MenuHandle.$data.data);
1244
- };
1245
- /**
1246
- * 卸载菜单
1247
- * @param {number} menuId 已注册的菜单id
1248
- */
1249
- this.delete = function (menuId) {
1250
- GM_Api.unregisterMenuCommand(menuId);
1251
- };
1252
- /**
1253
- * 根据键值获取enable
1254
- * @param {string} menuKey 菜单-键key
1255
- * @returns {boolean}
1256
- */
1257
- this.get = function (menuKey) {
1258
- return this.getEnable(menuKey);
1259
- };
1260
- /**
1261
- * 根据键值获取enable值
1262
- * @param {string} menuKey 菜单-键key
1263
- * @returns {boolean}
1264
- */
1265
- this.getEnable = function (menuKey) {
1266
- return MenuHandle.getMenuHandledOption(menuKey).enable;
1267
- };
1268
- /**
1269
- * 根据键值获取text值
1270
- * @param {string} menuKey 菜单-键key
1271
- * @returns {string}
1272
- */
1273
- this.getText = function (menuKey) {
1274
- return MenuHandle.getMenuHandledOption(menuKey).text;
1275
- };
1276
- /**
1277
- * 根据键值获取showText函数的值
1278
- * @param {string} menuKey 菜单-键key
1279
- * @returns {string}
1280
- */
1281
- this.getShowTextValue = function (menuKey) {
1282
- return MenuHandle.getMenuHandledOption(menuKey).showText(
1283
- this.getText(menuKey),
1284
- this.get(menuKey)
1285
- );
1286
- };
1287
- /**
1288
- * 获取当前已注册菜单的id
1289
- * @param {string} menuKey
1290
- * @returns {?number}
1291
- */
1292
- this.getMenuId = function (menuKey) {
1293
- let result = null;
1294
- for (let index = 0; index < MenuHandle.$data.data.length; index++) {
1295
- const optionData = MenuHandle.$data.data[index];
1296
- if (optionData.handleData.key === menuKey) {
1297
- result = optionData.id;
1298
- break;
1299
- }
1300
- }
1301
- return result;
1302
- };
1303
- /**
1304
- * 根据键值获取accessKey值
1305
- * @param {string} menuKey 菜单-键key
1306
- * @returns {?string}
1307
- */
1308
- this.getAccessKey = function (menuKey) {
1309
- return MenuHandle.getMenuHandledOption(menuKey).accessKey;
1310
- };
1311
- /**
1312
- * 根据键值获取autoClose值
1313
- * @param {string} menuKey 菜单-键key
1314
- * @returns {?boolean}
1315
- */
1316
- this.getAutoClose = function (menuKey) {
1317
- return MenuHandle.getMenuHandledOption(menuKey).autoClose;
1318
- };
1319
- /**
1320
- * 根据键值获取autoReload值
1321
- * @param {string} menuKey 菜单-键key
1322
- * @returns {boolean}
1323
- */
1324
- this.getAutoReload = function (menuKey) {
1325
- return MenuHandle.getMenuHandledOption(menuKey).autoReload;
1326
- };
1327
- /**
1328
- * 根据键值获取callback函数
1329
- * @param {string} menuKey 菜单-键key
1330
- * @returns {?Function}
1331
- */
1332
- this.getCallBack = function (menuKey) {
1333
- return MenuHandle.getMenuHandledOption(menuKey).callback;
1334
- };
1335
- /**
1336
- * 获取当enable为true时默认显示在菜单中前面的emoji图标
1337
- * @returns {string}
1338
- */
1339
- this.getEnableTrueEmoji = function () {
1340
- return MenuHandle.$emoji.success;
1341
- };
1342
- /**
1343
- * 获取当enable为false时默认显示在菜单中前面的emoji图标
1344
- * @returns {string}
1345
- */
1346
- this.getEnableFalseEmoji = function () {
1347
- return MenuHandle.$emoji.error;
1348
- };
1349
- /**
1350
- * 获取本地存储的菜单外部的键名
1351
- * @param {string} keyName
1352
- */
1353
- this.getLocalStorageKeyName = function () {
1354
- return MenuHandle.$data.key;
1355
- };
1356
- /**
1357
- * 设置菜单的值
1358
- * @param {string} menuKey 菜单-键key
1359
- * @param {any} value 需要设置的值
1360
- */
1361
- this.setValue = function (menuKey, value) {
1362
- MenuHandle.setLocalMenuData(menuKey, value);
1363
- };
1364
- /**
1365
- * 设置菜单的值
1366
- * @param {string} menuKey 菜单-键key
1367
- * @param {boolean} value 需要设置的值
1368
- */
1369
- this.setEnable = function (menuKey, value) {
1370
- this.setValue(menuKey, Boolean(value));
1371
- };
1372
- /**
1373
- * 设置当enable为true时默认显示在菜单中前面的emoji图标
1374
- * @param {string} emojiString
1375
- */
1376
- this.setEnableTrueEmoji = function (emojiString) {
1377
- if (typeof emojiString !== "string") {
1378
- throw new Error("参数emojiString必须是string类型");
1379
- }
1380
- MenuHandle.$emoji.success = emojiString;
1381
- };
1382
- /**
1383
- * 设置当enable为false时默认显示在菜单中前面的emoji图标
1384
- * @param {string} emojiString
1385
- */
1386
- this.setEnableFalseEmoji = function (emojiString) {
1387
- if (typeof emojiString !== "string") {
1388
- throw new Error("参数emojiString必须是string类型");
1389
- }
1390
- MenuHandle.$emoji.error = emojiString;
1391
- };
1392
- /**
1393
- * 设置本地存储的菜单外部的键名
1394
- * @param {string} keyName
1395
- */
1396
- this.setLocalStorageKeyName = function (keyName) {
1397
- if (typeof keyName !== "string") {
1398
- throw new Error("参数keyName必须是string类型");
1399
- }
1400
- MenuHandle.$data.key = keyName;
1401
- };
1402
-
1403
- this.add(details?.data || []);
1404
- };
943
+ class GMMenu {
944
+ GM_Api = {
945
+ /**
946
+ * 获取存储的数据
947
+ */
948
+ getValue: GM_getValue,
949
+ /**
950
+ * 设置数据到存储
951
+ */
952
+ setValue: GM_setValue,
953
+ /**
954
+ * 注册菜单
955
+ */
956
+ registerMenuCommand: GM_registerMenuCommand,
957
+ /**
958
+ * 卸载菜单
959
+ */
960
+ unregisterMenuCommand: GM_unregisterMenuCommand,
961
+ };
962
+ MenuHandle = {
963
+ context: this,
964
+ $data: {
965
+ /**
966
+ * 菜单数据
967
+ */
968
+ data: [],
969
+ /**
970
+ * 本地存储的键名
971
+ */
972
+ key: "GM_Menu_Local_Map",
973
+ },
974
+ $default: {
975
+ /** 自动刷新网页,默认为true */
976
+ autoReload: true,
977
+ /**
978
+ * 菜单isStoreValue的默认值
979
+ */
980
+ isStoreValue: true,
981
+ },
982
+ $emoji: {
983
+ /**
984
+ * 菜单enable为true的emoji
985
+ */
986
+ success: "✅",
987
+ /**
988
+ * 菜单enable为false的emoji
989
+ */
990
+ error: "",
991
+ },
992
+ /**
993
+ * 初始化数据
994
+ */
995
+ init() {
996
+ for (let index = 0; index < this.$data.data.length; index++) {
997
+ let menuOption = this.$data.data[index]["data"];
998
+ menuOption.enable = Boolean(this.getLocalMenuData(menuOption.key, menuOption.enable));
999
+ if (typeof menuOption.showText !== "function") {
1000
+ menuOption.showText = (menuText, menuEnable) => {
1001
+ if (menuEnable) {
1002
+ return this.$emoji.success + " " + menuText;
1003
+ }
1004
+ else {
1005
+ return this.$emoji.error + " " + menuText;
1006
+ }
1007
+ };
1008
+ }
1009
+ }
1010
+ },
1011
+ /**
1012
+ * 注册油猴菜单
1013
+ * @param menuOptions 如果存在,使用它
1014
+ */
1015
+ register(menuOptions) {
1016
+ let that = this;
1017
+ if (menuOptions == null) {
1018
+ throw new TypeError("register菜单数据不能为空");
1019
+ }
1020
+ if (!Array.isArray(menuOptions)) {
1021
+ menuOptions = [menuOptions];
1022
+ }
1023
+ for (let index = 0; index < menuOptions.length; index++) {
1024
+ let cloneMenuOptionData = utils.deepClone(menuOptions[index].data);
1025
+ const { showText, clickCallBack } = this.handleMenuData(cloneMenuOptionData);
1026
+ let menuId = that.context.GM_Api.registerMenuCommand(showText, clickCallBack);
1027
+ menuOptions[index].id = menuId;
1028
+ cloneMenuOptionData.deleteMenu = function () {
1029
+ that.context.GM_Api.unregisterMenuCommand(menuId);
1030
+ };
1031
+ Reflect.deleteProperty(menuOptions[index], "handleData");
1032
+ menuOptions[index].handleData = cloneMenuOptionData;
1033
+ }
1034
+ },
1035
+ /**
1036
+ * 获取本地存储菜单键值
1037
+ * @param {string} key 键
1038
+ */
1039
+ getLocalMenuData(key, defaultValue) {
1040
+ let localData = this.context.GM_Api.getValue(this.$data.key, {});
1041
+ if (key in localData) {
1042
+ return localData[key];
1043
+ }
1044
+ else {
1045
+ return defaultValue;
1046
+ }
1047
+ },
1048
+ /**
1049
+ * 设置本地存储菜单键值
1050
+ * @param key 键
1051
+ * @param value 值
1052
+ */
1053
+ setLocalMenuData(key, value) {
1054
+ let localData = this.context.GM_Api.getValue(this.$data.key, {});
1055
+ localData[key] = value;
1056
+ this.context.GM_Api.setValue(this.$data.key, localData);
1057
+ },
1058
+ /**
1059
+ * 处理初始化配置
1060
+ * @param menuOption
1061
+ */
1062
+ handleInitDetail(menuOption) {
1063
+ menuOption.enable = Boolean(this.getLocalMenuData(menuOption.key, menuOption.enable));
1064
+ if (typeof menuOption.showText !== "function") {
1065
+ menuOption.showText = (menuText, menuEnable) => {
1066
+ if (menuEnable) {
1067
+ return this.$emoji.success + " " + menuText;
1068
+ }
1069
+ else {
1070
+ return this.$emoji.error + " " + menuText;
1071
+ }
1072
+ };
1073
+ }
1074
+ return menuOption;
1075
+ },
1076
+ /**
1077
+ * 对菜单数据进行处理
1078
+ * @param menuOption
1079
+ */
1080
+ handleMenuData(menuOption) {
1081
+ let that = this;
1082
+ let menuLocalDataItemKey = menuOption.key;
1083
+ /* 菜单默认开启的状态 */
1084
+ let defaultEnable = Boolean(this.getLocalMenuData(menuLocalDataItemKey, menuOption.enable));
1085
+ /** 油猴菜单上显示的文本 */
1086
+ let showText = menuOption.showText(menuOption.text, defaultEnable);
1087
+ ({
1088
+ /**
1089
+ * 菜单的id
1090
+ */
1091
+ id: menuOption.id,
1092
+ /**
1093
+ * 点击菜单项后是否应关闭弹出菜单
1094
+ */
1095
+ autoClose: menuOption.autoClose,
1096
+ /**
1097
+ * 菜单项的可选访问键
1098
+ */
1099
+ accessKey: menuOption.accessKey,
1100
+ /**
1101
+ * 菜单项的鼠标悬浮上的工具提示
1102
+ */
1103
+ title: menuOption.title,
1104
+ });
1105
+ /* 点击菜单后触发callback后的网页是否刷新 */
1106
+ menuOption.autoReload =
1107
+ typeof menuOption.autoReload !== "boolean"
1108
+ ? this.$default.autoReload
1109
+ : menuOption.autoReload;
1110
+ /* 点击菜单后触发callback后的网页是否存储值 */
1111
+ menuOption.isStoreValue =
1112
+ typeof menuOption.isStoreValue !== "boolean"
1113
+ ? this.$default.isStoreValue
1114
+ : menuOption.isStoreValue;
1115
+ /**
1116
+ * 用户点击菜单后的回调函数
1117
+ * @param event
1118
+ */
1119
+ function clickCallBack(event) {
1120
+ let localEnable = Boolean(that.getLocalMenuData(menuLocalDataItemKey, defaultEnable));
1121
+ if (menuOption.isStoreValue) {
1122
+ that.setLocalMenuData(menuLocalDataItemKey, !localEnable);
1123
+ }
1124
+ if (typeof menuOption.callback === "function") {
1125
+ menuOption.callback({
1126
+ key: menuLocalDataItemKey,
1127
+ enable: !localEnable,
1128
+ oldEnable: localEnable,
1129
+ event: event,
1130
+ storeValue(value) {
1131
+ that.setLocalMenuData(menuLocalDataItemKey, value);
1132
+ },
1133
+ });
1134
+ }
1135
+ /* 不刷新网页就刷新菜单 */
1136
+ if (menuOption.autoReload) {
1137
+ window.location.reload();
1138
+ }
1139
+ else {
1140
+ that.context.update();
1141
+ }
1142
+ }
1143
+ return {
1144
+ showText,
1145
+ clickCallBack,
1146
+ };
1147
+ },
1148
+ /**
1149
+ * 获取目标菜单配置数据
1150
+ * @param menuKey 菜单-键key
1151
+ */
1152
+ getMenuData(menuKey) {
1153
+ return this.$data.data.find((item) => item.data.key === menuKey);
1154
+ },
1155
+ /**
1156
+ * 获取目标菜单配置
1157
+ * @param menuKey 菜单-键key
1158
+ */
1159
+ getMenuOption(menuKey) {
1160
+ return this.$data.data.find((item) => item.data.key === menuKey)?.data;
1161
+ },
1162
+ /**
1163
+ * 获取目标菜单处理后的配置
1164
+ * @param menuKey 菜单-键key
1165
+ */
1166
+ getMenuHandledOption(menuKey) {
1167
+ return this.$data.data.find((item) => item.handleData.key === menuKey)
1168
+ ?.handleData;
1169
+ },
1170
+ };
1171
+ constructor(details) {
1172
+ this.GM_Api.getValue = details.GM_getValue;
1173
+ this.GM_Api.setValue = details.GM_setValue;
1174
+ this.GM_Api.registerMenuCommand = details.GM_registerMenuCommand;
1175
+ this.GM_Api.unregisterMenuCommand = details.GM_unregisterMenuCommand;
1176
+ this.MenuHandle.$default.autoReload =
1177
+ typeof details.autoReload === "boolean" ? details.autoReload : true;
1178
+ for (const keyName of Object.keys(this.GM_Api)) {
1179
+ if (typeof this.GM_Api[keyName] !== "function") {
1180
+ throw new Error(`Utils.GM_Menu 请在脚本开头加上 @grant ${keyName},且传入该对象`);
1181
+ }
1182
+ }
1183
+ this.add(details?.data || []);
1184
+ }
1185
+ /**
1186
+ * 新增菜单数据
1187
+ * @param paramData
1188
+ */
1189
+ add(paramData) {
1190
+ if (Array.isArray(paramData)) {
1191
+ for (const _paramData of paramData) {
1192
+ // @ts-ignore
1193
+ this.MenuHandle.$data.data.push({
1194
+ data: _paramData,
1195
+ id: void 0,
1196
+ });
1197
+ }
1198
+ }
1199
+ else {
1200
+ // @ts-ignore
1201
+ this.MenuHandle.$data.data.push({
1202
+ data: paramData,
1203
+ id: void 0,
1204
+ });
1205
+ }
1206
+ this.update();
1207
+ }
1208
+ /**
1209
+ * 更新菜单数据
1210
+ * @param options 数据
1211
+ */
1212
+ update(options) {
1213
+ let optionsList = [];
1214
+ if (Array.isArray(options)) {
1215
+ optionsList = [...optionsList, ...options];
1216
+ }
1217
+ else if (options != null) {
1218
+ optionsList = [...optionsList, options];
1219
+ }
1220
+ optionsList.forEach((item) => {
1221
+ let targetMenu = this.MenuHandle.getMenuOption(item.key);
1222
+ if (targetMenu) {
1223
+ Object.assign(targetMenu, item);
1224
+ }
1225
+ });
1226
+ this.MenuHandle.$data.data.forEach((value) => {
1227
+ if (value.handleData) {
1228
+ value.handleData.deleteMenu();
1229
+ }
1230
+ });
1231
+ this.MenuHandle.init();
1232
+ this.MenuHandle.register(this.MenuHandle.$data.data);
1233
+ }
1234
+ /**
1235
+ * 卸载菜单
1236
+ * @param menuId 已注册的菜单id
1237
+ */
1238
+ delete(menuId) {
1239
+ this.GM_Api.unregisterMenuCommand(menuId);
1240
+ }
1241
+ /**
1242
+ * 根据键值获取enable值
1243
+ * @param menuKey 菜单-键key
1244
+ */
1245
+ get(menuKey) {
1246
+ return this.getEnable(menuKey);
1247
+ }
1248
+ /**
1249
+ * 根据键值获取enable值
1250
+ * @param menuKey 菜单-键key
1251
+ */
1252
+ getEnable(menuKey) {
1253
+ return this.MenuHandle.getMenuHandledOption(menuKey).enable;
1254
+ }
1255
+ /**
1256
+ * 根据键值获取text值
1257
+ * @param menuKey 菜单-键key
1258
+ */
1259
+ getText(menuKey) {
1260
+ return this.MenuHandle.getMenuHandledOption(menuKey).text;
1261
+ }
1262
+ /**
1263
+ * 根据键值获取showText函数的值
1264
+ * @param menuKey 菜单-键key
1265
+ */
1266
+ getShowTextValue(menuKey) {
1267
+ return this.MenuHandle.getMenuHandledOption(menuKey).showText(this.getText(menuKey), this.get(menuKey));
1268
+ }
1269
+ /**
1270
+ * 获取当前已注册菜单的id
1271
+ * @param menuKey
1272
+ */
1273
+ getMenuId(menuKey) {
1274
+ let result = null;
1275
+ for (let index = 0; index < this.MenuHandle.$data.data.length; index++) {
1276
+ const optionData = this.MenuHandle.$data.data[index];
1277
+ if (optionData.handleData.key === menuKey) {
1278
+ result = optionData.id;
1279
+ break;
1280
+ }
1281
+ }
1282
+ return result;
1283
+ }
1284
+ /**
1285
+ * 根据键值获取accessKey值
1286
+ * @param menuKey 菜单-键key
1287
+ */
1288
+ getAccessKey(menuKey) {
1289
+ return this.MenuHandle.getMenuHandledOption(menuKey)?.accessKey;
1290
+ }
1291
+ /**
1292
+ * 根据键值获取autoClose值
1293
+ * @param menuKey 菜单-键key
1294
+ */
1295
+ getAutoClose(menuKey) {
1296
+ return this.MenuHandle.getMenuHandledOption(menuKey)?.autoClose;
1297
+ }
1298
+ /**
1299
+ * 根据键值获取autoReload值
1300
+ * @param menuKey 菜单-键key
1301
+ */
1302
+ getAutoReload(menuKey) {
1303
+ return this.MenuHandle.getMenuHandledOption(menuKey)?.autoReload;
1304
+ }
1305
+ /**
1306
+ * 根据键值获取callback函数
1307
+ * @param menuKey 菜单-键key
1308
+ */
1309
+ getCallBack(menuKey) {
1310
+ return this.MenuHandle.getMenuHandledOption(menuKey)?.callback;
1311
+ }
1312
+ /**
1313
+ * 获取当enable为true时默认显示在菜单中前面的emoji图标
1314
+ */
1315
+ getEnableTrueEmoji() {
1316
+ return this.MenuHandle.$emoji.success;
1317
+ }
1318
+ /**
1319
+ * 获取当enable为false时默认显示在菜单中前面的emoji图标
1320
+ */
1321
+ getEnableFalseEmoji() {
1322
+ return this.MenuHandle.$emoji.error;
1323
+ }
1324
+ /**
1325
+ * 获取本地存储的菜单外部的键名
1326
+ * @param keyName
1327
+ */
1328
+ getLocalStorageKeyName() {
1329
+ return this.MenuHandle.$data.key;
1330
+ }
1331
+ /**
1332
+ * 设置菜单的值
1333
+ * @param menuKey 菜单-键key
1334
+ * @param value 需要设置的值
1335
+ */
1336
+ setValue(menuKey, value) {
1337
+ this.MenuHandle.setLocalMenuData(menuKey, value);
1338
+ }
1339
+ /**
1340
+ * 设置菜单的值
1341
+ * @param menuKey 菜单-键key
1342
+ * @param value 需要设置的值
1343
+ */
1344
+ setEnable(menuKey, value) {
1345
+ this.setValue(menuKey, Boolean(value));
1346
+ }
1347
+ /**
1348
+ * 设置当enable为true时默认显示在菜单中前面的emoji图标
1349
+ * @param emojiString
1350
+ */
1351
+ setEnableTrueEmoji(emojiString) {
1352
+ if (typeof emojiString !== "string") {
1353
+ throw new Error("参数emojiString必须是string类型");
1354
+ }
1355
+ this.MenuHandle.$emoji.success = emojiString;
1356
+ }
1357
+ /**
1358
+ * 设置当enable为false时默认显示在菜单中前面的emoji图标
1359
+ * @param emojiString
1360
+ */
1361
+ setEnableFalseEmoji(emojiString) {
1362
+ if (typeof emojiString !== "string") {
1363
+ throw new Error("参数emojiString必须是string类型");
1364
+ }
1365
+ this.MenuHandle.$emoji.error = emojiString;
1366
+ }
1367
+ /**
1368
+ * 设置本地存储的菜单外部的键名
1369
+ * @param keyName
1370
+ */
1371
+ setLocalStorageKeyName(keyName) {
1372
+ if (typeof keyName !== "string") {
1373
+ throw new Error("参数keyName必须是string类型");
1374
+ }
1375
+ this.MenuHandle.$data.key = keyName;
1376
+ }
1377
+ }
1405
1378
 
1406
- const Hooks = function () {
1407
- this.initEnv = function () {
1408
- Function.prototype.hook = function (realFunc, hookFunc, context) {
1409
- let _context = null; //函数上下文
1410
- let _funcName = null; //函数名
1411
-
1412
- _context = context || window;
1413
- _funcName = getFuncName(this);
1414
- _context["realFunc_" + _funcName] = this;
1415
-
1416
- if (
1417
- _context[_funcName].prototype &&
1418
- _context[_funcName].prototype.isHooked
1419
- ) {
1420
- console.log("Already has been hooked,unhook first");
1421
- return false;
1422
- }
1423
- function getFuncName(fn) {
1424
- // 获取函数名
1425
- let strFunc = fn.toString();
1426
- let _regex = /function\s+(\w+)\s*\(/;
1427
- let patten = strFunc.match(_regex);
1428
- if (patten) {
1429
- return patten[1];
1430
- }
1431
- return "";
1432
- }
1433
- try {
1434
- eval(
1435
- "_context[_funcName] = function " +
1436
- _funcName +
1437
- "(){\n" +
1438
- "let args = Array.prototype.slice.call(arguments,0);\n" +
1439
- "let obj = this;\n" +
1440
- "hookFunc.apply(obj,args);\n" +
1441
- "return _context['realFunc_" +
1442
- _funcName +
1443
- "'].apply(obj,args);\n" +
1444
- "};"
1445
- );
1446
- _context[_funcName].prototype.isHooked = true;
1447
- return true;
1448
- } catch (e) {
1449
- console.log("Hook failed,check the params.");
1450
- return false;
1451
- }
1452
- };
1453
- Function.prototype.unhook = function (realFunc, funcName, context) {
1454
- let _context = null;
1455
- let _funcName = null;
1456
- _context = context || window;
1457
- _funcName = funcName;
1458
- if (!_context[_funcName].prototype.isHooked) {
1459
- console.log("No function is hooked on");
1460
- return false;
1461
- }
1462
- _context[_funcName] = _context["realFunc" + _funcName];
1463
- Reflect.deleteProperty(_context, "realFunc_" + _funcName);
1464
- return true;
1465
- };
1466
- };
1467
- this.cleanEnv = function () {
1468
- if (Function.prototype.hasOwnProperty("hook")) {
1469
- Reflect.deleteProperty(unction.prototype, "hook");
1470
- }
1471
- if (Function.prototype.hasOwnProperty("unhook")) {
1472
- Reflect.deleteProperty(unction.prototype, "unhook");
1473
- }
1474
- return true;
1475
- };
1476
- };
1379
+ class Hooks {
1380
+ /**
1381
+ * 在Function原型上添加自定义方法.hook和.unhook
1382
+ */
1383
+ initEnv() {
1384
+ Function.prototype.hook = function (realFunc, hookFunc, context) {
1385
+ let _context = null; //函数上下文
1386
+ let _funcName = null; //函数名
1387
+ _context = context || window;
1388
+ _funcName = getFuncName(this);
1389
+ _context["realFunc_" + _funcName] = this;
1390
+ if (_context[_funcName].prototype &&
1391
+ _context[_funcName].prototype.isHooked) {
1392
+ console.log("Already has been hooked,unhook first");
1393
+ return false;
1394
+ }
1395
+ function getFuncName(fn) {
1396
+ // 获取函数名
1397
+ let strFunc = fn.toString();
1398
+ let _regex = /function\s+(\w+)\s*\(/;
1399
+ let patten = strFunc.match(_regex);
1400
+ if (patten) {
1401
+ return patten[1];
1402
+ }
1403
+ return "";
1404
+ }
1405
+ try {
1406
+ eval("_context[_funcName] = function " +
1407
+ _funcName +
1408
+ "(){\n" +
1409
+ "let args = Array.prototype.slice.call(arguments,0);\n" +
1410
+ "let obj = this;\n" +
1411
+ "hookFunc.apply(obj,args);\n" +
1412
+ "return _context['realFunc_" +
1413
+ _funcName +
1414
+ "'].apply(obj,args);\n" +
1415
+ "};");
1416
+ _context[_funcName].prototype.isHooked = true;
1417
+ return true;
1418
+ }
1419
+ catch (e) {
1420
+ console.log("Hook failed,check the params.");
1421
+ return false;
1422
+ }
1423
+ };
1424
+ Function.prototype.unhook = function (realFunc, funcName, context) {
1425
+ let _context = null;
1426
+ let _funcName = null;
1427
+ _context = context || window;
1428
+ _funcName = funcName;
1429
+ if (!_context[_funcName].prototype.isHooked) {
1430
+ console.log("No function is hooked on");
1431
+ return false;
1432
+ }
1433
+ _context[_funcName] = _context["realFunc" + _funcName];
1434
+ Reflect.deleteProperty(_context, "realFunc_" + _funcName);
1435
+ return true;
1436
+ };
1437
+ }
1438
+ /**
1439
+ * 删除在Function原型上添加的自定义方法.hook和.unhook
1440
+ */
1441
+ cleanEnv() {
1442
+ if (Function.prototype.hasOwnProperty("hook")) {
1443
+ Reflect.deleteProperty(Function.prototype, "hook");
1444
+ }
1445
+ if (Function.prototype.hasOwnProperty("unhook")) {
1446
+ Reflect.deleteProperty(Function.prototype, "unhook");
1447
+ }
1448
+ return true;
1449
+ }
1450
+ }
1477
1451
 
1478
- const Httpx = function (__xmlHttpRequest__) {
1479
- if (typeof __xmlHttpRequest__ !== "function") {
1480
- console.warn("Httpx未传入GM_xmlhttpRequest函数,强制默认使用fetch");
1481
- }
1482
- const GM_Api = {
1483
- /**
1484
- * @type {GM_xmlhttpRequest}
1485
- */
1486
- xmlHttpRequest: __xmlHttpRequest__,
1487
- };
1488
- /**
1489
- * @type {HttpxDetails}
1490
- */
1491
- let defaultDetails = {
1492
- url: void 0,
1493
- timeout: 5000,
1494
- async: false,
1495
- responseType: void 0,
1496
- headers: void 0,
1497
- data: void 0,
1498
- redirect: void 0,
1499
- cookie: void 0,
1500
- binary: void 0,
1501
- nocache: void 0,
1502
- revalidate: void 0,
1503
- context: void 0,
1504
- overrideMimeType: void 0,
1505
- anonymous: void 0,
1506
- fetch: void 0,
1507
- fetchInit: void 0,
1508
- user: void 0,
1509
- password: void 0,
1510
- onabort() {},
1511
- onerror() {},
1512
- ontimeout() {},
1513
- onloadstart() {},
1514
- onreadystatechange() {},
1515
- onprogress() {},
1516
- };
1517
- /**
1518
- * 输出请求配置
1519
- */
1520
- let LOG_DETAILS = false;
1521
-
1522
- const HttpxRequestHook = {
1523
- /**
1524
- * 发送请求前的回调
1525
- * 如果返回false则阻止本次返回
1526
- * @param {HttpxDetails} details 当前的请求配置
1527
- */
1528
- beforeRequestCallBack(details) {},
1529
- };
1530
-
1531
- const HttpxRequestDetails = {
1532
- /**
1533
- * 获取请求配置
1534
- * @param {HttpxMethod} method 当前请求方法,默认get
1535
- * @param {(...args: any[])=>void} resolve promise回调
1536
- * @param {HttpxDetails} details 请求配置
1537
- * @returns
1538
- */
1539
- getDetails(method, resolve, details) {
1540
- /**
1541
- * @type {HttpxDetails}
1542
- */
1543
- let result = {
1544
- url: details.url || defaultDetails.url,
1545
- method: (method || "GET").toString().toUpperCase(),
1546
- timeout: details.timeout || defaultDetails.timeout,
1547
- responseType: details.responseType || defaultDetails.responseType,
1548
- /* 对象使用深拷贝 */
1549
- headers: Utils.deepClone(defaultDetails.headers),
1550
- data: details.data || defaultDetails.data,
1551
- redirect: details.redirect || defaultDetails.redirect,
1552
- cookie: details.cookie || defaultDetails.cookie,
1553
- binary: details.binary || defaultDetails.binary,
1554
- nocache: details.nocache || defaultDetails.nocache,
1555
- revalidate: details.revalidate || defaultDetails.revalidate,
1556
- /* 对象使用深拷贝 */
1557
- context: Utils.deepClone(details.context || defaultDetails.context),
1558
- overrideMimeType:
1559
- details.overrideMimeType || defaultDetails.overrideMimeType,
1560
- anonymous: details.anonymous || defaultDetails.anonymous,
1561
- fetch: details.fetch || defaultDetails.fetch,
1562
- /* 对象使用深拷贝 */
1563
- fetchInit: Utils.deepClone(defaultDetails.fetchInit),
1564
- user: details.user || defaultDetails.user,
1565
- password: details.password || defaultDetails.password,
1566
- onabort(...args) {
1567
- HttpxCallBack.onAbort(details, resolve, args);
1568
- },
1569
- onerror(...args) {
1570
- HttpxCallBack.onError(details, resolve, args);
1571
- },
1572
- onloadstart(...args) {
1573
- HttpxCallBack.onLoadStart(details, args);
1574
- },
1575
- onprogress(...args) {
1576
- HttpxCallBack.onProgress(details, args);
1577
- },
1578
- onreadystatechange(...args) {
1579
- HttpxCallBack.onReadyStateChange(details, args);
1580
- },
1581
- ontimeout(...args) {
1582
- HttpxCallBack.onTimeout(details, resolve, args);
1583
- },
1584
- onload(...args) {
1585
- HttpxCallBack.onLoad(details, resolve, args);
1586
- },
1587
- };
1588
- if (typeof GM_Api.xmlHttpRequest !== "function") {
1589
- result.fetch = true;
1590
- }
1591
- if (typeof result.headers === "object") {
1592
- if (typeof details.headers === "object") {
1593
- Object.keys(details.headers).forEach((keyName, index) => {
1594
- if (keyName in result.headers && details.headers[keyName] == null) {
1595
- /* 在默认的header中存在,且设置它新的值为空,那么就是默认的值 */
1596
- Reflect.deleteProperty(result.headers, keyName);
1597
- } else {
1598
- result.headers[keyName] = details.headers[keyName];
1599
- }
1600
- });
1601
- }
1602
- } else {
1603
- result.headers = details.headers;
1604
- }
1605
- if (typeof result.fetchInit === "object") {
1606
- /* 使用assign替换且添加 */
1607
- if (typeof details.fetchInit === "object") {
1608
- Object.keys(details.fetchInit).forEach((keyName, index) => {
1609
- if (
1610
- keyName in result.fetchInit &&
1611
- details.fetchInit[keyName] == null
1612
- ) {
1613
- /* 在默认的fetchInit中存在,且设置它新的值为空,那么就是默认的值 */
1614
- Reflect.deleteProperty(result.fetchInit, keyName);
1615
- } else {
1616
- result.fetchInit[keyName] = details.fetchInit[keyName];
1617
- }
1618
- });
1619
- }
1620
- } else {
1621
- result.fetchInit = details.fetchInit;
1622
- }
1623
- return result;
1624
- },
1625
- /**
1626
- * 处理发送请求的details,去除值为undefined、空function的值
1627
- * @param {HttpxDetails} details
1628
- * @returns {HttpxDetails}
1629
- */
1630
- handle(details) {
1631
- Object.keys(details).forEach((keyName) => {
1632
- if (
1633
- details[keyName] == null ||
1634
- (details[keyName] instanceof Function &&
1635
- Utils.isNull(details[keyName]))
1636
- ) {
1637
- Reflect.deleteProperty(details, keyName);
1638
- return;
1639
- }
1640
- });
1641
- if (Utils.isNull(details.url)) {
1642
- throw new TypeError(`Utils.Httpx 参数 url不符合要求: ${details.url}`);
1643
- }
1644
- /* method值统一大写,兼容Via */
1645
- details.method = details.method.toUpperCase();
1646
- /* 判断是否是以http开头,否则主动加上origin */
1647
- try {
1648
- new URL(details.url);
1649
- } catch (error) {
1650
- if (details.url.startsWith("//")) {
1651
- details.url = globalThis.location.protocol + details.url;
1652
- } else if (details.url.startsWith("/")) {
1653
- details.url = globalThis.location.origin + details.url;
1654
- } else {
1655
- details.url = globalThis.location.origin + "/" + details.url;
1656
- }
1657
- }
1658
- return details;
1659
- },
1660
- /**
1661
- * 处理fetch的配置
1662
- * @param {HttpxDetails} details
1663
- */
1664
- handleFetchDetail(details) {
1665
- /**
1666
- * fetch的请求配置
1667
- * @type {RequestInit}
1668
- **/
1669
- let fetchRequestInit = {};
1670
- if (
1671
- (details.method === "GET" || details.method === "HEAD") &&
1672
- details.data != null
1673
- ) {
1674
- /* GET 或 HEAD 方法的请求不能包含 body 信息 */
1675
- Reflect.deleteProperty(details, "data");
1676
- }
1677
- /* 中止信号控制器 */
1678
- let abortController = new AbortController();
1679
- let signal = abortController.signal;
1680
- signal.onabort = () => {
1681
- details.onabort({
1682
- isFetch: true,
1683
- responseText: "",
1684
- response: null,
1685
- readyState: 4,
1686
- responseHeaders: "",
1687
- status: 0,
1688
- statusText: "",
1689
- error: "aborted",
1690
- });
1691
- };
1692
- fetchRequestInit.method = details.method ?? "GET";
1693
- fetchRequestInit.headers = details.headers;
1694
- fetchRequestInit.body = details.data;
1695
- fetchRequestInit.mode = "cors";
1696
- fetchRequestInit.credentials = "include";
1697
- fetchRequestInit.cache = "no-cache";
1698
- fetchRequestInit.redirect = "follow";
1699
- fetchRequestInit.referrerPolicy = "origin-when-cross-origin";
1700
- fetchRequestInit.signal = signal;
1701
- Object.assign(fetchRequestInit, details.fetchInit || {});
1702
- return {
1703
- fetchDetails: details,
1704
- fetchRequestInit: fetchRequestInit,
1705
- abortController: abortController,
1706
- };
1707
- },
1708
- };
1709
- const HttpxCallBack = {
1710
- /**
1711
- * onabort请求被取消-触发
1712
- * @param {HttpxDetails} details 配置
1713
- * @param {()=>void} resolve 回调
1714
- * @param {any[]} argumentsList 参数列表
1715
- */
1716
- onAbort(details, resolve, argumentsList) {
1717
- if ("onabort" in details) {
1718
- details.onabort.apply(this, argumentsList);
1719
- } else if ("onabort" in defaultDetails) {
1720
- defaultDetails.onabort.apply(this, argumentsList);
1721
- }
1722
- resolve({
1723
- status: false,
1724
- data: [...argumentsList],
1725
- msg: "请求被取消",
1726
- type: "onabort",
1727
- });
1728
- },
1729
-
1730
- /**
1731
- * onerror请求异常-触发
1732
- * @param {HttpxDetails} details 配置
1733
- * @param {()=>void} resolve 回调
1734
- * @param {any[]} argumentsList 响应的参数列表
1735
- */
1736
- onError(details, resolve, argumentsList) {
1737
- if ("onerror" in details) {
1738
- details.onerror.apply(this, argumentsList);
1739
- } else if ("onerror" in defaultDetails) {
1740
- defaultDetails.onerror.apply(this, argumentsList);
1741
- }
1742
- let response = argumentsList;
1743
- if (response.length) {
1744
- response = response[0];
1745
- }
1746
- resolve({
1747
- status: false,
1748
- data: response,
1749
- details: details,
1750
- msg: "请求异常",
1751
- type: "onerror",
1752
- });
1753
- },
1754
- /**
1755
- * ontimeout请求超时-触发
1756
- * @param {HttpxDetails} details 配置
1757
- * @param {()=>void} resolve 回调
1758
- * @param {any[]} argumentsList 参数列表
1759
- */
1760
- onTimeout(details, resolve, argumentsList) {
1761
- if ("ontimeout" in details) {
1762
- details.ontimeout.apply(this, argumentsList);
1763
- } else if ("ontimeout" in defaultDetails) {
1764
- defaultDetails.ontimeout.apply(this, argumentsList);
1765
- }
1766
- resolve({
1767
- status: false,
1768
- data: [...argumentsList],
1769
- msg: "请求超时",
1770
- type: "ontimeout",
1771
- });
1772
- },
1773
-
1774
- /**
1775
- * onloadstart请求开始-触发
1776
- * @param {HttpxDetails} details 配置
1777
- * @param {any[]} argumentsList 参数列表
1778
- */
1779
- onLoadStart(details, argumentsList) {
1780
- if ("onloadstart" in details) {
1781
- details.onloadstart.apply(this, argumentsList);
1782
- } else if ("onloadstart" in defaultDetails) {
1783
- defaultDetails.onloadstart.apply(this, argumentsList);
1784
- }
1785
- },
1786
- /**
1787
- * onload加载完毕-触发
1788
- * @param {HttpxDetails} details 请求的配置
1789
- * @param {()=>void} resolve 回调
1790
- * @param {...HttpxAsyncResultData[]} argumentsList 参数列表
1791
- */
1792
- onLoad(details, resolve, argumentsList) {
1793
- /* X浏览器会因为设置了responseType导致不返回responseText */
1794
- let Response = argumentsList[0];
1795
- /* responseText为空,response不为空的情况 */
1796
- if (
1797
- Utils.isNull(Response["responseText"]) &&
1798
- Utils.isNotNull(Response["response"])
1799
- ) {
1800
- if (typeof Response["response"] === "object") {
1801
- Utils.tryCatch().run(() => {
1802
- Response["responseText"] = JSON.stringify(Response["response"]);
1803
- });
1804
- } else {
1805
- Response["responseText"] = Response["response"];
1806
- }
1807
- }
1808
-
1809
- /* response为空,responseText不为空的情况 */
1810
- if (
1811
- Response["response"] == null &&
1812
- typeof Response["responseText"] === "string" &&
1813
- Response["responseText"].trim() !== ""
1814
- ) {
1815
- let newResponse = Response["responseText"];
1816
- if (details.responseType === "json") {
1817
- newResponse = Utils.toJSON(Response["responseText"]);
1818
- } else if (details.responseType === "document") {
1819
- let parser = new DOMParser();
1820
- newResponse = parser.parseFromString(
1821
- Response["responseText"],
1822
- "text/html"
1823
- );
1824
- } else if (details.responseType === "arraybuffer") {
1825
- let encoder = new TextEncoder();
1826
- let arrayBuffer = encoder.encode(Response["responseText"]);
1827
- newResponse = arrayBuffer;
1828
- } else if (details.responseType === "blob") {
1829
- let encoder = new TextEncoder();
1830
- let arrayBuffer = encoder.encode(Response["responseText"]);
1831
- newResponse = new Blob([arrayBuffer]);
1832
- } else {
1833
- newResponse = Response["responseText"];
1834
- }
1835
- try {
1836
- Response["response"] = newResponse;
1837
- } catch (error) {
1838
- console.warn("response 无法被覆盖");
1839
- }
1840
- }
1841
- /* Stay扩展中没有finalUrl,对应的是responseURL */
1842
- if (Response["finalUrl"] == null && Response["responseURL"] != null) {
1843
- Response["finalUrl"] = Response["responseURL"];
1844
- }
1845
- /* 状态码2xx都是成功的 */
1846
- if (Math.floor(Response.status / 100) === 2) {
1847
- resolve({
1848
- status: true,
1849
- data: Response,
1850
- details: details,
1851
- msg: "请求完毕",
1852
- type: "onload",
1853
- });
1854
- } else {
1855
- HttpxCallBack.onError(details, resolve, argumentsList);
1856
- }
1857
- },
1858
- /**
1859
- * onprogress上传进度-触发
1860
- * @param {HttpxDetails} details 配置
1861
- * @param {any[]} argumentsList 参数列表
1862
- */
1863
- onProgress(details, argumentsList) {
1864
- if ("onprogress" in details) {
1865
- details.onprogress.apply(this, argumentsList);
1866
- } else if ("onprogress" in defaultDetails) {
1867
- defaultDetails.onprogress.apply(this, argumentsList);
1868
- }
1869
- },
1870
- /**
1871
- * onreadystatechange准备状态改变-触发
1872
- * @param {HttpxDetails} details 配置
1873
- * @param {any[]} argumentsList 参数列表
1874
- */
1875
- onReadyStateChange(details, argumentsList) {
1876
- if ("onreadystatechange" in details) {
1877
- details.onreadystatechange.apply(this, argumentsList);
1878
- } else if ("onreadystatechange" in defaultDetails) {
1879
- defaultDetails.onreadystatechange.apply(this, argumentsList);
1880
- }
1881
- },
1882
- };
1883
-
1884
- const HttpxRequest = {
1885
- /**
1886
- * 发送请求
1887
- * @param {HttpxDetails} details
1888
- */
1889
- request(details) {
1890
- if (LOG_DETAILS) {
1891
- console.log("Httpx请求配置👇", details);
1892
- }
1893
- if (typeof HttpxRequestHook.beforeRequestCallBack === "function") {
1894
- let hookResult = HttpxRequestHook.beforeRequestCallBack(details);
1895
- if (typeof hookResult === "boolean" && !hookResult) {
1896
- return;
1897
- }
1898
- }
1899
- if (details.fetch) {
1900
- const { fetchDetails, fetchRequestInit, abortController } =
1901
- HttpxRequestDetails.handleFetchDetail(details);
1902
- this.fetch(fetchDetails, fetchRequestInit, abortController);
1903
- } else {
1904
- Reflect.deleteProperty(details, "fetchInit");
1905
- this.xmlHttpRequest(details);
1906
- }
1907
- },
1908
- /**
1909
- * 使用油猴函数GM_xmlhttpRequest发送请求
1910
- * @param {HttpxDetails} details
1911
- */
1912
- xmlHttpRequest(details) {
1913
- GM_Api.xmlHttpRequest(details);
1914
- },
1915
- /**
1916
- * 使用fetch发送请求
1917
- * @param {HttpxDetails} details
1918
- * @param {RequestInit} fetchRequestInit
1919
- * @param {AbortController} abortController
1920
- */
1921
- fetch(details, fetchRequestInit, abortController) {
1922
- fetch(details.url, fetchRequestInit)
1923
- .then(async (resp) => {
1924
- /**
1925
- * @type {HttpxAsyncResultData}
1926
- */
1927
- let httpxResponse = {
1928
- isFetch: true,
1929
- finalUrl: resp.url,
1930
- readyState: 4,
1931
- status: resp.status,
1932
- statusText: resp.statusText,
1933
- response: void 0,
1934
- responseFetchHeaders: resp.headers,
1935
- responseHeaders: "",
1936
- responseText: void 0,
1937
- responseType: details.responseType,
1938
- responseXML: void 0,
1939
- };
1940
- Object.assign(httpxResponse, details.context || {});
1941
-
1942
- for (const [key, value] of resp.headers.entries()) {
1943
- httpxResponse.responseHeaders += `${key}: ${value}\n`;
1944
- }
1945
-
1946
- /* 如果是流式传输,直接返回 */
1947
- if (
1948
- details.responseType === "stream" ||
1949
- (resp.headers.has("Content-Type") &&
1950
- resp.headers.get("Content-Type").includes("text/event-stream"))
1951
- ) {
1952
- httpxResponse["isStream"] = true;
1953
- httpxResponse.response = resp.body;
1954
- Reflect.deleteProperty(httpxResponse, "responseText");
1955
- Reflect.deleteProperty(httpxResponse, "responseXML");
1956
- details.onload(httpxResponse);
1957
- return;
1958
- }
1959
-
1960
- /** 响应 */
1961
- let response = "";
1962
- /** 响应字符串 */
1963
- let responseText = "";
1964
- /** 响应xml文档 */
1965
- let responseXML = "";
1966
-
1967
- let arrayBuffer = await resp.arrayBuffer;
1968
-
1969
- let encoding = "utf-8";
1970
- if (resp.headers.has("Content-Type")) {
1971
- let charsetMatched = resp.headers
1972
- .get("Content-Type")
1973
- .match(/charset=(.+)/);
1974
- if (charsetMatched) {
1975
- encoding = charsetMatched[1];
1976
- }
1977
- }
1978
- let textDecoder = new TextDecoder(encoding);
1979
- responseText = textDecoder.decode(await resp.arrayBuffer());
1980
- response = responseText;
1981
-
1982
- if (details.responseType === "arraybuffer") {
1983
- response = arrayBuffer;
1984
- } else if (details.responseType === "blob") {
1985
- response = new Blob([arrayBuffer]);
1986
- } else if (
1987
- details.responseType === "document" ||
1988
- details.responseType == null
1989
- ) {
1990
- let parser = new DOMParser();
1991
- response = parser.parseFromString(responseText, "text/html");
1992
- } else if (details.responseType === "json") {
1993
- response = Utils.toJSON(responseText);
1994
- }
1995
- let parser = new DOMParser();
1996
- responseXML = parser.parseFromString(responseText, "text/xml");
1997
-
1998
- httpxResponse.response = response;
1999
- httpxResponse.responseText = responseText;
2000
- httpxResponse.responseXML = responseXML;
2001
-
2002
- details.onload(httpxResponse);
2003
- })
2004
- .catch((err) => {
2005
- if (err.name === "AbortError") {
2006
- return;
2007
- }
2008
- details.onerror({
2009
- isFetch: true,
2010
- finalUrl: details.url,
2011
- readyState: 4,
2012
- status: 0,
2013
- statusText: "",
2014
- responseHeaders: "",
2015
- responseText: "",
2016
- error: err,
2017
- });
2018
- });
2019
- details.onloadstart({
2020
- isFetch: true,
2021
- finalUrl: details.url,
2022
- readyState: 1,
2023
- responseHeaders: "",
2024
- responseText: "",
2025
- status: 0,
2026
- statusText: "",
2027
- });
2028
- return {
2029
- abort() {
2030
- abortController.abort();
2031
- },
2032
- };
2033
- },
2034
- };
2035
-
2036
- /**
2037
- * 覆盖当前配置
2038
- * @param {HttpxDetailsConfig} details
2039
- */
2040
- this.config = function (details = {}) {
2041
- if ("logDetails" in details && typeof details["logDetails"] === "boolean") {
2042
- LOG_DETAILS = details["logDetails"];
2043
- }
2044
-
2045
- defaultDetails = Utils.assign(defaultDetails, details);
2046
- };
2047
-
2048
- /**
2049
- * 修改xmlHttpRequest
2050
- * @param {Function} httpRequest 网络请求函数
2051
- */
2052
- this.setXMLHttpRequest = function (httpRequest) {
2053
- GM_Api.xmlHttpRequest = httpRequest;
2054
- };
2055
-
2056
- /**
2057
- * GET 请求
2058
- * @param {...HttpxDetails|string} args
2059
- * @returns {Promise< HttpxAsyncResult >}
2060
- */
2061
- this.get = async function (...args) {
2062
- let details = {};
2063
- if (typeof args[0] === "string") {
2064
- details.url = args[0];
2065
- if (typeof args[1] === "object") {
2066
- details = args[1];
2067
- details.url = args[0];
2068
- }
2069
- } else {
2070
- details = args[0];
2071
- }
2072
- return new Promise((resolve) => {
2073
- let requestDetails = HttpxRequestDetails.getDetails(
2074
- "get",
2075
- resolve,
2076
- details
2077
- );
2078
- Reflect.deleteProperty(requestDetails, "onprogress");
2079
- requestDetails = HttpxRequestDetails.handle(requestDetails);
2080
- HttpxRequest.request(requestDetails);
2081
- });
2082
- };
2083
- /**
2084
- * POST 请求
2085
- * @param {...HttpxDetails|string} args
2086
- * @returns {Promise< HttpxAsyncResult >}
2087
- */
2088
- this.post = async function (...args) {
2089
- let details = {};
2090
- if (typeof args[0] === "string") {
2091
- details.url = args[0];
2092
- if (typeof args[1] === "object") {
2093
- details = args[1];
2094
- details.url = args[0];
2095
- }
2096
- } else {
2097
- details = args[0];
2098
- }
2099
- return new Promise((resolve) => {
2100
- let requestDetails = HttpxRequestDetails.getDetails(
2101
- "post",
2102
- resolve,
2103
- details
2104
- );
2105
- requestDetails = HttpxRequestDetails.handle(requestDetails);
2106
- HttpxRequest.request(requestDetails);
2107
- });
2108
- };
2109
- /**
2110
- * HEAD 请求
2111
- * @param {...HttpxDetails|string} args
2112
- * @returns {Promise< HttpxAsyncResult >}
2113
- */
2114
- this.head = async function (...args) {
2115
- let details = {};
2116
- if (typeof args[0] === "string") {
2117
- details.url = args[0];
2118
- if (typeof args[1] === "object") {
2119
- details = args[1];
2120
- details.url = args[0];
2121
- }
2122
- } else {
2123
- details = args[0];
2124
- }
2125
- return new Promise((resolve) => {
2126
- let requestDetails = HttpxRequestDetails.getDetails(
2127
- "head",
2128
- resolve,
2129
- details
2130
- );
2131
- Reflect.deleteProperty(requestDetails, "onprogress");
2132
- requestDetails = HttpxRequestDetails.handle(requestDetails);
2133
- HttpxRequest.request(requestDetails);
2134
- });
2135
- };
2136
-
2137
- /**
2138
- * OPTIONS 请求
2139
- * @param {...HttpxDetails|string} args
2140
- * @returns {Promise< HttpxAsyncResult >}
2141
- */
2142
- this.options = async function (...args) {
2143
- let details = {};
2144
- if (typeof args[0] === "string") {
2145
- details.url = args[0];
2146
- if (typeof args[1] === "object") {
2147
- details = args[1];
2148
- details.url = args[0];
2149
- }
2150
- } else {
2151
- details = args[0];
2152
- }
2153
- return new Promise((resolve) => {
2154
- let requestDetails = HttpxRequestDetails.getDetails(
2155
- "options",
2156
- resolve,
2157
- details
2158
- );
2159
- Reflect.deleteProperty(requestDetails, "onprogress");
2160
- requestDetails = HttpxRequestDetails.handle(requestDetails);
2161
- HttpxRequest.request(requestDetails);
2162
- });
2163
- };
2164
-
2165
- /**
2166
- * DELETE 请求
2167
- * @param {...HttpxDetails|string} args
2168
- * @returns {Promise< HttpxAsyncResult >}
2169
- */
2170
- this.delete = async function (...args) {
2171
- let details = {};
2172
- if (typeof args[0] === "string") {
2173
- details.url = args[0];
2174
- if (typeof args[1] === "object") {
2175
- details = args[1];
2176
- details.url = args[0];
2177
- }
2178
- } else {
2179
- details = args[0];
2180
- }
2181
- return new Promise((resolve) => {
2182
- let requestDetails = HttpxRequestDetails.getDetails(
2183
- "delete",
2184
- resolve,
2185
- details
2186
- );
2187
- Reflect.deleteProperty(requestDetails, "onprogress");
2188
- requestDetails = HttpxRequestDetails.handle(requestDetails);
2189
- HttpxRequest.request(requestDetails);
2190
- });
2191
- };
2192
-
2193
- /**
2194
- * PUT 请求
2195
- * @param {...HttpxDetails|string} args
2196
- * @returns {Promise< HttpxAsyncResult >}
2197
- */
2198
- this.put = async function (...args) {
2199
- let details = {};
2200
- if (typeof args[0] === "string") {
2201
- details.url = args[0];
2202
- if (typeof args[1] === "object") {
2203
- details = args[1];
2204
- details.url = args[0];
2205
- }
2206
- } else {
2207
- details = args[0];
2208
- }
2209
- return new Promise((resolve) => {
2210
- let requestDetails = HttpxRequestDetails.getDetails(
2211
- "put",
2212
- resolve,
2213
- details
2214
- );
2215
- requestDetails = HttpxRequestDetails.handle(requestDetails);
2216
- HttpxRequest.request(requestDetails);
2217
- });
2218
- };
2219
- };
1452
+ class Httpx {
1453
+ GM_Api = {
1454
+ xmlHttpRequest: null,
1455
+ };
1456
+ HttpxRequestHook = {
1457
+ /**
1458
+ * 发送请求前的回调
1459
+ * 如果返回false则阻止本次返回
1460
+ * @param details 当前的请求配置
1461
+ */
1462
+ beforeRequestCallBack(details) { },
1463
+ };
1464
+ HttpxRequestDetails = {
1465
+ context: this,
1466
+ /**
1467
+ * 根据传入的参数处理获取details配置
1468
+ */
1469
+ handleBeforeRequestDetails(...args) {
1470
+ let result = {};
1471
+ if (typeof args[0] === "string") {
1472
+ /* 传入的是url,details? */
1473
+ let url = args[0];
1474
+ result.url = url;
1475
+ if (typeof args[1] === "object") {
1476
+ /* 处理第二个参数details */
1477
+ let details = args[1];
1478
+ result = details;
1479
+ result.url = url;
1480
+ }
1481
+ }
1482
+ else {
1483
+ /* 传入的是details */
1484
+ result = args[0];
1485
+ }
1486
+ return result;
1487
+ },
1488
+ /**
1489
+ * 获取请求配置
1490
+ * @param method 当前请求方法,默认get
1491
+ * @param resolve promise回调
1492
+ * @param details 请求配置
1493
+ */
1494
+ getDetails(method, resolve, details) {
1495
+ let that = this;
1496
+ let result = {
1497
+ url: details.url || this.context.#defaultDetails.url,
1498
+ method: (method || "GET").toString().toUpperCase(),
1499
+ timeout: details.timeout || this.context.#defaultDetails.timeout,
1500
+ responseType: details.responseType || this.context.#defaultDetails.responseType,
1501
+ /* 对象使用深拷贝 */
1502
+ headers: utils.deepClone(this.context.#defaultDetails.headers),
1503
+ data: details.data || this.context.#defaultDetails.data,
1504
+ redirect: details.redirect || this.context.#defaultDetails.redirect,
1505
+ cookie: details.cookie || this.context.#defaultDetails.cookie,
1506
+ binary: details.binary || this.context.#defaultDetails.binary,
1507
+ nocache: details.nocache || this.context.#defaultDetails.nocache,
1508
+ revalidate: details.revalidate || this.context.#defaultDetails.revalidate,
1509
+ /* 对象使用深拷贝 */
1510
+ context: utils.deepClone(details.context || this.context.#defaultDetails.context),
1511
+ overrideMimeType: details.overrideMimeType ||
1512
+ this.context.#defaultDetails.overrideMimeType,
1513
+ anonymous: details.anonymous || this.context.#defaultDetails.anonymous,
1514
+ fetch: details.fetch || this.context.#defaultDetails.fetch,
1515
+ /* 对象使用深拷贝 */
1516
+ fetchInit: utils.deepClone(this.context.#defaultDetails.fetchInit),
1517
+ user: details.user || this.context.#defaultDetails.user,
1518
+ password: details.password || this.context.#defaultDetails.password,
1519
+ onabort(...args) {
1520
+ that.context.HttpxCallBack.onAbort(details, resolve, args);
1521
+ },
1522
+ onerror(...args) {
1523
+ that.context.HttpxCallBack.onError(details, resolve, args);
1524
+ },
1525
+ onloadstart(...args) {
1526
+ that.context.HttpxCallBack.onLoadStart(details, args);
1527
+ },
1528
+ onprogress(...args) {
1529
+ that.context.HttpxCallBack.onProgress(details, args);
1530
+ },
1531
+ onreadystatechange(...args) {
1532
+ that.context.HttpxCallBack.onReadyStateChange(details, args);
1533
+ },
1534
+ ontimeout(...args) {
1535
+ that.context.HttpxCallBack.onTimeout(details, resolve, args);
1536
+ },
1537
+ onload(...args) {
1538
+ that.context.HttpxCallBack.onLoad(details, resolve, args);
1539
+ },
1540
+ };
1541
+ if (typeof this.context.GM_Api.xmlHttpRequest !== "function") {
1542
+ result.fetch = true;
1543
+ }
1544
+ if (typeof result.headers === "object") {
1545
+ if (typeof details.headers === "object") {
1546
+ Object.keys(details.headers).forEach((keyName, index) => {
1547
+ if (keyName in result.headers &&
1548
+ details.headers?.[keyName] == null) {
1549
+ /* 在默认的header中存在,且设置它新的值为空,那么就是默认的值 */
1550
+ Reflect.deleteProperty(result.headers, keyName);
1551
+ }
1552
+ else {
1553
+ result.headers[keyName] = details?.headers?.[keyName];
1554
+ }
1555
+ });
1556
+ }
1557
+ }
1558
+ else {
1559
+ result.headers = details.headers;
1560
+ }
1561
+ if (typeof result.fetchInit === "object") {
1562
+ /* 使用assign替换且添加 */
1563
+ if (typeof details.fetchInit === "object") {
1564
+ Object.keys(details.fetchInit).forEach((keyName, index) => {
1565
+ if (keyName in result.fetchInit &&
1566
+ details.fetchInit[keyName] == null) {
1567
+ /* 在默认的fetchInit中存在,且设置它新的值为空,那么就是默认的值 */
1568
+ Reflect.deleteProperty(result.fetchInit, keyName);
1569
+ }
1570
+ else {
1571
+ result.fetchInit[keyName] = details.fetchInit[keyName];
1572
+ }
1573
+ });
1574
+ }
1575
+ }
1576
+ else {
1577
+ result.fetchInit = details.fetchInit;
1578
+ }
1579
+ return result;
1580
+ },
1581
+ /**
1582
+ * 处理发送请求的details,去除值为undefined、空function的值
1583
+ * @param details
1584
+ */
1585
+ handle(details) {
1586
+ Object.keys(details).forEach((keyName) => {
1587
+ if (details[keyName] == null ||
1588
+ (details[keyName] instanceof Function &&
1589
+ utils.isNull(details[keyName]))) {
1590
+ Reflect.deleteProperty(details, keyName);
1591
+ return;
1592
+ }
1593
+ });
1594
+ if (utils.isNull(details.url)) {
1595
+ throw new TypeError(`Utils.Httpx 参数 url不符合要求: ${details.url}`);
1596
+ }
1597
+ /* method值统一大写,兼容Via */
1598
+ details.method = details.method.toUpperCase();
1599
+ /* 判断是否是以http开头,否则主动加上origin */
1600
+ try {
1601
+ new URL(details.url);
1602
+ }
1603
+ catch (error) {
1604
+ if (details.url.startsWith("//")) {
1605
+ details.url = globalThis.location.protocol + details.url;
1606
+ }
1607
+ else if (details.url.startsWith("/")) {
1608
+ details.url = globalThis.location.origin + details.url;
1609
+ }
1610
+ else {
1611
+ details.url = globalThis.location.origin + "/" + details.url;
1612
+ }
1613
+ }
1614
+ return details;
1615
+ },
1616
+ /**
1617
+ * 处理fetch的配置
1618
+ * @param details
1619
+ */
1620
+ handleFetchDetail(details) {
1621
+ /**
1622
+ * fetch的请求配置
1623
+ **/
1624
+ let fetchRequestInit = {};
1625
+ if ((details.method === "GET" || details.method === "HEAD") &&
1626
+ details.data != null) {
1627
+ /* GET HEAD 方法的请求不能包含 body 信息 */
1628
+ Reflect.deleteProperty(details, "data");
1629
+ }
1630
+ /* 中止信号控制器 */
1631
+ let abortController = new AbortController();
1632
+ let signal = abortController.signal;
1633
+ signal.onabort = () => {
1634
+ details.onabort({
1635
+ isFetch: true,
1636
+ responseText: "",
1637
+ response: null,
1638
+ readyState: 4,
1639
+ responseHeaders: "",
1640
+ status: 0,
1641
+ statusText: "",
1642
+ error: "aborted",
1643
+ });
1644
+ };
1645
+ fetchRequestInit.method = details.method ?? "GET";
1646
+ fetchRequestInit.headers = details.headers;
1647
+ fetchRequestInit.body = details.data;
1648
+ fetchRequestInit.mode = "cors";
1649
+ fetchRequestInit.credentials = "include";
1650
+ fetchRequestInit.cache = "no-cache";
1651
+ fetchRequestInit.redirect = "follow";
1652
+ fetchRequestInit.referrerPolicy = "origin-when-cross-origin";
1653
+ fetchRequestInit.signal = signal;
1654
+ Object.assign(fetchRequestInit, details.fetchInit || {});
1655
+ return {
1656
+ fetchDetails: details,
1657
+ fetchRequestInit: fetchRequestInit,
1658
+ abortController: abortController,
1659
+ };
1660
+ },
1661
+ };
1662
+ HttpxCallBack = {
1663
+ context: this,
1664
+ /**
1665
+ * onabort请求被取消-触发
1666
+ * @param details 配置
1667
+ * @param resolve 回调
1668
+ * @param argumentsList 参数列表
1669
+ */
1670
+ onAbort(details, resolve, argumentsList) {
1671
+ if ("onabort" in details) {
1672
+ details.onabort.apply(this, argumentsList);
1673
+ }
1674
+ else if ("onabort" in this.context.#defaultDetails) {
1675
+ this.context.#defaultDetails.onabort.apply(this, argumentsList);
1676
+ }
1677
+ resolve({
1678
+ status: false,
1679
+ data: [...argumentsList],
1680
+ msg: "请求被取消",
1681
+ type: "onabort",
1682
+ });
1683
+ },
1684
+ /**
1685
+ * onerror请求异常-触发
1686
+ * @param details 配置
1687
+ * @param resolve 回调
1688
+ * @param argumentsList 响应的参数列表
1689
+ */
1690
+ onError(details, resolve, argumentsList) {
1691
+ if ("onerror" in details) {
1692
+ details.onerror.apply(this, argumentsList);
1693
+ }
1694
+ else if ("onerror" in this.context.#defaultDetails) {
1695
+ this.context.#defaultDetails.onerror.apply(this, argumentsList);
1696
+ }
1697
+ let response = argumentsList;
1698
+ if (response.length) {
1699
+ response = response[0];
1700
+ }
1701
+ resolve({
1702
+ status: false,
1703
+ data: response,
1704
+ details: details,
1705
+ msg: "请求异常",
1706
+ type: "onerror",
1707
+ });
1708
+ },
1709
+ /**
1710
+ * ontimeout请求超时-触发
1711
+ * @param details 配置
1712
+ * @param resolve 回调
1713
+ * @param argumentsList 参数列表
1714
+ */
1715
+ onTimeout(details, resolve, argumentsList) {
1716
+ if ("ontimeout" in details) {
1717
+ details.ontimeout.apply(this, argumentsList);
1718
+ }
1719
+ else if ("ontimeout" in this.context.#defaultDetails) {
1720
+ this.context.#defaultDetails.ontimeout.apply(this, argumentsList);
1721
+ }
1722
+ resolve({
1723
+ status: false,
1724
+ data: [...argumentsList],
1725
+ msg: "请求超时",
1726
+ type: "ontimeout",
1727
+ });
1728
+ },
1729
+ /**
1730
+ * onloadstart请求开始-触发
1731
+ * @param details 配置
1732
+ * @param argumentsList 参数列表
1733
+ */
1734
+ onLoadStart(details, argumentsList) {
1735
+ if ("onloadstart" in details) {
1736
+ details.onloadstart.apply(this, argumentsList);
1737
+ }
1738
+ else if ("onloadstart" in this.context.#defaultDetails) {
1739
+ this.context.#defaultDetails.onloadstart.apply(this, argumentsList);
1740
+ }
1741
+ },
1742
+ /**
1743
+ * onload加载完毕-触发
1744
+ * @param details 请求的配置
1745
+ * @param resolve 回调
1746
+ * @param argumentsList 参数列表
1747
+ */
1748
+ onLoad(details, resolve, argumentsList) {
1749
+ /* X浏览器会因为设置了responseType导致不返回responseText */
1750
+ let Response = argumentsList[0];
1751
+ /* responseText为空,response不为空的情况 */
1752
+ if (utils.isNull(Response["responseText"]) &&
1753
+ utils.isNotNull(Response["response"])) {
1754
+ if (typeof Response["response"] === "object") {
1755
+ utils.tryCatch().run(() => {
1756
+ Response["responseText"] = JSON.stringify(Response["response"]);
1757
+ });
1758
+ }
1759
+ else {
1760
+ Response["responseText"] = Response["response"];
1761
+ }
1762
+ }
1763
+ /* response为空,responseText不为空的情况 */
1764
+ if (Response["response"] == null &&
1765
+ typeof Response["responseText"] === "string" &&
1766
+ Response["responseText"].trim() !== "") {
1767
+ let newResponse = Response["responseText"];
1768
+ if (details.responseType === "json") {
1769
+ newResponse = utils.toJSON(Response["responseText"]);
1770
+ }
1771
+ else if (details.responseType === "document") {
1772
+ let parser = new DOMParser();
1773
+ newResponse = parser.parseFromString(Response["responseText"], "text/html");
1774
+ }
1775
+ else if (details.responseType === "arraybuffer") {
1776
+ let encoder = new TextEncoder();
1777
+ let arrayBuffer = encoder.encode(Response["responseText"]);
1778
+ newResponse = arrayBuffer;
1779
+ }
1780
+ else if (details.responseType === "blob") {
1781
+ let encoder = new TextEncoder();
1782
+ let arrayBuffer = encoder.encode(Response["responseText"]);
1783
+ newResponse = new Blob([arrayBuffer]);
1784
+ }
1785
+ else {
1786
+ newResponse = Response["responseText"];
1787
+ }
1788
+ try {
1789
+ Response["response"] = newResponse;
1790
+ }
1791
+ catch (error) {
1792
+ console.warn("response 无法被覆盖");
1793
+ }
1794
+ }
1795
+ /* Stay扩展中没有finalUrl,对应的是responseURL */
1796
+ if (Response["finalUrl"] == null &&
1797
+ Response["responseURL"] != null) {
1798
+ Response["finalUrl"] = Response["responseURL"];
1799
+ }
1800
+ /* 状态码2xx都是成功的 */
1801
+ if (Math.floor(Response.status / 100) === 2) {
1802
+ resolve({
1803
+ status: true,
1804
+ data: Response,
1805
+ details: details,
1806
+ msg: "请求完毕",
1807
+ type: "onload",
1808
+ });
1809
+ }
1810
+ else {
1811
+ this.context.HttpxCallBack.onError(details, resolve, argumentsList);
1812
+ }
1813
+ },
1814
+ /**
1815
+ * onprogress上传进度-触发
1816
+ * @param details 配置
1817
+ * @param argumentsList 参数列表
1818
+ */
1819
+ onProgress(details, argumentsList) {
1820
+ if ("onprogress" in details) {
1821
+ details.onprogress.apply(this, argumentsList);
1822
+ }
1823
+ else if ("onprogress" in this.context.#defaultDetails) {
1824
+ this.context.#defaultDetails.onprogress.apply(this, argumentsList);
1825
+ }
1826
+ },
1827
+ /**
1828
+ * onreadystatechange准备状态改变-触发
1829
+ * @param details 配置
1830
+ * @param argumentsList 参数列表
1831
+ */
1832
+ onReadyStateChange(details, argumentsList) {
1833
+ if ("onreadystatechange" in details) {
1834
+ details.onreadystatechange.apply(this, argumentsList);
1835
+ }
1836
+ else if ("onreadystatechange" in this.context.#defaultDetails) {
1837
+ this.context.#defaultDetails.onreadystatechange.apply(this, argumentsList);
1838
+ }
1839
+ },
1840
+ };
1841
+ HttpxRequest = {
1842
+ context: this,
1843
+ /**
1844
+ * 发送请求
1845
+ * @param details
1846
+ */
1847
+ request(details) {
1848
+ if (this.context.#LOG_DETAILS) {
1849
+ console.log("Httpx请求配置👇", details);
1850
+ }
1851
+ if (typeof this.context.HttpxRequestHook.beforeRequestCallBack ===
1852
+ "function") {
1853
+ let hookResult = this.context.HttpxRequestHook.beforeRequestCallBack(details);
1854
+ if (typeof hookResult === "boolean" && !hookResult) {
1855
+ return;
1856
+ }
1857
+ }
1858
+ if (details.fetch) {
1859
+ const { fetchDetails, fetchRequestInit, abortController } = this.context.HttpxRequestDetails.handleFetchDetail(details);
1860
+ this.fetch(fetchDetails, fetchRequestInit, abortController);
1861
+ }
1862
+ else {
1863
+ Reflect.deleteProperty(details, "fetchInit");
1864
+ this.xmlHttpRequest(details);
1865
+ }
1866
+ },
1867
+ /**
1868
+ * 使用油猴函数GM_xmlhttpRequest发送请求
1869
+ * @param details
1870
+ */
1871
+ xmlHttpRequest(details) {
1872
+ this.context.GM_Api.xmlHttpRequest(details);
1873
+ },
1874
+ /**
1875
+ * 使用fetch发送请求
1876
+ * @param details
1877
+ * @param fetchRequestInit
1878
+ * @param abortController
1879
+ */
1880
+ fetch(details, fetchRequestInit, abortController) {
1881
+ fetch(details.url, fetchRequestInit)
1882
+ .then(async (resp) => {
1883
+ /**
1884
+ * @type {HttpxAsyncResultData}
1885
+ */
1886
+ let httpxResponse = {
1887
+ isFetch: true,
1888
+ finalUrl: resp.url,
1889
+ readyState: 4,
1890
+ status: resp.status,
1891
+ statusText: resp.statusText,
1892
+ response: void 0,
1893
+ responseFetchHeaders: resp.headers,
1894
+ responseHeaders: "",
1895
+ responseText: void 0,
1896
+ responseType: details.responseType,
1897
+ responseXML: void 0,
1898
+ };
1899
+ Object.assign(httpxResponse, details.context || {});
1900
+ for (const [key, value] of resp.headers.entries()) {
1901
+ httpxResponse.responseHeaders += `${key}: ${value}\n`;
1902
+ }
1903
+ /* 如果是流式传输,直接返回 */
1904
+ if (details.responseType === "stream" ||
1905
+ (resp.headers.has("Content-Type") &&
1906
+ resp.headers.get("Content-Type").includes("text/event-stream"))) {
1907
+ httpxResponse["isStream"] = true;
1908
+ httpxResponse.response = resp.body;
1909
+ Reflect.deleteProperty(httpxResponse, "responseText");
1910
+ Reflect.deleteProperty(httpxResponse, "responseXML");
1911
+ details.onload(httpxResponse);
1912
+ return;
1913
+ }
1914
+ /** 响应 */
1915
+ let response = "";
1916
+ /** 响应字符串 */
1917
+ let responseText = "";
1918
+ /** 响应xml文档 */
1919
+ let responseXML = "";
1920
+ let arrayBuffer = await resp.arrayBuffer;
1921
+ let encoding = "utf-8";
1922
+ if (resp.headers.has("Content-Type")) {
1923
+ let charsetMatched = resp.headers
1924
+ .get("Content-Type")
1925
+ ?.match(/charset=(.+)/);
1926
+ if (charsetMatched) {
1927
+ encoding = charsetMatched[1];
1928
+ }
1929
+ }
1930
+ let textDecoder = new TextDecoder(encoding);
1931
+ responseText = textDecoder.decode(await resp.arrayBuffer());
1932
+ response = responseText;
1933
+ if (details.responseType === "arraybuffer") {
1934
+ response = arrayBuffer;
1935
+ }
1936
+ else if (details.responseType === "blob") {
1937
+ response = new Blob([arrayBuffer]);
1938
+ }
1939
+ else if (details.responseType === "document" ||
1940
+ details.responseType == null) {
1941
+ let parser = new DOMParser();
1942
+ response = parser.parseFromString(responseText, "text/html");
1943
+ }
1944
+ else if (details.responseType === "json") {
1945
+ response = utils.toJSON(responseText);
1946
+ }
1947
+ let parser = new DOMParser();
1948
+ responseXML = parser.parseFromString(responseText, "text/xml");
1949
+ httpxResponse.response = response;
1950
+ httpxResponse.responseText = responseText;
1951
+ httpxResponse.responseXML = responseXML;
1952
+ details.onload(httpxResponse);
1953
+ })
1954
+ .catch((err) => {
1955
+ if (err.name === "AbortError") {
1956
+ return;
1957
+ }
1958
+ details.onerror({
1959
+ isFetch: true,
1960
+ finalUrl: details.url,
1961
+ readyState: 4,
1962
+ status: 0,
1963
+ statusText: "",
1964
+ responseHeaders: "",
1965
+ responseText: "",
1966
+ error: err,
1967
+ });
1968
+ });
1969
+ details.onloadstart({
1970
+ isFetch: true,
1971
+ finalUrl: details.url,
1972
+ readyState: 1,
1973
+ responseHeaders: "",
1974
+ responseText: "",
1975
+ status: 0,
1976
+ statusText: "",
1977
+ });
1978
+ return {
1979
+ abort() {
1980
+ abortController.abort();
1981
+ },
1982
+ };
1983
+ },
1984
+ };
1985
+ /**
1986
+ * 默认配置
1987
+ */
1988
+ #defaultDetails = {
1989
+ url: void 0,
1990
+ timeout: 5000,
1991
+ async: false,
1992
+ responseType: void 0,
1993
+ headers: void 0,
1994
+ data: void 0,
1995
+ redirect: void 0,
1996
+ cookie: void 0,
1997
+ binary: void 0,
1998
+ nocache: void 0,
1999
+ revalidate: void 0,
2000
+ context: void 0,
2001
+ overrideMimeType: void 0,
2002
+ anonymous: void 0,
2003
+ fetch: void 0,
2004
+ fetchInit: void 0,
2005
+ user: void 0,
2006
+ password: void 0,
2007
+ onabort() { },
2008
+ onerror() { },
2009
+ ontimeout() { },
2010
+ onloadstart() { },
2011
+ onreadystatechange() { },
2012
+ onprogress() { },
2013
+ };
2014
+ /**
2015
+ * 当前使用请求时,输出请求的配置
2016
+ */
2017
+ #LOG_DETAILS = false;
2018
+ constructor(__xmlHttpRequest__) {
2019
+ if (typeof __xmlHttpRequest__ !== "function") {
2020
+ console.warn("Httpx未传入GM_xmlhttpRequest函数或传入的GM_xmlhttpRequest不是Function,强制使用window.fetch");
2021
+ }
2022
+ this.GM_Api.xmlHttpRequest = __xmlHttpRequest__;
2023
+ }
2024
+ /**
2025
+ * 覆盖当前配置
2026
+ * @param details
2027
+ */
2028
+ config(details = {}) {
2029
+ if ("logDetails" in details && typeof details["logDetails"] === "boolean") {
2030
+ this.#LOG_DETAILS = details["logDetails"];
2031
+ }
2032
+ this.#defaultDetails = utils.assign(this.#defaultDetails, details);
2033
+ }
2034
+ /**
2035
+ * 修改xmlHttpRequest
2036
+ * @param httpRequest 网络请求函数
2037
+ */
2038
+ setXMLHttpRequest(httpRequest) {
2039
+ this.GM_Api.xmlHttpRequest = httpRequest;
2040
+ }
2041
+ /**
2042
+ * GET 请求
2043
+ */
2044
+ async get(...args) {
2045
+ let that = this;
2046
+ let details = this.HttpxRequestDetails.handleBeforeRequestDetails(...args);
2047
+ return new Promise((resolve) => {
2048
+ let requestDetails = that.HttpxRequestDetails.getDetails("get", resolve, details);
2049
+ Reflect.deleteProperty(requestDetails, "onprogress");
2050
+ requestDetails = that.HttpxRequestDetails.handle(requestDetails);
2051
+ that.HttpxRequest.request(requestDetails);
2052
+ });
2053
+ }
2054
+ /**
2055
+ * POST 请求
2056
+ */
2057
+ async post(...args) {
2058
+ let that = this;
2059
+ let details = this.HttpxRequestDetails.handleBeforeRequestDetails(...args);
2060
+ return new Promise((resolve) => {
2061
+ let requestDetails = that.HttpxRequestDetails.getDetails("post", resolve, details);
2062
+ requestDetails = that.HttpxRequestDetails.handle(requestDetails);
2063
+ that.HttpxRequest.request(requestDetails);
2064
+ });
2065
+ }
2066
+ /**
2067
+ * HEAD 请求
2068
+ */
2069
+ async head(...args) {
2070
+ let that = this;
2071
+ let details = this.HttpxRequestDetails.handleBeforeRequestDetails(...args);
2072
+ return new Promise((resolve) => {
2073
+ let requestDetails = that.HttpxRequestDetails.getDetails("head", resolve, details);
2074
+ Reflect.deleteProperty(requestDetails, "onprogress");
2075
+ requestDetails = that.HttpxRequestDetails.handle(requestDetails);
2076
+ that.HttpxRequest.request(requestDetails);
2077
+ });
2078
+ }
2079
+ /**
2080
+ * OPTIONS 请求
2081
+ */
2082
+ async options(...args) {
2083
+ let that = this;
2084
+ let details = this.HttpxRequestDetails.handleBeforeRequestDetails(...args);
2085
+ return new Promise((resolve) => {
2086
+ let requestDetails = that.HttpxRequestDetails.getDetails("options", resolve, details);
2087
+ Reflect.deleteProperty(requestDetails, "onprogress");
2088
+ requestDetails = that.HttpxRequestDetails.handle(requestDetails);
2089
+ that.HttpxRequest.request(requestDetails);
2090
+ });
2091
+ }
2092
+ /**
2093
+ * DELETE 请求
2094
+ */
2095
+ async delete(...args) {
2096
+ let that = this;
2097
+ let details = this.HttpxRequestDetails.handleBeforeRequestDetails(...args);
2098
+ return new Promise((resolve) => {
2099
+ let requestDetails = that.HttpxRequestDetails.getDetails("delete", resolve, details);
2100
+ Reflect.deleteProperty(requestDetails, "onprogress");
2101
+ requestDetails = that.HttpxRequestDetails.handle(requestDetails);
2102
+ that.HttpxRequest.request(requestDetails);
2103
+ });
2104
+ }
2105
+ /**
2106
+ * PUT 请求
2107
+ */
2108
+ async put(...args) {
2109
+ let that = this;
2110
+ let details = this.HttpxRequestDetails.handleBeforeRequestDetails(...args);
2111
+ return new Promise((resolve) => {
2112
+ let requestDetails = that.HttpxRequestDetails.getDetails("put", resolve, details);
2113
+ requestDetails = that.HttpxRequestDetails.handle(requestDetails);
2114
+ that.HttpxRequest.request(requestDetails);
2115
+ });
2116
+ }
2117
+ }
2220
2118
 
2221
- const indexedDB = function (
2222
- dbName = "default_db",
2223
- storeName = "default_form",
2224
- dbVersion = 1
2225
- ) {
2226
- this.dbName = dbName;
2227
- /* websql的版本号,由于ios的问题,版本号的写法不一样 */
2228
- this.slqVersion = "1";
2229
- this.dbVersion = dbVersion;
2230
- this.storeName = storeName;
2231
- /* 监听IndexDB */
2232
- this.indexedDB =
2233
- window.indexedDB ||
2234
- window.mozIndexedDB ||
2235
- window.webkitIndexedDB ||
2236
- window.msIndexedDB;
2237
- if (!this.indexedDB) {
2238
- alert("很抱歉,您的浏览器不支持indexedDB");
2239
- }
2240
- /* 缓存数据库,避免同一个页面重复创建和销毁 */
2241
- this.db = {};
2242
- this.store = null;
2243
- this.errorCode = {
2244
- /* 错误码 */
2245
- success: {
2246
- code: 200,
2247
- msg: "操作成功",
2248
- },
2249
- error: {
2250
- code: 401,
2251
- msg: "操作失败",
2252
- },
2253
- open: { code: 91001, msg: "打开数据库失败" },
2254
- save: { code: 91002, msg: "保存数据失败" },
2255
- get: { code: 91003, msg: "获取数据失败" },
2256
- delete: { code: 91004, msg: "删除数据失败" },
2257
- deleteAll: { code: 91005, msg: "清空数据库失败" },
2258
- };
2259
- let that = this;
2260
- /**
2261
- * 创建 “表”
2262
- * @param {string} dbName 表名
2263
- * @returns
2264
- */
2265
- this.createStore = function (dbName) {
2266
- let txn, store;
2267
- if (that.indexedDB) {
2268
- /* 如果是支持IndexDB的 */
2269
- txn = that.db[dbName].transaction(that.storeName, "readwrite");
2270
- /* IndexDB的读写权限 */
2271
- store = txn.objectStore(that.storeName);
2272
- }
2273
- return store;
2274
- };
2275
- /**
2276
- * 打开数据库
2277
- * @param {function} callback 回调
2278
- * @param {string} dbName 数据库名
2279
- */
2280
- this.open = function (callback, dbName) {
2281
- /* 打开数据库 */
2282
- if (that.indexedDB) {
2283
- /* 如果支持IndexDB */
2284
- if (!that.db[dbName]) {
2285
- /* 如果缓存中没有,则进行数据库的创建或打开,提高效率 */
2286
- let request = that.indexedDB.open(dbName, that.dbVersion);
2287
- request.onerror = function (e) {
2288
- callback({
2289
- code: that.errorCode.open.code,
2290
- msg: that.errorCode.open.msg,
2291
- error: e,
2292
- });
2293
- };
2294
- request.onsuccess = function (e) {
2295
- if (!that.db[dbName]) {
2296
- that.db[dbName] = e.target.result;
2297
- }
2298
- let store = that.createStore(dbName);
2299
- callback(store);
2300
- };
2301
- request.onupgradeneeded = function (e) {
2302
- that.db[dbName] = e.target.result;
2303
- let store = that.db[dbName].createObjectStore(that.storeName, {
2304
- keyPath: "key",
2305
- });
2306
- store.transaction.oncomplete = function (event) {
2307
- callback(store);
2308
- };
2309
- };
2310
- } else {
2311
- /* 如果缓存中已经打开了数据库,就直接使用 */
2312
- let store = that.createStore(dbName);
2313
- callback(store);
2314
- }
2315
- }
2316
- };
2317
- /**
2318
- * 保存数据到数据库
2319
- * @param {string} key 数据key
2320
- * @param {any} value 数据值
2321
- * @returns {Promise< {
2322
- * code: number,
2323
- * msg: string,
2324
- * success: boolean
2325
- * }>}
2326
- */
2327
- this.save = async function (key, value) {
2328
- if (that.indexedDB) {
2329
- return new Promise((resolve, reject) => {
2330
- let dbName = that.dbName;
2331
- let inData = {
2332
- key: key,
2333
- value: value,
2334
- };
2335
- that.open(function (result) {
2336
- let error = result.hasOwnProperty("error");
2337
- if (error) {
2338
- resolve(result);
2339
- } else {
2340
- let request = result.put(inData);
2341
- request.onsuccess = function (e) {
2342
- /* 保存成功有success 字段 */
2343
- resolve({
2344
- code: that.errorCode.success.code,
2345
- msg: that.errorCode.success.msg,
2346
- success: true,
2347
- });
2348
- };
2349
- request.onerror = function (e) {
2350
- resolve({
2351
- code: that.errorCode.save.code,
2352
- msg: that.errorCode.save.msg,
2353
- error: e,
2354
- });
2355
- };
2356
- }
2357
- }, dbName);
2358
- });
2359
- }
2360
- };
2361
- /**
2362
- * 根据key获取值
2363
- * @param {string} key 数据key
2364
- * @returns {Promise< {
2365
- * code: number,
2366
- * msg: string,
2367
- * data: [...any],
2368
- * success: true
2369
- * }| {
2370
- * code: number,
2371
- * msg: string,
2372
- * error: Error,
2373
- * result: any,
2374
- * } >}
2375
- */
2376
- this.get = async function (key) {
2377
- return new Promise((resolve, reject) => {
2378
- let dbName = that.dbName;
2379
- if (that.indexedDB) {
2380
- that.open(function (result) {
2381
- /* 判断返回的数据中是否有error字段 */
2382
- let error = result.hasOwnProperty("error");
2383
- if (error) {
2384
- reject({
2385
- code: that.errorCode.open.get,
2386
- msg: that.errorCode.get.msg,
2387
- error: error,
2388
- result: result,
2389
- });
2390
- } else {
2391
- let request = result.get(key);
2392
- request.onsuccess = function (e) {
2393
- let result = e.target.result;
2394
- let data = result ? result.value : void 0;
2395
- resolve({
2396
- code: data
2397
- ? that.errorCode.success.code
2398
- : that.errorCode.error.code,
2399
- msg: data
2400
- ? that.errorCode.success.msg
2401
- : that.errorCode.error.msg,
2402
- data: data || [],
2403
- success: true,
2404
- });
2405
- };
2406
- request.onerror = function (e) {
2407
- reject({
2408
- code: that.errorCode.get.code,
2409
- msg: that.errorCode.get.msg,
2410
- result: result,
2411
- error: e,
2412
- });
2413
- };
2414
- }
2415
- }, dbName);
2416
- }
2417
- });
2418
- };
2419
- /**
2420
- * 正则获取数据
2421
- * @param {string} key 数据键
2422
- * @returns { Promise<{
2423
- * code: number,
2424
- * msg: string,
2425
- * data: [...any],
2426
- * success: true
2427
- * }|{
2428
- * code: number,
2429
- * msg: string,
2430
- * error: Error,
2431
- * result: any,
2432
- * }> }
2433
- */
2434
- this.regexpGet = async function (key) {
2435
- let list = [];
2436
- return new Promise((resolve, reject) => {
2437
- /* 正则查询 */
2438
- let dbName = that.dbName;
2439
- if (that.indexedDB) {
2440
- that.open(function (result) {
2441
- /* 判断返回的数据中是否有error字段 */
2442
- let error = result.hasOwnProperty("error");
2443
- if (error) {
2444
- reject({
2445
- code: that.errorCode.open.get,
2446
- msg: that.errorCode.get.msg,
2447
- error: error,
2448
- result: result,
2449
- });
2450
- } else {
2451
- let request = result.getAll();
2452
- request.onsuccess = function (e) {
2453
- let result = e.target.result;
2454
- if (result.length !== 0) {
2455
- result.forEach((item, index) => {
2456
- if (item["key"].match(key)) {
2457
- let concatList = item["value"];
2458
- concatList["key"] = item["key"];
2459
- list = [...list, concatList];
2460
- }
2461
- });
2462
- }
2463
- resolve({
2464
- code: that.errorCode.success.code,
2465
- msg: that.errorCode.success.msg,
2466
- data: list,
2467
- success: true,
2468
- });
2469
- };
2470
- request.onerror = function (e) {
2471
- reject({
2472
- code: that.errorCode.get.code,
2473
- msg: that.errorCode.get.msg,
2474
- result: result,
2475
- error: e,
2476
- });
2477
- };
2478
- }
2479
- }, dbName);
2480
- }
2481
- });
2482
- };
2483
- /**
2484
- * 删除数据
2485
- * @param {string} key 数据键
2486
- * @returns {Promise<{
2487
- * code: number,
2488
- * msg: string,
2489
- * success: true,
2490
- * }|{
2491
- * code: number,
2492
- * msg: string,
2493
- * error: Error,
2494
- * }>}
2495
- */
2496
- this.delete = async function (key) {
2497
- return new Promise((resolve, reject) => {
2498
- /* 根据key删除某条数据 */
2499
- let dbName = that.dbName;
2500
- if (that.indexedDB) {
2501
- that.open(function (result) {
2502
- let error = result.hasOwnProperty("error");
2503
- if (error) {
2504
- resolve(result);
2505
- } else {
2506
- let request = result.get(key);
2507
- request.onsuccess = function (e) {
2508
- let recode = e.target.result;
2509
- if (recode) {
2510
- request = result.delete(key);
2511
- }
2512
- resolve({
2513
- code: recode
2514
- ? that.errorCode.success.code
2515
- : that.errorCode.error.code,
2516
- msg: recode
2517
- ? that.errorCode.success.msg
2518
- : that.errorCode.error.msg,
2519
- success: true,
2520
- });
2521
- };
2522
- request.onerror = function (e) {
2523
- resolve({
2524
- code: that.errorCode.delete.code,
2525
- msg: that.errorCode.delete.msg,
2526
- error: e,
2527
- });
2528
- };
2529
- }
2530
- }, dbName);
2531
- }
2532
- });
2533
- };
2534
- /**
2535
- * 删除所有数据
2536
- * @returns {Promise<{
2537
- * code: number,
2538
- * msg: string,
2539
- * error: Error,
2540
- * result: any,
2541
- * }|{
2542
- * code: number,
2543
- * msg: string,
2544
- * success: true,
2545
- * }>}
2546
- */
2547
- this.deleteAll = async function () {
2548
- return new Promise((resolve, reject) => {
2549
- /* 清空数据库 */
2550
- let dbName = that.dbName;
2551
- if (that.indexedDB) {
2552
- that.open(function (result) {
2553
- let error = result.hasOwnProperty("error");
2554
- if (error) {
2555
- resolve({
2556
- code: that.errorCode.deleteAll.code,
2557
- msg: that.errorCode.deleteAll.msg,
2558
- error: error,
2559
- result: result,
2560
- });
2561
- } else {
2562
- result.clear();
2563
- resolve({
2564
- code: that.errorCode.success.code,
2565
- msg: that.errorCode.success.msg,
2566
- success: true,
2567
- });
2568
- }
2569
- }, dbName);
2570
- }
2571
- });
2572
- };
2573
- };
2119
+ class indexedDB {
2120
+ #dbName;
2121
+ #storeName;
2122
+ #dbVersion;
2123
+ /* websql的版本号,由于ios的问题,版本号的写法不一样 */
2124
+ #slqVersion = "1";
2125
+ /* 监听IndexDB */
2126
+ #indexedDB = window.indexedDB ||
2127
+ window.mozIndexedDB ||
2128
+ window.webkitIndexedDB ||
2129
+ window.msIndexedDB;
2130
+ /* 缓存数据库,避免同一个页面重复创建和销毁 */
2131
+ #db = {};
2132
+ #store = null;
2133
+ #errorCode = {
2134
+ /* 错误码 */
2135
+ success: {
2136
+ code: 200,
2137
+ msg: "操作成功",
2138
+ },
2139
+ error: {
2140
+ code: 401,
2141
+ msg: "操作失败",
2142
+ },
2143
+ open: { code: 91001, msg: "打开数据库失败" },
2144
+ save: { code: 91002, msg: "保存数据失败" },
2145
+ get: { code: 91003, msg: "获取数据失败" },
2146
+ delete: { code: 91004, msg: "删除数据失败" },
2147
+ deleteAll: { code: 91005, msg: "清空数据库失败" },
2148
+ regexpGet: { code: 91006, msg: "正则获取数据失败" },
2149
+ };
2150
+ /**
2151
+ * @param dbName 数据存储名,默认为:default_db
2152
+ * @param storeName 表名,默认为:default_form
2153
+ * @param dbVersion indexDB的版本号,默认为:1
2154
+ */
2155
+ constructor(dbName = "default_db", storeName = "default_form", dbVersion = 1) {
2156
+ this.#dbName = dbName;
2157
+ this.#storeName = storeName;
2158
+ this.#dbVersion = dbVersion;
2159
+ if (!this.#indexedDB) {
2160
+ alert("很抱歉,您的浏览器不支持indexedDB");
2161
+ throw new TypeError("很抱歉,您的浏览器不支持indexedDB");
2162
+ }
2163
+ }
2164
+ /**
2165
+ * 创建 “表”
2166
+ * @param dbName 表名
2167
+ */
2168
+ createStore(dbName) {
2169
+ let txn, store;
2170
+ txn = this.#db[dbName].transaction(this.#storeName, "readwrite");
2171
+ /* IndexDB的读写权限 */
2172
+ store = txn.objectStore(this.#storeName);
2173
+ this.#store = store;
2174
+ return store;
2175
+ }
2176
+ /**
2177
+ * 打开数据库
2178
+ * @param callback 回调
2179
+ * @param dbName 数据库名
2180
+ */
2181
+ open(callback, dbName) {
2182
+ let that = this;
2183
+ /* 打开数据库 */
2184
+ /* 如果支持IndexDB */
2185
+ if (!that.#db[dbName]) {
2186
+ /* 如果缓存中没有,则进行数据库的创建或打开,提高效率 */
2187
+ let request = that.#indexedDB.open(dbName, that.#dbVersion);
2188
+ request.onerror = function (event) {
2189
+ callback({
2190
+ code: that.#errorCode.open.code,
2191
+ msg: that.#errorCode.open.msg,
2192
+ event: event,
2193
+ }, false);
2194
+ };
2195
+ request.onsuccess = function (event) {
2196
+ if (!that.#db[dbName]) {
2197
+ let target = event.target;
2198
+ that.#db[dbName] = target.result;
2199
+ }
2200
+ let store = that.createStore(dbName);
2201
+ callback(store, true);
2202
+ };
2203
+ request.onupgradeneeded = function (event) {
2204
+ let target = event.target;
2205
+ that.#db[dbName] = target.result;
2206
+ let store = that.#db[dbName].createObjectStore(that.#storeName, {
2207
+ keyPath: "key",
2208
+ });
2209
+ store.transaction.oncomplete = function (event) {
2210
+ callback(store, true);
2211
+ };
2212
+ };
2213
+ }
2214
+ else {
2215
+ /* 如果缓存中已经打开了数据库,就直接使用 */
2216
+ let store = that.createStore(dbName);
2217
+ callback(store, true);
2218
+ }
2219
+ }
2220
+ /**
2221
+ * 保存数据到数据库
2222
+ * @param key 数据key
2223
+ * @param value 数据值
2224
+ */
2225
+ async save(key, value) {
2226
+ let that = this;
2227
+ return new Promise((resolve) => {
2228
+ let dbName = that.#dbName;
2229
+ let inData = {
2230
+ key: key,
2231
+ value: value,
2232
+ };
2233
+ that.open(function (idbStore, success) {
2234
+ if (!success) {
2235
+ resolve({
2236
+ success: false,
2237
+ code: that.#errorCode.save.code,
2238
+ msg: that.#errorCode.save.msg,
2239
+ });
2240
+ }
2241
+ else {
2242
+ idbStore = idbStore;
2243
+ let request = idbStore.put(inData);
2244
+ request.onsuccess = function (event) {
2245
+ /* 保存成功有success 字段 */
2246
+ event.target;
2247
+ resolve({
2248
+ success: true,
2249
+ code: that.#errorCode.success.code,
2250
+ msg: that.#errorCode.success.msg,
2251
+ event: event,
2252
+ });
2253
+ };
2254
+ request.onerror = function (event) {
2255
+ event.target;
2256
+ resolve({
2257
+ success: false,
2258
+ code: that.#errorCode.save.code,
2259
+ msg: that.#errorCode.save.msg,
2260
+ event: event,
2261
+ });
2262
+ };
2263
+ }
2264
+ }, dbName);
2265
+ });
2266
+ }
2267
+ /**
2268
+ * 根据key获取值
2269
+ * @param key 数据key
2270
+ */
2271
+ async get(key) {
2272
+ let that = this;
2273
+ return new Promise((resolve) => {
2274
+ let dbName = that.#dbName;
2275
+ that.open(function (idbStore, success) {
2276
+ /* 判断返回的数据中是否有error字段 */
2277
+ if (!success) {
2278
+ resolve({
2279
+ success: false,
2280
+ code: that.#errorCode.get.code,
2281
+ msg: that.#errorCode.get.msg,
2282
+ data: void 0,
2283
+ });
2284
+ }
2285
+ else {
2286
+ idbStore = idbStore;
2287
+ let request = idbStore.get(key);
2288
+ request.onsuccess = function (event) {
2289
+ let target = event.target;
2290
+ let result = target.result;
2291
+ /* result 返回的是 {key: string, value: any} */
2292
+ /* 键值对存储 */
2293
+ let data = result ? result.value : void 0;
2294
+ if (data) {
2295
+ resolve({
2296
+ success: true,
2297
+ code: that.#errorCode.success.code,
2298
+ msg: that.#errorCode.success.msg,
2299
+ data: data,
2300
+ event: event,
2301
+ result: result,
2302
+ });
2303
+ }
2304
+ else {
2305
+ resolve({
2306
+ success: false,
2307
+ code: that.#errorCode.error.code,
2308
+ msg: that.#errorCode.error.msg,
2309
+ data: void 0,
2310
+ event: event,
2311
+ result: result,
2312
+ });
2313
+ }
2314
+ };
2315
+ request.onerror = function (event) {
2316
+ event.target;
2317
+ resolve({
2318
+ success: false,
2319
+ code: that.#errorCode.get.code,
2320
+ msg: that.#errorCode.get.msg,
2321
+ data: void 0,
2322
+ event: event,
2323
+ });
2324
+ };
2325
+ }
2326
+ }, dbName);
2327
+ });
2328
+ }
2329
+ /**
2330
+ * 正则获取数据
2331
+ * @param key 数据键
2332
+ */
2333
+ async regexpGet(key) {
2334
+ let list = [];
2335
+ let that = this;
2336
+ return new Promise((resolve) => {
2337
+ /* 正则查询 */
2338
+ let dbName = that.#dbName;
2339
+ that.open(function (idbStore, success) {
2340
+ /* 判断返回的数据中是否有error字段 */
2341
+ if (!success) {
2342
+ resolve({
2343
+ success: false,
2344
+ code: that.#errorCode.regexpGet.code,
2345
+ msg: that.#errorCode.regexpGet.msg,
2346
+ data: [],
2347
+ });
2348
+ }
2349
+ else {
2350
+ idbStore = idbStore;
2351
+ let request = idbStore.getAll();
2352
+ request.onsuccess = function (event) {
2353
+ let target = event.target;
2354
+ let result = target.result;
2355
+ if (result.length !== 0) {
2356
+ result.forEach((item, index) => {
2357
+ if (item["key"].match(key)) {
2358
+ let concatList = item["value"];
2359
+ concatList["key"] = item["key"];
2360
+ list = [...list, concatList];
2361
+ }
2362
+ });
2363
+ }
2364
+ resolve({
2365
+ success: true,
2366
+ code: that.#errorCode.success.code,
2367
+ msg: that.#errorCode.success.msg,
2368
+ data: list,
2369
+ event: event,
2370
+ });
2371
+ };
2372
+ request.onerror = function (event) {
2373
+ event.target;
2374
+ resolve({
2375
+ success: false,
2376
+ code: that.#errorCode.get.code,
2377
+ msg: that.#errorCode.get.msg,
2378
+ data: [],
2379
+ event: event,
2380
+ });
2381
+ };
2382
+ }
2383
+ }, dbName);
2384
+ });
2385
+ }
2386
+ /**
2387
+ * 删除数据
2388
+ * @param {string} key 数据键
2389
+ */
2390
+ async delete(key) {
2391
+ let that = this;
2392
+ return new Promise((resolve) => {
2393
+ /* 根据key删除某条数据 */
2394
+ let dbName = that.#dbName;
2395
+ that.open(function (idbStore, success) {
2396
+ if (!success) {
2397
+ resolve({
2398
+ success: false,
2399
+ code: that.#errorCode.delete.code,
2400
+ msg: that.#errorCode.delete.msg,
2401
+ });
2402
+ }
2403
+ else {
2404
+ idbStore = idbStore;
2405
+ let request = idbStore.get(key);
2406
+ request.onsuccess = function (event) {
2407
+ let target = event.target;
2408
+ let recode = target.result;
2409
+ if (recode) {
2410
+ /* 成功 */
2411
+ request = idbStore.delete(key);
2412
+ resolve({
2413
+ success: true,
2414
+ code: that.#errorCode.success.code,
2415
+ msg: that.#errorCode.success.msg,
2416
+ });
2417
+ }
2418
+ else {
2419
+ resolve({
2420
+ success: false,
2421
+ code: that.#errorCode.error.code,
2422
+ msg: that.#errorCode.error.msg,
2423
+ });
2424
+ }
2425
+ };
2426
+ request.onerror = function (event) {
2427
+ event.target;
2428
+ resolve({
2429
+ success: false,
2430
+ code: that.#errorCode.delete.code,
2431
+ msg: that.#errorCode.delete.msg,
2432
+ event: event,
2433
+ });
2434
+ };
2435
+ }
2436
+ }, dbName);
2437
+ });
2438
+ }
2439
+ /**
2440
+ * 删除所有数据
2441
+ */
2442
+ async deleteAll() {
2443
+ let that = this;
2444
+ return new Promise((resolve) => {
2445
+ /* 清空数据库 */
2446
+ let dbName = that.#dbName;
2447
+ that.open(function (idbStore, success) {
2448
+ if (!success) {
2449
+ resolve({
2450
+ success: false,
2451
+ code: that.#errorCode.deleteAll.code,
2452
+ msg: that.#errorCode.deleteAll.msg,
2453
+ });
2454
+ }
2455
+ else {
2456
+ idbStore = idbStore;
2457
+ idbStore.clear();
2458
+ resolve({
2459
+ success: true,
2460
+ code: that.#errorCode.success.code,
2461
+ msg: that.#errorCode.success.msg,
2462
+ });
2463
+ }
2464
+ }, dbName);
2465
+ });
2466
+ }
2467
+ }
2574
2468
 
2575
- const LockFunction = function (callback, context, delayTime = 0) {
2576
- let flag = false;
2577
- let that = this;
2578
- context = context || this;
2579
- /**
2580
- *
2581
- */
2582
- this.lock = function () {
2583
- flag = true;
2584
- };
2585
- /**
2586
- * 解锁
2587
- */
2588
- this.unlock = function () {
2589
- setTimeout(() => {
2590
- flag = false;
2591
- }, delayTime);
2592
- };
2593
- /**
2594
- * 执行
2595
- */
2596
- this.run = async function (...args) {
2597
- if (flag) {
2598
- return;
2599
- }
2600
- that.lock();
2601
- await callback.apply(context, args);
2602
- that.unlock();
2603
- };
2604
- };
2469
+ class LockFunction {
2470
+ #flag = false;
2471
+ #delayTime = 0;
2472
+ #callback;
2473
+ #context;
2474
+ constructor(callback, context, delayTime) {
2475
+ this.#callback = callback;
2476
+ if (typeof context === "number") {
2477
+ this.#delayTime = context;
2478
+ this.#context = utils;
2479
+ }
2480
+ else {
2481
+ this.#delayTime = delayTime;
2482
+ this.#context = context;
2483
+ }
2484
+ }
2485
+ /**
2486
+ * 判断是否被锁
2487
+ */
2488
+ isLock() {
2489
+ return this.#flag;
2490
+ }
2491
+ /**
2492
+ * 锁
2493
+ */
2494
+ lock() {
2495
+ this.#flag = true;
2496
+ }
2497
+ /**
2498
+ * 解锁
2499
+ */
2500
+ unlock() {
2501
+ setTimeout(() => {
2502
+ this.#flag = false;
2503
+ }, this.#delayTime);
2504
+ }
2505
+ /**
2506
+ * 执行
2507
+ */
2508
+ async run(...args) {
2509
+ if (this.isLock()) {
2510
+ return;
2511
+ }
2512
+ this.lock();
2513
+ await this.#callback.apply(this.#context, args);
2514
+ this.unlock();
2515
+ }
2516
+ }
2605
2517
 
2606
- const Log = function (
2607
- _GM_info_ = {
2608
- script: {
2609
- name: "Utils.Log",
2610
- },
2611
- },
2612
- console = globalThis.console
2613
- ) {
2614
- let msgColorDetails = [
2615
- "font-weight: bold; color: cornflowerblue",
2616
- "font-weight: bold; color: cornflowerblue",
2617
- "font-weight: bold; color: darkorange",
2618
- "font-weight: bold; color: cornflowerblue",
2619
- ];
2620
- /**
2621
- * @type {UtilsLogOptions}
2622
- */
2623
- let details = {
2624
- tag: true,
2625
- successColor: "#0000FF",
2626
- errorColor: "#FF0000",
2627
- infoColor: "0",
2628
- warnColor: "0",
2629
- debug: false,
2630
- autoClearConsole: false,
2631
- logMaxCount: 999,
2632
- };
2633
- let logCount = 0;
2634
- /**
2635
- * 解析Error的堆栈获取实际调用者的函数名及函数所在的位置
2636
- * @param {string[]} stack
2637
- * @returns {{
2638
- * name: string,
2639
- * position: string,
2640
- * }}
2641
- */
2642
- let parseErrorStack = function (stack) {
2643
- let result = {
2644
- name: "",
2645
- position: "",
2646
- };
2647
- for (let stackString of stack) {
2648
- stackString = stackString.trim();
2649
- let stackFunctionName = stackString.match(/^at[\s]+(.+?)[\s]+/i);
2650
- let stackFunctionNamePosition = stackString.match(
2651
- /^at[\s]+.+[\s]+\((.+?)\)/i
2652
- );
2653
- if (stackFunctionName == null) {
2654
- continue;
2655
- }
2656
- stackFunctionName = stackFunctionName[stackFunctionName.length - 1];
2657
- stackFunctionNamePosition =
2658
- stackFunctionNamePosition[stackFunctionNamePosition.length - 1];
2659
- if (
2660
- stackFunctionName === "" ||
2661
- stackFunctionName.match(
2662
- new RegExp(
2663
- "(^Utils.Log.|.<anonymous>$|^Function.each|^NodeList.forEach|^k.fn.init.each)",
2664
- "g"
2665
- )
2666
- )
2667
- ) {
2668
- continue;
2669
- } else {
2670
- result.name = stackFunctionName;
2671
- result.position = stackFunctionNamePosition;
2672
- break;
2673
- }
2674
- }
2675
- if (result.position === "") {
2676
- let lastStackString = stack[stack.length - 1].trim();
2677
- if (lastStackString.startsWith("at chrome-extension://")) {
2678
- let lastStackMatch = lastStackString.match(/^at[\s]+(.+)/);
2679
- if (lastStackMatch) {
2680
- result.position = lastStackMatch[lastStackMatch.length - 1];
2681
- }
2682
- }
2683
- }
2684
- if (result.position === "") {
2685
- result.position = stack[stack.length - 1].trim().replace(/^at[\s]*/g, "");
2686
- }
2687
- return result;
2688
- };
2689
- /**
2690
- * 待恢复的函数或对象
2691
- */
2692
- let recoveryList = [];
2693
- /**
2694
- * 检测清理控制台
2695
- * @this {Utils.Log}
2696
- */
2697
- let checkClearConsole = function () {
2698
- logCount++;
2699
- if (details.autoClearConsole && logCount > details.logMaxCount) {
2700
- console.clear();
2701
- logCount = 0;
2702
- }
2703
- };
2704
- /**
2705
- * 输出内容
2706
- * @param {any} msg 需要输出的内容
2707
- * @param {string} color 颜色
2708
- * @param {string|undefined} otherStyle 其它CSS
2709
- * @this {Utils.Log}
2710
- */
2711
- let printContent = function (msg, color, otherStyle) {
2712
- checkClearConsole.apply(this);
2713
- otherStyle = otherStyle || "";
2714
- let stackSplit = new Error().stack.split("\n");
2715
- stackSplit.splice(0, 2);
2716
- let { name: callerName, position: callerPosition } =
2717
- parseErrorStack(stackSplit);
2718
- let tagName = this.tag;
2719
- function consoleMsg(_msg_) {
2720
- if (typeof _msg_ === "string") {
2721
- console.log(
2722
- `%c[${tagName}%c-%c${callerName}%c]%c %s`,
2723
- ...msgColorDetails,
2724
- `color: ${color};${otherStyle}`,
2725
- _msg_
2726
- );
2727
- } else if (typeof _msg_ === "number") {
2728
- console.log(
2729
- `%c[${tagName}%c-%c${callerName}%c]%c %d`,
2730
- ...msgColorDetails,
2731
- `color: ${color};${otherStyle}`,
2732
- _msg_
2733
- );
2734
- } else if (typeof _msg_ === "object") {
2735
- console.log(
2736
- `%c[${tagName}%c-%c${callerName}%c]%c %o`,
2737
- ...msgColorDetails,
2738
- `color: ${color};${otherStyle}`,
2739
- _msg_
2740
- );
2741
- } else {
2742
- console.log(_msg_);
2743
- }
2744
- }
2745
- if (Array.isArray(msg)) {
2746
- msg.forEach((item) => {
2747
- consoleMsg(item);
2748
- });
2749
- } else {
2750
- consoleMsg(msg);
2751
- }
2752
- if (details.debug) {
2753
- /* 如果开启调试模式,输出堆栈位置 */
2754
- console.log(callerPosition);
2755
- }
2756
- };
2757
- /**
2758
- * 前面的TAG标志
2759
- */
2760
- this.tag = _GM_info_?.script?.name || "Utils.Log";
2761
- /**
2762
- * 控制台-普通输出
2763
- * @param {any} msg 需要输出的内容,如果想输出多个,修改成数组,且数组内的长度最大值为4个
2764
- * @param {string|undefined} color 输出的颜色
2765
- * @param {string|undefined} otherStyle 其它CSS
2766
- */
2767
- this.info = function (msg, color = details.infoColor, otherStyle) {
2768
- printContent.call(this, msg, color, otherStyle);
2769
- };
2770
- /**
2771
- * 控制台-警告输出
2772
- * @param {any} msg 需要输出的内容,如果想输出多个,修改成数组,且数组内的长度最大值为4个
2773
- * @param {string|undefined} color 输出的颜色
2774
- * @param {string|undefined} otherStyle 其它CSS
2775
- */
2776
- this.warn = function (
2777
- msg,
2778
- color = details.warnColor,
2779
- otherStyle = "background: #FEF6D5;padding: 4px 6px 4px 0px;"
2780
- ) {
2781
- printContent.call(this, msg, color, otherStyle);
2782
- };
2783
- /**
2784
- * 控制台-错误输出
2785
- * @param {any} msg 需要输出的内容,如果想输出多个,修改成数组,且数组内的长度最大值为4个
2786
- * @param {string|undefined} color 输出的颜色
2787
- * @param {string|undefined} otherStyle 其它CSS
2788
- */
2789
- this.error = function (msg, color = details.errorColor, otherStyle) {
2790
- printContent.call(this, msg, color, otherStyle);
2791
- };
2792
- /**
2793
- * 控制台-成功输出
2794
- * @param {any} msg 需要输出的内容,如果想输出多个,修改成数组,且数组内的长度最大值为4个
2795
- * @param {string|undefined} color 输出的颜色
2796
- * @param {string|undefined} otherStyle 其它CSS
2797
- */
2798
- this.success = function (msg, color = details.successColor, otherStyle) {
2799
- printContent.call(this, msg, color, otherStyle);
2800
- };
2801
- /**
2802
- * 控制台-输出表格
2803
- * @param {object[]} msg
2804
- * @param {string|undefined} color 输出的颜色
2805
- * @param {string|undefined} otherStyle 其它CSS
2806
- * @example
2807
- * log.table([{"名字":"example","值":"123"},{"名字":"example2","值":"345"}])
2808
- */
2809
- this.table = function (msg, color = details.infoColor, otherStyle = "") {
2810
- checkClearConsole.apply(this);
2811
- let stack = new Error().stack.split("\n");
2812
- stack.splice(0, 1);
2813
- let errorStackParse = parseErrorStack(stack);
2814
- let stackFunctionName = errorStackParse.name;
2815
- let stackFunctionNamePosition = errorStackParse.position;
2816
- let callerName = stackFunctionName;
2817
- console.log(
2818
- `%c[${this.tag}%c-%c${callerName}%c]%c`,
2819
- ...msgColorDetails,
2820
- `color: ${color};${otherStyle}`
2821
- );
2822
- console.table(msg);
2823
- if (details.debug) {
2824
- console.log(stackFunctionNamePosition);
2825
- }
2826
- };
2827
- /**
2828
- * 配置Log对象的颜色
2829
- * @param {UtilsLogOptions} paramDetails 配置信息
2830
- */
2831
- this.config = function (paramDetails) {
2832
- details = Object.assign(details, paramDetails);
2833
- };
2834
- /**
2835
- * 禁用输出
2836
- */
2837
- this.disable = function () {
2838
- let that = this;
2839
- Object.keys(this)
2840
- .filter((keyName) => Boolean(keyName.match(/info|error|success|table/)))
2841
- .forEach((keyName) => {
2842
- let value = {};
2843
- value[keyName] = that[keyName];
2844
- recoveryList = [...recoveryList, value];
2845
- that[keyName] = () => {};
2846
- });
2847
- };
2848
- /**
2849
- * 恢复输出
2850
- */
2851
- this.recovery = function () {
2852
- let that = this;
2853
- recoveryList.forEach((item) => {
2854
- let keyName = Object.keys(item);
2855
- that[keyName] = item[keyName];
2856
- });
2857
- recoveryList = [];
2858
- };
2859
- };
2518
+ class Log {
2519
+ /** 前面的TAG标志 */
2520
+ #disable = false;
2521
+ tag = "";
2522
+ #console = null;
2523
+ #logCount = 0;
2524
+ #details = {
2525
+ tag: true,
2526
+ successColor: "#0000FF",
2527
+ errorColor: "#FF0000",
2528
+ infoColor: "0",
2529
+ warnColor: "0",
2530
+ debug: false,
2531
+ autoClearConsole: false,
2532
+ logMaxCount: 999,
2533
+ };
2534
+ /**
2535
+ * 待恢复的函数或对象
2536
+ */
2537
+ #recoveryList = [];
2538
+ #msgColorDetails = [
2539
+ "font-weight: bold; color: cornflowerblue",
2540
+ "font-weight: bold; color: cornflowerblue",
2541
+ "font-weight: bold; color: darkorange",
2542
+ "font-weight: bold; color: cornflowerblue",
2543
+ ];
2544
+ /**
2545
+ * @param _GM_info_ 油猴管理器的API GM_info,或者是一个对象,如{"script":{name:"Utils.Log"}}
2546
+ * @param console 可指定console对象为unsafeWindow下的console或者是油猴window下的console
2547
+ */
2548
+ constructor(_GM_info_ = {
2549
+ script: {
2550
+ name: "Utils.Log",
2551
+ },
2552
+ }, console = global.console) {
2553
+ this.tag = _GM_info_.script.name;
2554
+ this.#console = console;
2555
+ }
2556
+ /**
2557
+ * 解析Error的堆栈获取实际调用者的函数名及函数所在的位置
2558
+ * @param stack
2559
+ */
2560
+ parseErrorStack(stack) {
2561
+ let result = {
2562
+ name: "",
2563
+ position: "",
2564
+ };
2565
+ for (let stackString of stack) {
2566
+ stackString = stackString.trim();
2567
+ let stackFunctionNameMatch = stackString.match(/^at[\s]+(.+?)[\s]+/i);
2568
+ let stackFunctionNamePositionMatch = stackString.match(/^at[\s]+.+[\s]+\((.+?)\)/i);
2569
+ if (stackFunctionNameMatch == null) {
2570
+ continue;
2571
+ }
2572
+ if (stackFunctionNamePositionMatch == null) {
2573
+ continue;
2574
+ }
2575
+ let stackFunctionName = stackFunctionNameMatch[stackFunctionNameMatch.length - 1];
2576
+ let stackFunctionNamePosition = stackFunctionNamePositionMatch[stackFunctionNamePositionMatch.length - 1];
2577
+ if (stackFunctionName === "" ||
2578
+ stackFunctionName.match(new RegExp("(^Utils.Log.|.<anonymous>$|^Function.each|^NodeList.forEach|^k.fn.init.each)", "g"))) {
2579
+ continue;
2580
+ }
2581
+ else {
2582
+ result.name = stackFunctionName;
2583
+ result.position = stackFunctionNamePosition;
2584
+ break;
2585
+ }
2586
+ }
2587
+ if (result.position === "") {
2588
+ let lastStackString = stack[stack.length - 1].trim();
2589
+ if (lastStackString.startsWith("at chrome-extension://")) {
2590
+ let lastStackMatch = lastStackString.match(/^at[\s]+(.+)/);
2591
+ if (lastStackMatch) {
2592
+ result.position = lastStackMatch[lastStackMatch.length - 1];
2593
+ }
2594
+ }
2595
+ }
2596
+ if (result.position === "") {
2597
+ result.position = stack[stack.length - 1].trim().replace(/^at[\s]*/g, "");
2598
+ }
2599
+ return result;
2600
+ }
2601
+ /**
2602
+ * 检测清理控制台
2603
+ */
2604
+ checkClearConsole() {
2605
+ this.#logCount++;
2606
+ if (this.#details.autoClearConsole &&
2607
+ this.#logCount > this.#details.logMaxCount) {
2608
+ this.#console.clear();
2609
+ this.#logCount = 0;
2610
+ }
2611
+ }
2612
+ /**
2613
+ * 输出内容
2614
+ * @param msg 需要输出的内容
2615
+ * @param color 颜色
2616
+ * @param otherStyle 其它CSS
2617
+ */
2618
+ printContent(msg, color, otherStyle) {
2619
+ this.checkClearConsole.apply(this);
2620
+ otherStyle = otherStyle || "";
2621
+ let stackSplit = new Error().stack.split("\n");
2622
+ stackSplit.splice(0, 2);
2623
+ let { name: callerName, position: callerPosition } = this.parseErrorStack(stackSplit);
2624
+ let tagName = this.tag;
2625
+ let that = this;
2626
+ function consoleMsg(_msg_) {
2627
+ if (typeof _msg_ === "string") {
2628
+ that.#console.log(`%c[${tagName}%c-%c${callerName}%c]%c %s`, ...that.#msgColorDetails, `color: ${color};${otherStyle}`, _msg_);
2629
+ }
2630
+ else if (typeof _msg_ === "number") {
2631
+ that.#console.log(`%c[${tagName}%c-%c${callerName}%c]%c %d`, ...that.#msgColorDetails, `color: ${color};${otherStyle}`, _msg_);
2632
+ }
2633
+ else if (typeof _msg_ === "object") {
2634
+ that.#console.log(`%c[${tagName}%c-%c${callerName}%c]%c %o`, ...that.#msgColorDetails, `color: ${color};${otherStyle}`, _msg_);
2635
+ }
2636
+ else {
2637
+ that.#console.log(_msg_);
2638
+ }
2639
+ }
2640
+ if (Array.isArray(msg)) {
2641
+ msg.forEach((item) => {
2642
+ consoleMsg(item);
2643
+ });
2644
+ }
2645
+ else {
2646
+ consoleMsg(msg);
2647
+ }
2648
+ if (this.#details.debug) {
2649
+ /* 如果开启调试模式,输出堆栈位置 */
2650
+ this.#console.log(callerPosition);
2651
+ }
2652
+ }
2653
+ /**
2654
+ * 控制台-普通输出
2655
+ * @param msg 需要输出的内容,如果想输出多个,修改成数组,且数组内的长度最大值为4个
2656
+ * @param color 输出的颜色
2657
+ * @param otherStyle 其它CSS
2658
+ */
2659
+ info(msg, color = this.#details.infoColor, otherStyle) {
2660
+ if (this.#disable)
2661
+ return;
2662
+ this.printContent.call(this, msg, color, otherStyle);
2663
+ }
2664
+ /**
2665
+ * 控制台-警告输出
2666
+ * @param msg 需要输出的内容,如果想输出多个,修改成数组,且数组内的长度最大值为4个
2667
+ * @param color 输出的颜色
2668
+ * @param otherStyle 其它CSS
2669
+ */
2670
+ warn(msg, color = this.#details.warnColor, otherStyle = "background: #FEF6D5;padding: 4px 6px 4px 0px;") {
2671
+ if (this.#disable)
2672
+ return;
2673
+ this.printContent.call(this, msg, color, otherStyle);
2674
+ }
2675
+ /**
2676
+ * 控制台-错误输出
2677
+ * @param msg 需要输出的内容,如果想输出多个,修改成数组,且数组内的长度最大值为4个
2678
+ * @param color 输出的颜色
2679
+ * @param otherStyle 其它CSS
2680
+ */
2681
+ error(msg, color = this.#details.errorColor, otherStyle) {
2682
+ if (this.#disable)
2683
+ return;
2684
+ this.printContent.call(this, msg, color, otherStyle);
2685
+ }
2686
+ /**
2687
+ * 控制台-成功输出
2688
+ * @param msg 需要输出的内容,如果想输出多个,修改成数组,且数组内的长度最大值为4个
2689
+ * @param color 输出的颜色
2690
+ * @param otherStyle 其它CSS
2691
+ */
2692
+ success(msg, color = this.#details.successColor, otherStyle) {
2693
+ if (this.#disable)
2694
+ return;
2695
+ this.printContent.call(this, msg, color, otherStyle);
2696
+ }
2697
+ /**
2698
+ * 控制台-输出表格
2699
+ * @param msg
2700
+ * @param color 输出的颜色
2701
+ * @param otherStyle 其它CSS
2702
+ * @example
2703
+ * log.table([{"名字":"example","值":"123"},{"名字":"example2","值":"345"}])
2704
+ */
2705
+ table(msg, color = this.#details.infoColor, otherStyle = "") {
2706
+ if (this.#disable)
2707
+ return;
2708
+ this.checkClearConsole.apply(this);
2709
+ let stack = new Error().stack.split("\n");
2710
+ stack.splice(0, 1);
2711
+ let errorStackParse = this.parseErrorStack(stack);
2712
+ let stackFunctionName = errorStackParse.name;
2713
+ let stackFunctionNamePosition = errorStackParse.position;
2714
+ let callerName = stackFunctionName;
2715
+ this.#console.log(`%c[${this.tag}%c-%c${callerName}%c]%c`, ...this.#msgColorDetails, `color: ${color};${otherStyle}`);
2716
+ this.#console.table(msg);
2717
+ if (this.#details.debug) {
2718
+ this.#console.log(stackFunctionNamePosition);
2719
+ }
2720
+ }
2721
+ /**
2722
+ * 配置Log对象的颜色
2723
+ * @param paramDetails 配置信息
2724
+ */
2725
+ config(paramDetails) {
2726
+ this.#details = Object.assign(this.#details, paramDetails);
2727
+ }
2728
+ /** 禁用输出 */
2729
+ disable() {
2730
+ this.#disable = true;
2731
+ }
2732
+ /** 恢复输出 */
2733
+ recovery() {
2734
+ this.#disable = false;
2735
+ }
2736
+ }
2860
2737
 
2861
- const Progress = function (paramConfig) {
2862
- this.config = {
2863
- /**
2864
- * canvas元素节点
2865
- * @type {HTMLCanvasElement}
2866
- */
2867
- canvasNode: null,
2868
- /**
2869
- * 绘制角度
2870
- */
2871
- deg: 95,
2872
- /**
2873
- * 进度
2874
- */
2875
- progress: 0,
2876
- /**
2877
- * 绘制的线宽度
2878
- */
2879
- lineWidth: 10,
2880
- /**
2881
- * 绘制的背景颜色
2882
- */
2883
- lineBgColor: "#1e637c",
2884
- /**
2885
- * 绘制的线的颜色
2886
- */
2887
- lineColor: "#25deff",
2888
- /**
2889
- * 绘制的字体颜色
2890
- */
2891
- textColor: "#000000",
2892
- /**
2893
- * 绘制的字体大小(px)
2894
- */
2895
- fontSize: 22,
2896
- /**
2897
- * 绘制的圆的半径
2898
- */
2899
- circleRadius: 50,
2900
- /**
2901
- * 控制绘制的函数
2902
- */
2903
- draw: () => {},
2904
- };
2905
- this.config = Utils.assign(this.config, paramConfig);
2906
- if (!(this.config.canvasNode instanceof HTMLCanvasElement)) {
2907
- throw new Error("Utils.Progress 参数 canvasNode 必须是 HTMLCanvasElement");
2908
- }
2909
- /* 获取画笔 */
2910
- let ctx = this.config.canvasNode.getContext("2d");
2911
- /* 元素宽度 */
2912
- let width = this.config.canvasNode.width;
2913
- /* 元素高度 */
2914
- let height = this.config.canvasNode.height;
2915
-
2916
- /* 清除锯齿 */
2917
- if (window.devicePixelRatio) {
2918
- this.config.canvasNode.style.width = width + "px";
2919
- this.config.canvasNode.style.height = height + "px";
2920
- this.config.canvasNode.height = height * window.devicePixelRatio;
2921
- this.config.canvasNode.width = width * window.devicePixelRatio;
2922
- ctx.scale(window.devicePixelRatio, window.devicePixelRatio);
2923
- }
2924
- /* 设置线宽 */
2925
- ctx.lineWidth = this.config.lineWidth;
2926
- /* 绘制 */
2927
- this.draw = function () {
2928
- let degActive = (this.config.progress * 360) / 100;
2929
- /* 清除画布 */
2930
- ctx.clearRect(0, 0, width, height);
2931
- /* 开始绘制底圆 */
2932
- ctx.beginPath();
2933
- ctx.arc(width / 2, height / 2, this.config.circleRadius, 1, 8);
2934
- ctx.strokeStyle = this.config.lineBgColor;
2935
- ctx.stroke();
2936
- /* 开始绘制动态圆 */
2937
- ctx.beginPath();
2938
- ctx.arc(
2939
- width / 2,
2940
- height / 2,
2941
- this.config.circleRadius,
2942
- -Math.PI / 2,
2943
- (degActive * Math.PI) / 180 - Math.PI / 2
2944
- );
2945
- ctx.strokeStyle = this.config.lineColor;
2946
- ctx.stroke();
2947
- /* 获取百分比 */
2948
- let txt = parseInt(this.config.progress) + "%";
2949
- ctx.font = this.config.fontSize + "px SimHei";
2950
- /* 获取文本宽度 */
2951
- let w = ctx.measureText(txt).width;
2952
- let h = this.config.fontSize / 2;
2953
- ctx.fillStyle = this.config.textColor;
2954
- ctx.fillText(txt, width / 2 - w / 2, height / 2 + h / 2);
2955
- }.bind(this);
2956
- };
2738
+ class Progress {
2739
+ #config = {
2740
+ /**
2741
+ * canvas元素节点
2742
+ */
2743
+ canvasNode: null,
2744
+ /**
2745
+ * 绘制角度
2746
+ */
2747
+ deg: 95,
2748
+ /**
2749
+ * 进度
2750
+ */
2751
+ progress: 0,
2752
+ /**
2753
+ * 绘制的线宽度
2754
+ */
2755
+ lineWidth: 10,
2756
+ /**
2757
+ * 绘制的背景颜色
2758
+ */
2759
+ lineBgColor: "#1e637c",
2760
+ /**
2761
+ * 绘制的线的颜色
2762
+ */
2763
+ lineColor: "#25deff",
2764
+ /**
2765
+ * 绘制的字体颜色
2766
+ */
2767
+ textColor: "#000000",
2768
+ /**
2769
+ * 绘制的字体大小(px)
2770
+ */
2771
+ fontSize: 22,
2772
+ /**
2773
+ * 绘制的圆的半径
2774
+ */
2775
+ circleRadius: 50,
2776
+ };
2777
+ #ctx = null;
2778
+ #width = null;
2779
+ #height = null;
2780
+ /**
2781
+ *
2782
+ * @param paramConfig 配置信息
2783
+ */
2784
+ constructor(paramConfig) {
2785
+ this.#config = utils.assign(this.#config, paramConfig);
2786
+ if (!(this.#config.canvasNode instanceof HTMLCanvasElement)) {
2787
+ throw new Error("Utils.Progress 参数 canvasNode 必须是 HTMLCanvasElement");
2788
+ }
2789
+ this.init();
2790
+ }
2791
+ /**
2792
+ * 初始化
2793
+ */
2794
+ init() {
2795
+ /* 获取画笔 */
2796
+ let ctx = this.#config.canvasNode.getContext("2d");
2797
+ if (ctx == null) {
2798
+ throw new Error("Utils.Progress 获取画笔失败");
2799
+ }
2800
+ this.#ctx = ctx;
2801
+ /* 元素宽度 */
2802
+ this.#width = this.#config.canvasNode.width;
2803
+ /* 元素高度 */
2804
+ this.#height = this.#config.canvasNode.height;
2805
+ /* 清除锯齿 */
2806
+ if (window.devicePixelRatio) {
2807
+ this.#config.canvasNode.style.width = this.#width + "px";
2808
+ this.#config.canvasNode.style.height = this.#height + "px";
2809
+ this.#config.canvasNode.height = this.#height * window.devicePixelRatio;
2810
+ this.#config.canvasNode.width = this.#width * window.devicePixelRatio;
2811
+ this.#ctx.scale(window.devicePixelRatio, window.devicePixelRatio);
2812
+ }
2813
+ /* 设置线宽 */
2814
+ this.#ctx.lineWidth = this.#config.lineWidth;
2815
+ }
2816
+ /**
2817
+ * 绘制
2818
+ */
2819
+ draw() {
2820
+ let degActive = (this.#config.progress * 360) / 100;
2821
+ /* 清除画布 */
2822
+ this.#ctx.clearRect(0, 0, this.#width, this.#height);
2823
+ /* 开始绘制底圆 */
2824
+ this.#ctx.beginPath();
2825
+ this.#ctx.arc(this.#width / 2, this.#height / 2, this.#config.circleRadius, 1, 8);
2826
+ this.#ctx.strokeStyle = this.#config.lineBgColor;
2827
+ this.#ctx.stroke();
2828
+ /* 开始绘制动态圆 */
2829
+ this.#ctx.beginPath();
2830
+ this.#ctx.arc(this.#width / 2, this.#height / 2, this.#config.circleRadius, -Math.PI / 2, (degActive * Math.PI) / 180 - Math.PI / 2);
2831
+ this.#ctx.strokeStyle = this.#config.lineColor;
2832
+ this.#ctx.stroke();
2833
+ /* 获取百分比 */
2834
+ let txt = parseInt(this.#config.progress.toString()) + "%";
2835
+ this.#ctx.font = this.#config.fontSize + "px SimHei";
2836
+ /* 获取文本宽度 */
2837
+ let w = this.#ctx.measureText(txt).width;
2838
+ let h = this.#config.fontSize / 2;
2839
+ this.#ctx.fillStyle = this.#config.textColor;
2840
+ this.#ctx.fillText(txt, this.#width / 2 - w / 2, this.#height / 2 + h / 2);
2841
+ }
2842
+ }
2957
2843
 
2958
- /**
2959
- *
2960
- * @param {...any} args
2961
- * @returns
2962
- */
2963
- const TryCatch = function (...args) {
2964
- /* 定义变量和函数 */
2965
- let callbackFunction = null;
2966
- let context = null;
2967
- let handleError = null;
2968
- let defaultDetails = {
2969
- log: true,
2970
- };
2971
- /**
2972
- * @function tryCatchObj
2973
- * @description 空函数,用于链式调用。
2974
- */
2975
- function tryCatchObj() {}
2976
-
2977
- /**
2978
- * 配置
2979
- * @param {{
2980
- * log: boolean
2981
- * }} paramDetails
2982
- */
2983
- tryCatchObj.config = function (paramDetails) {
2984
- defaultDetails = Utils.assign(defaultDetails, paramDetails);
2985
- return tryCatchObj;
2986
- };
2987
- /**
2988
- * 设置错误处理函数。
2989
- * @param {function|string} handler 错误处理函数,可以是 function 或者 string 类型。如果是 string 类型,则会被当做代码进行执行。
2990
- * @returns 返回 tryCatchObj 函数。
2991
- */
2992
- tryCatchObj.error = function (handler) {
2993
- handleError = handler;
2994
- return tryCatchObj;
2995
- };
2996
-
2997
- /**
2998
- * 执行传入的函数并捕获其可能抛出的错误,并通过传入的错误处理函数进行处理。
2999
- * @param {function|string} callback 待执行函数,可以是 function 或者 string 类型。如果是 string 类型,则会被当做代码进行执行。
3000
- * @param {object|null} [__context__] 待执行函数的作用域,用于apply指定
3001
- * @returns 如果函数有返回值,则返回该返回值;否则返回 tryCatchObj 函数以支持链式调用。
3002
- * @throws {Error} 如果传入参数不符合要求,则会抛出相应类型的错误。
3003
- */
3004
- tryCatchObj.run = function (callback, __context__) {
3005
- callbackFunction = callback;
3006
- context = __context__;
3007
- let result = executeTryCatch(callbackFunction, handleError, context);
3008
- return result !== void 0 ? result : tryCatchObj;
3009
- };
3010
-
3011
- /**
3012
- * 执行传入的函数并捕获其可能抛出的错误,并通过传入的错误处理函数进行处理。
3013
- * @param {function|string} callback - 待执行函数,可以是 function 或者 string 类型。如果是 string 类型,则会被当做代码进行执行。
3014
- * @param {function|string|null} handleErrorFunc - 错误处理函数,可以是 function 或者 string 类型。如果是 string 类型,则会被当做代码进行执行。
3015
- * @param {object|null} funcThis - 待执行函数的作用域,用于apply指定
3016
- * @returns {any|undefined} - 如果函数有返回值,则返回该返回值;否则返回 undefined。
3017
- */
3018
- function executeTryCatch(callback, handleErrorFunc, funcThis) {
3019
- let result = void 0;
3020
- try {
3021
- if (typeof callback === "string") {
3022
- (function () {
3023
- eval(callback);
3024
- }).apply(funcThis, args);
3025
- } else {
3026
- result = callback.apply(funcThis, args);
3027
- }
3028
- } catch (error) {
3029
- if (defaultDetails.log) {
3030
- console.log(
3031
- `%c ${callback?.name ? callback?.name : callback + "出现错误"} `,
3032
- "color: #f20000"
3033
- );
3034
- console.log(`%c 错误原因:${error}`, "color: #f20000");
3035
- console.trace(callback);
3036
- }
3037
- if (handleErrorFunc) {
3038
- if (typeof handleErrorFunc === "string") {
3039
- result = function () {
3040
- return eval(handleErrorFunc);
3041
- }.apply(funcThis, [...args, error]);
3042
- } else {
3043
- result = handleErrorFunc.apply(funcThis, [...args, error]);
3044
- }
3045
- }
3046
- }
3047
- return result;
3048
- }
3049
-
3050
- // 返回 tryCatchObj 函数
3051
- return tryCatchObj;
2844
+ const TryCatch = function (...args) {
2845
+ /* 定义变量和函数 */
2846
+ let callbackFunction = null;
2847
+ let context = null;
2848
+ let handleError = (error) => { };
2849
+ let defaultDetails = {
2850
+ log: true,
2851
+ };
2852
+ const TryCatchCore = {
2853
+ /**
2854
+ *
2855
+ * @param paramDetails 配置
2856
+ * @returns
2857
+ */
2858
+ config(paramDetails) {
2859
+ defaultDetails = utils.assign(defaultDetails, paramDetails);
2860
+ return TryCatchCore;
2861
+ },
2862
+ /**
2863
+ * 处理错误
2864
+ * @param handler
2865
+ */
2866
+ error(handler) {
2867
+ // @ts-ignore
2868
+ handleError = handler;
2869
+ return TryCatchCore;
2870
+ },
2871
+ /**
2872
+ * 执行传入的函数并捕获其可能抛出的错误,并通过传入的错误处理函数进行处理。
2873
+ * @param callback 待执行函数,可以是 function 或者 string 类型。如果是 string 类型,则会被当做代码进行执行。
2874
+ * @param __context__ 待执行函数的作用域,用于apply指定
2875
+ * @returns 如果函数有返回值,则返回该返回值;否则返回 tryCatchObj 函数以支持链式调用。
2876
+ * @throws {Error} 如果传入参数不符合要求,则会抛出相应类型的错误。
2877
+ */
2878
+ run(callback, __context__) {
2879
+ callbackFunction = callback;
2880
+ context = __context__ || this;
2881
+ let result = executeTryCatch(callbackFunction, handleError, context);
2882
+ // @ts-ignore
2883
+ return result !== void 0 ? result : TryCatchCore;
2884
+ },
2885
+ };
2886
+ /**
2887
+ * 执行传入的函数并捕获其可能抛出的错误,并通过传入的错误处理函数进行处理。
2888
+ * @param callback - 待执行函数,可以是 function 或者 string 类型。如果是 string 类型,则会被当做代码进行执行。
2889
+ * @param handleErrorFunc - 错误处理函数,可以是 function 或者 string 类型。如果是 string 类型,则会被当做代码进行执行。
2890
+ * @param funcThis - 待执行函数的作用域,用于apply指定
2891
+ * @returns 如果函数有返回值,则返回该返回值;否则返回 undefined。
2892
+ */
2893
+ function executeTryCatch(callback, handleErrorFunc, funcThis) {
2894
+ let result = void 0;
2895
+ try {
2896
+ if (typeof callback === "string") {
2897
+ (function () {
2898
+ eval(callback);
2899
+ }).apply(funcThis, args);
2900
+ }
2901
+ else {
2902
+ result = callback.apply(funcThis, args);
2903
+ }
2904
+ }
2905
+ catch (error) {
2906
+ if (defaultDetails.log) {
2907
+ callback = callback;
2908
+ console.log(`%c ${callback?.name ? callback?.name : callback + "出现错误"} `, "color: #f20000");
2909
+ console.log(`%c 错误原因:${error}`, "color: #f20000");
2910
+ console.trace(callback);
2911
+ }
2912
+ if (handleErrorFunc) {
2913
+ if (typeof handleErrorFunc === "string") {
2914
+ result = function () {
2915
+ return eval(handleErrorFunc);
2916
+ // @ts-ignore
2917
+ }.apply(funcThis, [...args, error]);
2918
+ }
2919
+ else {
2920
+ result = handleErrorFunc.apply(funcThis, [...args, error]);
2921
+ }
2922
+ }
2923
+ }
2924
+ return result;
2925
+ }
2926
+ return TryCatchCore;
3052
2927
  };
3053
2928
 
3054
- class UtilsDictionary {
3055
- items = {};
3056
- /**
3057
- * 检查是否有某一个键
3058
- * @param {string} key 键
3059
- * @returns {boolean}
3060
- */
3061
- has(key) {
3062
- return this.items.hasOwnProperty(key);
3063
- }
3064
- /**
3065
- * 检查已有的键中是否以xx开头
3066
- * @param {string} key 需要匹配的键
3067
- * @returns {boolean}
3068
- */
3069
- startsWith(key) {
3070
- let allKeys = this.keys();
3071
- for (const keyName of allKeys) {
3072
- if (keyName.startsWith(key)) {
3073
- return true;
3074
- }
3075
- }
3076
- return false;
3077
- }
3078
- /**
3079
- * 获取以xx开头的键的值
3080
- * @param {string} key 需要匹配的键
3081
- * @returns {any}
3082
- */
3083
- getStartsWith(key) {
3084
- let allKeys = this.keys();
3085
- for (const keyName of allKeys) {
3086
- if (keyName.startsWith(key)) {
3087
- return this.items[keyName];
3088
- }
3089
- }
3090
- }
3091
- /**
3092
- * 为字典添加某一个值
3093
- * @param {string} key 键
3094
- * @param {any} val 值,默认为""
3095
- */
3096
- set(key, val = "") {
3097
- if (key === void 0) {
3098
- throw new Error("Utils.Dictionary().set 参数 key 不能为空");
3099
- }
3100
- this.items[key] = val;
3101
- }
3102
- /**
3103
- * 删除某一个键
3104
- * @param {string} key 键
3105
- * @returns {boolean}
3106
- */
3107
- delete(key) {
3108
- if (this.has(key)) {
3109
- Reflect.deleteProperty(this.items, key);
3110
- return true;
3111
- }
3112
- return false;
3113
- }
3114
- /**
3115
- * 获取某个键的值
3116
- * @param {string} key 键
3117
- * @returns {any}
3118
- */
3119
- get(key) {
3120
- return this.has(key) ? this.items[key] : void 0;
3121
- }
3122
- /**
3123
- * 返回字典中的所有值
3124
- * @returns {any[]}
3125
- */
3126
- values() {
3127
- let resultList = [];
3128
- for (let prop in this.items) {
3129
- if (this.has(prop)) {
3130
- resultList.push(this.items[prop]);
3131
- }
3132
- }
3133
- return resultList;
3134
- }
3135
- /**
3136
- * 清空字典
3137
- */
3138
- clear() {
3139
- this.items = null;
3140
- this.items = {};
3141
- }
3142
- /**
3143
- * 获取字典的长度
3144
- * @returns {number}
3145
- */
3146
- size() {
3147
- return Object.keys(this.items).length;
3148
- }
3149
- /**
3150
- * 获取字典所有的键
3151
- */
3152
- keys() {
3153
- return Object.keys(this.items);
3154
- }
3155
- /**
3156
- * 返回字典本身
3157
- * @returns {object}
3158
- */
3159
- getItems() {
3160
- return this.items;
3161
- }
3162
- /**
3163
- * 合并另一个字典
3164
- * @param {object} data 需要合并的字典
3165
- */
3166
- concat(data) {
3167
- this.items = Utils.assign(this.items, data.getItems());
3168
- }
3169
- forEach(callbackfn) {
3170
- for (const key in this.items) {
3171
- callbackfn(this.get(key), key, this.items);
3172
- }
3173
- }
3174
- /**
3175
- * 获取字典的长度,同this.size
3176
- * @returns {number}
3177
- */
3178
- get length() {
3179
- return this.size();
3180
- }
3181
- /**
3182
- * 迭代器
3183
- */
3184
- get entries() {
3185
- let that = this;
3186
- return function* () {
3187
- let itemKeys = Object.keys(that.getItems());
3188
- for (const keyName of itemKeys) {
3189
- yield [keyName, that.get(keyName)];
3190
- }
3191
- };
3192
- }
3193
- /**
3194
- * 是否可遍历
3195
- */
3196
- get [Symbol.iterator]() {
3197
- let that = this;
3198
- return function () {
3199
- return that.entries();
3200
- };
3201
- }
3202
- /**
3203
- * .toString()和.toLocaleString()输出的字符串
3204
- */
3205
- get [Symbol.toStringTag]() {
3206
- return "UtilsDictionary";
3207
- }
2929
+ class UtilsDictionary {
2930
+ #items = {};
2931
+ constructor() { }
2932
+ /**
2933
+ * 检查是否有某一个键
2934
+ * @param key 键
2935
+ */
2936
+ has(key) {
2937
+ return this.#items.hasOwnProperty(key);
2938
+ }
2939
+ /**
2940
+ * 检查已有的键中是否以xx开头
2941
+ * @param key 需要匹配的键
2942
+ */
2943
+ startsWith(key) {
2944
+ let allKeys = this.keys();
2945
+ for (const keyName of allKeys) {
2946
+ if (keyName.startsWith(key)) {
2947
+ return true;
2948
+ }
2949
+ }
2950
+ return false;
2951
+ }
2952
+ /**
2953
+ * 获取以xx开头的键的值
2954
+ * @param key 需要匹配的键
2955
+ */
2956
+ getStartsWith(key) {
2957
+ let allKeys = this.keys();
2958
+ let result = null;
2959
+ for (const keyName of allKeys) {
2960
+ if (keyName.startsWith(key)) {
2961
+ result = this.#items[keyName];
2962
+ break;
2963
+ }
2964
+ }
2965
+ return result;
2966
+ }
2967
+ /**
2968
+ * 为字典添加某一个值
2969
+ * @param key
2970
+ * @param val 值,默认为""
2971
+ */
2972
+ set(key, val) {
2973
+ if (key === void 0) {
2974
+ throw new Error("Utils.Dictionary().set 参数 key 不能为空");
2975
+ }
2976
+ this.#items[key] = val;
2977
+ }
2978
+ /**
2979
+ * 删除某一个键
2980
+ * @param key 键
2981
+ */
2982
+ delete(key) {
2983
+ if (this.has(key)) {
2984
+ Reflect.deleteProperty(this.#items, key);
2985
+ return true;
2986
+ }
2987
+ return false;
2988
+ }
2989
+ /**
2990
+ * 获取某个键的值
2991
+ * @param key 键
2992
+ */
2993
+ get(key) {
2994
+ return this.has(key) ? this.getItems()[key] : void 0;
2995
+ }
2996
+ /**
2997
+ * 返回字典中的所有值
2998
+ */
2999
+ values() {
3000
+ let resultList = [];
3001
+ for (let prop in this.getItems()) {
3002
+ if (this.has(prop)) {
3003
+ resultList.push(this.getItems()[prop]);
3004
+ }
3005
+ }
3006
+ return resultList;
3007
+ }
3008
+ /**
3009
+ * 清空字典
3010
+ */
3011
+ clear() {
3012
+ this.#items = void 0;
3013
+ this.#items = {};
3014
+ }
3015
+ /**
3016
+ * 获取字典的长度
3017
+ */
3018
+ size() {
3019
+ return Object.keys(this.getItems()).length;
3020
+ }
3021
+ /**
3022
+ * 获取字典所有的键
3023
+ */
3024
+ keys() {
3025
+ return Object.keys(this.getItems());
3026
+ }
3027
+ /**
3028
+ * 返回字典本身
3029
+ */
3030
+ getItems() {
3031
+ return this.#items;
3032
+ }
3033
+ /**
3034
+ * 合并另一个字典
3035
+ * @param data 需要合并的字典
3036
+ */
3037
+ concat(data) {
3038
+ this.#items = utils.assign(this.#items, data.getItems());
3039
+ }
3040
+ forEach(callbackfn) {
3041
+ for (const key in this.getItems()) {
3042
+ callbackfn(this.get(key), key, this.getItems());
3043
+ }
3044
+ }
3045
+ /**
3046
+ * 获取字典的长度,同this.size
3047
+ */
3048
+ get length() {
3049
+ return this.size();
3050
+ }
3051
+ /**
3052
+ * 迭代器
3053
+ */
3054
+ get entries() {
3055
+ let that = this;
3056
+ return function* () {
3057
+ let itemKeys = Object.keys(that.getItems());
3058
+ for (const keyName of itemKeys) {
3059
+ yield [keyName, that.get(keyName)];
3060
+ }
3061
+ };
3062
+ }
3063
+ /**
3064
+ * 是否可遍历
3065
+ */
3066
+ get [Symbol.iterator]() {
3067
+ let that = this;
3068
+ return function () {
3069
+ return that.entries();
3070
+ };
3071
+ }
3208
3072
  }
3209
3073
 
3210
3074
  /// <reference path="./ajaxHooker/index.d.ts" />
3211
- /// <reference path="./Dictionary/index.d.ts" />
3212
- /// <reference path="./Hooks/index.d.ts" />
3213
- /// <reference path="./Httpx/index.d.ts" />
3214
- /// <reference path="./indexedDB/index.d.ts" />
3215
- /// <reference path="./LockFunction/index.d.ts" />
3216
- /// <reference path="./Log/index.d.ts" />
3217
- /// <reference path="./Progress/index.d.ts" />
3218
- /// <reference path="./tryCatch/index.d.ts" />
3219
- /// <reference path="./UtilsGMMenu/index.d.ts" />
3220
- let Utils$1 = class Utils {
3075
+ class Utils {
3221
3076
  /** 版本号 */
3222
3077
  version = "2024.5.25";
3223
3078
  addStyle(cssText) {
@@ -5631,7 +5486,6 @@ let Utils$1 = class Utils {
5631
5486
  }
5632
5487
  /**
5633
5488
  * 提供一个封装了 try-catch 的函数,可以执行传入的函数并捕获其可能抛出的错误,并通过传入的错误处理函数进行处理。
5634
- * @returns 返回一个对象,其中包含 error 和 run 两个方法。
5635
5489
  * @example
5636
5490
  * Utils.tryCatch().error().run(()=>{console.log(1)});
5637
5491
  * > 1
@@ -6037,8 +5891,8 @@ let Utils$1 = class Utils {
6037
5891
  });
6038
5892
  }
6039
5893
  }
6040
- };
6041
- let utils = new Utils$1();
5894
+ }
5895
+ let utils = new Utils();
6042
5896
 
6043
5897
  export { utils as default };
6044
5898
  //# sourceMappingURL=index.esm.js.map