@whitesev/utils 2.8.0 → 2.8.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (70) hide show
  1. package/README.md +176 -176
  2. package/dist/index.amd.js +893 -874
  3. package/dist/index.amd.js.map +1 -1
  4. package/dist/index.cjs.js +893 -874
  5. package/dist/index.cjs.js.map +1 -1
  6. package/dist/index.esm.js +893 -874
  7. package/dist/index.esm.js.map +1 -1
  8. package/dist/index.iife.js +893 -874
  9. package/dist/index.iife.js.map +1 -1
  10. package/dist/index.system.js +893 -874
  11. package/dist/index.system.js.map +1 -1
  12. package/dist/index.umd.js +893 -874
  13. package/dist/index.umd.js.map +1 -1
  14. package/dist/types/src/CommonUtil.d.ts +59 -59
  15. package/dist/types/src/DOMUtils.d.ts +1 -1
  16. package/dist/types/src/Dictionary.d.ts +1 -1
  17. package/dist/types/src/Httpx.d.ts +2 -2
  18. package/dist/types/src/Progress.d.ts +0 -4
  19. package/dist/types/src/TryCatch.d.ts +2 -2
  20. package/dist/types/src/Utils.d.ts +365 -365
  21. package/dist/types/src/UtilsGMCookie.d.ts +2 -2
  22. package/dist/types/src/UtilsGMMenu.d.ts +1 -1
  23. package/dist/types/src/indexedDB.d.ts +3 -3
  24. package/dist/types/src/types/Event.d.ts +188 -188
  25. package/dist/types/src/types/Httpx.d.ts +1344 -1343
  26. package/dist/types/src/types/Log.d.ts +19 -19
  27. package/dist/types/src/types/Progress.d.ts +20 -20
  28. package/dist/types/src/types/React.d.ts +119 -119
  29. package/dist/types/src/types/TryCatch.d.ts +9 -9
  30. package/dist/types/src/types/UtilsGMCookie.d.ts +93 -93
  31. package/dist/types/src/types/UtilsGMMenu.d.ts +77 -77
  32. package/dist/types/src/types/Vue2.d.ts +166 -166
  33. package/dist/types/src/types/WindowApi.d.ts +14 -14
  34. package/dist/types/src/types/ajaxHooker.d.ts +151 -151
  35. package/dist/types/src/types/env.d.ts +7 -2
  36. package/dist/types/src/types/global.d.ts +31 -31
  37. package/package.json +16 -7
  38. package/src/ColorConversion.ts +105 -106
  39. package/src/CommonUtil.ts +280 -279
  40. package/src/DOMUtils.ts +251 -272
  41. package/src/Dictionary.ts +153 -154
  42. package/src/GBKEncoder.ts +108 -112
  43. package/src/Hooks.ts +73 -81
  44. package/src/Httpx.ts +1457 -1466
  45. package/src/LockFunction.ts +62 -62
  46. package/src/Log.ts +258 -259
  47. package/src/ModuleRaid.js +1 -0
  48. package/src/Progress.ts +108 -114
  49. package/src/TryCatch.ts +86 -86
  50. package/src/Utils.ts +4772 -4825
  51. package/src/UtilsCommon.ts +14 -14
  52. package/src/UtilsGMCookie.ts +254 -261
  53. package/src/UtilsGMMenu.ts +445 -454
  54. package/src/Vue.ts +233 -229
  55. package/src/WindowApi.ts +59 -59
  56. package/src/ajaxHooker/ajaxHooker.js +1 -0
  57. package/src/indexedDB.ts +497 -502
  58. package/src/types/Event.d.ts +188 -188
  59. package/src/types/Httpx.d.ts +1344 -1343
  60. package/src/types/Log.d.ts +19 -19
  61. package/src/types/Progress.d.ts +20 -20
  62. package/src/types/React.d.ts +119 -119
  63. package/src/types/TryCatch.d.ts +9 -9
  64. package/src/types/UtilsGMCookie.d.ts +93 -93
  65. package/src/types/UtilsGMMenu.d.ts +77 -77
  66. package/src/types/Vue2.d.ts +166 -166
  67. package/src/types/WindowApi.d.ts +14 -14
  68. package/src/types/ajaxHooker.d.ts +151 -151
  69. package/src/types/env.d.ts +7 -2
  70. package/src/types/global.d.ts +31 -31
