@whitesev/utils 1.0.5 → 1.0.8

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