@whitesev/utils 2.3.8 → 2.4.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.
Files changed (56) hide show
  1. package/dist/index.amd.js +379 -268
  2. package/dist/index.amd.js.map +1 -1
  3. package/dist/index.cjs.js +379 -268
  4. package/dist/index.cjs.js.map +1 -1
  5. package/dist/index.esm.js +379 -268
  6. package/dist/index.esm.js.map +1 -1
  7. package/dist/index.iife.js +379 -268
  8. package/dist/index.iife.js.map +1 -1
  9. package/dist/index.system.js +379 -268
  10. package/dist/index.system.js.map +1 -1
  11. package/dist/index.umd.js +379 -268
  12. package/dist/index.umd.js.map +1 -1
  13. package/dist/types/src/Httpx.d.ts +25 -1178
  14. package/dist/types/src/Log.d.ts +1 -19
  15. package/dist/types/src/Progress.d.ts +1 -20
  16. package/dist/types/src/TryCatch.d.ts +2 -11
  17. package/dist/types/src/Utils.d.ts +8 -32
  18. package/dist/types/src/UtilsGMCookie.d.ts +1 -51
  19. package/dist/types/src/UtilsGMMenu.d.ts +1 -50
  20. package/dist/types/src/WindowApi.d.ts +3 -11
  21. package/dist/types/src/types/Httpx.d.ts +1316 -0
  22. package/dist/types/src/types/Log.d.ts +19 -0
  23. package/dist/types/src/types/Progress.d.ts +20 -0
  24. package/dist/types/src/types/TryCatch.d.ts +9 -0
  25. package/dist/types/src/types/UtilsGMCookie.d.ts +55 -0
  26. package/dist/types/src/types/UtilsGMMenu.d.ts +77 -0
  27. package/{src/VueObject.ts → dist/types/src/types/Vue2.d.ts} +28 -15
  28. package/dist/types/src/types/Vue3.d.ts +0 -0
  29. package/dist/types/src/types/WindowApi.d.ts +10 -0
  30. package/dist/types/src/types/global.d.ts +24 -1
  31. package/package.json +1 -1
  32. package/src/GBKEncoder.ts +1 -1
  33. package/src/Httpx.ts +535 -1627
  34. package/src/Log.ts +1 -19
  35. package/src/Progress.ts +1 -20
  36. package/src/TryCatch.ts +2 -12
  37. package/src/Utils.ts +15 -38
  38. package/src/UtilsGMCookie.ts +7 -56
  39. package/src/UtilsGMMenu.ts +5 -78
  40. package/src/WindowApi.ts +4 -11
  41. package/src/types/Event.d.ts +189 -0
  42. package/src/types/Httpx.d.ts +1316 -0
  43. package/src/types/Log.d.ts +19 -0
  44. package/src/types/Progress.d.ts +20 -0
  45. package/src/types/TryCatch.d.ts +9 -0
  46. package/src/types/UtilsGMCookie.d.ts +55 -0
  47. package/src/types/UtilsGMMenu.d.ts +77 -0
  48. package/src/types/Vue2.d.ts +154 -0
  49. package/src/types/Vue3.d.ts +0 -0
  50. package/src/types/WindowApi.d.ts +10 -0
  51. package/{dist/types/src/AjaxHookerType.d.ts → src/types/ajaxHooker.d.ts} +155 -147
  52. package/src/types/global.d.ts +24 -1
  53. package/dist/types/src/Event.d.ts +0 -156
  54. package/dist/types/src/VueObject.d.ts +0 -123
  55. /package/{src/Event.ts → dist/types/src/types/Event.d.ts} +0 -0
  56. /package/{src/AjaxHookerType.ts → dist/types/src/types/ajaxHooker.d.ts} +0 -0
