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