Skip to content

Commit a509c2a

Browse files
committed
feat: import tasks
1 parent fb3500c commit a509c2a

File tree

13 files changed

+320
-130
lines changed

13 files changed

+320
-130
lines changed

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,9 @@ if (detail.type === "media") {
127127

128128
函数列表如下:
129129
* 通过 `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.nanoid` 生成随机字符串。
130131
* 通过 `this.task` 获取任务相关内容。请参考[任务](./task.md)
132+
* 注意:不建议解构后使用 Task 相关函数,可能影响导入/导出功能。
131133
* 注意:storage 仅在任务的自定义函数中可用,规则中无 `this.sessionStorage``this.localStorage`
132134

133135
相关函数定义如下:
@@ -145,6 +147,14 @@ declare const this: {
145147
}
146148
```
147149
150+
例如:
151+
```ts
152+
if (detail.type === "media") {
153+
const run = this.task.getValidRun("exampleTask");
154+
return val.replace("example.com", run ? run.result.url : "example.org");
155+
}
156+
```
157+
148158
## 如何调试自定义函数
149159

150160
所有自定义函数的运行均位于后台页面,因此,要调试自定义函数,请打开后台页面的控制台

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,12 @@ return res.json();
8080

8181
函数列表如下:
8282
* 通过 `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.nanoid` 生成随机字符串。
8384
* 通过 `this.task` 获取任务相关内容。
8485
* `this.task.get`: 获取任务信息。
8586
* `this.task.getLastRun`: 获取任务上一次运行结果。
8687
* `this.task.getValidRun`: 获取任务上一次成功运行的结果。
88+
* 注意:不建议解构后使用 Task 相关函数,可能影响导入/导出功能。
8789
* 通过 `this.sessionStorage``this.localStorage` 进行数据存取。其中,`localStorage` 为持久化存储,`sessionStorage` 为会话级存储(浏览器关闭时清空)。
8890

8991
相关函数定义如下:

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
"fast-deep-equal": "^2.0.1",
3737
"json-logic-js": "^2.0.5",
3838
"lodash-es": "^4.17.21",
39+
"nanoid": "^5.1.6",
3940
"query-string": "^8.1.0",
4041
"re2js": "^1.1.0",
4142
"react": "^18.3.1",

pnpm-lock.yaml

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/pages/background/core/function-helper/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
import { nanoid } from 'nanoid';
12
import type { Storage } from 'webextension-polyfill';
23
import { getLastTaskRun, getTask, getValidTaskRun } from '../task';
34
import { lodash } from './lodash';
45

56
export const basicHelper = {
67
_: lodash,
8+
nanoid,
79
task: {
810
get: getTask,
911
getLastRun: getLastTaskRun,

src/pages/background/request-handler/dnr-handler/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { prefs } from '@/share/core/prefs';
77
import { detectRunner } from '@/share/core/rule-utils';
88
import SessionMessage from '@/share/core/session-message';
99
import { getSession, readStorage } from '@/share/core/storage';
10-
import { getRuleUsedTasks } from '@/share/core/tasks';
10+
import { collectRuleUsedTasks } from '@/share/core/tasks';
1111
import type { Rule, Task } from '@/share/core/types';
1212
import {
1313
getVirtualKey,
@@ -62,7 +62,7 @@ class DNRRequestHandler {
6262

6363
private writeDependency(rule: Rule) {
6464
const rk = getVirtualKey(rule);
65-
const dep = getRuleUsedTasks(rule);
65+
const dep = collectRuleUsedTasks(rule);
6666
let shouldSave = false;
6767

6868
for (const key in this.taskDependencies) {

src/pages/options/sections/group-select/index.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Input, Modal, Select } from '@douyinfe/semi-ui';
2+
import { nanoid } from 'nanoid';
23
import * as React from 'react';
34
import emitter from '@/share/core/emitter';
45
import { t } from '@/share/core/utils';
@@ -14,7 +15,7 @@ export default class GroupSelect extends React.Component<
1415
any,
1516
GroupSelectState
1617
> {
17-
private newValue = `_new_${Math.random().toString()}`;
18+
private newValue = `_new_${nanoid()}`;
1819

1920
constructor(props: any) {
2021
super(props);

0 commit comments

Comments
 (0)