Skip to content

Commit 9d31872

Browse files
committed
🐛 修复脚本设置网站匹配更新不断累积include的问题 #581
1 parent 3677278 commit 9d31872

2 files changed

Lines changed: 29 additions & 12 deletions

File tree

src/app/service/service_worker/runtime.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -714,7 +714,8 @@ export class RuntimeService {
714714
// 构建userScript注册信息
715715
async getAndSetUserScriptRegister(script: Script) {
716716
const scriptRes = await this.script.buildScriptRunResource(script);
717-
const matches = scriptRes.metadata["match"] || [];
717+
// concat 浅拷贝是为了避免修改原数组
718+
const matches = (scriptRes.metadata["match"] || []).concat();
718719
matches.push(...(scriptRes.metadata["include"] || []));
719720
if (!matches.length) {
720721
return undefined;
@@ -739,7 +740,8 @@ export class RuntimeService {
739740

740741
// 排除由loadPage时决定, 不使用userScript的excludeMatches处理
741742
if (script.metadata["exclude"]) {
742-
const excludeMatches = script.metadata["exclude"];
743+
// concat 浅拷贝是为了避免修改原数组
744+
const excludeMatches = script.metadata["exclude"].concat();
743745
const result = dealPatternMatches(excludeMatches, {
744746
exclude: true,
745747
});

src/pages/components/ScriptSetting/Match.tsx

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,18 @@ const Match: React.FC<{
2929
const [excludeVisible, setExcludeVisible] = useState<boolean>(false);
3030
const { t } = useTranslation(); // 使用 react-i18next 的 useTranslation 钩子函数获取翻译函数
3131

32-
useEffect(() => {
32+
// 自定义的状态更新函数,会在更新后自动刷新数据
33+
const updateMatchAndRefresh = (newMatch: MatchItem[]) => {
34+
setMatch(newMatch);
35+
refreshMatch();
36+
};
37+
38+
const updateExcludeAndRefresh = (newExclude: MatchItem[]) => {
39+
setExclude(newExclude);
40+
refreshMatch();
41+
};
42+
43+
const refreshMatch = () => {
3344
if (script) {
3445
// 从数据库中获取是简单处理数据一致性的问题
3546
scriptDAO.get(script.uuid).then((res) => {
@@ -72,7 +83,11 @@ const Match: React.FC<{
7283
setExclude(e);
7384
});
7485
}
75-
}, [script, exclude, match]);
86+
};
87+
88+
useEffect(() => {
89+
refreshMatch();
90+
}, [script]);
7691

7792
const columns: ColumnProps[] = [
7893
{
@@ -109,7 +124,7 @@ const Match: React.FC<{
109124
exclude.map((m) => m.match)
110125
)
111126
.then(() => {
112-
setExclude([...exclude]);
127+
updateExcludeAndRefresh([...exclude]);
113128
// 如果包含在里面,再加回match
114129
if (item.hasMatch) {
115130
match.push(item);
@@ -120,7 +135,7 @@ const Match: React.FC<{
120135
match.map((m) => m.match)
121136
)
122137
.then(() => {
123-
setMatch([...match]);
138+
updateMatchAndRefresh([...match]);
124139
});
125140
}
126141
});
@@ -143,7 +158,7 @@ const Match: React.FC<{
143158
match.map((m) => m.match)
144159
)
145160
.then(() => {
146-
setMatch([...match]);
161+
updateMatchAndRefresh([...match]);
147162
// 添加到exclue
148163
if (!item.byUser) {
149164
exclude.push(item);
@@ -153,7 +168,7 @@ const Match: React.FC<{
153168
exclude.map((m) => m.match)
154169
)
155170
.then(() => {
156-
setExclude([...exclude]);
171+
updateExcludeAndRefresh([...exclude]);
157172
});
158173
}
159174
});
@@ -188,7 +203,7 @@ const Match: React.FC<{
188203
match.map((m) => m.match)
189204
)
190205
.then(() => {
191-
setMatch([...match]);
206+
updateMatchAndRefresh([...match]);
192207
setMatchVisible(false);
193208
});
194209
}
@@ -220,7 +235,7 @@ const Match: React.FC<{
220235
exclude.map((m) => m.match)
221236
)
222237
.then(() => {
223-
setExclude([...exclude]);
238+
updateExcludeAndRefresh([...exclude]);
224239
setExcludeVisible(false);
225240
});
226241
}
@@ -250,7 +265,7 @@ const Match: React.FC<{
250265
title={t("confirm_reset")}
251266
onOk={() => {
252267
scriptClient.resetMatch(script.uuid, undefined).then(() => {
253-
setMatch([]);
268+
updateMatchAndRefresh([]);
254269
});
255270
}}
256271
>
@@ -279,7 +294,7 @@ const Match: React.FC<{
279294
title={t("confirm_reset")}
280295
onOk={() => {
281296
scriptClient.resetExclude(script.uuid, undefined).then(() => {
282-
setExclude([]);
297+
updateExcludeAndRefresh([]);
283298
});
284299
}}
285300
>

0 commit comments

Comments
 (0)