Skip to content

Commit 3573165

Browse files
committed
feat: add utils for custom functions
1 parent 96425e2 commit 3573165

13 files changed

Lines changed: 371 additions & 75 deletions

File tree

docs/docs/zh-CN/guide/custom-function.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,30 @@ if (detail.type === "media") {
121121
}
122122
```
123123

124+
## 工具函数
125+
126+
自5.3.0起,Header Editor 提供了一些工具函数,用于简化自定义函数编写。
127+
128+
函数列表如下:
129+
* 通过 `this._` 调用部分 [lodash](https://lodash.com/docs/4.17.21) 函数:clone, cloneDeep, cloneDeepWith, cloneWith, difference, differenceBy, differenceWith, eq, first, flatten, get, has, head, isEqual, isEqualWith, last, pick, pickBy, random, set, setWith, uniq, uniqBy, uniqWith
130+
* 通过 `this.task` 获取任务相关内容。请参考[任务](./task.md)
131+
* 注意:storage 仅在任务的自定义函数中可用,规则中无 `this.sessionStorage``this.localStorage`
132+
133+
相关函数定义如下:
134+
```ts
135+
declare const this: {
136+
_: { /* lodash */ },
137+
task: {
138+
// 获取任务信息
139+
get: (key: string) => Promise<Task | null>,
140+
// 获取任务上一次运行结果
141+
getLastRun: (key: string) => Promise<TaskRun | undefined>,
142+
// 获取任务上一次成功运行的结果
143+
getValidRun: (key: string) => Promise<TaskRun | undefined>,
144+
},
145+
}
146+
```
147+
124148
## 如何调试自定义函数
125149
126150
所有自定义函数的运行均位于后台页面,因此,要调试自定义函数,请打开后台页面的控制台

docs/docs/zh-CN/guide/task.md

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,98 @@ return res.json();
7373
* 可以在规则的 重定向至、请求/响应头内容、响应体中使用此语法。
7474
* 如果响应类型配置为“文本”,则任何 `{$TASK.task1.*}` 均会获取到完整的响应文本。
7575
* 当请求失败或尚未完成请求时,该语法会被替换为空字符串。
76+
77+
## 工具函数
78+
79+
在任务的自定义函数中,Header Editor 提供了以下工具函数:
80+
81+
函数列表如下:
82+
* 通过 `this._` 调用部分 [lodash](https://lodash.com/docs/4.17.21) 函数:clone, cloneDeep, cloneDeepWith, cloneWith, difference, differenceBy, differenceWith, eq, first, flatten, get, has, head, isEqual, isEqualWith, last, pick, pickBy, random, set, setWith, uniq, uniqBy, uniqWith
83+
* 通过 `this.task` 获取任务相关内容。
84+
* `this.task.get`: 获取任务信息。
85+
* `this.task.getLastRun`: 获取任务上一次运行结果。
86+
* `this.task.getValidRun`: 获取任务上一次成功运行的结果。
87+
* 通过 `this.sessionStorage``this.localStorage` 进行数据存取。其中,`localStorage` 为持久化存储,`sessionStorage` 为会话级存储(浏览器关闭时清空)。
88+
89+
相关函数定义如下:
90+
```ts
91+
declare const this: {
92+
_: { /* lodash */ },
93+
task: {
94+
// 获取任务信息
95+
get: (key: string) => Promise<Task | null>,
96+
// 获取任务上一次运行结果
97+
getLastRun: (key: string) => Promise<TaskRun | undefined>,
98+
// 获取任务上一次成功运行的结果
99+
getValidRun: (key: string) => Promise<TaskRun | undefined>,
100+
},
101+
sessionStorage: Storage,
102+
localStorage: Storage,
103+
}
104+
105+
declare interface Storage {
106+
get: (key: string) => Promise<any>,
107+
set: (key: string, value: any) => Promise<void>,
108+
remove: (key: string) => Promise<void>,
109+
has: (key: string) => Promise<boolean>,
110+
}
111+
```
112+
113+
## Task 相关类型定义
114+
115+
Task 相关类型定义如下:
116+
```ts
117+
// 任务运行记录
118+
interface TaskRun {
119+
// 任务 Key
120+
key: string;
121+
// 开始运行的时间
122+
time: number;
123+
// 运行状态
124+
status: 'running' | 'done' | 'error';
125+
// 错误信息
126+
error?: string;
127+
// 运行结果
128+
result?: any;
129+
}
130+
131+
// 任务信息
132+
interface Task {
133+
// 任务 Key
134+
key: string;
135+
// 任务名称
136+
name: string;
137+
// 运行类型
138+
execute: 'once' | 'interval' | 'cron';
139+
// Cron 表达式
140+
cron?: string;
141+
// 时间间隔(分)
142+
interval?: number;
143+
// 是否是函数
144+
isFunction: boolean;
145+
// 重试设置
146+
retry?: {
147+
// 最大重试次数
148+
max: number;
149+
// 重试等待时间(秒)
150+
wait: number;
151+
};
152+
// Fetch 设置
153+
fetch?: {
154+
// 请求 URL
155+
url: string;
156+
// 请求方法
157+
method: string;
158+
// 请求头
159+
headers?: Record<string, string>;
160+
// 请求体
161+
body?: string;
162+
// 响应类型
163+
responseType?: 'json' | 'text';
164+
// 验证器
165+
validator?: RulesLogic;
166+
};
167+
// 自定义函数代码
168+
code?: string;
169+
}
170+
```

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
"@types/node": "^18.15.5",
5858
"@types/react": "^18.3.1",
5959
"@types/react-dom": "^18.3.1",
60-
"@types/webextension-polyfill": "^0.12.3",
60+
"@types/webextension-polyfill": "^0.12.4",
6161
"amo-upload": "^1.0.0",
6262
"axios": "^1.7.2",
6363
"cpr": "^3.0.1",

pnpm-lock.yaml

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import type { Storage } from 'webextension-polyfill';
2+
import { getLastTaskRun, getTask, getValidTaskRun } from '../task';
3+
import { lodash } from './lodash';
4+
5+
export const basicHelper = {
6+
_: lodash,
7+
task: {
8+
get: getTask,
9+
getLastRun: getLastTaskRun,
10+
getValidRun: getValidTaskRun,
11+
},
12+
};
13+
14+
export const createStorage = (name: string, storage: Storage.StorageArea) => {
15+
const getKey = (key: string) => `f#${name}#${key}`;
16+
17+
return {
18+
async get(key: string) {
19+
const k = getKey(key);
20+
const data = await storage.get(k);
21+
return data[k];
22+
},
23+
set(key: string, value: any) {
24+
return storage.set({ [getKey(key)]: value });
25+
},
26+
remove(key: string) {
27+
return storage.remove(getKey(key));
28+
},
29+
async has(key: string) {
30+
const k = getKey(key);
31+
const data = await storage.get(k);
32+
return k in data;
33+
},
34+
};
35+
};
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import {
2+
clone,
3+
cloneDeep,
4+
cloneDeepWith,
5+
cloneWith,
6+
difference,
7+
differenceBy,
8+
differenceWith,
9+
eq,
10+
first,
11+
flatten,
12+
get,
13+
has,
14+
head,
15+
isEqual,
16+
isEqualWith,
17+
last,
18+
pick,
19+
pickBy,
20+
random,
21+
set,
22+
setWith,
23+
uniq,
24+
uniqBy,
25+
uniqWith,
26+
} from 'lodash-es';
27+
28+
export const lodash = {
29+
difference,
30+
differenceBy,
31+
differenceWith,
32+
first,
33+
head,
34+
last,
35+
flatten,
36+
uniq,
37+
uniqBy,
38+
uniqWith,
39+
clone,
40+
cloneWith,
41+
cloneDeep,
42+
cloneDeepWith,
43+
eq,
44+
isEqual,
45+
isEqualWith,
46+
random,
47+
get,
48+
has,
49+
set,
50+
setWith,
51+
pick,
52+
pickBy,
53+
};

0 commit comments

Comments
 (0)