@@ -0,0 +1,19 @@
1
+ /** Utils.Log的初始化配置 */
2
+ export interface UtilsLogOptions {
3
+ /** 是否输出Tag,false的话其它的颜色也不输出 @default true */
4
+ tag: boolean;
5
+ /** log.success的颜色 @default "#0000FF" */
6
+ successColor: string;
7
+ /** log.warn的颜色 @default "0" */
8
+ warnColor: string;
9
+ /** log.error的颜色 @default "#FF0000" */
10
+ errorColor: string;
11
+ /** log.info的颜色 @default "0" */
12
+ infoColor: string;
13
+ /** 是否开启debug模式,true会在控制台每次调用时输出调用函数的所在位置,false不会输出位置 @default false */
14
+ debug: boolean;
15
+ /** 当console输出超过logMaxCount数量自动清理控制台 @default false */
16
+ autoClearConsole: boolean;
17
+ /** console输出的最高数量,autoClearConsole开启则生效 @default 999 */
18
+ logMaxCount: number;
19
+ }
@@ -0,0 +1,20 @@
1
+ export interface ProgressParamConfig {
2
+ /** canvas元素节点 */
3
+ canvasNode: HTMLCanvasElement;
4
+ /** 绘制角度,默认:95 */
5
+ deg: number;
6
+ /** 进度,默认:0 */
7
+ progress: number;
8
+ /** 绘制的线宽度,默认:10 */
9
+ lineWidth: number;
10
+ /** 绘制的背景颜色,默认:#1e637c */
11
+ lineBgColor: string;
12
+ /** 绘制的线的颜色,默认:#25deff */
13
+ lineColor: string;
14
+ /** 绘制的字体颜色,默认:#000000 */
15
+ textColor: string;
16
+ /** 绘制的字体大小(px),默认:22px */
17
+ fontSize: number;
18
+ /** 绘制的圆的半径,默认:50 */
19
+ circleRadius: number;
20
+ }
@@ -0,0 +1,9 @@
1
+ export declare interface UtilsTryCatchConfig {
2
+ log: boolean;
3
+ }
4
+ /** tryCatch */
5
+ export declare interface UtilsTryCatchType {
6
+ run: UtilsTryCatchType;
7
+ config: UtilsTryCatchType;
8
+ error: UtilsTryCatchType;
9
+ }
@@ -0,0 +1,55 @@
1
+ export interface UtilsGMCookieResult {
2
+ /** 为 window.location.hostname */
3
+ domain: string;
4
+ expirationDate: null;
5
+ hostOnly: true;
6
+ httpOnly: false;
7
+ name: string;
8
+ path: "/";
9
+ sameSite: "unspecified";
10
+ secure: true;
11
+ session: false;
12
+ value: string;
13
+ }
14
+
15
+ export interface UtilsGMCookieListOptions {
16
+ /** 默认为当前的url */
17
+ url: string;
18
+ /** 默认为当前的域名(window.location.hostname) */
19
+ domain: string;
20
+ /** 需要检索的Cookie的名字 */
21
+ name: string | RegExp;
22
+ /** 需要检索的Cookie的路径,默认为"/" */
23
+ path: string;
24
+ }
25
+
26
+ export interface UtilsGMCookieSetOptions {
27
+ /** 默认为当前的url */
28
+ url?: string;
29
+ /** 默认为当前的域名(window.location.hostname) */
30
+ domain?: string;
31
+ /** 需要检索的Cookie的名字 */
32
+ name?: string;
33
+ /** 需要检索的Cookie的路径,默认为"/" */
34
+ path?: string;
35
+ /** 值 */
36
+ value?: string | number;
37
+ /** 确保Cookie只在通过安全协议(如HTTPS)的情况下传输 */
38
+ secure?: boolean;
39
+ /** 是否防止JavaScript代码访问Cookie */
40
+ httpOnly?: boolean;
41
+ /** Cookie过期时间,默认为30天 */
42
+ expirationDate?: number;
43
+ }
44
+
45
+ export interface UtilsGMCookieDeleteOptions {
46
+ /** 默认为当前的url */
47
+ url: string;
48
+ /** 需要检索的Cookie的名字 */
49
+ name: string;
50
+ }
51
+
52
+ export interface WindowApiOption {
53
+ window: Window & typeof globalThis;
54
+ document: Document;
55
+ }
@@ -0,0 +1,77 @@
1
+ export interface UtilsGMMenuClickCallBackData {
2
+ /** 菜单键名 */
3
+ key: string;
4
+ /** 是否启用 */
5
+ enable: boolean;
6
+ /** 点击前的enable值 */
7
+ oldEnable: boolean;
8
+ /** 触发的事件 */
9
+ event: MouseEvent | KeyboardEvent;
10
+ /** 将enable值写入本地的回调,设置参数false就不保存到本地 */
11
+ storeValue(enable: boolean): void;
12
+ }
13
+
14
+ export interface UtilsGMMenuOption {
15
+ /** 菜单的本地键key,不可重复,会覆盖 */
16
+ key: string;
17
+ /** 菜单的文本 */
18
+ text: string;
19
+ /** (可选)菜单的开启状态,默认为false */
20
+ enable?: boolean;
21
+ /** (可选)使用条件:TamperMonkey版本>5.0,如果id和已注册的菜单id相同,可修改当前已注册菜单的options */
22
+ id?: number;
23
+ /** (可选)An optional access key. Please see the description below. Either options or accessKey can be specified. */
24
+ accessKey?: string;
25
+ /** (可选)自动关闭菜单,可不设置 */
26
+ autoClose?: boolean;
27
+ /** 使用条件:TamperMonkey版本>5.0,使用菜单项的鼠标悬浮上的工具提示*/
28
+ title?: string;
29
+ /** (可选)点击菜单后自动刷新网页,默认为true */
30
+ autoReload?: boolean;
31
+ /** 菜单的显示文本,未设置的话则自动根据enable在前面加上图标 */
32
+ showText(text: string, enable: boolean): string;
33
+ /** 点击菜单的回调 */
34
+ callback(data: UtilsGMMenuClickCallBackData): void;
35
+ /** 是否允许菜单进行存储值,默认true允许 */
36
+ isStoreValue?: boolean;
37
+ }
38
+
39
+ export interface UtilsGMMenuHandledOption extends UtilsGMMenuOption {
40
+ /**
41
+ * 删除该菜单
42
+ */
43
+ deleteMenu(): void;
44
+ }
45
+
46
+ export interface UtilsGMMenuOptionData {
47
+ /**
48
+ * 菜单id
49
+ */
50
+ id?: number;
51
+ /**
52
+ * 菜单配置
53
+ */
54
+ data: UtilsGMMenuOption;
55
+ /**
56
+ * 处理后的菜单配置
57
+ * 对autoReload进行处理,如果未赋值,按默认的true赋值
58
+ * 对isStoreValue进行处理,如果未赋值,按默认的true赋值
59
+ * 新增一个deleteMenu
60
+ */
61
+ handleData: UtilsGMMenuHandledOption;
62
+ }
63
+
64
+ export interface UtilsGMMenuConstructorOptions {
65
+ /** (可选)配置*/
66
+ data?: UtilsGMMenuOption[];
67
+ /** (可选)全局菜单点击菜单后自动刷新网页,默认为true */
68
+ autoReload?: boolean;
69
+ /** 油猴函数 @grant GM_getValue */
70
+ GM_getValue: any;
71
+ /** 油猴函数 @grant GM_setValue */
72
+ GM_setValue: any;
73
+ /** 油猴函数 @grant GM_registerMenuCommand */
74
+ GM_registerMenuCommand: any;
75
+ /** 油猴函数 @grant GM_unregisterMenuCommand */
76
+ GM_unregisterMenuCommand: any;
77
+ }
@@ -0,0 +1,154 @@
1
+ /**
2
+ * Vue2实例
3
+ */
4
+ export declare interface Vue2Instance {
5
+ $attrs: any;
6
+ $children: Vue2Instance[];
7
+ $createElement: (...args: any[]) => any;
8
+ $el: HTMLElement;
9
+ $listeners: any;
10
+ $options: any;
11
+ $parent: Vue2Instance;
12
+ $refs: any;
13
+ $root: Vue2Instance;
14
+ $scopedSlots: any;
15
+ $slots: any;
16
+ $store: any;
17
+ $vnode: any;
18
+
19
+ _data: any;
20
+ _directInactive: boolean;
21
+ _events: any;
22
+ _hasHookEvent: boolean;
23
+ _isBeingDestroyed: boolean;
24
+ _isDestroyed: boolean;
25
+ _isMounted: boolean;
26
+ _isVue: boolean;
27
+
28
+ $data: any;
29
+ $isServer: boolean;
30
+ $props: any;
31
+ $route: {
32
+ fullPath: string;
33
+ hash: string;
34
+ matched: any[];
35
+ meta: any;
36
+ name: string;
37
+ params: any;
38
+ path: string;
39
+ query: any;
40
+ [key: string]: any;
41
+ };
42
+ $router: {
43
+ afterHooks: Function[];
44
+ app: Vue2Instance;
45
+ apps: Vue2Instance[];
46
+ beforeHooks: Function[];
47
+ fallback: boolean;
48
+ history: {
49
+ base: string;
50
+ current: Vue2Instance["$route"];
51
+ listeners: any[];
52
+ router: Vue2Instance["$router"];
53
+ /**
54
+ *
55
+ * @param delta 访问的距离。如果 delta < 0 则后退相应数量的记录,如果 > 0 则前进。
56
+ * @param triggerListeners 是否应该触发连接到该历史的监听器
57
+ */
58
+ go: (delta: number, triggerListeners?: boolean) => void;
59
+ /**
60
+ *
61
+ * @param to 要设置的地址
62
+ * @param data 可选的 HistoryState 以关联该导航记录
63
+ */
64
+ push: (to: string, data?: any) => void;
65
+ /**
66
+ *
67
+ * @param to 要设置的地址
68
+ * @param data 可选的 HistoryState 以关联该导航记录
69
+ */
70
+ replace: (to: string, data?: any) => void;
71
+ [key: string]: any;
72
+ };
73
+ matcher: {
74
+ addRoute: (...args: any[]) => any;
75
+ addRoutes: (...args: any[]) => any;
76
+ getRoutes: () => any;
77
+ match: (...args: any[]) => any;
78
+ [key: string]: any;
79
+ };
80
+ mode: string;
81
+ options: {
82
+ mode: string;
83
+ routes: any[];
84
+ scrollBehavior: (...args: any[]) => any;
85
+ [key: string]: any;
86
+ };
87
+ resolveHooks: ((...args: any[]) => any)[];
88
+ currentRoute: Vue2Instance["$route"];
89
+ beforeEach: (
90
+ callback:
91
+ | ((
92
+ /** 即将要进入的目标 路由对象 */
93
+ to: Vue2Instance["$route"],
94
+ /** 当前导航正要离开的路由 */
95
+ from: Vue2Instance["$route"],
96
+ /**
97
+ *
98
+ * + next(): 进行管道中的下一个钩子。如果全部钩子执行完了,则导航的状态就是 confirmed (确认的)。
99
+ * + next(false): 中断当前的导航。如果浏览器的 URL 改变了 (可能是用户手动或者浏览器后退按钮),那么 URL 地址会重置到 from 路由对应的地址。
100
+ * + next('/') 或者 next({ path: '/' }): 跳转到一个不同的地址。当前的导航被中断,然后进行一个新的导航。你可以向 next 传递任意位置对象,且允许设置诸如 replace: true、name: 'home' 之类的选项以及任何用在 router-link 的 to prop 或 router.push 中的选项。
101
+ * + next(error): (2.4.0+) 如果传入 next 的参数是一个 Error 实例,则导航会被终止且该错误会被传递给 router.onError() 注册过的回调。
102
+ */
103
+ next: Function
104
+ ) => void)
105
+ /** 移除上一个添加的监听 */
106
+ | (() => void)
107
+ ) => void;
108
+
109
+ afterEach: (
110
+ callback:
111
+ | ((
112
+ /** 即将要进入的目标 路由对象 */
113
+ to: Vue2Instance["$route"],
114
+ /** 当前导航正要离开的路由 */
115
+ from: Vue2Instance["$route"]
116
+ ) => void)
117
+ /** 移除上一个添加的监听 */
118
+ | (() => void)
119
+ ) => void;
120
+ push: (...args: any[]) => void;
121
+ back: () => void;
122
+ go: (...args: any[]) => void;
123
+ replace: (...args: any[]) => void;
124
+ addRoute: (...args: any[]) => void;
125
+ addRoutes: (...args: any[]) => void;
126
+ [key: string]: any;
127
+ };
128
+ $ssrContext: any;
129
+
130
+ $watch: (
131
+ key: string | string[] | (() => any),
132
+ handler: (this: any, newVal: any, oldVal: any) => void,
133
+ options?: {
134
+ immediate?: boolean;
135
+ deep?: boolean;
136
+ }
137
+ ) => void;
138
+
139
+ [key: string]: any;
140
+ }
141
+
142
+ /**
143
+ * 包含vue属性的HTMLElement
144
+ */
145
+ export declare interface Vue2HTMLElement extends HTMLElement {
146
+ __vue__: Vue2Instance;
147
+ }
148
+
149
+ /**
150
+ * 包含vue属性的HTMLElement
151
+ */
152
+ export declare interface Vue2HTMLDivElement extends HTMLDivElement {
153
+ __vue__: Vue2Instance;
154
+ }
File without changes
@@ -0,0 +1,10 @@
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
+ };
@@ -1,147 +1,155 @@
1
- import type { HttpxHeaders, HttpxMethod, HttpxStatus } from "./Httpx";
2
- /** 请求的response配置 */
3
- export declare interface UtilsAjaxHookResponseOptions {
4
- /**
5
- * (重定向后的)Url
6
- */
7
- finalUrl: string;
8
- /**
9
- * 响应码
10
- */
11
- status: HttpxStatus;
12
- /**
13
- * 响应头
14
- */
15
- responseHeaders: HttpxHeaders;
16
- /**
17
- * 响应内容
18
- */
19
- response?: string | Blob | ArrayBuffer | XMLDocument | FormData;
20
- /**
21
- * 响应内容文本
22
- */
23
- responseText?: string;
24
- }
25
- /** hook请求的配置 */
26
- export declare interface UtilsAjaxHookRequestOptions {
27
- /**
28
- * 只读属性。一个字符串,表明请求类型是xhr还是fetch
29
- */
30
- type: "xhr";
31
- /**
32
- * 请求的Url
33
- */
34
- url: string;
35
- /**
36
- * 请求的url和method,可以直接修改
37
- */
38
- method: HttpxMethod;
39
- /**
40
- * 是否取消请求,设置为true即可取消本次请求
41
- */
42
- abort: boolean;
43
- /**
44
- * 请求头,可以直接修改
45
- */
46
- headers: HttpxHeaders;
47
- /**
48
- * 请求携带的数据,可以直接修改
49
- */
50
- data?: any;
51
- /**
52
- * 响应内容,必须通过一个回调函数进行读取和修改。
53
- *
54
- * 响应内容为一个对象,包含finalUrl、status、responseHeaders和被读取的响应数据,除响应数据可修改,其他属性是只读的。
55
- *
56
- * 响应数据是哪个属性取决于哪个属性被读取,xhr可能的属性为responseresponseTextresponseXML,fetch可能的属性为arrayBuffer、blob、formData、json、text。
57
- *
58
- * 在控制台输出时,xhr响应将包含所有属性,但只有被读取过的属性具有明确的值。修改对应属性即可影响读取结果,进而实现响应数据的修改。
59
- */
60
- response?: (res: Required<UtilsAjaxHookResponseOptions>) => void;
61
- /**
62
- * 只读属性。异步请求为true,同步请求为false,异步特性无法作用于同步请求
63
- */
64
- async: boolean;
65
- }
66
- /** 过滤规则配置 */
67
- export declare interface UtilsAjaxHookFilterOptions {
68
- /**
69
- * 应是xhr或fetch
70
- */
71
- type?: "xhr" | "fetch";
72
- /**
73
- * 字符串或正则表达式,无需完全匹配
74
- */
75
- url?: string;
76
- /**
77
- * 请求方法
78
- */
79
- method?: HttpxMethod;
80
- /**
81
- * 是否异步
82
- */
83
- async?: boolean;
84
- }
85
- /** Utils.ajaxHooker */
86
- export declare interface UtilsAjaxHookResult {
87
- /**
88
- * 劫持
89
- * @example
90
- ajaxHooker.hook(request => {
91
- if (request.url === 'https://www.example.com/') {
92
- request.response = res => {
93
- console.log(res);
94
- res.responseText += 'test';
95
- };
96
- }
97
- });
98
- * @example
99
- // 异步特性无法作用于同步请求,但同步修改仍然有效
100
- // 你可以将以上所有可修改属性赋值为Promise,原请求将被阻塞直至Promise完成(若发生reject,数据将不会被修改)
101
- // 此特性可用于异步劫持。以下是一个异步修改响应数据的例子
102
- ajaxHooker.hook(request => {
103
- request.response = res => {
104
- const responseText = res.responseText; // 注意保存原数据
105
- res.responseText = new Promise(resolve => {
106
- setTimeout(() => {
107
- resolve(responseText + 'test');
108
- }, 3000);
109
- });
110
- };
111
- });
112
-
113
- // 也可以传入async回调函数以实现异步
114
- ajaxHooker.hook(async request => {
115
- request.data = await modifyData(request.data);
116
- request.response = async res => {
117
- res.responseText = await modifyResponse(res.responseText);
118
- };
119
- });
120
- */
121
- hook(callback: (request: UtilsAjaxHookRequestOptions) => void | Promise<undefined>): void;
122
- /**
123
- * 过滤
124
- * @example
125
- // 应于hook方法之前执行,此方法若尽早执行,有助于提升性能。
126
- // 为hook方法设置过滤规则,只有符合规则的请求才会触发hook。过滤规则是一个对象数组,参考下例
127
- ajaxHooker.filter([
128
- {type: 'xhr', url: 'www.example.com', method: 'GET', async: true},
129
- {url: /^http/},
130
- ]);
131
- */
132
- filter(filterOptions: UtilsAjaxHookFilterOptions[]): void;
133
- /**
134
- * 阻止xhr和fetch被改写
135
- * @example
136
- // 如果库劫持失败,可能是其他代码对xhr/fetch进行了二次劫持,protect方法会尝试阻止xhr和fetch被改写。应于document-start阶段尽早执行,部分网页下可能引发错误,谨慎使用。
137
- ajaxHooker.protect();
138
- */
139
- protect(): void;
140
- /**
141
- * 取消劫持
142
- * @example
143
- // 将xhr和fetch恢复至劫持前的状态,调用此方法后,hook方法不再生效。
144
- ajaxHooker.unhook();
145
- */
146
- unhook(): void;
147
- }
1
+ import type { HttpxHeaders, HttpxMethod, HttpxStatus } from "./Httpx";
2
+
3
+ /** 请求的response配置 */
4
+ export declare interface UtilsAjaxHookResponseOptions {
5
+ /**
6
+ * (重定向后的)Url
7
+ */
8
+ finalUrl: string;
9
+ /**
10
+ * 响应码
11
+ */
12
+ status: HttpxStatus;
13
+ /**
14
+ * 响应头
15
+ */
16
+ responseHeaders: HttpxHeaders;
17
+ /**
18
+ * 响应内容
19
+ */
20
+ response?: string | Blob | ArrayBuffer | XMLDocument | FormData;
21
+ /**
22
+ * 响应内容文本
23
+ */
24
+ responseText?: string;
25
+ }
26
+
27
+ /** hook请求的配置 */
28
+ export declare interface UtilsAjaxHookRequestOptions {
29
+ /**
30
+ * 只读属性。一个字符串,表明请求类型是xhr还是fetch
31
+ */
32
+ type: "xhr";
33
+ /**
34
+ * 请求的Url
35
+ */
36
+ url: string;
37
+ /**
38
+ * 请求的url和method,可以直接修改
39
+ */
40
+ method: HttpxMethod;
41
+ /**
42
+ * 是否取消请求,设置为true即可取消本次请求
43
+ */
44
+ abort: boolean;
45
+ /**
46
+ * 请求头,可以直接修改
47
+ */
48
+ headers: HttpxHeaders;
49
+ /**
50
+ * 请求携带的数据,可以直接修改
51
+ */
52
+ data?: any;
53
+ /**
54
+ * 响应内容,必须通过一个回调函数进行读取和修改。
55
+ *
56
+ * 响应内容为一个对象,包含finalUrlstatusresponseHeaders和被读取的响应数据,除响应数据可修改,其他属性是只读的。
57
+ *
58
+ * 响应数据是哪个属性取决于哪个属性被读取,xhr可能的属性为response、responseText、responseXML,fetch可能的属性为arrayBuffer、blob、formData、json、text。
59
+ *
60
+ * 在控制台输出时,xhr响应将包含所有属性,但只有被读取过的属性具有明确的值。修改对应属性即可影响读取结果,进而实现响应数据的修改。
61
+ */
62
+ response?: (res: Required<UtilsAjaxHookResponseOptions>) => void;
63
+ /**
64
+ * 只读属性。异步请求为true,同步请求为false,异步特性无法作用于同步请求
65
+ */
66
+ async: boolean;
67
+ }
68
+
69
+ /** 过滤规则配置 */
70
+ export declare interface UtilsAjaxHookFilterOptions {
71
+ /**
72
+ * 应是xhr或fetch
73
+ */
74
+ type?: "xhr" | "fetch";
75
+ /**
76
+ * 字符串或正则表达式,无需完全匹配
77
+ */
78
+ url?: string;
79
+ /**
80
+ * 请求方法
81
+ */
82
+ method?: HttpxMethod;
83
+ /**
84
+ * 是否异步
85
+ */
86
+ async?: boolean;
87
+ }
88
+
89
+ /** Utils.ajaxHooker */
90
+ export declare interface UtilsAjaxHookResult {
91
+ /**
92
+ * 劫持
93
+ * @example
94
+ ajaxHooker.hook(request => {
95
+ if (request.url === 'https://www.example.com/') {
96
+ request.response = res => {
97
+ console.log(res);
98
+ res.responseText += 'test';
99
+ };
100
+ }
101
+ });
102
+ * @example
103
+ // 异步特性无法作用于同步请求,但同步修改仍然有效
104
+ // 你可以将以上所有可修改属性赋值为Promise,原请求将被阻塞直至Promise完成(若发生reject,数据将不会被修改)
105
+ // 此特性可用于异步劫持。以下是一个异步修改响应数据的例子
106
+ ajaxHooker.hook(request => {
107
+ request.response = res => {
108
+ const responseText = res.responseText; // 注意保存原数据
109
+ res.responseText = new Promise(resolve => {
110
+ setTimeout(() => {
111
+ resolve(responseText + 'test');
112
+ }, 3000);
113
+ });
114
+ };
115
+ });
116
+
117
+ // 也可以传入async回调函数以实现异步
118
+ ajaxHooker.hook(async request => {
119
+ request.data = await modifyData(request.data);
120
+ request.response = async res => {
121
+ res.responseText = await modifyResponse(res.responseText);
122
+ };
123
+ });
124
+ */
125
+ hook(
126
+ callback: (
127
+ request: UtilsAjaxHookRequestOptions
128
+ ) => void | Promise<undefined>
129
+ ): void;
130
+ /**
131
+ * 过滤
132
+ * @example
133
+ // 应于hook方法之前执行,此方法若尽早执行,有助于提升性能。
134
+ // 为hook方法设置过滤规则,只有符合规则的请求才会触发hook。过滤规则是一个对象数组,参考下例
135
+ ajaxHooker.filter([
136
+ {type: 'xhr', url: 'www.example.com', method: 'GET', async: true},
137
+ {url: /^http/},
138
+ ]);
139
+ */
140
+ filter(filterOptions: UtilsAjaxHookFilterOptions[]): void;
141
+ /**
142
+ * 阻止xhr和fetch被改写
143
+ * @example
144
+ // 如果库劫持失败,可能是其他代码对xhr/fetch进行了二次劫持,protect方法会尝试阻止xhr和fetch被改写。应于document-start阶段尽早执行,部分网页下可能引发错误,谨慎使用。
145
+ ajaxHooker.protect();
146
+ */
147
+ protect(): void;
148
+ /**
149
+ * 取消劫持
150
+ * @example
151
+ // 将xhr和fetch恢复至劫持前的状态,调用此方法后,hook方法不再生效。
152
+ ajaxHooker.unhook();
153
+ */
154
+ unhook(): void;
155
+ }