@@ -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
- var 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
+ };
@@ -1,261 +1,254 @@
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
- let cookies = this.getCookiesList();
38
- let findValue: UtilsGMCookieResult | undefined = void 0;
39
- for (const cookieItem of cookies) {
40
- let item = cookieItem.trim();
41
- let itemSplit = item.split("=");
42
- let itemName = itemSplit[0];
43
- itemSplit.splice(0, 1);
44
- let 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(
71
- option: UtilsGMCookieListOptions | {},
72
- callback?: (data: UtilsGMCookieResult[], error?: Error) => void
73
- ) {
74
- if (option == null) {
75
- throw new Error("Utils.GMCookie.list 参数不能为空");
76
- }
77
- let resultData: UtilsGMCookieResult[] = [];
78
- try {
79
- let defaultOption: Required<UtilsGMCookieListOptions> = {
80
- url: this.windowApi.window.location.href,
81
- domain: this.windowApi.window.location.hostname,
82
- name: "",
83
- path: "/",
84
- };
85
- defaultOption = CommonUtil.assign(defaultOption, option);
86
- let cookies = this.getCookiesList();
87
- cookies.forEach((item) => {
88
- item = item.trim();
89
- let itemSplit = item.split("=");
90
- let itemName = itemSplit[0];
91
- itemSplit.splice(0, 1);
92
- let itemValue = decodeURIComponent(itemSplit.join(""));
93
- let nameRegexp =
94
- (defaultOption.name as RegExp) instanceof RegExp
95
- ? defaultOption.name
96
- : new RegExp("^" + defaultOption.name, "g");
97
- if (itemName.match(nameRegexp as RegExp)) {
98
- resultData.push({
99
- domain: this.windowApi.window.location.hostname,
100
- expirationDate: null,
101
- hostOnly: true,
102
- httpOnly: false,
103
- name: itemName,
104
- path: "/",
105
- sameSite: "unspecified",
106
- secure: true,
107
- session: false,
108
- value: itemValue,
109
- });
110
- }
111
- });
112
- if (typeof callback === "function") {
113
- callback(resultData);
114
- }
115
- } catch (error) {
116
- if (typeof callback === "function") {
117
- callback(resultData, error as Error);
118
- }
119
- }
120
- }
121
- /**
122
- * 获取多组Cookie
123
- * @param option 配置
124
- **/
125
- getList(option: UtilsGMCookieListOptions | {}): UtilsGMCookieResult[] {
126
- if (option == null) {
127
- throw new Error("Utils.GMCookie.list 参数不能为空");
128
- }
129
- let resultData: UtilsGMCookieResult[] = [];
130
- let defaultOption: Required<UtilsGMCookieListOptions> = {
131
- url: this.windowApi.window.location.href,
132
- domain: this.windowApi.window.location.hostname,
133
- name: "",
134
- path: "/",
135
- };
136
- defaultOption = CommonUtil.assign(defaultOption, option);
137
- let cookies = this.getCookiesList();
138
- cookies.forEach((item) => {
139
- item = item.trim();
140
- let itemSplit = item.split("=");
141
- let itemName = itemSplit[0];
142
- itemSplit.splice(0, 1);
143
- let itemValue = decodeURIComponent(itemSplit.join(""));
144
- let nameRegexp =
145
- (defaultOption.name as RegExp) instanceof RegExp
146
- ? defaultOption.name
147
- : new RegExp("^" + defaultOption.name, "g");
148
- if (itemName.match(nameRegexp as RegExp)) {
149
- resultData.push({
150
- domain: this.windowApi.window.location.hostname,
151
- expirationDate: null,
152
- hostOnly: true,
153
- httpOnly: false,
154
- name: itemName,
155
- path: "/",
156
- sameSite: "unspecified",
157
- secure: true,
158
- session: false,
159
- value: itemValue,
160
- });
161
- }
162
- });
163
- return resultData;
164
- }
165
- /**
166
- * 设置Cookie
167
- * @param option 配置
168
- * @param callback 设置操作后的回调(成功/失败)
169
- */
170
- set(option: UtilsGMCookieSetOptions, callback?: (error?: Error) => void) {
171
- let errorInfo;
172
- try {
173
- let defaultOption: Required<UtilsGMCookieSetOptions> = {
174
- url: this.windowApi.window.location.href,
175
- name: "",
176
- value: "",
177
- domain: "",
178
- path: "/",
179
- secure: true,
180
- httpOnly: false,
181
- /**
182
- * Expires in 30 days
183
- */
184
- expirationDate: Math.floor(Date.now()) + 60 * 60 * 24 * 30,
185
- };
186
- defaultOption = CommonUtil.assign(defaultOption, option);
187
- let life = defaultOption.expirationDate
188
- ? defaultOption.expirationDate
189
- : Math.floor(Date.now()) + 60 * 60 * 24 * 30;
190
- let cookieStr =
191
- defaultOption.name +
192
- "=" +
193
- decodeURIComponent(defaultOption.value as string) +
194
- ";expires=" +
195
- (new Date(life) as any).toGMTString() +
196
- "; path=/";
197
- if (CommonUtil.isNull(defaultOption.domain)) {
198
- cookieStr += "; domain=" + defaultOption.domain;
199
- }
200
- this.windowApi.document.cookie = cookieStr;
201
- } catch (error: any) {
202
- errorInfo = error;
203
- } finally {
204
- if (typeof callback === "function") {
205
- callback(errorInfo);
206
- }
207
- }
208
- }
209
- /**
210
- * 删除Cookie
211
- * @param option 配置
212
- * @param callback 删除操作后的回调(成功/失败)
213
- */
214
- delete(option: UtilsGMCookieDeleteOptions, callback?: (error?: Error) => void) {
215
- let errorInfo;
216
- try {
217
- let defaultOption: Required<UtilsGMCookieDeleteOptions> = {
218
- url: this.windowApi.window.location.href,
219
- name: "",
220
- path: "/",
221
- firstPartyDomain: "",
222
- };
223
- defaultOption = CommonUtil.assign(defaultOption, option);
224
- let cookieStr = `${defaultOption.name}=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=${defaultOption.path}`;
225
- if (CommonUtil.isNull(defaultOption.firstPartyDomain)) {
226
- cookieStr += `; domain=${defaultOption.firstPartyDomain};`;
227
- }
228
- this.windowApi.document.cookie = cookieStr;
229
- } catch (error: any) {
230
- errorInfo = error;
231
- } finally {
232
- if (typeof callback === "function") {
233
- callback(errorInfo);
234
- }
235
- }
236
- }
237
- /**
238
- * 解析cookie字符串
239
- * 例如:document.cookie
240
- * @param cookieStr
241
- */
242
- parseCookie(cookieStr: string) {
243
- if (cookieStr.trim() === "") {
244
- return [];
245
- }
246
- let cookies = cookieStr.split(";");
247
- let result: { key: string; value: string }[] = [];
248
- for (const cookieItem of cookies) {
249
- let item = cookieItem.trim();
250
- let itemSplit = item.split("=");
251
- let itemName = itemSplit[0];
252
- itemSplit.splice(0, 1);
253
- let itemValue = decodeURIComponent(itemSplit.join(""));
254
- result.push({
255
- key: itemName,
256
- value: itemValue,
257
- });
258
- }
259
- return result;
260
- }
261
- }
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, callback?: (data: UtilsGMCookieResult[], error?: Error) => void) {
71
+ if (option == null) {
72
+ throw new Error("Utils.GMCookie.list 参数不能为空");
73
+ }
74
+ const resultData: UtilsGMCookieResult[] = [];
75
+ try {
76
+ let defaultOption: Required<UtilsGMCookieListOptions> = {
77
+ url: this.windowApi.window.location.href,
78
+ domain: this.windowApi.window.location.hostname,
79
+ name: "",
80
+ path: "/",
81
+ };
82
+ defaultOption = CommonUtil.assign(defaultOption, option);
83
+ const cookies = this.getCookiesList();
84
+ cookies.forEach((item) => {
85
+ item = item.trim();
86
+ const itemSplit = item.split("=");
87
+ const itemName = itemSplit[0];
88
+ itemSplit.splice(0, 1);
89
+ const itemValue = decodeURIComponent(itemSplit.join(""));
90
+ const nameRegexp =
91
+ (defaultOption.name as RegExp) instanceof RegExp
92
+ ? defaultOption.name
93
+ : new RegExp(`^${defaultOption.name}`, "g");
94
+ if (itemName.match(nameRegexp as RegExp)) {
95
+ resultData.push({
96
+ domain: this.windowApi.window.location.hostname,
97
+ expirationDate: null,
98
+ hostOnly: true,
99
+ httpOnly: false,
100
+ name: itemName,
101
+ path: "/",
102
+ sameSite: "unspecified",
103
+ secure: true,
104
+ session: false,
105
+ value: itemValue,
106
+ });
107
+ }
108
+ });
109
+ if (typeof callback === "function") {
110
+ callback(resultData);
111
+ }
112
+ } catch (error) {
113
+ if (typeof callback === "function") {
114
+ callback(resultData, error as Error);
115
+ }
116
+ }
117
+ }
118
+ /**
119
+ * 获取多组Cookie
120
+ * @param option 配置
121
+ **/
122
+ getList(option: UtilsGMCookieListOptions | object): UtilsGMCookieResult[] {
123
+ if (option == null) {
124
+ throw new Error("Utils.GMCookie.list 参数不能为空");
125
+ }
126
+ const resultData: UtilsGMCookieResult[] = [];
127
+ let defaultOption: Required<UtilsGMCookieListOptions> = {
128
+ url: this.windowApi.window.location.href,
129
+ domain: this.windowApi.window.location.hostname,
130
+ name: "",
131
+ path: "/",
132
+ };
133
+ defaultOption = CommonUtil.assign(defaultOption, option);
134
+ const cookies = this.getCookiesList();
135
+ cookies.forEach((item) => {
136
+ item = item.trim();
137
+ const itemSplit = item.split("=");
138
+ const itemName = itemSplit[0];
139
+ itemSplit.splice(0, 1);
140
+ const itemValue = decodeURIComponent(itemSplit.join(""));
141
+ const nameRegexp =
142
+ (defaultOption.name as RegExp) instanceof RegExp
143
+ ? defaultOption.name
144
+ : new RegExp(`^${defaultOption.name}`, "g");
145
+ if (itemName.match(nameRegexp as RegExp)) {
146
+ resultData.push({
147
+ domain: this.windowApi.window.location.hostname,
148
+ expirationDate: null,
149
+ hostOnly: true,
150
+ httpOnly: false,
151
+ name: itemName,
152
+ path: "/",
153
+ sameSite: "unspecified",
154
+ secure: true,
155
+ session: false,
156
+ value: itemValue,
157
+ });
158
+ }
159
+ });
160
+ return resultData;
161
+ }
162
+ /**
163
+ * 设置Cookie
164
+ * @param option 配置
165
+ * @param callback 设置操作后的回调(成功/失败)
166
+ */
167
+ set(option: UtilsGMCookieSetOptions, callback?: (error?: Error) => void) {
168
+ let errorInfo;
169
+ try {
170
+ let defaultOption: Required<UtilsGMCookieSetOptions> = {
171
+ url: this.windowApi.window.location.href,
172
+ name: "",
173
+ value: "",
174
+ domain: "",
175
+ path: "/",
176
+ secure: true,
177
+ httpOnly: false,
178
+ /**
179
+ * Expires in 30 days
180
+ */
181
+ expirationDate: Math.floor(Date.now()) + 60 * 60 * 24 * 30,
182
+ };
183
+ defaultOption = CommonUtil.assign(defaultOption, option);
184
+ const life = defaultOption.expirationDate
185
+ ? defaultOption.expirationDate
186
+ : Math.floor(Date.now()) + 60 * 60 * 24 * 30;
187
+ let cookieStr = `${defaultOption.name}=${decodeURIComponent(defaultOption.value as string)};expires=${(
188
+ new Date(life) as any
189
+ ).toGMTString()}; path=/`;
190
+ if (CommonUtil.isNull(defaultOption.domain)) {
191
+ cookieStr += `; domain=${defaultOption.domain}`;
192
+ }
193
+ this.windowApi.document.cookie = cookieStr;
194
+ } catch (error: any) {
195
+ errorInfo = error;
196
+ } finally {
197
+ if (typeof callback === "function") {
198
+ callback(errorInfo);
199
+ }
200
+ }
201
+ }
202
+ /**
203
+ * 删除Cookie
204
+ * @param option 配置
205
+ * @param callback 删除操作后的回调(成功/失败)
206
+ */
207
+ delete(option: UtilsGMCookieDeleteOptions, callback?: (error?: Error) => void) {
208
+ let errorInfo;
209
+ try {
210
+ let defaultOption: Required<UtilsGMCookieDeleteOptions> = {
211
+ url: this.windowApi.window.location.href,
212
+ name: "",
213
+ path: "/",
214
+ firstPartyDomain: "",
215
+ };
216
+ defaultOption = CommonUtil.assign(defaultOption, option);
217
+ let cookieStr = `${defaultOption.name}=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=${defaultOption.path}`;
218
+ if (CommonUtil.isNull(defaultOption.firstPartyDomain)) {
219
+ cookieStr += `; domain=${defaultOption.firstPartyDomain};`;
220
+ }
221
+ this.windowApi.document.cookie = cookieStr;
222
+ } catch (error: any) {
223
+ errorInfo = error;
224
+ } finally {
225
+ if (typeof callback === "function") {
226
+ callback(errorInfo);
227
+ }
228
+ }
229
+ }
230
+ /**
231
+ * 解析cookie字符串
232
+ * 例如:document.cookie
233
+ * @param cookieStr
234
+ */
235
+ parseCookie(cookieStr: string) {
236
+ if (cookieStr.trim() === "") {
237
+ return [];
238
+ }
239
+ const cookies = cookieStr.split(";");
240
+ const result: { key: string; value: string }[] = [];
241
+ for (const cookieItem of cookies) {
242
+ const item = cookieItem.trim();
243
+ const itemSplit = item.split("=");
244
+ const itemName = itemSplit[0];
245
+ itemSplit.splice(0, 1);
246
+ const itemValue = decodeURIComponent(itemSplit.join(""));
247
+ result.push({
248
+ key: itemName,
249
+ value: itemValue,
250
+ });
251
+ }
252
+ return result;
253
+ }
254
+ }