Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions src/pages/options/routes/ScriptEditor/tabs/SettingsPane.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,32 @@ describe("SettingsPane 网站匹配/排除", () => {
fireEvent.click(screen.getByText(t("confirm"), { selector: "button" }));
expect(resetMatch).toHaveBeenCalledWith("u1", undefined);
});

it("重置匹配后应恢复脚本自带规则而非清空列表", async () => {
render(<SettingsPane uuid="u1" />);
await screen.findByText("*://user.com/*");
fireEvent.click(screen.getAllByText(t("reset"), { selector: "button" })[0]);
fireEvent.click(screen.getByText(t("confirm"), { selector: "button" }));
// 用户添加的 user.com 被清除,脚本自带的 script.com 应保留并标记为「脚本」
expect(screen.getByText("*://script.com/*")).toBeInTheDocument();
expect(screen.queryByText("*://user.com/*")).toBeNull();
expect(screen.getByText(t("editor:from_script"))).toBeInTheDocument();
});

it("重置排除后应恢复脚本自带规则而非清空列表", async () => {
fetchScript.mockResolvedValue({
...sampleScript(),
metadata: { ...sampleScript().metadata, exclude: ["*://ads.script.com/*"] },
selfMetadata: { ...sampleScript().selfMetadata, exclude: ["*://ads.script.com/*", "*://exclude.com/*"] },
});
render(<SettingsPane uuid="u1" />);
await screen.findByText("*://exclude.com/*");
fireEvent.click(screen.getAllByText(t("reset"), { selector: "button" })[1]);
fireEvent.click(screen.getByText(t("confirm"), { selector: "button" }));
// 用户添加的 exclude.com 被清除,脚本自带的 ads.script.com 应保留
expect(screen.getByText("*://ads.script.com/*")).toBeInTheDocument();
expect(screen.queryByText("*://exclude.com/*")).toBeNull();
});
});

describe("SettingsPane 授权管理(CORS)", () => {
Expand Down
17 changes: 14 additions & 3 deletions src/pages/options/routes/ScriptEditor/tabs/SettingsPane.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -234,14 +234,25 @@ function SettingsPaneContent({ uuid, data }: SettingsPaneProps & { data: Setting

// ===== 匹配 / 排除 =====
const setMatchList = (kind: "match" | "exclude", next: string[] | undefined) => {
// next 为 undefined 表示重置:删除用户覆盖,生效列表回落到脚本自带的 metadata(与后端 selfMetadataUpdate 一致)
const metaList = kind === "match" ? metaMatch : metaExclude;
if (kind === "match") {
void scriptClient.resetMatch(uuid, next);
setMatches(next ?? []);
setMatches(next ?? metaList);
} else {
void scriptClient.resetExclude(uuid, next);
setExcludes(next ?? []);
setExcludes(next ?? metaList);
}
patchSelf({ [kind]: next ?? [] });
setScript((prev) => {
if (!prev) return prev;
const selfMetadata = { ...prev.selfMetadata };
if (next === undefined) {
delete selfMetadata[kind];
} else {
selfMetadata[kind] = next;
}
return { ...prev, selfMetadata };
});
};
const openBulkMatch = (kind: "match" | "exclude") => {
setBulkMatchValue("");
Expand Down
Loading