@whitesev/utils 1.0.2 → 1.0.4

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 (59) hide show
  1. package/dist/index.amd.js +1814 -6002
  2. package/dist/index.amd.js.map +1 -1
  3. package/dist/index.cjs.js +1430 -5616
  4. package/dist/index.cjs.js.map +1 -1
  5. package/dist/index.d.ts +1 -1
  6. package/dist/index.esm.js +1427 -5611
  7. package/dist/index.esm.js.map +1 -1
  8. package/dist/index.iife.js +1814 -6003
  9. package/dist/index.iife.js.map +1 -1
  10. package/dist/index.system.js +1826 -6017
  11. package/dist/index.system.js.map +1 -1
  12. package/dist/index.umd.js +1814 -6006
  13. package/dist/index.umd.js.map +1 -1
  14. package/dist/src/UtilsCore.d.ts +7 -0
  15. package/dist/src/ajaxHooker/ajaxHooker.d.ts +6 -0
  16. package/dist/src/{Utils.d.ts → index.d.ts} +206 -1
  17. package/index.ts +1 -1
  18. package/package.json +37 -35
  19. package/rollup.config.js +14 -1
  20. package/src/GBKEncoder.ts +2 -0
  21. package/src/UtilsCore.ts +9 -0
  22. package/src/UtilsGMCookie.ts +1 -1
  23. package/src/ajaxHooker/ajaxHooker.js +574 -0
  24. package/src/{Utils.ts → index.ts} +267 -17
  25. package/tsconfig.json +1 -2
  26. package/dist/src/ajaxHooker.d.ts +0 -6
  27. package/src/GBKEncoder.js +0 -111
  28. package/src/ajaxHooker.js +0 -558
  29. package/src/types/DOMUtils.d.ts +0 -188
  30. package/src/types/UtilsCore.d.ts +0 -7
  31. package/src/types/global.d.ts +0 -58
  32. /package/dist/src/{Dictionary.d.ts → Dictionary/Dictionary.d.ts} +0 -0
  33. /package/dist/src/{Hooks.d.ts → Hooks/Hooks.d.ts} +0 -0
  34. /package/dist/src/{Httpx.d.ts → Httpx/Httpx.d.ts} +0 -0
  35. /package/dist/src/{LockFunction.d.ts → LockFunction/LockFunction.d.ts} +0 -0
  36. /package/dist/src/{Log.d.ts → Log/Log.d.ts} +0 -0
  37. /package/dist/src/{Progress.d.ts → Progress/Progress.d.ts} +0 -0
  38. /package/dist/src/{UtilsGMMenu.d.ts → UtilsGMMenu/UtilsGMMenu.d.ts} +0 -0
  39. /package/dist/src/{indexedDB.d.ts → indexedDB/indexedDB.d.ts} +0 -0
  40. /package/dist/src/{tryCatch.d.ts → tryCatch/tryCatch.d.ts} +0 -0
  41. /package/src/{Dictionary.js → Dictionary/Dictionary.js} +0 -0
  42. /package/src/{types/Dictionary.d.ts → Dictionary/index.d.ts} +0 -0
  43. /package/src/{Hooks.js → Hooks/Hooks.js} +0 -0
  44. /package/src/{types/Hook.d.ts → Hooks/index.d.ts} +0 -0
  45. /package/src/{Httpx.js → Httpx/Httpx.js} +0 -0
  46. /package/src/{types/Httpx.d.ts → Httpx/index.d.ts} +0 -0
  47. /package/src/{LockFunction.js → LockFunction/LockFunction.js} +0 -0
  48. /package/src/{types/LockFunction.d.ts → LockFunction/index.d.ts} +0 -0
  49. /package/src/{Log.js → Log/Log.js} +0 -0
  50. /package/src/{types/Log.d.ts → Log/index.d.ts} +0 -0
  51. /package/src/{Progress.js → Progress/Progress.js} +0 -0
  52. /package/src/{types/Progress.d.ts → Progress/index.d.ts} +0 -0
  53. /package/src/{UtilsGMMenu.js → UtilsGMMenu/UtilsGMMenu.js} +0 -0
  54. /package/src/{types/UtilsGMMenu.d.ts → UtilsGMMenu/index.d.ts} +0 -0
  55. /package/src/{types/AjaxHooker.d.ts → ajaxHooker/index.d.ts} +0 -0
  56. /package/src/{types/Indexdb.d.ts → indexedDB/index.d.ts} +0 -0
  57. /package/src/{indexedDB.js → indexedDB/indexedDB.js} +0 -0
  58. /package/src/{types/TryCatch.d.ts → tryCatch/index.d.ts} +0 -0
  59. /package/src/{tryCatch.js → tryCatch/tryCatch.js} +0 -0
