@whitesev/utils 2.8.0 → 2.8.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.
Files changed (71) hide show
  1. package/README.md +176 -176
  2. package/dist/index.amd.js +896 -875
  3. package/dist/index.amd.js.map +1 -1
  4. package/dist/index.cjs.js +896 -875
  5. package/dist/index.cjs.js.map +1 -1
  6. package/dist/index.esm.js +896 -875
  7. package/dist/index.esm.js.map +1 -1
  8. package/dist/index.iife.js +896 -875
  9. package/dist/index.iife.js.map +1 -1
  10. package/dist/index.system.js +896 -875
  11. package/dist/index.system.js.map +1 -1
  12. package/dist/index.umd.js +896 -875
  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/index.ts +3 -0
  38. package/package.json +19 -8
  39. package/src/ColorConversion.ts +105 -106
  40. package/src/CommonUtil.ts +280 -279
  41. package/src/DOMUtils.ts +251 -272
  42. package/src/Dictionary.ts +153 -154
  43. package/src/GBKEncoder.ts +108 -112
  44. package/src/Hooks.ts +73 -81
  45. package/src/Httpx.ts +1457 -1466
  46. package/src/LockFunction.ts +62 -62
  47. package/src/Log.ts +258 -259
  48. package/src/ModuleRaid.js +1 -0
  49. package/src/Progress.ts +108 -114
  50. package/src/TryCatch.ts +86 -86
  51. package/src/Utils.ts +4773 -4825
  52. package/src/UtilsCommon.ts +14 -14
  53. package/src/UtilsGMCookie.ts +254 -261
  54. package/src/UtilsGMMenu.ts +445 -454
  55. package/src/Vue.ts +233 -229
  56. package/src/WindowApi.ts +59 -59
  57. package/src/ajaxHooker/ajaxHooker.js +1 -0
  58. package/src/indexedDB.ts +497 -502
  59. package/src/types/Event.d.ts +188 -188
  60. package/src/types/Httpx.d.ts +1344 -1343
  61. package/src/types/Log.d.ts +19 -19
  62. package/src/types/Progress.d.ts +20 -20
  63. package/src/types/React.d.ts +119 -119
  64. package/src/types/TryCatch.d.ts +9 -9
  65. package/src/types/UtilsGMCookie.d.ts +93 -93
  66. package/src/types/UtilsGMMenu.d.ts +77 -77
  67. package/src/types/Vue2.d.ts +166 -166
  68. package/src/types/WindowApi.d.ts +14 -14
  69. package/src/types/ajaxHooker.d.ts +151 -151
  70. package/src/types/env.d.ts +7 -2
  71. package/src/types/global.d.ts +31 -31
