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