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