package/src/Vue.ts CHANGED
@@ -1,229 +1,233 @@
1
- const VueUtils = {
2
- /** 标签 */
3
- ReactiveFlags: {
4
- IS_REACTIVE: Symbol("isReactive"),
5
- },
6
- /**
7
- * 判断是否是对象
8
- * @param value
9
- */
10
- isObject(value: any) {
11
- return typeof value === "object" && value !== null;
12
- },
13
- /**
14
- * 判断是否是函数
15
- * @param val
16
- */
17
- isFunction(val: any) {
18
- return typeof val === "function";
19
- },
20
- /**
21
- * 处理对象再次代理,可以直接返回
22
- * @param value
23
- */
24
- isReactive(value: any) {
25
- return !!(value && value[VueUtils.ReactiveFlags.IS_REACTIVE]);
26
- },
27
- /**
28
- * 判断是否是数组
29
- * @param value
30
- */
31
- isArray(value: any): boolean {
32
- return Array.isArray(value);
33
- },
34
- };
35
-
36
- class ReactiveEffect {
37
- deps: any[] = [];
38
- private active = true;
39
- private fn;
40
- // private scheduler;
41
- constructor(fn: Function, scheduler: any) {
42
- this.fn = fn;
43
- // this.scheduler = scheduler;
44
- }
45
- run(cb: (activeEffect: any) => void) {
46
- if (!this.active) {
47
- this.fn();
48
- }
49
- try {
50
- if (typeof cb === "function") {
51
- cb(this);
52
- }
53
- return this.fn();
54
- } finally {
55
- if (typeof cb === "function") {
56
- cb(undefined);
57
- }
58
- }
59
- }
60
- }
61
- class RefImpl {
62
- _value;
63
- _isRef = true;
64
- _rawValue;
65
- _vue: Vue;
66
- constructor(vueIns: Vue, rawValue: any) {
67
- this._vue = vueIns;
68
- this._rawValue = rawValue;
69
- this._value = this._vue.toReactive(rawValue);
70
- }
71
- get value() {
72
- return this._value;
73
- }
74
- set value(newValue) {
75
- if (newValue !== this._rawValue) {
76
- this._value = this._vue.toReactive(newValue);
77
- this._rawValue = newValue;
78
- }
79
- }
80
- }
81
- class ObjectRefImpl {
82
- object;
83
- key;
84
- constructor(object: any, key: any) {
85
- this.object = object;
86
- this.key = key;
87
- }
88
- get value() {
89
- return this.object[this.key];
90
- }
91
- set value(newValue) {
92
- this.object[this.key] = newValue;
93
- }
94
- }
95
- export class Vue {
96
- private reactMap = new WeakMap();
97
- private targetMap = new WeakMap();
98
- private activeEffect = undefined as any as ReactiveEffect;
99
- constructor() {
100
- // 将数据转化成响应式的数据,只能做对象的代理
101
- }
102
- /**
103
- * 生成一个被代理的对象
104
- * @param target 需要代理的对象
105
- */
106
- reactive<T extends object>(target: T): T {
107
- const that = this;
108
- if (!(typeof target === "object" && target !== null)) {
109
- return void 0 as any as T;
110
- }
111
- if (VueUtils.isReactive(target)) {
112
- return target;
113
- }
114
- let exisProxy = this.reactMap.get(target);
115
- if (exisProxy) {
116
- return exisProxy;
117
- }
118
- const proxy = new Proxy(target, {
119
- get(target, key, receiver) {
120
- if (key === VueUtils.ReactiveFlags.IS_REACTIVE) {
121
- return true;
122
- }
123
- that.track(target, "get", key);
124
- return Reflect.get(target, key, receiver);
125
- },
126
- set(target, key, value, receiver) {
127
- let oldValue = target[key as keyof T];
128
- let result = Reflect.set(target, key, value, receiver);
129
- if (oldValue !== value) {
130
- that.trigger(target, "set", key, oldValue, value);
131
- }
132
- return result;
133
- },
134
- });
135
- that.reactMap.set(target, proxy);
136
- return proxy;
137
- }
138
- /**
139
- * 观察被reactive的对象值改变
140
- * @param source 被观察的对象,这里采用函数返回对象
141
- * @param changeCallBack 值改变的回调
142
- */
143
- watch<T>(source: () => T, changeCallBack: (newValue: T | undefined, oldValue: T | undefined) => void) {
144
- let getter;
145
- if (VueUtils.isReactive(source)) {
146
- getter = () => this.traversal(source);
147
- } else if (VueUtils.isFunction(source)) {
148
- getter = source;
149
- } else {
150
- return;
151
- }
152
- let oldValue: any;
153
- const job = () => {
154
- const newValue = effect.run((activeEffect) => {
155
- this.activeEffect = activeEffect;
156
- });
157
- changeCallBack(newValue, oldValue);
158
- oldValue = newValue;
159
- };
160
- const effect = new ReactiveEffect(getter, job);
161
- oldValue = effect.run((activeEffect) => {
162
- this.activeEffect = activeEffect;
163
- });
164
- }
165
- toReactive(value: any) {
166
- return VueUtils.isObject(value) ? this.reactive(value) : value;
167
- }
168
- ref(value: any) {
169
- return new RefImpl(this, value);
170
- }
171
- toRef(object: any, key: any) {
172
- return new ObjectRefImpl(object, key);
173
- }
174
- toRefs(object: any) {
175
- const result = VueUtils.isArray(object) ? new Array(object.length) : {};
176
- for (let key in object) {
177
- (result as any)[key as any] = this.toRef(object, key);
178
- }
179
- return result;
180
- }
181
- private trigger(target: any, type: string, key: string | symbol, oldValue: any, value: any) {
182
- const depsMap = this.targetMap.get(target);
183
- if (!depsMap) return;
184
- const effects = depsMap.get(key);
185
- this.triggerEffect(effects, "effects");
186
- }
187
- private triggerEffect(effects: any[], name: string) {
188
- effects &&
189
- effects.forEach((effect) => {
190
- if (effect.scheduler) {
191
- effect.scheduler();
192
- } else {
193
- effect.run();
194
- }
195
- });
196
- }
197
- private track(target: WeakKey, type: string, key: string | symbol) {
198
- if (!this.activeEffect) return;
199
- let depsMap = this.targetMap.get(target);
200
- if (!depsMap) {
201
- this.targetMap.set(target, (depsMap = new Map()));
202
- }
203
- let dep = depsMap.get(key);
204
- if (!dep) {
205
- depsMap.set(key, (dep = new Set()));
206
- }
207
- this.trackEffect(dep);
208
- }
209
- private trackEffect(dep: any) {
210
- if (this.activeEffect) {
211
- let shouldTrack = !dep.has(this.activeEffect);
212
- if (shouldTrack) {
213
- dep.add(this.activeEffect);
214
- this.activeEffect.deps.push(dep);
215
- }
216
- }
217
- }
218
- private traversal(value: any, set = new Set()) {
219
- if (!VueUtils.isObject(value)) return value;
220
- if (set.has(value)) {
221
- return value;
222
- }
223
- set.add(value);
224
- for (let key in value) {
225
- this.traversal(value[key], set);
226
- }
227
- return value;
228
- }
229
- }
1
+ // @ts-nocheck
2
+ const VueUtils = {
3
+ /** 标签 */
4
+ ReactiveFlags: {
5
+ IS_REACTIVE: Symbol("isReactive"),
6
+ },
7
+ /**
8
+ * 判断是否是对象
9
+ * @param value
10
+ */
11
+ isObject(value: any) {
12
+ return typeof value === "object" && value !== null;
13
+ },
14
+ /**
15
+ * 判断是否是函数
16
+ * @param val
17
+ */
18
+ isFunction(val: any) {
19
+ return typeof val === "function";
20
+ },
21
+ /**
22
+ * 处理对象再次代理,可以直接返回
23
+ * @param value
24
+ */
25
+ isReactive(value: any) {
26
+ return !!(value && value[VueUtils.ReactiveFlags.IS_REACTIVE]);
27
+ },
28
+ /**
29
+ * 判断是否是数组
30
+ * @param value
31
+ */
32
+ isArray(value: any): boolean {
33
+ return Array.isArray(value);
34
+ },
35
+ };
36
+
37
+ class ReactiveEffect {
38
+ deps: any[] = [];
39
+ private active = true;
40
+ private fn;
41
+ private scheduler;
42
+ constructor(fn: (...args: any[]) => any, scheduler: any) {
43
+ this.fn = fn;
44
+ this.scheduler = scheduler;
45
+ }
46
+ run(cb: (activeEffect: any) => void) {
47
+ if (!this.active) {
48
+ this.fn();
49
+ }
50
+ try {
51
+ if (typeof cb === "function") {
52
+ cb(this);
53
+ }
54
+ return this.fn();
55
+ } finally {
56
+ if (typeof cb === "function") {
57
+ cb(undefined);
58
+ }
59
+ }
60
+ }
61
+ }
62
+ class RefImpl {
63
+ _value;
64
+ _isRef = true;
65
+ _rawValue;
66
+ _vue: Vue;
67
+ constructor(vueIns: Vue, rawValue: any) {
68
+ this._vue = vueIns;
69
+ this._rawValue = rawValue;
70
+ this._value = this._vue.toReactive(rawValue);
71
+ }
72
+ get value() {
73
+ return this._value;
74
+ }
75
+ set value(newValue) {
76
+ if (newValue !== this._rawValue) {
77
+ this._value = this._vue.toReactive(newValue);
78
+ this._rawValue = newValue;
79
+ }
80
+ }
81
+ }
82
+ class ObjectRefImpl {
83
+ object;
84
+ key;
85
+ constructor(object: any, key: any) {
86
+ this.object = object;
87
+ this.key = key;
88
+ }
89
+ get value() {
90
+ return this.object[this.key];
91
+ }
92
+ set value(newValue) {
93
+ this.object[this.key] = newValue;
94
+ }
95
+ }
96
+ export class Vue {
97
+ private reactMap = new WeakMap();
98
+ private targetMap = new WeakMap();
99
+ private activeEffect = undefined as any as ReactiveEffect;
100
+ constructor() {
101
+ // 将数据转化成响应式的数据,只能做对象的代理
102
+ }
103
+ /**
104
+ * 生成一个被代理的对象
105
+ * @param target 需要代理的对象
106
+ */
107
+ reactive<T extends object>(target: T): T {
108
+ const that = this;
109
+ if (!(typeof target === "object" && target !== null)) {
110
+ return void 0 as any as T;
111
+ }
112
+ if (VueUtils.isReactive(target)) {
113
+ return target;
114
+ }
115
+ const exisProxy = this.reactMap.get(target);
116
+ if (exisProxy) {
117
+ return exisProxy;
118
+ }
119
+ const proxy = new Proxy(target, {
120
+ get(target, key, receiver) {
121
+ if (key === VueUtils.ReactiveFlags.IS_REACTIVE) {
122
+ return true;
123
+ }
124
+ that.track(target, "get", key);
125
+ return Reflect.get(target, key, receiver);
126
+ },
127
+ set(target, key, value, receiver) {
128
+ const oldValue = target[key as keyof T];
129
+ const result = Reflect.set(target, key, value, receiver);
130
+ if (oldValue !== value) {
131
+ that.trigger(target, "set", key, oldValue, value);
132
+ }
133
+ return result;
134
+ },
135
+ });
136
+ that.reactMap.set(target, proxy);
137
+ return proxy;
138
+ }
139
+ /**
140
+ * 观察被reactive的对象值改变
141
+ * @param source 被观察的对象,这里采用函数返回对象
142
+ * @param changeCallBack 值改变的回调
143
+ */
144
+ watch<T>(source: () => T, changeCallBack: (newValue: T | undefined, oldValue: T | undefined) => void) {
145
+ let getter;
146
+ if (VueUtils.isReactive(source)) {
147
+ getter = () => this.traversal(source);
148
+ } else if (VueUtils.isFunction(source)) {
149
+ getter = source;
150
+ } else {
151
+ return;
152
+ }
153
+ let oldValue: any;
154
+ const job = () => {
155
+ const newValue = effect.run((activeEffect) => {
156
+ this.activeEffect = activeEffect;
157
+ });
158
+ changeCallBack(newValue, oldValue);
159
+ oldValue = newValue;
160
+ };
161
+ const effect = new ReactiveEffect(getter, job);
162
+ oldValue = effect.run((activeEffect) => {
163
+ this.activeEffect = activeEffect;
164
+ });
165
+ }
166
+ toReactive(value: any) {
167
+ return VueUtils.isObject(value) ? this.reactive(value) : value;
168
+ }
169
+ ref(value: any) {
170
+ return new RefImpl(this, value);
171
+ }
172
+ toRef(object: any, key: any) {
173
+ return new ObjectRefImpl(object, key);
174
+ }
175
+ toRefs(object: any) {
176
+ const result = VueUtils.isArray(object) ? new Array(object.length) : {};
177
+ for (const key in object) {
178
+ (result as any)[key as any] = this.toRef(object, key);
179
+ }
180
+ return result;
181
+ }
182
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
183
+ private trigger(target: any, type: string, key: string | symbol, oldValue: any, value: any) {
184
+ const depsMap = this.targetMap.get(target);
185
+ if (!depsMap) return;
186
+ const effects = depsMap.get(key);
187
+ this.triggerEffect(effects, "effects");
188
+ }
189
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
190
+ private triggerEffect(effects: any[], name: string) {
191
+ if (effects) {
192
+ effects.forEach((effect) => {
193
+ if (effect.scheduler) {
194
+ effect.scheduler();
195
+ } else {
196
+ effect.run();
197
+ }
198
+ });
199
+ }
200
+ }
201
+ private track(target: WeakKey, type: string, key: string | symbol) {
202
+ if (!this.activeEffect) return;
203
+ let depsMap = this.targetMap.get(target);
204
+ if (!depsMap) {
205
+ this.targetMap.set(target, (depsMap = new Map()));
206
+ }
207
+ let dep = depsMap.get(key);
208
+ if (!dep) {
209
+ depsMap.set(key, (dep = new Set()));
210
+ }
211
+ this.trackEffect(dep);
212
+ }
213
+ private trackEffect(dep: any) {
214
+ if (this.activeEffect) {
215
+ const shouldTrack = !dep.has(this.activeEffect);
216
+ if (shouldTrack) {
217
+ dep.add(this.activeEffect);
218
+ this.activeEffect.deps.push(dep);
219
+ }
220
+ }
221
+ }
222
+ private traversal(value: any, set = new Set()) {
223
+ if (!VueUtils.isObject(value)) return value;
224
+ if (set.has(value)) {
225
+ return value;
226
+ }
227
+ set.add(value);
228
+ for (const key in value) {
229
+ this.traversal(value[key], set);
230
+ }
231
+ return value;
232
+ }
233
+ }
package/src/WindowApi.ts CHANGED
@@ -1,59 +1,59 @@
1
- import type { WindowApiOption } from "./types/WindowApi";
2
-
3
- export class WindowApi {
4
- /** 默认的配置 */
5
- private defaultApi: Required<WindowApiOption> = {
6
- document: document,
7
- window: window,
8
- globalThis: globalThis,
9
- self: self,
10
- top: top!,
11
- setTimeout: globalThis.setTimeout.bind(globalThis),
12
- setInterval: globalThis.setInterval.bind(globalThis),
13
- clearTimeout: globalThis.clearTimeout.bind(globalThis),
14
- clearInterval: globalThis.clearInterval.bind(globalThis),
15
- };
16
- /** 使用的配置 */
17
- private api: Required<WindowApiOption>;
18
- constructor(option?: WindowApiOption) {
19
- if (option) {
20
- if (option.globalThis == null) {
21
- option.globalThis = option.window;
22
- }
23
- if (option.self == null) {
24
- option.self = option.window;
25
- }
26
- }
27
- if (!option) {
28
- option = Object.assign({}, this.defaultApi);
29
- }
30
- this.api = Object.assign({}, option as Required<WindowApiOption>);
31
- }
32
- get document() {
33
- return this.api.document;
34
- }
35
- get window() {
36
- return this.api.window;
37
- }
38
- get globalThis() {
39
- return this.api.globalThis;
40
- }
41
- get self() {
42
- return this.api.self;
43
- }
44
- get top() {
45
- return this.api.top;
46
- }
47
- get setTimeout() {
48
- return this.api.setTimeout;
49
- }
50
- get setInterval() {
51
- return this.api.setInterval;
52
- }
53
- get clearTimeout() {
54
- return this.api.clearTimeout;
55
- }
56
- get clearInterval() {
57
- return this.api.clearInterval;
58
- }
59
- }
1
+ import type { WindowApiOption } from "./types/WindowApi";
2
+
3
+ export class WindowApi {
4
+ /** 默认的配置 */
5
+ private defaultApi: Required<WindowApiOption> = {
6
+ document: document,
7
+ window: window,
8
+ globalThis: globalThis,
9
+ self: self,
10
+ top: top!,
11
+ setTimeout: globalThis.setTimeout.bind(globalThis),
12
+ setInterval: globalThis.setInterval.bind(globalThis),
13
+ clearTimeout: globalThis.clearTimeout.bind(globalThis),
14
+ clearInterval: globalThis.clearInterval.bind(globalThis),
15
+ };
16
+ /** 使用的配置 */
17
+ private api: Required<WindowApiOption>;
18
+ constructor(option?: WindowApiOption) {
19
+ if (option) {
20
+ if (option.globalThis == null) {
21
+ option.globalThis = option.window;
22
+ }
23
+ if (option.self == null) {
24
+ option.self = option.window;
25
+ }
26
+ }
27
+ if (!option) {
28
+ option = Object.assign({}, this.defaultApi);
29
+ }
30
+ this.api = Object.assign({}, option as Required<WindowApiOption>);
31
+ }
32
+ get document() {
33
+ return this.api.document;
34
+ }
35
+ get window() {
36
+ return this.api.window;
37
+ }
38
+ get globalThis() {
39
+ return this.api.globalThis;
40
+ }
41
+ get self() {
42
+ return this.api.self;
43
+ }
44
+ get top() {
45
+ return this.api.top;
46
+ }
47
+ get setTimeout() {
48
+ return this.api.setTimeout;
49
+ }
50
+ get setInterval() {
51
+ return this.api.setInterval;
52
+ }
53
+ get clearTimeout() {
54
+ return this.api.clearTimeout;
55
+ }
56
+ get clearInterval() {
57
+ return this.api.clearInterval;
58
+ }
59
+ }
@@ -1,3 +1,4 @@
1
+ /* eslint-disable */
1
2
  // ==UserScript==
2
3
  // @name ajaxHooker
3
4
  // @author cxxjackie