@whitesev/utils 2.9.12 → 2.10.0
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/README.md +176 -176
- package/dist/index.amd.js +206 -117
- package/dist/index.amd.js.map +1 -1
- package/dist/index.amd.min.js +1 -1
- package/dist/index.amd.min.js.map +1 -1
- package/dist/index.cjs.js +206 -117
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.cjs.min.js +1 -1
- package/dist/index.cjs.min.js.map +1 -1
- package/dist/index.esm.js +206 -117
- package/dist/index.esm.js.map +1 -1
- package/dist/index.esm.min.js +1 -1
- package/dist/index.esm.min.js.map +1 -1
- package/dist/index.iife.js +206 -117
- package/dist/index.iife.js.map +1 -1
- package/dist/index.iife.min.js +1 -1
- package/dist/index.iife.min.js.map +1 -1
- package/dist/index.system.js +206 -117
- package/dist/index.system.js.map +1 -1
- package/dist/index.system.min.js +1 -1
- package/dist/index.system.min.js.map +1 -1
- package/dist/index.umd.js +206 -117
- package/dist/index.umd.js.map +1 -1
- package/dist/index.umd.min.js +1 -1
- package/dist/index.umd.min.js.map +1 -1
- package/dist/types/src/Dictionary.d.ts +2 -0
- package/dist/types/src/Utils.d.ts +9 -2
- package/dist/types/src/types/Httpx.d.ts +1344 -1344
- package/dist/types/src/types/Log.d.ts +19 -19
- package/dist/types/src/types/Progress.d.ts +20 -20
- package/dist/types/src/types/React.d.ts +119 -119
- package/dist/types/src/types/TryCatch.d.ts +9 -9
- package/dist/types/src/types/UtilsGMCookie.d.ts +93 -93
- package/dist/types/src/types/UtilsGMMenu.d.ts +77 -77
- package/dist/types/src/types/Vue2.d.ts +166 -166
- package/dist/types/src/types/WindowApi.d.ts +14 -14
- package/dist/types/src/types/ajaxHooker.d.ts +155 -155
- package/dist/types/src/types/env.d.ts +7 -7
- package/dist/types/src/types/global.d.ts +31 -31
- package/package.json +26 -24
- package/src/ColorConversion.ts +118 -118
- package/src/CommonUtil.ts +301 -301
- package/src/DOMUtils.ts +251 -251
- package/src/Dictionary.ts +205 -199
- package/src/GBKEncoder.ts +108 -108
- package/src/Hooks.ts +73 -73
- package/src/Httpx.ts +1457 -1457
- package/src/LockFunction.ts +65 -62
- package/src/Log.ts +233 -233
- package/src/ModuleRaid.js +378 -360
- package/src/Progress.ts +108 -108
- package/src/TryCatch.ts +86 -86
- package/src/Utils.ts +3907 -3852
- package/src/UtilsCommon.ts +14 -14
- package/src/UtilsGMCookie.ts +273 -273
- package/src/UtilsGMMenu.ts +460 -460
- package/src/Vue.ts +233 -233
- package/src/WindowApi.ts +59 -59
- package/src/ajaxHooker/ajaxHooker.js +606 -538
- package/src/ajaxHooker/ajaxHooker1.2.4.js +440 -438
- package/src/indexedDB.ts +497 -497
- package/src/types/Httpx.d.ts +1344 -1344
- package/src/types/Log.d.ts +19 -19
- package/src/types/Progress.d.ts +20 -20
- package/src/types/React.d.ts +119 -119
- package/src/types/TryCatch.d.ts +9 -9
- package/src/types/UtilsGMCookie.d.ts +93 -93
- package/src/types/UtilsGMMenu.d.ts +77 -77
- package/src/types/Vue2.d.ts +166 -166
- package/src/types/WindowApi.d.ts +14 -14
- package/src/types/ajaxHooker.d.ts +155 -155
- package/src/types/env.d.ts +7 -7
- package/src/types/global.d.ts +31 -31
package/src/UtilsCommon.ts
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 生成uuid
|
|
3
|
-
*/
|
|
4
|
-
export const GenerateUUID = function () {
|
|
5
|
-
if (typeof window?.crypto?.randomUUID === "function") {
|
|
6
|
-
return window.crypto.randomUUID();
|
|
7
|
-
} else {
|
|
8
|
-
return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function (charStr) {
|
|
9
|
-
const randomValue = (Math.random() * 16) | 0,
|
|
10
|
-
randomCharValue = charStr === "x" ? randomValue : (randomValue & 0x3) | 0x8;
|
|
11
|
-
return randomCharValue.toString(16);
|
|
12
|
-
});
|
|
13
|
-
}
|
|
14
|
-
};
|
|
1
|
+
/**
|
|
2
|
+
* 生成uuid
|
|
3
|
+
*/
|
|
4
|
+
export const GenerateUUID = function () {
|
|
5
|
+
if (typeof window?.crypto?.randomUUID === "function") {
|
|
6
|
+
return window.crypto.randomUUID();
|
|
7
|
+
} else {
|
|
8
|
+
return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function (charStr) {
|
|
9
|
+
const randomValue = (Math.random() * 16) | 0,
|
|
10
|
+
randomCharValue = charStr === "x" ? randomValue : (randomValue & 0x3) | 0x8;
|
|
11
|
+
return randomCharValue.toString(16);
|
|
12
|
+
});
|
|
13
|
+
}
|
|
14
|
+
};
|
package/src/UtilsGMCookie.ts
CHANGED
|
@@ -1,273 +1,273 @@
|
|
|
1
|
-
import { CommonUtil } from "./CommonUtil";
|
|
2
|
-
import type {
|
|
3
|
-
UtilsGMCookieDeleteOptions,
|
|
4
|
-
UtilsGMCookieListOptions,
|
|
5
|
-
UtilsGMCookieResult,
|
|
6
|
-
UtilsGMCookieSetOptions,
|
|
7
|
-
WindowApiOption,
|
|
8
|
-
} from "./types/UtilsGMCookie";
|
|
9
|
-
|
|
10
|
-
export class UtilsGMCookie {
|
|
11
|
-
private windowApi = {
|
|
12
|
-
window: window,
|
|
13
|
-
document: document,
|
|
14
|
-
};
|
|
15
|
-
constructor(windowApiOption?: WindowApiOption) {
|
|
16
|
-
if (windowApiOption) {
|
|
17
|
-
this.windowApi = Object.assign({}, windowApiOption);
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
/**
|
|
21
|
-
* 获取Cookie分组
|
|
22
|
-
*/
|
|
23
|
-
private getCookiesList() {
|
|
24
|
-
if (this.windowApi.document.cookie.trim() === "") {
|
|
25
|
-
return [];
|
|
26
|
-
}
|
|
27
|
-
return this.windowApi.document.cookie.split(";");
|
|
28
|
-
}
|
|
29
|
-
/**
|
|
30
|
-
* 获取单个cookie
|
|
31
|
-
* @param cookieName cookie名
|
|
32
|
-
*/
|
|
33
|
-
get(cookieName: string) {
|
|
34
|
-
if (typeof cookieName !== "string") {
|
|
35
|
-
throw new TypeError("Utils.GMCookie.get 参数cookieName 必须为字符串");
|
|
36
|
-
}
|
|
37
|
-
const cookies = this.getCookiesList();
|
|
38
|
-
let findValue: UtilsGMCookieResult | undefined = void 0;
|
|
39
|
-
for (const cookieItem of cookies) {
|
|
40
|
-
const item = cookieItem.trim();
|
|
41
|
-
const itemSplit = item.split("=");
|
|
42
|
-
const itemName = itemSplit[0];
|
|
43
|
-
itemSplit.splice(0, 1);
|
|
44
|
-
const itemValue = decodeURIComponent(itemSplit.join(""));
|
|
45
|
-
if (itemName === cookieName) {
|
|
46
|
-
findValue = {
|
|
47
|
-
domain: this.windowApi.window.location.hostname,
|
|
48
|
-
expirationDate: null,
|
|
49
|
-
hostOnly: true,
|
|
50
|
-
httpOnly: false,
|
|
51
|
-
name: cookieName,
|
|
52
|
-
path: "/",
|
|
53
|
-
sameSite: "unspecified",
|
|
54
|
-
secure: true,
|
|
55
|
-
session: false,
|
|
56
|
-
value: itemValue,
|
|
57
|
-
};
|
|
58
|
-
break;
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
return findValue;
|
|
62
|
-
}
|
|
63
|
-
/**
|
|
64
|
-
* 获取多组Cookie
|
|
65
|
-
* @param option 配置
|
|
66
|
-
* @param callback 获取操作后的回调
|
|
67
|
-
* + cookies object[]
|
|
68
|
-
* + error string|undefined
|
|
69
|
-
**/
|
|
70
|
-
list(option: UtilsGMCookieListOptions | object): {
|
|
71
|
-
cookies: UtilsGMCookieResult[];
|
|
72
|
-
error?: Error;
|
|
73
|
-
};
|
|
74
|
-
list(
|
|
75
|
-
option: UtilsGMCookieListOptions | object,
|
|
76
|
-
callback?: (data: UtilsGMCookieResult[], error?: Error) => void
|
|
77
|
-
): void;
|
|
78
|
-
list(
|
|
79
|
-
option: UtilsGMCookieListOptions | object,
|
|
80
|
-
callback?: (data: UtilsGMCookieResult[], error?: Error) => void
|
|
81
|
-
): {
|
|
82
|
-
cookies: UtilsGMCookieResult[];
|
|
83
|
-
error?: Error;
|
|
84
|
-
} | void {
|
|
85
|
-
if (option == null) {
|
|
86
|
-
throw new Error("Utils.GMCookie.list 参数不能为空");
|
|
87
|
-
}
|
|
88
|
-
const resultData: UtilsGMCookieResult[] = [];
|
|
89
|
-
let error;
|
|
90
|
-
try {
|
|
91
|
-
let defaultOption: Required<UtilsGMCookieListOptions> = {
|
|
92
|
-
url: this.windowApi.window.location.href,
|
|
93
|
-
domain: this.windowApi.window.location.hostname,
|
|
94
|
-
name: "",
|
|
95
|
-
path: "/",
|
|
96
|
-
};
|
|
97
|
-
defaultOption = CommonUtil.assign(defaultOption, option);
|
|
98
|
-
const cookies = this.getCookiesList();
|
|
99
|
-
cookies.forEach((item) => {
|
|
100
|
-
item = item.trim();
|
|
101
|
-
const itemSplit = item.split("=");
|
|
102
|
-
const itemName = itemSplit[0];
|
|
103
|
-
itemSplit.splice(0, 1);
|
|
104
|
-
const itemValue = decodeURIComponent(itemSplit.join(""));
|
|
105
|
-
const nameRegexp =
|
|
106
|
-
(defaultOption.name as RegExp) instanceof RegExp
|
|
107
|
-
? defaultOption.name
|
|
108
|
-
: new RegExp(`^${defaultOption.name}`, "g");
|
|
109
|
-
if (itemName.match(nameRegexp as RegExp)) {
|
|
110
|
-
resultData.push({
|
|
111
|
-
domain: this.windowApi.window.location.hostname,
|
|
112
|
-
expirationDate: null,
|
|
113
|
-
hostOnly: true,
|
|
114
|
-
httpOnly: false,
|
|
115
|
-
name: itemName,
|
|
116
|
-
path: "/",
|
|
117
|
-
sameSite: "unspecified",
|
|
118
|
-
secure: true,
|
|
119
|
-
session: false,
|
|
120
|
-
value: itemValue,
|
|
121
|
-
});
|
|
122
|
-
}
|
|
123
|
-
});
|
|
124
|
-
} catch (e) {
|
|
125
|
-
error = e;
|
|
126
|
-
}
|
|
127
|
-
if (typeof callback === "function") {
|
|
128
|
-
callback(resultData, error as Error);
|
|
129
|
-
} else {
|
|
130
|
-
return {
|
|
131
|
-
cookies: resultData,
|
|
132
|
-
error: error as Error,
|
|
133
|
-
};
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
/**
|
|
137
|
-
* 获取多组Cookie
|
|
138
|
-
* @param option 配置
|
|
139
|
-
**/
|
|
140
|
-
getList(option: UtilsGMCookieListOptions | object): UtilsGMCookieResult[] {
|
|
141
|
-
if (option == null) {
|
|
142
|
-
throw new Error("Utils.GMCookie.list 参数不能为空");
|
|
143
|
-
}
|
|
144
|
-
const resultData: UtilsGMCookieResult[] = [];
|
|
145
|
-
let defaultOption: Required<UtilsGMCookieListOptions> = {
|
|
146
|
-
url: this.windowApi.window.location.href,
|
|
147
|
-
domain: this.windowApi.window.location.hostname,
|
|
148
|
-
name: "",
|
|
149
|
-
path: "/",
|
|
150
|
-
};
|
|
151
|
-
defaultOption = CommonUtil.assign(defaultOption, option);
|
|
152
|
-
const cookies = this.getCookiesList();
|
|
153
|
-
cookies.forEach((item) => {
|
|
154
|
-
item = item.trim();
|
|
155
|
-
const itemSplit = item.split("=");
|
|
156
|
-
const itemName = itemSplit[0];
|
|
157
|
-
itemSplit.splice(0, 1);
|
|
158
|
-
const itemValue = decodeURIComponent(itemSplit.join(""));
|
|
159
|
-
const nameRegexp =
|
|
160
|
-
(defaultOption.name as RegExp) instanceof RegExp
|
|
161
|
-
? defaultOption.name
|
|
162
|
-
: new RegExp(`^${defaultOption.name}`, "g");
|
|
163
|
-
if (itemName.match(nameRegexp as RegExp)) {
|
|
164
|
-
resultData.push({
|
|
165
|
-
domain: this.windowApi.window.location.hostname,
|
|
166
|
-
expirationDate: null,
|
|
167
|
-
hostOnly: true,
|
|
168
|
-
httpOnly: false,
|
|
169
|
-
name: itemName,
|
|
170
|
-
path: "/",
|
|
171
|
-
sameSite: "unspecified",
|
|
172
|
-
secure: true,
|
|
173
|
-
session: false,
|
|
174
|
-
value: itemValue,
|
|
175
|
-
});
|
|
176
|
-
}
|
|
177
|
-
});
|
|
178
|
-
return resultData;
|
|
179
|
-
}
|
|
180
|
-
/**
|
|
181
|
-
* 设置Cookie
|
|
182
|
-
* @param option 配置
|
|
183
|
-
* @param callback 设置操作后的回调(成功/失败)
|
|
184
|
-
*/
|
|
185
|
-
set(option: UtilsGMCookieSetOptions, callback?: (error?: Error) => void) {
|
|
186
|
-
let errorInfo;
|
|
187
|
-
try {
|
|
188
|
-
let defaultOption: Required<UtilsGMCookieSetOptions> = {
|
|
189
|
-
url: this.windowApi.window.location.href,
|
|
190
|
-
name: "",
|
|
191
|
-
value: "",
|
|
192
|
-
domain: "",
|
|
193
|
-
path: "/",
|
|
194
|
-
secure: true,
|
|
195
|
-
httpOnly: false,
|
|
196
|
-
/**
|
|
197
|
-
* Expires in 30 days
|
|
198
|
-
*/
|
|
199
|
-
expirationDate: Math.floor(Date.now()) + 60 * 60 * 24 * 30,
|
|
200
|
-
};
|
|
201
|
-
defaultOption = CommonUtil.assign(defaultOption, option);
|
|
202
|
-
const life = defaultOption.expirationDate
|
|
203
|
-
? defaultOption.expirationDate
|
|
204
|
-
: Math.floor(Date.now()) + 60 * 60 * 24 * 30;
|
|
205
|
-
let cookieStr = `${defaultOption.name}=${decodeURIComponent(defaultOption.value as string)};expires=${(
|
|
206
|
-
new Date(life) as any
|
|
207
|
-
).toGMTString()}; path=/`;
|
|
208
|
-
if (CommonUtil.isNull(defaultOption.domain)) {
|
|
209
|
-
cookieStr += `; domain=${defaultOption.domain}`;
|
|
210
|
-
}
|
|
211
|
-
this.windowApi.document.cookie = cookieStr;
|
|
212
|
-
} catch (error: any) {
|
|
213
|
-
errorInfo = error;
|
|
214
|
-
} finally {
|
|
215
|
-
if (typeof callback === "function") {
|
|
216
|
-
callback(errorInfo);
|
|
217
|
-
}
|
|
218
|
-
}
|
|
219
|
-
}
|
|
220
|
-
/**
|
|
221
|
-
* 删除Cookie
|
|
222
|
-
* @param option 配置
|
|
223
|
-
* @param callback 删除操作后的回调(成功/失败)
|
|
224
|
-
*/
|
|
225
|
-
delete(option: UtilsGMCookieDeleteOptions, callback?: (error?: Error) => void) {
|
|
226
|
-
let errorInfo;
|
|
227
|
-
try {
|
|
228
|
-
let defaultOption: Required<UtilsGMCookieDeleteOptions> = {
|
|
229
|
-
url: this.windowApi.window.location.href,
|
|
230
|
-
name: "",
|
|
231
|
-
path: "/",
|
|
232
|
-
firstPartyDomain: "",
|
|
233
|
-
};
|
|
234
|
-
defaultOption = CommonUtil.assign(defaultOption, option);
|
|
235
|
-
let cookieStr = `${defaultOption.name}=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=${defaultOption.path}`;
|
|
236
|
-
if (CommonUtil.isNull(defaultOption.firstPartyDomain)) {
|
|
237
|
-
cookieStr += `; domain=${defaultOption.firstPartyDomain};`;
|
|
238
|
-
}
|
|
239
|
-
this.windowApi.document.cookie = cookieStr;
|
|
240
|
-
} catch (error: any) {
|
|
241
|
-
errorInfo = error;
|
|
242
|
-
} finally {
|
|
243
|
-
if (typeof callback === "function") {
|
|
244
|
-
callback(errorInfo);
|
|
245
|
-
}
|
|
246
|
-
}
|
|
247
|
-
}
|
|
248
|
-
/**
|
|
249
|
-
* 解析cookie字符串,按`;`分割
|
|
250
|
-
*
|
|
251
|
-
* 例如:document.cookie
|
|
252
|
-
* @param cookieStr
|
|
253
|
-
*/
|
|
254
|
-
parseCookie(cookieStr: string) {
|
|
255
|
-
if (cookieStr.trim() === "") {
|
|
256
|
-
return [];
|
|
257
|
-
}
|
|
258
|
-
const cookies = cookieStr.split(";");
|
|
259
|
-
const result: { key: string; value: string }[] = [];
|
|
260
|
-
for (const cookieItem of cookies) {
|
|
261
|
-
const item = cookieItem.trim();
|
|
262
|
-
const itemSplit = item.split("=");
|
|
263
|
-
const itemName = itemSplit[0];
|
|
264
|
-
itemSplit.splice(0, 1);
|
|
265
|
-
const itemValue = decodeURIComponent(itemSplit.join(""));
|
|
266
|
-
result.push({
|
|
267
|
-
key: itemName,
|
|
268
|
-
value: itemValue,
|
|
269
|
-
});
|
|
270
|
-
}
|
|
271
|
-
return result;
|
|
272
|
-
}
|
|
273
|
-
}
|
|
1
|
+
import { CommonUtil } from "./CommonUtil";
|
|
2
|
+
import type {
|
|
3
|
+
UtilsGMCookieDeleteOptions,
|
|
4
|
+
UtilsGMCookieListOptions,
|
|
5
|
+
UtilsGMCookieResult,
|
|
6
|
+
UtilsGMCookieSetOptions,
|
|
7
|
+
WindowApiOption,
|
|
8
|
+
} from "./types/UtilsGMCookie";
|
|
9
|
+
|
|
10
|
+
export class UtilsGMCookie {
|
|
11
|
+
private windowApi = {
|
|
12
|
+
window: window,
|
|
13
|
+
document: document,
|
|
14
|
+
};
|
|
15
|
+
constructor(windowApiOption?: WindowApiOption) {
|
|
16
|
+
if (windowApiOption) {
|
|
17
|
+
this.windowApi = Object.assign({}, windowApiOption);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* 获取Cookie分组
|
|
22
|
+
*/
|
|
23
|
+
private getCookiesList() {
|
|
24
|
+
if (this.windowApi.document.cookie.trim() === "") {
|
|
25
|
+
return [];
|
|
26
|
+
}
|
|
27
|
+
return this.windowApi.document.cookie.split(";");
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* 获取单个cookie
|
|
31
|
+
* @param cookieName cookie名
|
|
32
|
+
*/
|
|
33
|
+
get(cookieName: string) {
|
|
34
|
+
if (typeof cookieName !== "string") {
|
|
35
|
+
throw new TypeError("Utils.GMCookie.get 参数cookieName 必须为字符串");
|
|
36
|
+
}
|
|
37
|
+
const cookies = this.getCookiesList();
|
|
38
|
+
let findValue: UtilsGMCookieResult | undefined = void 0;
|
|
39
|
+
for (const cookieItem of cookies) {
|
|
40
|
+
const item = cookieItem.trim();
|
|
41
|
+
const itemSplit = item.split("=");
|
|
42
|
+
const itemName = itemSplit[0];
|
|
43
|
+
itemSplit.splice(0, 1);
|
|
44
|
+
const itemValue = decodeURIComponent(itemSplit.join(""));
|
|
45
|
+
if (itemName === cookieName) {
|
|
46
|
+
findValue = {
|
|
47
|
+
domain: this.windowApi.window.location.hostname,
|
|
48
|
+
expirationDate: null,
|
|
49
|
+
hostOnly: true,
|
|
50
|
+
httpOnly: false,
|
|
51
|
+
name: cookieName,
|
|
52
|
+
path: "/",
|
|
53
|
+
sameSite: "unspecified",
|
|
54
|
+
secure: true,
|
|
55
|
+
session: false,
|
|
56
|
+
value: itemValue,
|
|
57
|
+
};
|
|
58
|
+
break;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
return findValue;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* 获取多组Cookie
|
|
65
|
+
* @param option 配置
|
|
66
|
+
* @param callback 获取操作后的回调
|
|
67
|
+
* + cookies object[]
|
|
68
|
+
* + error string|undefined
|
|
69
|
+
**/
|
|
70
|
+
list(option: UtilsGMCookieListOptions | object): {
|
|
71
|
+
cookies: UtilsGMCookieResult[];
|
|
72
|
+
error?: Error;
|
|
73
|
+
};
|
|
74
|
+
list(
|
|
75
|
+
option: UtilsGMCookieListOptions | object,
|
|
76
|
+
callback?: (data: UtilsGMCookieResult[], error?: Error) => void
|
|
77
|
+
): void;
|
|
78
|
+
list(
|
|
79
|
+
option: UtilsGMCookieListOptions | object,
|
|
80
|
+
callback?: (data: UtilsGMCookieResult[], error?: Error) => void
|
|
81
|
+
): {
|
|
82
|
+
cookies: UtilsGMCookieResult[];
|
|
83
|
+
error?: Error;
|
|
84
|
+
} | void {
|
|
85
|
+
if (option == null) {
|
|
86
|
+
throw new Error("Utils.GMCookie.list 参数不能为空");
|
|
87
|
+
}
|
|
88
|
+
const resultData: UtilsGMCookieResult[] = [];
|
|
89
|
+
let error;
|
|
90
|
+
try {
|
|
91
|
+
let defaultOption: Required<UtilsGMCookieListOptions> = {
|
|
92
|
+
url: this.windowApi.window.location.href,
|
|
93
|
+
domain: this.windowApi.window.location.hostname,
|
|
94
|
+
name: "",
|
|
95
|
+
path: "/",
|
|
96
|
+
};
|
|
97
|
+
defaultOption = CommonUtil.assign(defaultOption, option);
|
|
98
|
+
const cookies = this.getCookiesList();
|
|
99
|
+
cookies.forEach((item) => {
|
|
100
|
+
item = item.trim();
|
|
101
|
+
const itemSplit = item.split("=");
|
|
102
|
+
const itemName = itemSplit[0];
|
|
103
|
+
itemSplit.splice(0, 1);
|
|
104
|
+
const itemValue = decodeURIComponent(itemSplit.join(""));
|
|
105
|
+
const nameRegexp =
|
|
106
|
+
(defaultOption.name as RegExp) instanceof RegExp
|
|
107
|
+
? defaultOption.name
|
|
108
|
+
: new RegExp(`^${defaultOption.name}`, "g");
|
|
109
|
+
if (itemName.match(nameRegexp as RegExp)) {
|
|
110
|
+
resultData.push({
|
|
111
|
+
domain: this.windowApi.window.location.hostname,
|
|
112
|
+
expirationDate: null,
|
|
113
|
+
hostOnly: true,
|
|
114
|
+
httpOnly: false,
|
|
115
|
+
name: itemName,
|
|
116
|
+
path: "/",
|
|
117
|
+
sameSite: "unspecified",
|
|
118
|
+
secure: true,
|
|
119
|
+
session: false,
|
|
120
|
+
value: itemValue,
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
});
|
|
124
|
+
} catch (e) {
|
|
125
|
+
error = e;
|
|
126
|
+
}
|
|
127
|
+
if (typeof callback === "function") {
|
|
128
|
+
callback(resultData, error as Error);
|
|
129
|
+
} else {
|
|
130
|
+
return {
|
|
131
|
+
cookies: resultData,
|
|
132
|
+
error: error as Error,
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* 获取多组Cookie
|
|
138
|
+
* @param option 配置
|
|
139
|
+
**/
|
|
140
|
+
getList(option: UtilsGMCookieListOptions | object): UtilsGMCookieResult[] {
|
|
141
|
+
if (option == null) {
|
|
142
|
+
throw new Error("Utils.GMCookie.list 参数不能为空");
|
|
143
|
+
}
|
|
144
|
+
const resultData: UtilsGMCookieResult[] = [];
|
|
145
|
+
let defaultOption: Required<UtilsGMCookieListOptions> = {
|
|
146
|
+
url: this.windowApi.window.location.href,
|
|
147
|
+
domain: this.windowApi.window.location.hostname,
|
|
148
|
+
name: "",
|
|
149
|
+
path: "/",
|
|
150
|
+
};
|
|
151
|
+
defaultOption = CommonUtil.assign(defaultOption, option);
|
|
152
|
+
const cookies = this.getCookiesList();
|
|
153
|
+
cookies.forEach((item) => {
|
|
154
|
+
item = item.trim();
|
|
155
|
+
const itemSplit = item.split("=");
|
|
156
|
+
const itemName = itemSplit[0];
|
|
157
|
+
itemSplit.splice(0, 1);
|
|
158
|
+
const itemValue = decodeURIComponent(itemSplit.join(""));
|
|
159
|
+
const nameRegexp =
|
|
160
|
+
(defaultOption.name as RegExp) instanceof RegExp
|
|
161
|
+
? defaultOption.name
|
|
162
|
+
: new RegExp(`^${defaultOption.name}`, "g");
|
|
163
|
+
if (itemName.match(nameRegexp as RegExp)) {
|
|
164
|
+
resultData.push({
|
|
165
|
+
domain: this.windowApi.window.location.hostname,
|
|
166
|
+
expirationDate: null,
|
|
167
|
+
hostOnly: true,
|
|
168
|
+
httpOnly: false,
|
|
169
|
+
name: itemName,
|
|
170
|
+
path: "/",
|
|
171
|
+
sameSite: "unspecified",
|
|
172
|
+
secure: true,
|
|
173
|
+
session: false,
|
|
174
|
+
value: itemValue,
|
|
175
|
+
});
|
|
176
|
+
}
|
|
177
|
+
});
|
|
178
|
+
return resultData;
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* 设置Cookie
|
|
182
|
+
* @param option 配置
|
|
183
|
+
* @param callback 设置操作后的回调(成功/失败)
|
|
184
|
+
*/
|
|
185
|
+
set(option: UtilsGMCookieSetOptions, callback?: (error?: Error) => void) {
|
|
186
|
+
let errorInfo;
|
|
187
|
+
try {
|
|
188
|
+
let defaultOption: Required<UtilsGMCookieSetOptions> = {
|
|
189
|
+
url: this.windowApi.window.location.href,
|
|
190
|
+
name: "",
|
|
191
|
+
value: "",
|
|
192
|
+
domain: "",
|
|
193
|
+
path: "/",
|
|
194
|
+
secure: true,
|
|
195
|
+
httpOnly: false,
|
|
196
|
+
/**
|
|
197
|
+
* Expires in 30 days
|
|
198
|
+
*/
|
|
199
|
+
expirationDate: Math.floor(Date.now()) + 60 * 60 * 24 * 30,
|
|
200
|
+
};
|
|
201
|
+
defaultOption = CommonUtil.assign(defaultOption, option);
|
|
202
|
+
const life = defaultOption.expirationDate
|
|
203
|
+
? defaultOption.expirationDate
|
|
204
|
+
: Math.floor(Date.now()) + 60 * 60 * 24 * 30;
|
|
205
|
+
let cookieStr = `${defaultOption.name}=${decodeURIComponent(defaultOption.value as string)};expires=${(
|
|
206
|
+
new Date(life) as any
|
|
207
|
+
).toGMTString()}; path=/`;
|
|
208
|
+
if (CommonUtil.isNull(defaultOption.domain)) {
|
|
209
|
+
cookieStr += `; domain=${defaultOption.domain}`;
|
|
210
|
+
}
|
|
211
|
+
this.windowApi.document.cookie = cookieStr;
|
|
212
|
+
} catch (error: any) {
|
|
213
|
+
errorInfo = error;
|
|
214
|
+
} finally {
|
|
215
|
+
if (typeof callback === "function") {
|
|
216
|
+
callback(errorInfo);
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
/**
|
|
221
|
+
* 删除Cookie
|
|
222
|
+
* @param option 配置
|
|
223
|
+
* @param callback 删除操作后的回调(成功/失败)
|
|
224
|
+
*/
|
|
225
|
+
delete(option: UtilsGMCookieDeleteOptions, callback?: (error?: Error) => void) {
|
|
226
|
+
let errorInfo;
|
|
227
|
+
try {
|
|
228
|
+
let defaultOption: Required<UtilsGMCookieDeleteOptions> = {
|
|
229
|
+
url: this.windowApi.window.location.href,
|
|
230
|
+
name: "",
|
|
231
|
+
path: "/",
|
|
232
|
+
firstPartyDomain: "",
|
|
233
|
+
};
|
|
234
|
+
defaultOption = CommonUtil.assign(defaultOption, option);
|
|
235
|
+
let cookieStr = `${defaultOption.name}=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=${defaultOption.path}`;
|
|
236
|
+
if (CommonUtil.isNull(defaultOption.firstPartyDomain)) {
|
|
237
|
+
cookieStr += `; domain=${defaultOption.firstPartyDomain};`;
|
|
238
|
+
}
|
|
239
|
+
this.windowApi.document.cookie = cookieStr;
|
|
240
|
+
} catch (error: any) {
|
|
241
|
+
errorInfo = error;
|
|
242
|
+
} finally {
|
|
243
|
+
if (typeof callback === "function") {
|
|
244
|
+
callback(errorInfo);
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
/**
|
|
249
|
+
* 解析cookie字符串,按`;`分割
|
|
250
|
+
*
|
|
251
|
+
* 例如:document.cookie
|
|
252
|
+
* @param cookieStr
|
|
253
|
+
*/
|
|
254
|
+
parseCookie(cookieStr: string) {
|
|
255
|
+
if (cookieStr.trim() === "") {
|
|
256
|
+
return [];
|
|
257
|
+
}
|
|
258
|
+
const cookies = cookieStr.split(";");
|
|
259
|
+
const result: { key: string; value: string }[] = [];
|
|
260
|
+
for (const cookieItem of cookies) {
|
|
261
|
+
const item = cookieItem.trim();
|
|
262
|
+
const itemSplit = item.split("=");
|
|
263
|
+
const itemName = itemSplit[0];
|
|
264
|
+
itemSplit.splice(0, 1);
|
|
265
|
+
const itemValue = decodeURIComponent(itemSplit.join(""));
|
|
266
|
+
result.push({
|
|
267
|
+
key: itemName,
|
|
268
|
+
value: itemValue,
|
|
269
|
+
});
|
|
270
|
+
}
|
|
271
|
+
return result;
|
|
272
|
+
}
|
|
273
|
+
}
|