@whitesev/utils 2.0.0 → 2.0.2
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.map +1 -1
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.esm.js.map +1 -1
- package/dist/index.iife.js.map +1 -1
- package/dist/index.system.js.map +1 -1
- package/dist/index.umd.js.map +1 -1
- package/package.json +5 -3
- package/src/AjaxHookerType.ts +155 -0
- package/src/ColorConversion.ts +130 -0
- package/src/Dictionary.ts +161 -0
- package/src/Event.ts +189 -0
- package/src/GBKEncoder.ts +121 -0
- package/src/Hooks.ts +87 -0
- package/src/Httpx.ts +2524 -0
- package/src/LockFunction.ts +67 -0
- package/src/Log.ts +285 -0
- package/src/Progress.ts +149 -0
- package/src/TryCatch.ts +107 -0
- package/src/Utils.ts +4937 -0
- package/src/UtilsCommon.ts +20 -0
- package/src/UtilsCore.ts +47 -0
- package/src/UtilsGMCookie.ts +262 -0
- package/src/UtilsGMMenu.ts +532 -0
- package/src/VueObject.ts +128 -0
- package/src/ajaxHooker/ajaxHooker.js +566 -0
- package/src/indexedDB.ts +427 -0
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { UtilsCore } from "./UtilsCore";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* 生成uuid
|
|
5
|
+
*/
|
|
6
|
+
export const GenerateUUID = function () {
|
|
7
|
+
if (typeof UtilsCore.globalThis?.crypto?.randomUUID === "function") {
|
|
8
|
+
return UtilsCore.globalThis.crypto.randomUUID();
|
|
9
|
+
} else {
|
|
10
|
+
return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(
|
|
11
|
+
/[xy]/g,
|
|
12
|
+
function (charStr) {
|
|
13
|
+
var randomValue = (Math.random() * 16) | 0,
|
|
14
|
+
randomCharValue =
|
|
15
|
+
charStr === "x" ? randomValue : (randomValue & 0x3) | 0x8;
|
|
16
|
+
return randomCharValue.toString(16);
|
|
17
|
+
}
|
|
18
|
+
);
|
|
19
|
+
}
|
|
20
|
+
};
|
package/src/UtilsCore.ts
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
export declare interface UtilsCoreOption {
|
|
2
|
+
document: Document;
|
|
3
|
+
window: Window & typeof globalThis;
|
|
4
|
+
globalThis: typeof globalThis | Window;
|
|
5
|
+
self: Window & typeof globalThis;
|
|
6
|
+
top: Window;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
const UtilsCoreDefaultEnv: UtilsCoreOption = {
|
|
10
|
+
document: document,
|
|
11
|
+
window: window,
|
|
12
|
+
globalThis: globalThis,
|
|
13
|
+
self: self,
|
|
14
|
+
top: top!,
|
|
15
|
+
};
|
|
16
|
+
const UtilsCoreEnv: UtilsCoreOption = {
|
|
17
|
+
document: document,
|
|
18
|
+
window: window,
|
|
19
|
+
globalThis: globalThis,
|
|
20
|
+
self: self,
|
|
21
|
+
top: top!,
|
|
22
|
+
};
|
|
23
|
+
const UtilsCore = {
|
|
24
|
+
init(option?: UtilsCoreOption) {
|
|
25
|
+
if (!option) {
|
|
26
|
+
option = Object.assign({}, UtilsCoreDefaultEnv);
|
|
27
|
+
}
|
|
28
|
+
Object.assign(UtilsCoreEnv, option);
|
|
29
|
+
},
|
|
30
|
+
get document() {
|
|
31
|
+
return UtilsCoreEnv.document;
|
|
32
|
+
},
|
|
33
|
+
get window() {
|
|
34
|
+
return UtilsCoreEnv.window;
|
|
35
|
+
},
|
|
36
|
+
get globalThis() {
|
|
37
|
+
return UtilsCoreEnv.globalThis;
|
|
38
|
+
},
|
|
39
|
+
get self() {
|
|
40
|
+
return UtilsCoreEnv.self;
|
|
41
|
+
},
|
|
42
|
+
get top() {
|
|
43
|
+
return UtilsCoreEnv.top;
|
|
44
|
+
},
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
export { UtilsCore };
|
|
@@ -0,0 +1,262 @@
|
|
|
1
|
+
import { Utils } from "./Utils";
|
|
2
|
+
import { UtilsCore } from "./UtilsCore";
|
|
3
|
+
|
|
4
|
+
declare interface UtilsGMCookieResult {
|
|
5
|
+
/** 为 window.location.hostname */
|
|
6
|
+
domain: string;
|
|
7
|
+
expirationDate: null;
|
|
8
|
+
hostOnly: true;
|
|
9
|
+
httpOnly: false;
|
|
10
|
+
name: string;
|
|
11
|
+
path: "/";
|
|
12
|
+
sameSite: "unspecified";
|
|
13
|
+
secure: true;
|
|
14
|
+
session: false;
|
|
15
|
+
value: string;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
declare interface UtilsGMCookieListOptions {
|
|
19
|
+
/** 默认为当前的url */
|
|
20
|
+
url: string;
|
|
21
|
+
/** 默认为当前的域名(window.location.hostname) */
|
|
22
|
+
domain: string;
|
|
23
|
+
/** 需要检索的Cookie的名字 */
|
|
24
|
+
name: string | RegExp;
|
|
25
|
+
/** 需要检索的Cookie的路径,默认为"/" */
|
|
26
|
+
path: string;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
declare interface UtilsGMCookieSetOptions {
|
|
30
|
+
/** 默认为当前的url */
|
|
31
|
+
url?: string;
|
|
32
|
+
/** 默认为当前的域名(window.location.hostname) */
|
|
33
|
+
domain?: string;
|
|
34
|
+
/** 需要检索的Cookie的名字 */
|
|
35
|
+
name?: string;
|
|
36
|
+
/** 需要检索的Cookie的路径,默认为"/" */
|
|
37
|
+
path?: string;
|
|
38
|
+
/** 值 */
|
|
39
|
+
value?: string | number;
|
|
40
|
+
/** 确保Cookie只在通过安全协议(如HTTPS)的情况下传输 */
|
|
41
|
+
secure?: boolean;
|
|
42
|
+
/** 是否防止JavaScript代码访问Cookie */
|
|
43
|
+
httpOnly?: boolean;
|
|
44
|
+
/** Cookie过期时间,默认为30天 */
|
|
45
|
+
expirationDate?: number;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
declare interface UtilsGMCookieDeleteOptions {
|
|
49
|
+
/** 默认为当前的url */
|
|
50
|
+
url: string;
|
|
51
|
+
/** 需要检索的Cookie的名字 */
|
|
52
|
+
name: string;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
class UtilsGMCookie {
|
|
56
|
+
/**
|
|
57
|
+
* 获取单个cookie
|
|
58
|
+
* @param cookieName cookie名
|
|
59
|
+
*/
|
|
60
|
+
get(cookieName: string) {
|
|
61
|
+
if (typeof cookieName !== "string") {
|
|
62
|
+
throw new TypeError("Utils.GMCookie.get 参数cookieName 必须为字符串");
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
let cookies = UtilsCore.document.cookie.split(";");
|
|
66
|
+
let findValue: UtilsGMCookieResult | undefined = void 0;
|
|
67
|
+
for (const cookieItem of cookies) {
|
|
68
|
+
let item = cookieItem.trim();
|
|
69
|
+
let itemSplit = item.split("=");
|
|
70
|
+
let itemName = itemSplit[0];
|
|
71
|
+
itemSplit.splice(0, 1);
|
|
72
|
+
let itemValue = decodeURIComponent(itemSplit.join(""));
|
|
73
|
+
if (itemName === cookieName) {
|
|
74
|
+
findValue = {
|
|
75
|
+
domain: UtilsCore.globalThis.location.hostname,
|
|
76
|
+
expirationDate: null,
|
|
77
|
+
hostOnly: true,
|
|
78
|
+
httpOnly: false,
|
|
79
|
+
name: cookieName,
|
|
80
|
+
path: "/",
|
|
81
|
+
sameSite: "unspecified",
|
|
82
|
+
secure: true,
|
|
83
|
+
session: false,
|
|
84
|
+
value: itemValue,
|
|
85
|
+
};
|
|
86
|
+
break;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
return findValue;
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* 获取多组Cookie
|
|
93
|
+
* @param paramDetails 配置
|
|
94
|
+
* @param callback 获取操作后的回调
|
|
95
|
+
* + cookies object[]
|
|
96
|
+
* + error string|undefined
|
|
97
|
+
**/
|
|
98
|
+
list(
|
|
99
|
+
paramDetails: Partial<UtilsGMCookieListOptions>,
|
|
100
|
+
callback?: (data: UtilsGMCookieResult[], error?: Error) => void
|
|
101
|
+
) {
|
|
102
|
+
if (paramDetails == null) {
|
|
103
|
+
throw new Error("Utils.GMCookie.list 参数不能为空");
|
|
104
|
+
}
|
|
105
|
+
let resultData: UtilsGMCookieResult[] = [];
|
|
106
|
+
try {
|
|
107
|
+
let details: Partial<UtilsGMCookieListOptions> = {
|
|
108
|
+
url: UtilsCore.globalThis.location.href,
|
|
109
|
+
domain: UtilsCore.globalThis.location.hostname,
|
|
110
|
+
name: "",
|
|
111
|
+
path: "/",
|
|
112
|
+
};
|
|
113
|
+
details = Utils.assign(details, paramDetails);
|
|
114
|
+
let cookies = UtilsCore.document.cookie.split(";");
|
|
115
|
+
cookies.forEach((item) => {
|
|
116
|
+
item = item.trim();
|
|
117
|
+
let itemSplit = item.split("=");
|
|
118
|
+
let itemName = itemSplit[0];
|
|
119
|
+
itemSplit.splice(0, 1);
|
|
120
|
+
let itemValue = decodeURIComponent(itemSplit.join(""));
|
|
121
|
+
let nameRegexp =
|
|
122
|
+
(details.name as RegExp) instanceof RegExp
|
|
123
|
+
? details.name
|
|
124
|
+
: new RegExp("^" + details.name, "g");
|
|
125
|
+
if (itemName.match(nameRegexp as RegExp)) {
|
|
126
|
+
resultData.push({
|
|
127
|
+
domain: UtilsCore.globalThis.location.hostname,
|
|
128
|
+
expirationDate: null,
|
|
129
|
+
hostOnly: true,
|
|
130
|
+
httpOnly: false,
|
|
131
|
+
name: itemName,
|
|
132
|
+
path: "/",
|
|
133
|
+
sameSite: "unspecified",
|
|
134
|
+
secure: true,
|
|
135
|
+
session: false,
|
|
136
|
+
value: itemValue,
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
});
|
|
140
|
+
if (typeof callback === "function") {
|
|
141
|
+
callback(resultData);
|
|
142
|
+
}
|
|
143
|
+
} catch (error) {
|
|
144
|
+
if (typeof callback === "function") {
|
|
145
|
+
callback(resultData, error as Error);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* 获取多组Cookie
|
|
151
|
+
* @param paramDetails 配置
|
|
152
|
+
**/
|
|
153
|
+
getList(
|
|
154
|
+
paramDetails: Partial<UtilsGMCookieListOptions>
|
|
155
|
+
): UtilsGMCookieResult[] {
|
|
156
|
+
if (paramDetails == null) {
|
|
157
|
+
throw new Error("Utils.GMCookie.list 参数不能为空");
|
|
158
|
+
}
|
|
159
|
+
let resultData: UtilsGMCookieResult[] = [];
|
|
160
|
+
let details: Partial<UtilsGMCookieListOptions> = {
|
|
161
|
+
url: UtilsCore.globalThis.location.href,
|
|
162
|
+
domain: UtilsCore.globalThis.location.hostname,
|
|
163
|
+
name: "",
|
|
164
|
+
path: "/",
|
|
165
|
+
};
|
|
166
|
+
details = Utils.assign(details, paramDetails);
|
|
167
|
+
let cookies = UtilsCore.document.cookie.split(";");
|
|
168
|
+
cookies.forEach((item) => {
|
|
169
|
+
item = item.trim();
|
|
170
|
+
let itemSplit = item.split("=");
|
|
171
|
+
let itemName = itemSplit[0];
|
|
172
|
+
itemSplit.splice(0, 1);
|
|
173
|
+
let itemValue = decodeURIComponent(itemSplit.join(""));
|
|
174
|
+
let nameRegexp =
|
|
175
|
+
(details.name as RegExp) instanceof RegExp
|
|
176
|
+
? details.name
|
|
177
|
+
: new RegExp("^" + details.name, "g");
|
|
178
|
+
if (itemName.match(nameRegexp as RegExp)) {
|
|
179
|
+
resultData.push({
|
|
180
|
+
domain: UtilsCore.globalThis.location.hostname,
|
|
181
|
+
expirationDate: null,
|
|
182
|
+
hostOnly: true,
|
|
183
|
+
httpOnly: false,
|
|
184
|
+
name: itemName,
|
|
185
|
+
path: "/",
|
|
186
|
+
sameSite: "unspecified",
|
|
187
|
+
secure: true,
|
|
188
|
+
session: false,
|
|
189
|
+
value: itemValue,
|
|
190
|
+
});
|
|
191
|
+
}
|
|
192
|
+
});
|
|
193
|
+
return resultData;
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* 设置Cookie
|
|
197
|
+
* @param paramDetails 配置
|
|
198
|
+
* @param callback 设置操作后的回调(成功/失败)
|
|
199
|
+
*/
|
|
200
|
+
set(
|
|
201
|
+
paramDetails: Partial<UtilsGMCookieSetOptions>,
|
|
202
|
+
callback = (error?: Error) => {}
|
|
203
|
+
) {
|
|
204
|
+
try {
|
|
205
|
+
let details: Partial<UtilsGMCookieSetOptions> = {
|
|
206
|
+
url: UtilsCore.window.location.href,
|
|
207
|
+
name: "",
|
|
208
|
+
value: "",
|
|
209
|
+
domain: UtilsCore.window.location.hostname,
|
|
210
|
+
path: "/",
|
|
211
|
+
secure: true,
|
|
212
|
+
httpOnly: false,
|
|
213
|
+
/**
|
|
214
|
+
* Expires in 30 days
|
|
215
|
+
*/
|
|
216
|
+
expirationDate: Math.floor(Date.now()) + 60 * 60 * 24 * 30,
|
|
217
|
+
};
|
|
218
|
+
details = Utils.assign(details, paramDetails);
|
|
219
|
+
let life = details.expirationDate
|
|
220
|
+
? details.expirationDate
|
|
221
|
+
: Math.floor(Date.now()) + 60 * 60 * 24 * 30;
|
|
222
|
+
let cookieStr =
|
|
223
|
+
details.name +
|
|
224
|
+
"=" +
|
|
225
|
+
decodeURIComponent(details.value as string) +
|
|
226
|
+
";expires=" +
|
|
227
|
+
(new Date(life) as any).toGMTString() +
|
|
228
|
+
"; path=/";
|
|
229
|
+
UtilsCore.document.cookie = cookieStr;
|
|
230
|
+
callback();
|
|
231
|
+
} catch (error: any) {
|
|
232
|
+
callback(error);
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
/**
|
|
236
|
+
* 删除Cookie
|
|
237
|
+
* @param paramDetails 配置
|
|
238
|
+
* @param callback 删除操作后的回调(成功/失败)
|
|
239
|
+
*/
|
|
240
|
+
delete(
|
|
241
|
+
paramDetails: Partial<UtilsGMCookieDeleteOptions>,
|
|
242
|
+
callback = (error?: Error) => {}
|
|
243
|
+
) {
|
|
244
|
+
try {
|
|
245
|
+
let details: Partial<UtilsGMCookieDeleteOptions> = {
|
|
246
|
+
url: UtilsCore.window.location.href,
|
|
247
|
+
name: "",
|
|
248
|
+
// @ts-ignore
|
|
249
|
+
firstPartyDomain: "",
|
|
250
|
+
};
|
|
251
|
+
details = Utils.assign(details, paramDetails);
|
|
252
|
+
let cookieStr =
|
|
253
|
+
details.name + "=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;";
|
|
254
|
+
UtilsCore.document.cookie = cookieStr;
|
|
255
|
+
callback();
|
|
256
|
+
} catch (error: any) {
|
|
257
|
+
callback(error);
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
export { UtilsGMCookie };
|