Skip to content

Commit 27c4551

Browse files
committed
docs: add task
1 parent a509c2a commit 27c4551

File tree

3 files changed

+208
-0
lines changed

3 files changed

+208
-0
lines changed

docs/docs/en-US/guide/_meta.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"type": "divider",
1313
"dashed": true
1414
},
15+
"task",
1516
"custom-function",
1617
"modify-body",
1718
{

docs/docs/en-US/guide/custom-function.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,40 @@ if (detail.type === "media") {
123123
}
124124
```
125125

126+
## Utility Functions
127+
128+
Starting from version 5.3.0, the Header Editor provides several utility functions to simplify the writing of custom functions.
129+
130+
The function list is as follows:
131+
132+
* Calling some [lodash](https://lodash.com/docs/4.17.21) functions via `this._`: clone, cloneDeep, cloneDeepWith, cloneWith, difference, differenceBy, differenceWith, eq, first, flatten, get, has, head, isEqual, isEqualWith, last, pick, pickBy, random, set, setWith, uniq, uniqBy, uniqWith
133+
* Generating a random string via `this.nanoid`.
134+
* Retrieving task-related content via `this.task`. Please refer to [Task](./task.md)
135+
* Note: It is not recommended to use Task-related functions after destructuring, as it may affect import/export functionality.
136+
* Note: `storage` is only available in custom functions of a task; `this.sessionStorage` and `this.localStorage` are not specified in the rules.
137+
138+
The relevant function definitions are as follows:
139+
```ts
140+
declare const this: {
141+
_: { /* lodash */ },
142+
task: {
143+
// Get task information
144+
get: (key: string) => Promise<Task | null>,
145+
// Get the result of the last task execution
146+
getLastRun: (key: string) => Promise<TaskRun | undefined>,
147+
// Get the result of the last successful task execution
148+
getValidRun: (key: string) => Promise<TaskRun | undefined>,
149+
},
150+
}
151+
```
152+
153+
For example:
154+
```ts
155+
if (detail.type === "media") {
156+
const run = this.task.getValidRun("exampleTask");
157+
return val.replace("example.com", run ? run.result.url : "example.org");
158+
}
159+
``
126160
## How to debug a custom function
127161

128162
All custom functions are run in the background page, so to debug custom functions, open the console of the background page

docs/docs/en-US/guide/task.md

Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
---
2+
title: Tasks
3+
---
4+
5+
## What is a Task?
6+
7+
This feature allows you to silently run code, initiate requests, and use the responses in rules, according to your configuration. For example, you can use this feature to periodically refresh a token and include it in the header.
8+
9+
## Configuration Guide
10+
11+
### Task Key
12+
13+
The task key is a unique identifier for the task. It must begin with a letter and can only contain letters, numbers, and underscores. The key cannot be modified after it is set.
14+
15+
### Execution Type
16+
17+
You can choose when to run the task:
18+
19+
* Run only once: Runs only once when the browser starts.
20+
* Fixed interval operation: Runs once when the browser starts and then again at fixed intervals.
21+
* Scheduled operation: Configure a Cron expression to run once when the browser starts and then repeat at specified intervals.
22+
23+
You can refer to [this document](https://crontab.cronhub.io/) to learn about Cron expressions.
24+
25+
### Execution Type
26+
27+
In the standard approach, you need to configure a request address and request parameters. The Header Editor will periodically send requests to this address.
28+
29+
The response type can be used for subsequent processing.
30+
31+
You can use [JsonLogic](https://jsonlogic.com/) to validate the results. Use `status` to get the HTTP status code of the response and `body` to get the response body. For example:
32+
33+
```js
34+
// Response result
35+
{
36+
"code": 200,
37+
}
38+
39+
// JsonLogic
40+
{
41+
"==": [{ "var" : "body.code" }, 200]
42+
}
43+
```
44+
45+
You can also use a custom function. A custom function does not take any parameters; it handles the request and validation logic within the function and returns the result. For example:
46+
47+
```js
48+
const res = await fetch('https://example.com');
49+
if (res.status !== 200) {
50+
throw new Error('Request failed');
51+
}
52+
return res.json();
53+
```
54+
55+
## Using in Rules
56+
57+
In rules, use `{$TASK.TASK_KEY.Path}` to get the response returned by the task.
58+
59+
For example, you have a task with the key `task1` that returned the following result:
60+
61+
```json
62+
{
63+
"code": 200,
64+
"data": {
65+
"token": "123456"
66+
}
67+
}
68+
```
69+
70+
In rules, you can use `{$TASK.task1.data.token}` to get the token returned by the task.
71+
72+
Note:
73+
* This syntax can be used in the redirect to, request/response header content, and response body of the rule.
74+
* If the response type is configured as "text", any `{$TASK.task1.*}` will retrieve the complete response text, but `{$TASK.task1}` cannot be used directly.
75+
* This syntax will be replaced with an empty string when the request fails or is not yet complete.
76+
77+
## Utility Functions
78+
79+
The Header Editor provides the following utility functions in the task's custom functions:
80+
81+
The function list is as follows:
82+
* Calling some [lodash](https://lodash.com/docs/4.17.21) functions via `this._`: 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+
* Generating a random string via `this.nanoid`.
84+
* Retrieve task-related content using `this.task`.
85+
* `this.task.get`: Retrieve task information.
86+
* `this.task.getLastRun`: Retrieve the last run result of the task.
87+
* `this.task.getValidRun`: Retrieve the last successfully run result of the task.
88+
* Note: It is not recommended to use Task-related functions after destructuring, as it may affect import/export functionality.
89+
* Store and retrieve data using `this.sessionStorage` or `this.localStorage`. `localStorage` is persistent storage, while `sessionStorage` is session-level storage (cleared when the browser is closed).
90+
91+
The relevant function definitions are as follows:
92+
```ts
93+
declare const this: {
94+
_: { /* lodash */ },
95+
task: {
96+
// Get task information
97+
get: (key: string) => Promise<Task | null>,
98+
// Get the result of the last task execution
99+
getLastRun: (key: string) => Promise<TaskRun | undefined>,
100+
// Get the result of the last successful task execution
101+
getValidRun: (key: string) => Promise<TaskRun | undefined>,
102+
},
103+
sessionStorage: Storage,
104+
localStorage: Storage,
105+
}
106+
107+
declare interface Storage {
108+
get: (key: string) => Promise<any>,
109+
set: (key: string, value: any) => Promise<void>,
110+
remove: (key: string) => Promise<void>,
111+
has: (key: string) => Promise<boolean>,
112+
}
113+
```
114+
115+
## Task Related Type Definitions
116+
117+
Task related type definitions are as follows:
118+
119+
```ts
120+
// Task execution record
121+
interface TaskRun {
122+
// Task Key
123+
key: string;
124+
// Start time
125+
time: number;
126+
// Running status
127+
status: 'running' | 'done' | 'error';
128+
// Error message
129+
error?: string;
130+
// Execution result
131+
result?: any;
132+
}
133+
134+
// Task information
135+
interface Task {
136+
// Task Key
137+
key: string;
138+
// Task name
139+
name: string;
140+
// Execution type
141+
execute: 'once' | 'interval' | 'cron';
142+
// Cron expression
143+
cron?: string;
144+
// Time interval (minutes)
145+
interval?: number;
146+
// Whether it is a function
147+
isFunction: boolean;
148+
// Retry settings
149+
retry?: {
150+
// Maximum number of retries
151+
max: number;
152+
// Retry wait time (seconds)
153+
wait: number;
154+
};
155+
// Fetch settings
156+
fetch?: {
157+
// Request URL
158+
url: string;
159+
// Request method
160+
method: string;
161+
// Request headers
162+
headers?: Record<string, string>;
163+
// Request body
164+
body?: string;
165+
// Response type
166+
responseType?: 'json' | 'text';
167+
// Validator
168+
validator?: RulesLogic;
169+
};
170+
// Custom function code
171+
code?: string;
172+
}
173+
```

0 commit comments

Comments
 (0)