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