Skip to content

Commit 949a69d

Browse files
committed
fix: allow using regex #339
1 parent f3d9772 commit 949a69d

3 files changed

Lines changed: 28 additions & 8 deletions

File tree

src/pages/options/sections/rules/edit/form-content/test.tsx

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,18 @@ const Test = () => {
3030
try {
3131
rule.current = initRule(ruleContent, true);
3232
if (ruleContent.condition?.regex) {
33-
// check re2 syntax
34-
rule.current._re2 = RE2JS.compile(ruleContent.condition.regex);
33+
try {
34+
rule.current._re2 = RE2JS.compile(ruleContent.condition.regex);
35+
} catch (e) {
36+
if (ENABLE_WEB_REQUEST) {
37+
new RegExp(ruleContent.condition.regex);
38+
rule.current.forceRunner = 'web_request';
39+
} else {
40+
throw e;
41+
}
42+
}
3543
}
3644
} catch (e) {
37-
// 出错
3845
setResult((e as Error).message);
3946
return;
4047
}

src/pages/options/sections/rules/edit/index.tsx

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@ import type { FormApi } from '@douyinfe/semi-ui/lib/es/form';
44
import { css } from '@emotion/css';
55
import { useRequest } from 'ahooks';
66
import { RE2JS } from 're2js';
7-
import React, { useEffect, useMemo, useRef } from 'react';
7+
import { useEffect, useMemo, useRef } from 'react';
88
import { RULE_TYPE } from '@/share/core/constant';
99
import { prefs } from '@/share/core/prefs';
10-
import { detectRunner } from '@/share/core/rule-utils';
1110
import type { Rule } from '@/share/core/types';
1211
import { t } from '@/share/core/utils';
1312
import Api from '@/share/pages/api';
@@ -51,7 +50,16 @@ const Edit = ({ visible, rule: ruleProp, onClose }: EditProps) => {
5150
throw new Error(t('match_rule_empty'));
5251
}
5352
if (rule.condition.regex) {
54-
RE2JS.compile(rule.condition.regex);
53+
try {
54+
RE2JS.compile(rule.condition.regex);
55+
} catch (e) {
56+
if (ENABLE_WEB_REQUEST) {
57+
new RegExp(rule.condition.regex);
58+
rule.forceRunner = 'web_request';
59+
} else {
60+
throw e;
61+
}
62+
}
5563
}
5664
if (
5765
[
@@ -62,7 +70,11 @@ const Edit = ({ visible, rule: ruleProp, onClose }: EditProps) => {
6270
'domain',
6371
'regex',
6472
'resourceTypes',
65-
].every(x => typeof rule.condition![x] === 'undefined')
73+
].every(
74+
x =>
75+
typeof rule.condition![x as keyof Rule['condition']] ===
76+
'undefined',
77+
)
6678
) {
6779
throw new Error(t('match_rule_empty'));
6880
}

tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
"baseUrl": "./",
88
"paths": {
99
"@/*": ["./src/*"]
10-
}
10+
},
11+
"types": ["chrome"]
1112
},
1213
"include": ["src", "scripts", "rsbuild.config.ts"],
1314
"exclude": ["**/node_modules"]

0 commit comments

Comments
 (0)