@whitesev/utils 2.0.2 → 2.1.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.
@@ -1,5 +1,4 @@
1
1
  import { Utils } from "./Utils";
2
- import { UtilsCore } from "./UtilsCore";
3
2
 
4
3
  declare interface UtilsGMCookieResult {
5
4
  /** 为 window.location.hostname */
@@ -52,7 +51,21 @@ declare interface UtilsGMCookieDeleteOptions {
52
51
  name: string;
53
52
  }
54
53
 
54
+ interface WindowApiOption {
55
+ window: Window & typeof globalThis;
56
+ document: Document;
57
+ }
58
+
55
59
  class UtilsGMCookie {
60
+ private windowApi = {
61
+ window: window,
62
+ document: document,
63
+ };
64
+ constructor(windowApiOption?: WindowApiOption) {
65
+ if (windowApiOption) {
66
+ this.windowApi = Object.assign({}, windowApiOption);
67
+ }
68
+ }
56
69
  /**
57
70
  * 获取单个cookie
58
71
  * @param cookieName cookie名
@@ -62,7 +75,7 @@ class UtilsGMCookie {
62
75
  throw new TypeError("Utils.GMCookie.get 参数cookieName 必须为字符串");
63
76
  }
64
77
 
65
- let cookies = UtilsCore.document.cookie.split(";");
78
+ let cookies = this.windowApi.document.cookie.split(";");
66
79
  let findValue: UtilsGMCookieResult | undefined = void 0;
67
80
  for (const cookieItem of cookies) {
68
81
  let item = cookieItem.trim();
@@ -72,7 +85,7 @@ class UtilsGMCookie {
72
85
  let itemValue = decodeURIComponent(itemSplit.join(""));
73
86
  if (itemName === cookieName) {
74
87
  findValue = {
75
- domain: UtilsCore.globalThis.location.hostname,
88
+ domain: this.windowApi.window.location.hostname,
76
89
  expirationDate: null,
77
90
  hostOnly: true,
78
91
  httpOnly: false,
@@ -105,13 +118,13 @@ class UtilsGMCookie {
105
118
  let resultData: UtilsGMCookieResult[] = [];
106
119
  try {
107
120
  let details: Partial<UtilsGMCookieListOptions> = {
108
- url: UtilsCore.globalThis.location.href,
109
- domain: UtilsCore.globalThis.location.hostname,
121
+ url: this.windowApi.window.location.href,
122
+ domain: this.windowApi.window.location.hostname,
110
123
  name: "",
111
124
  path: "/",
112
125
  };
113
126
  details = Utils.assign(details, paramDetails);
114
- let cookies = UtilsCore.document.cookie.split(";");
127
+ let cookies = this.windowApi.document.cookie.split(";");
115
128
  cookies.forEach((item) => {
116
129
  item = item.trim();
117
130
  let itemSplit = item.split("=");
@@ -124,7 +137,7 @@ class UtilsGMCookie {
124
137
  : new RegExp("^" + details.name, "g");
125
138
  if (itemName.match(nameRegexp as RegExp)) {
126
139
  resultData.push({
127
- domain: UtilsCore.globalThis.location.hostname,
140
+ domain: this.windowApi.window.location.hostname,
128
141
  expirationDate: null,
129
142
  hostOnly: true,
130
143
  httpOnly: false,
@@ -158,13 +171,13 @@ class UtilsGMCookie {
158
171
  }
159
172
  let resultData: UtilsGMCookieResult[] = [];
160
173
  let details: Partial<UtilsGMCookieListOptions> = {
161
- url: UtilsCore.globalThis.location.href,
162
- domain: UtilsCore.globalThis.location.hostname,
174
+ url: this.windowApi.window.location.href,
175
+ domain: this.windowApi.window.location.hostname,
163
176
  name: "",
164
177
  path: "/",
165
178
  };
166
179
  details = Utils.assign(details, paramDetails);
167
- let cookies = UtilsCore.document.cookie.split(";");
180
+ let cookies = this.windowApi.document.cookie.split(";");
168
181
  cookies.forEach((item) => {
169
182
  item = item.trim();
170
183
  let itemSplit = item.split("=");
@@ -177,7 +190,7 @@ class UtilsGMCookie {
177
190
  : new RegExp("^" + details.name, "g");
178
191
  if (itemName.match(nameRegexp as RegExp)) {
179
192
  resultData.push({
180
- domain: UtilsCore.globalThis.location.hostname,
193
+ domain: this.windowApi.window.location.hostname,
181
194
  expirationDate: null,
182
195
  hostOnly: true,
183
196
  httpOnly: false,
@@ -203,10 +216,10 @@ class UtilsGMCookie {
203
216
  ) {
204
217
  try {
205
218
  let details: Partial<UtilsGMCookieSetOptions> = {
206
- url: UtilsCore.window.location.href,
219
+ url: this.windowApi.window.location.href,
207
220
  name: "",
208
221
  value: "",
209
- domain: UtilsCore.window.location.hostname,
222
+ domain: this.windowApi.window.location.hostname,
210
223
  path: "/",
211
224
  secure: true,
212
225
  httpOnly: false,
@@ -226,7 +239,7 @@ class UtilsGMCookie {
226
239
  ";expires=" +
227
240
  (new Date(life) as any).toGMTString() +
228
241
  "; path=/";
229
- UtilsCore.document.cookie = cookieStr;
242
+ this.windowApi.document.cookie = cookieStr;
230
243
  callback();
231
244
  } catch (error: any) {
232
245
  callback(error);
@@ -243,7 +256,7 @@ class UtilsGMCookie {
243
256
  ) {
244
257
  try {
245
258
  let details: Partial<UtilsGMCookieDeleteOptions> = {
246
- url: UtilsCore.window.location.href,
259
+ url: this.windowApi.window.location.href,
247
260
  name: "",
248
261
  // @ts-ignore
249
262
  firstPartyDomain: "",
@@ -251,7 +264,7 @@ class UtilsGMCookie {
251
264
  details = Utils.assign(details, paramDetails);
252
265
  let cookieStr =
253
266
  details.name + "=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;";
254
- UtilsCore.document.cookie = cookieStr;
267
+ this.windowApi.document.cookie = cookieStr;
255
268
  callback();
256
269
  } catch (error: any) {
257
270
  callback(error);
@@ -1,5 +1,4 @@
1
1
  import { Utils } from "./Utils";
2
- import { UtilsCore } from "./UtilsCore";
3
2
 
4
3
  declare interface UtilsGMMenuClickCallBackData {
5
4
  /** 菜单键名 */
@@ -284,7 +283,7 @@ class GMMenu {
284
283
  }
285
284
  /* 不刷新网页就刷新菜单 */
286
285
  if (menuOption.autoReload) {
287
- UtilsCore.window.location.reload();
286
+ window.location.reload();
288
287
  } else {
289
288
  that.context.update();
290
289
  }
@@ -0,0 +1,44 @@
1
+ /**
2
+ * 配置类型
3
+ */
4
+ export type UtilsWindowApiOption = {
5
+ document: Document;
6
+ window: Window & typeof globalThis;
7
+ globalThis: typeof globalThis | Window;
8
+ self: Window & typeof globalThis;
9
+ top: Window;
10
+ };
11
+
12
+ export class UtilsWindowApi {
13
+ /** 默认的配置 */
14
+ private defaultApi: UtilsWindowApiOption = {
15
+ document: document,
16
+ window: window,
17
+ globalThis: globalThis,
18
+ self: self,
19
+ top: top!,
20
+ };
21
+ /** 使用的配置 */
22
+ private api: UtilsWindowApiOption;
23
+ constructor(option?: UtilsWindowApiOption) {
24
+ if (!option) {
25
+ option = Object.assign({}, this.defaultApi);
26
+ }
27
+ this.api = Object.assign({}, option);
28
+ }
29
+ get document() {
30
+ return this.api.document;
31
+ }
32
+ get window() {
33
+ return this.api.window;
34
+ }
35
+ get globalThis() {
36
+ return this.api.globalThis;
37
+ }
38
+ get self() {
39
+ return this.api.self;
40
+ }
41
+ get top() {
42
+ return this.api.top;
43
+ }
44
+ }
package/src/indexedDB.ts CHANGED
@@ -1,5 +1,3 @@
1
- import { UtilsCore } from "./UtilsCore";
2
-
3
1
  declare interface UtilsIDBOpenErrorResult {
4
2
  code: number;
5
3
  msg: string;
@@ -14,10 +12,10 @@ class indexedDB {
14
12
  #slqVersion = "1";
15
13
  /* 监听IndexDB */
16
14
  #indexedDB =
17
- UtilsCore.window.indexedDB ||
18
- (UtilsCore.window as any).mozIndexedDB ||
19
- (UtilsCore.window as any).webkitIndexedDB ||
20
- (UtilsCore.window as any).msIndexedDB;
15
+ window.indexedDB ||
16
+ (window as any).mozIndexedDB ||
17
+ (window as any).webkitIndexedDB ||
18
+ (window as any).msIndexedDB;
21
19
  /* 缓存数据库,避免同一个页面重复创建和销毁 */
22
20
  #db: {
23
21
  [key: string]: IDBDatabase;
@@ -0,0 +1 @@
1
+ declare var GM_xmlhttpRequest: any;
@@ -1,16 +0,0 @@
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
- declare const UtilsCore: {
9
- init(option?: UtilsCoreOption): void;
10
- readonly document: Document;
11
- readonly window: Window & typeof globalThis;
12
- readonly globalThis: typeof globalThis | Window;
13
- readonly self: Window & typeof globalThis;
14
- readonly top: Window;
15
- };
16
- export { UtilsCore };
package/src/UtilsCore.ts DELETED
@@ -1,47 +0,0 @@
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 };