package/src/ajaxHooker.js DELETED
@@ -1,558 +0,0 @@
1
- // @name ajaxHooker
2
- // @author cxxjackie
3
- // @version 1.4.1
4
- // @supportURL https://bbs.tampermonkey.net.cn/thread-3284-1-1.html
5
-
6
- var ajaxHooker = (function () {
7
- "use strict";
8
- const version = "1.4.1";
9
- const hookInst = {
10
- hookFns: [],
11
- filters: [],
12
- };
13
- const win = window.unsafeWindow || document.defaultView || window;
14
- let winAh = win.__ajaxHooker;
15
- const resProto = win.Response.prototype;
16
- const xhrResponses = ["response", "responseText", "responseXML"];
17
- const fetchResponses = ["arrayBuffer", "blob", "formData", "json", "text"];
18
- const fetchInitProps = [
19
- "method",
20
- "headers",
21
- "body",
22
- "mode",
23
- "credentials",
24
- "cache",
25
- "redirect",
26
- "referrer",
27
- "referrerPolicy",
28
- "integrity",
29
- "keepalive",
30
- "signal",
31
- "priority",
32
- ];
33
- const xhrAsyncEvents = ["readystatechange", "load", "loadend"];
34
- const getType = {}.toString.call.bind({}.toString);
35
- const getDescriptor = Object.getOwnPropertyDescriptor.bind(Object);
36
- const emptyFn = () => {};
37
- const errorFn = (e) => console.error(e);
38
- function isThenable(obj) {
39
- return (
40
- obj &&
41
- ["object", "function"].includes(typeof obj) &&
42
- typeof obj.then === "function"
43
- );
44
- }
45
- function catchError(fn, ...args) {
46
- try {
47
- const result = fn(...args);
48
- if (isThenable(result)) return result.then(null, errorFn);
49
- return result;
50
- } catch (err) {
51
- console.error(err);
52
- }
53
- }
54
- function defineProp(obj, prop, getter, setter) {
55
- Object.defineProperty(obj, prop, {
56
- configurable: true,
57
- enumerable: true,
58
- get: getter,
59
- set: setter,
60
- });
61
- }
62
- function readonly(obj, prop, value = obj[prop]) {
63
- defineProp(obj, prop, () => value, emptyFn);
64
- }
65
- function writable(obj, prop, value = obj[prop]) {
66
- Object.defineProperty(obj, prop, {
67
- configurable: true,
68
- enumerable: true,
69
- writable: true,
70
- value: value,
71
- });
72
- }
73
- function parseHeaders(obj) {
74
- const headers = {};
75
- switch (getType(obj)) {
76
- case "[object String]":
77
- for (const line of obj.trim().split(/[\r\n]+/)) {
78
- const [header, value] = line.split(/\s*:\s*/);
79
- if (!header) break;
80
- const lheader = header.toLowerCase();
81
- headers[lheader] =
82
- lheader in headers ? `${headers[lheader]}, ${value}` : value;
83
- }
84
- break;
85
- case "[object Headers]":
86
- for (const [key, val] of obj) {
87
- headers[key] = val;
88
- }
89
- break;
90
- case "[object Object]":
91
- return { ...obj };
92
- }
93
- return headers;
94
- }
95
- function stopImmediatePropagation() {
96
- this.ajaxHooker_isStopped = true;
97
- }
98
- class SyncThenable {
99
- then(fn) {
100
- fn && fn();
101
- return new SyncThenable();
102
- }
103
- }
104
- class AHRequest {
105
- constructor(request) {
106
- this.request = request;
107
- this.requestClone = { ...this.request };
108
- }
109
- shouldFilter(filters) {
110
- const { type, url, method, async } = this.request;
111
- return (
112
- filters.length &&
113
- !filters.find((obj) => {
114
- switch (true) {
115
- case obj.type && obj.type !== type:
116
- case getType(obj.url) === "[object String]" &&
117
- !url.includes(obj.url):
118
- case getType(obj.url) === "[object RegExp]" && !obj.url.test(url):
119
- case obj.method &&
120
- obj.method.toUpperCase() !== method.toUpperCase():
121
- case "async" in obj && obj.async !== async:
122
- return false;
123
- }
124
- return true;
125
- })
126
- );
127
- }
128
- waitForRequestKeys() {
129
- const requestKeys = ["url", "method", "abort", "headers", "data"];
130
- if (!this.request.async) {
131
- win.__ajaxHooker.hookInsts.forEach(({ hookFns, filters }) => {
132
- if (this.shouldFilter(filters)) return;
133
- hookFns.forEach((fn) => {
134
- if (getType(fn) === "[object Function]")
135
- catchError(fn, this.request);
136
- });
137
- requestKeys.forEach((key) => {
138
- if (isThenable(this.request[key]))
139
- this.request[key] = this.requestClone[key];
140
- });
141
- });
142
- return new SyncThenable();
143
- }
144
- const promises = [];
145
- win.__ajaxHooker.hookInsts.forEach(({ hookFns, filters }) => {
146
- if (this.shouldFilter(filters)) return;
147
- promises.push(
148
- Promise.all(hookFns.map((fn) => catchError(fn, this.request))).then(
149
- () =>
150
- Promise.all(
151
- requestKeys.map((key) =>
152
- Promise.resolve(this.request[key]).then(
153
- (val) => (this.request[key] = val),
154
- () => (this.request[key] = this.requestClone[key])
155
- )
156
- )
157
- )
158
- )
159
- );
160
- });
161
- return Promise.all(promises);
162
- }
163
- waitForResponseKeys(response) {
164
- const responseKeys =
165
- this.request.type === "xhr" ? xhrResponses : fetchResponses;
166
- if (!this.request.async) {
167
- if (getType(this.request.response) === "[object Function]") {
168
- catchError(this.request.response, response);
169
- responseKeys.forEach((key) => {
170
- if (
171
- "get" in getDescriptor(response, key) ||
172
- isThenable(response[key])
173
- ) {
174
- delete response[key];
175
- }
176
- });
177
- }
178
- return new SyncThenable();
179
- }
180
- return Promise.resolve(catchError(this.request.response, response)).then(
181
- () =>
182
- Promise.all(
183
- responseKeys.map((key) => {
184
- const descriptor = getDescriptor(response, key);
185
- if (descriptor && "value" in descriptor) {
186
- return Promise.resolve(descriptor.value).then(
187
- (val) => (response[key] = val),
188
- () => delete response[key]
189
- );
190
- } else {
191
- delete response[key];
192
- }
193
- })
194
- )
195
- );
196
- }
197
- }
198
- const proxyHandler = {
199
- get(target, prop) {
200
- const descriptor = getDescriptor(target, prop);
201
- if (
202
- descriptor &&
203
- !descriptor.configurable &&
204
- !descriptor.writable &&
205
- !descriptor.get
206
- )
207
- return target[prop];
208
- const ah = target.__ajaxHooker;
209
- if (ah && ah.proxyProps) {
210
- if (prop in ah.proxyProps) {
211
- const pDescriptor = ah.proxyProps[prop];
212
- if ("get" in pDescriptor) return pDescriptor.get();
213
- if (typeof pDescriptor.value === "function")
214
- return pDescriptor.value.bind(ah);
215
- return pDescriptor.value;
216
- }
217
- if (typeof target[prop] === "function")
218
- return target[prop].bind(target);
219
- }
220
- return target[prop];
221
- },
222
- set(target, prop, value) {
223
- const descriptor = getDescriptor(target, prop);
224
- if (
225
- descriptor &&
226
- !descriptor.configurable &&
227
- !descriptor.writable &&
228
- !descriptor.set
229
- )
230
- return true;
231
- const ah = target.__ajaxHooker;
232
- if (ah && ah.proxyProps && prop in ah.proxyProps) {
233
- const pDescriptor = ah.proxyProps[prop];
234
- pDescriptor.set ? pDescriptor.set(value) : (pDescriptor.value = value);
235
- } else {
236
- target[prop] = value;
237
- }
238
- return true;
239
- },
240
- };
241
- class XhrHooker {
242
- constructor(xhr) {
243
- const ah = this;
244
- Object.assign(ah, {
245
- originalXhr: xhr,
246
- proxyXhr: new Proxy(xhr, proxyHandler),
247
- resThenable: new SyncThenable(),
248
- proxyProps: {},
249
- proxyEvents: {},
250
- });
251
- xhr.addEventListener("readystatechange", (e) => {
252
- if (
253
- ah.proxyXhr.readyState === 4 &&
254
- ah.request &&
255
- typeof ah.request.response === "function"
256
- ) {
257
- const response = {
258
- finalUrl: ah.proxyXhr.responseURL,
259
- status: ah.proxyXhr.status,
260
- responseHeaders: parseHeaders(ah.proxyXhr.getAllResponseHeaders()),
261
- };
262
- const tempValues = {};
263
- for (const key of xhrResponses) {
264
- try {
265
- tempValues[key] = ah.originalXhr[key];
266
- } catch (err) {}
267
- defineProp(
268
- response,
269
- key,
270
- () => {
271
- return (response[key] = tempValues[key]);
272
- },
273
- (val) => {
274
- delete response[key];
275
- response[key] = val;
276
- }
277
- );
278
- }
279
- ah.resThenable = new AHRequest(ah.request)
280
- .waitForResponseKeys(response)
281
- .then(() => {
282
- for (const key of xhrResponses) {
283
- ah.proxyProps[key] = {
284
- get: () => {
285
- if (!(key in response)) response[key] = tempValues[key];
286
- return response[key];
287
- },
288
- };
289
- }
290
- });
291
- }
292
- ah.dispatchEvent(e);
293
- });
294
- xhr.addEventListener("load", (e) => ah.dispatchEvent(e));
295
- xhr.addEventListener("loadend", (e) => ah.dispatchEvent(e));
296
- for (const evt of xhrAsyncEvents) {
297
- const onEvt = "on" + evt;
298
- ah.proxyProps[onEvt] = {
299
- get: () => ah.proxyEvents[onEvt] || null,
300
- set: (val) => ah.addEvent(onEvt, val),
301
- };
302
- }
303
- for (const method of [
304
- "setRequestHeader",
305
- "addEventListener",
306
- "removeEventListener",
307
- "open",
308
- "send",
309
- ]) {
310
- ah.proxyProps[method] = { value: ah[method] };
311
- }
312
- }
313
- toJSON() {} // Converting circular structure to JSON
314
- addEvent(type, event) {
315
- if (type.startsWith("on")) {
316
- this.proxyEvents[type] = typeof event === "function" ? event : null;
317
- } else {
318
- if (typeof event === "object" && event !== null)
319
- event = event.handleEvent;
320
- if (typeof event !== "function") return;
321
- this.proxyEvents[type] = this.proxyEvents[type] || new Set();
322
- this.proxyEvents[type].add(event);
323
- }
324
- }
325
- removeEvent(type, event) {
326
- if (type.startsWith("on")) {
327
- this.proxyEvents[type] = null;
328
- } else {
329
- if (typeof event === "object" && event !== null)
330
- event = event.handleEvent;
331
- this.proxyEvents[type] && this.proxyEvents[type].delete(event);
332
- }
333
- }
334
- dispatchEvent(e) {
335
- e.stopImmediatePropagation = stopImmediatePropagation;
336
- defineProp(e, "target", () => this.proxyXhr);
337
- this.proxyEvents[e.type] &&
338
- this.proxyEvents[e.type].forEach((fn) => {
339
- this.resThenable.then(
340
- () => !e.ajaxHooker_isStopped && fn.call(this.proxyXhr, e)
341
- );
342
- });
343
- if (e.ajaxHooker_isStopped) return;
344
- const onEvent = this.proxyEvents["on" + e.type];
345
- onEvent && this.resThenable.then(onEvent.bind(this.proxyXhr, e));
346
- }
347
- setRequestHeader(header, value) {
348
- this.originalXhr.setRequestHeader(header, value);
349
- if (this.originalXhr.readyState !== 1) return;
350
- const headers = this.request.headers;
351
- headers[header] =
352
- header in headers ? `${headers[header]}, ${value}` : value;
353
- }
354
- addEventListener(...args) {
355
- if (xhrAsyncEvents.includes(args[0])) {
356
- this.addEvent(args[0], args[1]);
357
- } else {
358
- this.originalXhr.addEventListener(...args);
359
- }
360
- }
361
- removeEventListener(...args) {
362
- if (xhrAsyncEvents.includes(args[0])) {
363
- this.removeEvent(args[0], args[1]);
364
- } else {
365
- this.originalXhr.removeEventListener(...args);
366
- }
367
- }
368
- open(method, url, async = true, ...args) {
369
- this.request = {
370
- type: "xhr",
371
- url: url.toString(),
372
- method: method.toUpperCase(),
373
- abort: false,
374
- headers: {},
375
- data: null,
376
- response: null,
377
- async: !!async,
378
- };
379
- this.openArgs = args;
380
- this.resThenable = new SyncThenable();
381
- [
382
- "responseURL",
383
- "readyState",
384
- "status",
385
- "statusText",
386
- ...xhrResponses,
387
- ].forEach((key) => {
388
- delete this.proxyProps[key];
389
- });
390
- return this.originalXhr.open(method, url, async, ...args);
391
- }
392
- send(data) {
393
- const ah = this;
394
- const xhr = ah.originalXhr;
395
- const request = ah.request;
396
- if (!request) return xhr.send(data);
397
- request.data = data;
398
- new AHRequest(request).waitForRequestKeys().then(() => {
399
- if (request.abort) {
400
- if (typeof request.response === "function") {
401
- Object.assign(ah.proxyProps, {
402
- responseURL: { value: request.url },
403
- readyState: { value: 4 },
404
- status: { value: 200 },
405
- statusText: { value: "OK" },
406
- });
407
- xhrAsyncEvents.forEach((evt) => xhr.dispatchEvent(new Event(evt)));
408
- }
409
- } else {
410
- xhr.open(request.method, request.url, request.async, ...ah.openArgs);
411
- for (const header in request.headers) {
412
- xhr.setRequestHeader(header, request.headers[header]);
413
- }
414
- xhr.send(request.data);
415
- }
416
- });
417
- }
418
- }
419
- function fakeXHR() {
420
- const xhr = new winAh.realXHR();
421
- if ("__ajaxHooker" in xhr)
422
- console.warn("检测到不同版本的ajaxHooker,可能发生冲突!");
423
- xhr.__ajaxHooker = new XhrHooker(xhr);
424
- return xhr.__ajaxHooker.proxyXhr;
425
- }
426
- fakeXHR.prototype = win.XMLHttpRequest.prototype;
427
- Object.keys(win.XMLHttpRequest).forEach(
428
- (key) => (fakeXHR[key] = win.XMLHttpRequest[key])
429
- );
430
- function fakeFetch(url, options = {}) {
431
- if (!url) return winAh.realFetch.call(win, url, options);
432
- const init = {};
433
- if (getType(url) === "[object Request]") {
434
- for (const prop of fetchInitProps) init[prop] = url[prop];
435
- url = url.url;
436
- }
437
- url = url.toString();
438
- Object.assign(init, options);
439
- init.method = init.method || "GET";
440
- init.headers = init.headers || {};
441
- const request = {
442
- type: "fetch",
443
- url: url,
444
- method: init.method.toUpperCase(),
445
- abort: false,
446
- headers: parseHeaders(init.headers),
447
- data: init.body,
448
- response: null,
449
- async: true,
450
- };
451
- const req = new AHRequest(request);
452
- return new Promise((resolve, reject) => {
453
- req
454
- .waitForRequestKeys()
455
- .then(() => {
456
- if (request.abort) {
457
- if (typeof request.response === "function") {
458
- const response = {
459
- finalUrl: request.url,
460
- status: 200,
461
- responseHeaders: {},
462
- };
463
- req.waitForResponseKeys(response).then(() => {
464
- const key = fetchResponses.find((k) => k in response);
465
- let val = response[key];
466
- if (key === "json" && typeof val === "object") {
467
- val = catchError(JSON.stringify.bind(JSON), val);
468
- }
469
- const res = new Response(val, {
470
- status: 200,
471
- statusText: "OK",
472
- });
473
- defineProp(res, "type", () => "basic");
474
- defineProp(res, "url", () => request.url);
475
- resolve(res);
476
- });
477
- } else {
478
- reject(new DOMException("aborted", "AbortError"));
479
- }
480
- return;
481
- }
482
- init.method = request.method;
483
- init.headers = request.headers;
484
- init.body = request.data;
485
- winAh.realFetch.call(win, request.url, init).then((res) => {
486
- if (typeof request.response === "function") {
487
- const response = {
488
- finalUrl: res.url,
489
- status: res.status,
490
- responseHeaders: parseHeaders(res.headers),
491
- };
492
- fetchResponses.forEach(
493
- (key) =>
494
- (res[key] = function () {
495
- if (key in response) return Promise.resolve(response[key]);
496
- return resProto[key].call(this).then((val) => {
497
- response[key] = val;
498
- return req
499
- .waitForResponseKeys(response)
500
- .then(() => (key in response ? response[key] : val));
501
- });
502
- })
503
- );
504
- }
505
- resolve(res);
506
- }, reject);
507
- })
508
- .catch((err) => {
509
- console.error(err);
510
- resolve(winAh.realFetch.call(win, url, init));
511
- });
512
- });
513
- }
514
- function fakeFetchClone() {
515
- const descriptors = Object.getOwnPropertyDescriptors(this);
516
- const res = winAh.realFetchClone.call(this);
517
- Object.defineProperties(res, descriptors);
518
- return res;
519
- }
520
- winAh = win.__ajaxHooker = winAh || {
521
- version,
522
- fakeXHR,
523
- fakeFetch,
524
- fakeFetchClone,
525
- realXHR: win.XMLHttpRequest,
526
- realFetch: win.fetch,
527
- realFetchClone: resProto.clone,
528
- hookInsts: new Set(),
529
- };
530
- if (winAh.version !== version)
531
- console.warn("检测到不同版本的ajaxHooker,可能发生冲突!");
532
- win.XMLHttpRequest = winAh.fakeXHR;
533
- win.fetch = winAh.fakeFetch;
534
- resProto.clone = winAh.fakeFetchClone;
535
- winAh.hookInsts.add(hookInst);
536
- return {
537
- hook: (fn) => hookInst.hookFns.push(fn),
538
- filter: (arr) => {
539
- if (Array.isArray(arr)) hookInst.filters = arr;
540
- },
541
- protect: () => {
542
- readonly(win, "XMLHttpRequest", winAh.fakeXHR);
543
- readonly(win, "fetch", winAh.fakeFetch);
544
- readonly(resProto, "clone", winAh.fakeFetchClone);
545
- },
546
- unhook: () => {
547
- winAh.hookInsts.delete(hookInst);
548
- if (!winAh.hookInsts.size) {
549
- writable(win, "XMLHttpRequest", winAh.realXHR);
550
- writable(win, "fetch", winAh.realFetch);
551
- writable(resProto, "clone", winAh.realFetchClone);
552
- delete win.__ajaxHooker;
553
- }
554
- },
555
- };
556
- })();
557
-
558
- export { ajaxHooker };