From ed2c0323f56c85cfc8a359183f828b3142cbd625 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E4=B8=80=E4=B9=8B?= Date: Tue, 10 Nov 2026 17:06:49 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=F0=9F=92=84=20=E4=BC=98=E5=8C=96=E6=9B=B4?= =?UTF-8?q?=E6=96=B0=E9=A1=B5=E6=9D=83=E9=99=90=E5=8F=98=E5=8C=96=E7=9A=84?= =?UTF-8?q?=E5=B1=95=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 地址类取值(@match/@require/@resource)解码后单行展示:域名突出,中段路径优先省略, 文件名与参数保留,悬停看原文、点击展开完整地址;解码出不可见/双向控制字符时退回原文 - @require/@resource 只换了查询参数(如 ?v=版本号)且能一一对应时合并为一条「更新」, 不再每次发版都冒出一对增删 - 普通新增用新增色描边与记号;危险/敏感项的新增继续使用风险色,不被新增色盖掉; 移除改为虚线框 + 删除线 - 新增、移除各最多显示 8 条,其余收进「再显示 N 项」;危险、敏感、不生效取值永不收起 - 更新态行头只给变动数,去掉与「未变动 N 项」重复的总数徽章;卡头写「新增 N · 移除 M」 与「旧版本 → 新版本」,移动端版本号另起一行 --- src/locales/de-DE/install.json | 8 +- src/locales/en-US/install.json | 8 +- src/locales/ja-JP/install.json | 8 +- src/locales/ko-KR/install.json | 8 +- src/locales/pt-BR/install.json | 8 +- src/locales/ru-RU/install.json | 8 +- src/locales/tr-TR/install.json | 8 +- src/locales/vi-VN/install.json | 8 +- src/locales/zh-CN/install.json | 8 +- src/locales/zh-TW/install.json | 8 +- src/pages/install/App.test.tsx | 12 +- src/pages/install/App.tsx | 1 + .../components/PermissionCard.test.tsx | 74 ++++++-- .../install/components/PermissionCard.tsx | 76 ++++++-- .../install/components/PermissionRow.test.tsx | 178 +++++++++++++++++- .../install/components/PermissionRow.tsx | 174 +++++++++++++++-- src/pages/install/permissions.test.ts | 56 +++++- src/pages/install/permissions.ts | 44 ++++- src/pages/install/url_display.test.ts | 75 ++++++++ src/pages/install/url_display.ts | 40 ++++ src/pages/install/useInstallData.test.ts | 2 +- 21 files changed, 735 insertions(+), 77 deletions(-) create mode 100644 src/pages/install/url_display.test.ts create mode 100644 src/pages/install/url_display.ts diff --git a/src/locales/de-DE/install.json b/src/locales/de-DE/install.json index da6ed50e7..1f88225d5 100644 --- a/src/locales/de-DE/install.json +++ b/src/locales/de-DE/install.json @@ -14,12 +14,18 @@ "perm_card_hint": "Bitte vor der Installation bestätigen", "perm_card_empty": "Dieses Skript fordert keine besonderen Berechtigungen an", "perm_card_title_changed": "Berechtigungsänderungen", - "perm_card_compare": "Verglichen mit installierter Version {{version}}", + "perm_card_version_range": "Aktualisierung von installierter Version {{from}} auf {{to}}", + "perm_delta_added": "{{count}} hinzugefügt", + "perm_delta_removed": "{{count}} entfernt", + "perm_delta_updated": "{{count}} aktualisiert", "perm_no_change": "Keine Änderung", "perm_change_added": "Hinzugefügt", "perm_change_removed": "Entfernt", "perm_change_unchanged": "Unverändert", + "perm_change_updated": "Aktualisiert", "perm_unchanged_more": "{{count}} unverändert", + "perm_added_more": "{{count}} weitere hinzugefügte anzeigen", + "perm_removed_more": "{{count}} weitere entfernte anzeigen", "perm_card_title_short": "Berechtigungen", "perm_card_same_as": "Identisch mit {{version}}", "perm_card_summary": "{{kinds}} Kategorien · {{count}} Einträge", diff --git a/src/locales/en-US/install.json b/src/locales/en-US/install.json index b964a0e73..22c36d289 100644 --- a/src/locales/en-US/install.json +++ b/src/locales/en-US/install.json @@ -14,12 +14,18 @@ "perm_card_hint": "Please confirm before installing", "perm_card_empty": "This script does not request any special permissions", "perm_card_title_changed": "Permission changes", - "perm_card_compare": "Compared with installed {{version}}", + "perm_card_version_range": "Updating from installed {{from}} to {{to}}", + "perm_delta_added": "{{count}} added", + "perm_delta_removed": "{{count}} removed", + "perm_delta_updated": "{{count}} updated", "perm_no_change": "No change", "perm_change_added": "Added", "perm_change_removed": "Removed", "perm_change_unchanged": "Unchanged", + "perm_change_updated": "Updated", "perm_unchanged_more": "{{count}} unchanged", + "perm_added_more": "Show {{count}} more added", + "perm_removed_more": "Show {{count}} more removed", "perm_card_title_short": "Permissions", "perm_card_same_as": "Same as {{version}}", "perm_card_summary": "{{kinds}} categories · {{count}} items", diff --git a/src/locales/ja-JP/install.json b/src/locales/ja-JP/install.json index fe238ecff..bae988729 100644 --- a/src/locales/ja-JP/install.json +++ b/src/locales/ja-JP/install.json @@ -14,12 +14,18 @@ "perm_card_hint": "インストール前に確認してください", "perm_card_empty": "このスクリプトは特別な権限を要求しません", "perm_card_title_changed": "権限の変更", - "perm_card_compare": "インストール済み {{version}} との比較", + "perm_card_version_range": "インストール済み {{from}} から {{to}} への更新", + "perm_delta_added": "追加 {{count}}", + "perm_delta_removed": "削除 {{count}}", + "perm_delta_updated": "更新 {{count}}", "perm_no_change": "変更なし", "perm_change_added": "追加", "perm_change_removed": "削除", "perm_change_unchanged": "変更なし", + "perm_change_updated": "更新", "perm_unchanged_more": "変更なし {{count}} 件", + "perm_added_more": "追加をあと {{count}} 件表示", + "perm_removed_more": "削除をあと {{count}} 件表示", "perm_card_title_short": "権限", "perm_card_same_as": "{{version}} と同じ", "perm_card_summary": "{{kinds}} カテゴリ · {{count}} 件", diff --git a/src/locales/ko-KR/install.json b/src/locales/ko-KR/install.json index da8c9b4c9..0d8fa40cf 100644 --- a/src/locales/ko-KR/install.json +++ b/src/locales/ko-KR/install.json @@ -14,12 +14,18 @@ "perm_card_hint": "설치하기 전에 확인해 주세요", "perm_card_empty": "이 스크립트는 특별한 권한을 요청하지 않습니다", "perm_card_title_changed": "권한 변경", - "perm_card_compare": "설치된 {{version}}과 비교", + "perm_card_version_range": "설치된 {{from}}에서 {{to}}(으)로 업데이트", + "perm_delta_added": "추가 {{count}}", + "perm_delta_removed": "제거 {{count}}", + "perm_delta_updated": "업데이트 {{count}}", "perm_no_change": "변경 없음", "perm_change_added": "추가됨", "perm_change_removed": "제거됨", "perm_change_unchanged": "변경 없음", + "perm_change_updated": "업데이트됨", "perm_unchanged_more": "변경 없음 {{count}}개", + "perm_added_more": "추가 항목 {{count}}개 더 보기", + "perm_removed_more": "제거 항목 {{count}}개 더 보기", "perm_card_title_short": "권한", "perm_card_same_as": "{{version}}과(와) 동일", "perm_card_summary": "{{kinds}}개 분류 · {{count}}개 항목", diff --git a/src/locales/pt-BR/install.json b/src/locales/pt-BR/install.json index 88fa46d16..e9cd5f380 100644 --- a/src/locales/pt-BR/install.json +++ b/src/locales/pt-BR/install.json @@ -14,12 +14,18 @@ "perm_card_hint": "Por favor, confirme antes de instalar", "perm_card_empty": "Este script não solicita nenhuma permissão especial", "perm_card_title_changed": "Alterações de permissões", - "perm_card_compare": "Comparado com a versão instalada {{version}}", + "perm_card_version_range": "Atualização da versão instalada {{from}} para {{to}}", + "perm_delta_added": "{{count}} adicionado(s)", + "perm_delta_removed": "{{count}} removido(s)", + "perm_delta_updated": "{{count}} atualizado(s)", "perm_no_change": "Sem alterações", "perm_change_added": "Adicionado", "perm_change_removed": "Removido", "perm_change_unchanged": "Inalterado", + "perm_change_updated": "Atualizado", "perm_unchanged_more": "{{count}} inalterado(s)", + "perm_added_more": "Mostrar mais {{count}} adicionado(s)", + "perm_removed_more": "Mostrar mais {{count}} removido(s)", "perm_card_title_short": "Permissões", "perm_card_same_as": "Iguais à {{version}}", "perm_card_summary": "{{kinds}} categorias · {{count}} itens", diff --git a/src/locales/ru-RU/install.json b/src/locales/ru-RU/install.json index 554c9d233..6aa4beaa8 100644 --- a/src/locales/ru-RU/install.json +++ b/src/locales/ru-RU/install.json @@ -14,12 +14,18 @@ "perm_card_hint": "Подтвердите перед установкой", "perm_card_empty": "Этот скрипт не запрашивает особых разрешений", "perm_card_title_changed": "Изменения разрешений", - "perm_card_compare": "Сравнение с установленной {{version}}", + "perm_card_version_range": "Обновление с установленной {{from}} до {{to}}", + "perm_delta_added": "Добавлено: {{count}}", + "perm_delta_removed": "Удалено: {{count}}", + "perm_delta_updated": "Обновлено: {{count}}", "perm_no_change": "Без изменений", "perm_change_added": "Добавлено", "perm_change_removed": "Удалено", "perm_change_unchanged": "Без изменений", + "perm_change_updated": "Обновлено", "perm_unchanged_more": "Без изменений: {{count}}", + "perm_added_more": "Показать ещё добавленные: {{count}}", + "perm_removed_more": "Показать ещё удалённые: {{count}}", "perm_card_title_short": "Разрешения", "perm_card_same_as": "Совпадают с {{version}}", "perm_card_summary": "Категорий: {{kinds}} · элементов: {{count}}", diff --git a/src/locales/tr-TR/install.json b/src/locales/tr-TR/install.json index 9f709facc..3a759a0a7 100644 --- a/src/locales/tr-TR/install.json +++ b/src/locales/tr-TR/install.json @@ -14,12 +14,18 @@ "perm_card_hint": "Lütfen yüklemeden önce onaylayın", "perm_card_empty": "Bu betik herhangi bir özel izin istemiyor", "perm_card_title_changed": "İzin değişiklikleri", - "perm_card_compare": "Yüklü {{version}} ile karşılaştırma", + "perm_card_version_range": "Yüklü {{from}} sürümünden {{to}} sürümüne güncelleme", + "perm_delta_added": "{{count}} eklendi", + "perm_delta_removed": "{{count}} kaldırıldı", + "perm_delta_updated": "{{count}} güncellendi", "perm_no_change": "Değişiklik yok", "perm_change_added": "Eklendi", "perm_change_removed": "Kaldırıldı", "perm_change_unchanged": "Değişmedi", + "perm_change_updated": "Güncellendi", "perm_unchanged_more": "{{count}} değişmedi", + "perm_added_more": "{{count}} eklenen daha göster", + "perm_removed_more": "{{count}} kaldırılan daha göster", "perm_card_title_short": "İzinler", "perm_card_same_as": "{{version}} ile aynı", "perm_card_summary": "{{kinds}} kategori · {{count}} öğe", diff --git a/src/locales/vi-VN/install.json b/src/locales/vi-VN/install.json index 566d27b8a..067ad79e2 100644 --- a/src/locales/vi-VN/install.json +++ b/src/locales/vi-VN/install.json @@ -14,12 +14,18 @@ "perm_card_hint": "Vui lòng xác nhận trước khi cài đặt", "perm_card_empty": "Script này không yêu cầu bất kỳ quyền đặc biệt nào", "perm_card_title_changed": "Thay đổi quyền", - "perm_card_compare": "So với {{version}} đã cài đặt", + "perm_card_version_range": "Cập nhật từ {{from}} đã cài đặt lên {{to}}", + "perm_delta_added": "Thêm {{count}}", + "perm_delta_removed": "Xóa {{count}}", + "perm_delta_updated": "Cập nhật {{count}}", "perm_no_change": "Không thay đổi", "perm_change_added": "Đã thêm", "perm_change_removed": "Đã xóa", "perm_change_unchanged": "Không đổi", + "perm_change_updated": "Đã cập nhật", "perm_unchanged_more": "{{count}} không đổi", + "perm_added_more": "Hiện thêm {{count}} mục đã thêm", + "perm_removed_more": "Hiện thêm {{count}} mục đã xóa", "perm_card_title_short": "Quyền", "perm_card_same_as": "Giống {{version}}", "perm_card_summary": "{{kinds}} nhóm · {{count}} mục", diff --git a/src/locales/zh-CN/install.json b/src/locales/zh-CN/install.json index b64fab433..e6e84bd2f 100644 --- a/src/locales/zh-CN/install.json +++ b/src/locales/zh-CN/install.json @@ -14,12 +14,18 @@ "perm_card_hint": "安装前请确认", "perm_card_empty": "此脚本不请求任何特殊权限", "perm_card_title_changed": "权限变化", - "perm_card_compare": "对比已安装的 {{version}}", + "perm_card_version_range": "从已安装的 {{from}} 更新到 {{to}}", + "perm_delta_added": "新增 {{count}}", + "perm_delta_removed": "移除 {{count}}", + "perm_delta_updated": "更新 {{count}}", "perm_no_change": "无变化", "perm_change_added": "新增", "perm_change_removed": "已移除", "perm_change_unchanged": "未变动", + "perm_change_updated": "已更新", "perm_unchanged_more": "未变动 {{count}} 项", + "perm_added_more": "再显示 {{count}} 项新增", + "perm_removed_more": "再显示 {{count}} 项已移除", "perm_card_title_short": "权限", "perm_card_same_as": "与 {{version}} 相同", "perm_card_summary": "{{kinds}} 类 {{count}} 项", diff --git a/src/locales/zh-TW/install.json b/src/locales/zh-TW/install.json index 172cd680d..73aca0743 100644 --- a/src/locales/zh-TW/install.json +++ b/src/locales/zh-TW/install.json @@ -14,12 +14,18 @@ "perm_card_hint": "安裝前請確認", "perm_card_empty": "此腳本不要求任何特殊權限", "perm_card_title_changed": "權限變更", - "perm_card_compare": "對比已安裝的 {{version}}", + "perm_card_version_range": "從已安裝的 {{from}} 更新到 {{to}}", + "perm_delta_added": "新增 {{count}}", + "perm_delta_removed": "移除 {{count}}", + "perm_delta_updated": "更新 {{count}}", "perm_no_change": "無變更", "perm_change_added": "新增", "perm_change_removed": "已移除", "perm_change_unchanged": "未變更", + "perm_change_updated": "已更新", "perm_unchanged_more": "未變更 {{count}} 項", + "perm_added_more": "再顯示 {{count}} 項新增", + "perm_removed_more": "再顯示 {{count}} 項已移除", "perm_card_title_short": "權限", "perm_card_same_as": "與 {{version}} 相同", "perm_card_summary": "{{kinds}} 類 {{count}} 項", diff --git a/src/pages/install/App.test.tsx b/src/pages/install/App.test.tsx index 70881ef07..deafff673 100644 --- a/src/pages/install/App.test.tsx +++ b/src/pages/install/App.test.tsx @@ -394,14 +394,14 @@ describe("Install App 权限卡的更新基线", () => { risk: "warn", values: ["api.a.com"], sensitive: [], - diff: { added: ["api.a.com"], removed: [] }, + diff: { added: ["api.a.com"], removed: [], updated: [] }, }, ], }), }, }); render(); - expect(screen.getByText("对比已安装的 1.2.3")).toBeInTheDocument(); + expect(screen.getByTestId("permission-card-versions")).toHaveTextContent("1.2.3→2.3.1"); expect(screen.getByText("权限变化")).toBeInTheDocument(); }); @@ -431,7 +431,13 @@ describe("Install App 不再渲染安全警示条", () => { version: { kind: "update", oldVersion: "1.2.3", newVersion: "2.3.1", changed: true }, antifeatures: ["ads"], permissions: [ - { kind: "connect", risk: "danger", values: ["*"], sensitive: [], diff: { added: [], removed: [] } }, + { + kind: "connect", + risk: "danger", + values: ["*"], + sensitive: [], + diff: { added: [], removed: [], updated: [] }, + }, ], }), }, diff --git a/src/pages/install/App.tsx b/src/pages/install/App.tsx index 48ad6b1b0..8f7c8fec1 100644 --- a/src/pages/install/App.tsx +++ b/src/pages/install/App.tsx @@ -265,6 +265,7 @@ export default function App() { codePreviewRef.current?.jumpToLine(line) }} /> )} diff --git a/src/pages/install/components/PermissionCard.test.tsx b/src/pages/install/components/PermissionCard.test.tsx index 9cf20f0ff..f87620e48 100644 --- a/src/pages/install/components/PermissionCard.test.tsx +++ b/src/pages/install/components/PermissionCard.test.tsx @@ -44,7 +44,7 @@ describe("PermissionCard 权限卡", () => { // danger(跨域访问)默认展开,其取值可见 expect(screen.getByText("*")).toBeInTheDocument(); // normal(运行网站)默认折叠,其取值不在 DOM - expect(screen.queryByText("https://a.com/*")).not.toBeInTheDocument(); + expect(screen.queryByTitle("https://a.com/*")).not.toBeInTheDocument(); }); }); @@ -54,22 +54,50 @@ describe("PermissionCard 更新差异态", () => { risk: "danger", values: ["*", "api.a.com"], sensitive: [], - diff: { added: ["*"], removed: ["old.a.com"] }, + diff: { added: ["*"], removed: ["old.a.com"], updated: [] }, }; const sameMatch: PermissionRow = { kind: "match", risk: "normal", values: ["https://a.com/*"], sensitive: [], - diff: { added: [], removed: [] }, + diff: { added: [], removed: [], updated: [] }, }; - it("有变动时卡头显示变化标题、全卡增删计数与对比版本", () => { - render(); + it("有变动时卡头显示变化标题、全卡增删计数与旧版本到新版本", () => { + render(); + expect(screen.getByText("权限变化")).toBeInTheDocument(); + expect(screen.getByTestId("permission-card-delta")).toHaveTextContent("新增 1"); + expect(screen.getByTestId("permission-card-delta")).toHaveTextContent("移除 1"); + const range = screen.getByTestId("permission-card-versions"); + expect(range).toHaveTextContent("1.2.3→2.0.0"); + expect(within(range).getByText("从已安装的 1.2.3 更新到 2.0.0")).toHaveClass("sr-only"); + }); + + it("只换了查询参数的外部资源在卡头计为更新,不算新增或移除", () => { + const requireRow: PermissionRow = { + kind: "require", + risk: "normal", + values: ["https://a.com/lib.js?v2"], + sensitive: [], + diff: { added: [], removed: [], updated: [{ from: "https://a.com/lib.js?v1", to: "https://a.com/lib.js?v2" }] }, + }; + render(); expect(screen.getByText("权限变化")).toBeInTheDocument(); - expect(screen.getByTestId("permission-card-delta")).toHaveTextContent("+1"); - expect(screen.getByTestId("permission-card-delta")).toHaveTextContent("−1"); - expect(screen.getByText("对比已安装的 1.2.3")).toBeInTheDocument(); + const delta = screen.getByTestId("permission-card-delta"); + expect(delta).toHaveTextContent("更新 1"); + expect(delta).not.toHaveTextContent("新增"); + expect(delta).not.toHaveTextContent("移除"); + }); + + it("移动端更新态的类别标题同样只给变动数,不给总数徽章;全新安装仍给总数", () => { + mobile = true; + const { rerender } = render( + + ); + expect(screen.queryByTestId("permission-count")).not.toBeInTheDocument(); + rerender(); + expect(screen.getAllByTestId("permission-count")).toHaveLength(2); }); it("有变动的类别展开,无变动的类别塌成单行", () => { @@ -78,13 +106,13 @@ describe("PermissionCard 更新差异态", () => { const collapsed = screen.getByTestId("permission-row-collapsed"); expect(within(collapsed).getByText("运行网站")).toBeInTheDocument(); expect(within(collapsed).getByText("无变化")).toBeInTheDocument(); - expect(screen.queryByText("https://a.com/*")).not.toBeInTheDocument(); + expect(screen.queryByTitle("https://a.com/*")).not.toBeInTheDocument(); }); it("点开塌行后该类别取值可见", () => { render(); fireEvent.click(screen.getByTestId("permission-row-collapsed")); - expect(screen.getByText("https://a.com/*")).toBeInTheDocument(); + expect(screen.getByTitle("https://a.com/*")).toBeInTheDocument(); expect(screen.getAllByTestId("permission-row")).toHaveLength(2); }); @@ -98,7 +126,7 @@ describe("PermissionCard 更新差异态", () => { expect(screen.getByText("此脚本不请求任何特殊权限")).toBeInTheDocument(); expect(screen.getByText("安装前请确认")).toBeInTheDocument(); expect(screen.queryByText("无变化")).not.toBeInTheDocument(); - expect(screen.queryByText("对比已安装的 1.2.3")).not.toBeInTheDocument(); + expect(screen.queryByTestId("permission-card-versions")).not.toBeInTheDocument(); }); it("更新但一项未变时不显示变化标题与增删计数", () => { @@ -111,7 +139,7 @@ describe("PermissionCard 更新差异态", () => { mobile = true; render(); expect(screen.getByText("*")).toBeInTheDocument(); - expect(screen.queryByText("https://a.com/*")).not.toBeInTheDocument(); + expect(screen.queryByTitle("https://a.com/*")).not.toBeInTheDocument(); }); it("全新安装(无 diff)卡头维持原样", () => { @@ -128,7 +156,7 @@ describe("PermissionCard 更新零变化态", () => { risk: "normal", values, sensitive: [], - diff: { added: [], removed: [] }, + diff: { added: [], removed: [], updated: [] }, }); const rows = [same("connect", ["api.a.com"]), same("match", ["https://a.com/*", "https://b.com/*"])]; @@ -136,7 +164,7 @@ describe("PermissionCard 更新零变化态", () => { render(); expect(screen.getByTestId("permission-card-collapsed")).toBeInTheDocument(); expect(screen.queryByText("api.a.com")).not.toBeInTheDocument(); - expect(screen.queryByText("https://a.com/*")).not.toBeInTheDocument(); + expect(screen.queryByTitle("https://a.com/*")).not.toBeInTheDocument(); expect(screen.queryAllByTestId("permission-row")).toHaveLength(0); }); @@ -153,7 +181,7 @@ describe("PermissionCard 更新零变化态", () => { fireEvent.click(screen.getByTestId("permission-card-collapsed")); expect(screen.getAllByTestId("permission-row")).toHaveLength(2); expect(screen.getByText("api.a.com")).toBeInTheDocument(); - expect(screen.getByText("https://a.com/*")).toBeInTheDocument(); + expect(screen.getByTitle("https://a.com/*")).toBeInTheDocument(); expect(screen.getAllByText("无变化")).toHaveLength(3); }); @@ -171,7 +199,7 @@ describe("PermissionCard 更新零变化态", () => { render(); fireEvent.click(screen.getByTestId("permission-card-collapsed")); expect(screen.getByText("*")).toBeInTheDocument(); - expect(screen.queryByText("https://a.com/*")).not.toBeInTheDocument(); + expect(screen.queryByTitle("https://a.com/*")).not.toBeInTheDocument(); }); it("有变动时不折叠整卡", () => { @@ -180,7 +208,7 @@ describe("PermissionCard 更新零变化态", () => { risk: "warn", values: ["api.a.com"], sensitive: [], - diff: { added: ["api.a.com"], removed: [] }, + diff: { added: ["api.a.com"], removed: [], updated: [] }, }; render(); expect(screen.queryByTestId("permission-card-collapsed")).not.toBeInTheDocument(); @@ -356,7 +384,7 @@ describe("PermissionCard 其他声明行的取值与脚本猫独有指令", () = describe("不生效标记与折叠形态的关系", () => { const rows: PermissionRow[] = [ - { kind: "grant", risk: "warn", values: ["GM_audio"], sensitive: [], diff: { added: [], removed: [] } }, + { kind: "grant", risk: "warn", values: ["GM_audio"], sensitive: [], diff: { added: [], removed: [], updated: [] } }, ]; it("权限一项没变但有不生效项时整卡不塌——塌了这些标记就没人看得见", () => { @@ -393,9 +421,15 @@ describe("有变动时未变动类别的塌行与不生效标记", () => { risk: "normal", values: ["*://a.com/*"], sensitive: [], - diff: { added: ["*://a.com/*"], removed: [] }, + diff: { added: ["*://a.com/*"], removed: [], updated: [] }, + }, + { + kind: "grant", + risk: "warn", + values: ["GM_audio"], + sensitive: [], + diff: { added: [], removed: [], updated: [] }, }, - { kind: "grant", risk: "warn", values: ["GM_audio"], sensitive: [], diff: { added: [], removed: [] } }, ]} baselineVersion="1.0.0" compat={{ marks: { grants: new Map([["GM_audio", 9]]), matches: new Map(), tags: [], scriptcatOnlyTags: [] } }} diff --git a/src/pages/install/components/PermissionCard.tsx b/src/pages/install/components/PermissionCard.tsx index ff517c62b..a953e496a 100644 --- a/src/pages/install/components/PermissionCard.tsx +++ b/src/pages/install/components/PermissionCard.tsx @@ -8,7 +8,15 @@ import { compatMarkCount, rowHasCompatMarks, type CompatView } from "../compat"; import { metadataDocHref } from "../compat_docs"; import { CompatChip, ScriptCatOnlyBadge } from "./CompatChip"; import { isPermissionChanged, type PermissionRow as PermissionRowData } from "../permissions"; -import { PermissionRow, PermissionChips, PermissionDelta, NoChangeTag, KIND_META, RISK_STYLE } from "./PermissionRow"; +import { + PermissionRow, + PermissionChips, + PermissionCount, + PermissionDelta, + NoChangeTag, + KIND_META, + RISK_STYLE, +} from "./PermissionRow"; function MobilePermissions({ rows, compat }: { rows: PermissionRowData[]; compat?: CompatView }) { const { t } = useTranslation(["install", "common"]); @@ -33,9 +41,7 @@ function MobilePermissions({ rows, compat }: { rows: PermissionRowData[]; compat {t(labelKey)} - - {row.values.length} - + {row.diff && !isPermissionChanged(row) && } @@ -171,10 +177,13 @@ function OtherDirectivesRow({ compat }: { compat: CompatView }) { export function PermissionCard({ rows, baselineVersion, + version, compat, }: { rows: PermissionRowData[]; baselineVersion?: string; + /** 本次将要安装的版本;更新场景与 baselineVersion 一起写成「旧 → 新」 */ + version?: string; compat?: CompatView; }) { const { t } = useTranslation(["install", "common"]); @@ -185,6 +194,7 @@ export function PermissionCard({ const changed = rows.filter(isPermissionChanged); const added = changed.reduce((n, r) => n + r.diff!.added.length, 0); const removed = changed.reduce((n, r) => n + r.diff!.removed.length, 0); + const updated = changed.reduce((n, r) => n + r.diff!.updated.length, 0); const noChange = isUpdate && changed.length === 0; const compatCount = compat ? compatMarkCount(compat.marks) : 0; @@ -195,18 +205,42 @@ export function PermissionCard({ return (
-
+
-

+

{changed.length ? t("install:perm_card_title_changed") : t("install:perm_card_title")}

{changed.length > 0 && ( - {added > 0 && {`+${added}`}} - {removed > 0 && {`−${removed}`}} + {[ + added > 0 && ( + + {t("install:perm_delta_added", { count: added })} + + ), + removed > 0 && ( + + {t("install:perm_delta_removed", { count: removed })} + + ), + updated > 0 && ( + + {t("install:perm_delta_updated", { count: updated })} + + ), + ] + .filter(Boolean) + .flatMap((part, i) => [ + i > 0 && ( + + ), + part, + ])} )} {isUpdate && !changed.length && } @@ -218,9 +252,27 @@ export function PermissionCard({ {t("install:compat_count", { count: compatCount })} )} - - {isUpdate ? t("install:perm_card_compare", { version: baselineVersion }) : t("install:perm_card_hint")} - + {isUpdate ? ( + + + + {t("install:perm_card_version_range", { from: baselineVersion, to: version })} + + + ) : ( + {t("install:perm_card_hint")} + )}
{rows.length === 0 ? ( diff --git a/src/pages/install/components/PermissionRow.test.tsx b/src/pages/install/components/PermissionRow.test.tsx index 5fb316c8e..64a07de4a 100644 --- a/src/pages/install/components/PermissionRow.test.tsx +++ b/src/pages/install/components/PermissionRow.test.tsx @@ -77,7 +77,7 @@ describe("PermissionRow 更新差异态", () => { risk: "danger" as const, values: ["*", "api.a.com", "api.b.com"], sensitive: [], - diff: { added: ["*"], removed: ["old.a.com"] }, + diff: { added: ["*"], removed: ["old.a.com"], updated: [] }, }; it("新增与移除的取值常驻可见,未变动的收进折叠桶", () => { @@ -116,7 +116,13 @@ describe("PermissionRow 更新差异态", () => { it("差异为空的行不渲染变动计数,取值全部按未变动呈现", () => { render( ); const row = screen.getByTestId("permission-row"); @@ -131,14 +137,14 @@ describe("PermissionRow 零变动行的取值折叠", () => { risk: "normal" as const, values: ["https://a.com/*", "https://b.com/*", "https://c.com/*", "https://d.com/*"], sensitive: [], - diff: { added: [], removed: [] }, + diff: { added: [], removed: [], updated: [] }, }; it("没有可钉住的增删时,取值仍按 maxVisible 折叠为 +N", () => { render(); const row = screen.getByTestId("permission-row"); - expect(within(row).getByText("https://a.com/*")).toBeInTheDocument(); - expect(within(row).queryByText("https://c.com/*")).not.toBeInTheDocument(); + expect(within(row).getByTitle("https://a.com/*")).toBeInTheDocument(); + expect(within(row).queryByTitle("https://c.com/*")).not.toBeInTheDocument(); expect(within(row).getByTestId("permission-more")).toHaveTextContent("+2"); }); @@ -146,7 +152,7 @@ describe("PermissionRow 零变动行的取值折叠", () => { render(); const row = screen.getByTestId("permission-row"); fireEvent.click(within(row).getByTestId("permission-more")); - expect(within(row).getByText("https://d.com/*")).toBeInTheDocument(); + expect(within(row).getByTitle("https://d.com/*")).toBeInTheDocument(); expect(within(row).queryByTestId("permission-more")).not.toBeInTheDocument(); }); }); @@ -212,7 +218,7 @@ describe("PermissionRow 上的不生效标记", () => { risk: "warn", values: ["GM_setValue"], sensitive: [], - diff: { added: [], removed: ["GM_audio"] }, + diff: { added: [], removed: ["GM_audio"], updated: [] }, }} compat={compat({ grants: new Map([["GM_audio", 9]]) })} /> @@ -249,7 +255,7 @@ describe("PermissionRow 上的仅限脚本猫标记", () => { risk: "warn", values: ["GM_setValue"], sensitive: [], - diff: { added: [], removed: ["CAT_fileStorage"] }, + diff: { added: [], removed: ["CAT_fileStorage"], updated: [] }, }} /> ); @@ -304,7 +310,7 @@ describe("不生效标记不会被折叠藏起来", () => { risk: "warn", values: ["GM_getValue", "GM_setValue", "GM_audio"], sensitive: [], - diff: { added: ["GM_getValue"], removed: [] }, + diff: { added: ["GM_getValue"], removed: [], updated: [] }, }} compat={marks([["GM_audio", 9]])} /> @@ -323,7 +329,7 @@ describe("不生效标记不会被折叠藏起来", () => { risk: "warn", values: ["GM_setValue", "GM_getValue", "GM_audio"], sensitive: [], - diff: { added: [], removed: [] }, + diff: { added: [], removed: [], updated: [] }, }} maxVisible={1} compat={marks([["GM_audio", 9]])} @@ -333,3 +339,155 @@ describe("不生效标记不会被折叠藏起来", () => { expect(screen.getByTestId("permission-more")).toHaveTextContent("+1"); }); }); + +describe("PermissionRow 更新态的行头", () => { + it("更新态只给变动数,不再给类别总数徽章;全新安装仍显示总数", () => { + const { rerender } = render( + + ); + expect(screen.queryByTestId("permission-count")).not.toBeInTheDocument(); + rerender(); + expect(screen.getByTestId("permission-count")).toHaveTextContent("1"); + }); +}); + +describe("PermissionRow 新增取值的配色", () => { + it("普通新增用新增色记号,危险与敏感的新增沿用风险色,不被染成新增色", () => { + render( + <> + + + + ); + const glyph = (value: string) => + within(screen.getByText(value).closest("[data-chip]") as HTMLElement).getByText("+"); + expect(glyph("GM_addStyle")).toHaveClass("text-success-fg"); + expect(glyph("*")).not.toHaveClass("text-success-fg"); + expect(glyph("GM_cookie")).not.toHaveClass("text-success-fg"); + }); +}); + +describe("PermissionRow 只换了查询参数的外部资源", () => { + const row = { + kind: "require" as const, + risk: "normal" as const, + values: ["https://cdn.a.com/x/lib.js?v1.3.0"], + sensitive: [], + diff: { + added: [], + removed: [], + updated: [{ from: "https://cdn.a.com/x/lib.js?v1.2.0", to: "https://cdn.a.com/x/lib.js?v1.3.0" }], + }, + }; + + it("合并成一条更新,给出旧参数到新参数,并计入行头的更新数", () => { + render(); + const chip = screen.getByTitle("https://cdn.a.com/x/lib.js?v1.3.0").closest("[data-chip]") as HTMLElement; + expect(chip).toHaveAttribute("data-change", "updated"); + expect(within(chip).getByText("?v1.2.0")).toBeInTheDocument(); + expect(within(chip).getByText("?v1.3.0")).toBeInTheDocument(); + expect(within(chip).getByText("已更新")).toBeInTheDocument(); + expect(screen.getByTestId("permission-delta")).toHaveTextContent("~1"); + expect(screen.queryByTestId("permission-more")).not.toBeInTheDocument(); + }); +}); + +describe("PermissionRow 增删很多时的截断", () => { + const many = (n: number) => Array.from({ length: n }, (_, i) => `https://s${i + 1}.com/*`); + + it("新增超过 8 条时只显示前 8 条,其余收进「再显示 N 项新增」,点开后全部可见", () => { + const added = many(10); + render( + + ); + expect(screen.getByTitle("https://s8.com/*")).toBeInTheDocument(); + expect(screen.queryByTitle("https://s9.com/*")).not.toBeInTheDocument(); + fireEvent.click(screen.getByRole("button", { name: /再显示 2 项新增/ })); + expect(screen.getByTitle("https://s10.com/*")).toBeInTheDocument(); + expect(screen.queryByRole("button", { name: /项新增/ })).not.toBeInTheDocument(); + }); + + it("危险与敏感的新增取值永远不被收起", () => { + const added = ["GM_a", "GM_b", "GM_c", "GM_cookie"]; + render( + + ); + expect(screen.getByText("GM_cookie")).toBeInTheDocument(); + expect(screen.queryByText("GM_c")).not.toBeInTheDocument(); + expect(screen.getByRole("button", { name: /再显示 1 项新增/ })).toBeInTheDocument(); + }); + + it("移除超过 8 条时只显示前 8 条,其余收进「再显示 N 项已移除」", () => { + const removed = many(9); + render( + + ); + expect(screen.queryByTitle("https://s9.com/*")).not.toBeInTheDocument(); + fireEvent.click(screen.getByRole("button", { name: /再显示 1 项已移除/ })); + expect(screen.getByTitle("https://s9.com/*")).toBeInTheDocument(); + }); +}); + +describe("PermissionRow 地址类取值的展示", () => { + const value = "https://greasyfork.org/scripts/435207/code/GitHub%20%E4%B8%AD%E6%96%87.js?v1.9.2"; + + it("地址解码后单行展示,域名单独突出,悬停给出原文", () => { + render(); + const toggle = screen.getByTitle(value); + expect(within(toggle).getByText("greasyfork.org")).toBeInTheDocument(); + expect(within(toggle).getByText("/GitHub 中文.js?v1.9.2")).toBeInTheDocument(); + expect(toggle).toHaveAttribute("aria-expanded", "false"); + }); + + it("点击后展开为完整地址,再点收起", () => { + render(); + fireEvent.click(screen.getByTitle(value)); + expect(screen.getByTitle(value)).toHaveAttribute("aria-expanded", "true"); + expect(screen.getByText("https://greasyfork.org/scripts/435207/code/GitHub 中文.js?v1.9.2")).toBeInTheDocument(); + fireEvent.click(screen.getByTitle(value)); + expect(screen.getByTitle(value)).toHaveAttribute("aria-expanded", "false"); + }); + + it("跨域访问与 GM 能力的取值不是地址,保持原样", () => { + render(); + expect(screen.getByText("api.a.com")).toBeInTheDocument(); + expect(screen.queryByTitle("api.a.com")).not.toBeInTheDocument(); + }); +}); diff --git a/src/pages/install/components/PermissionRow.tsx b/src/pages/install/components/PermissionRow.tsx index 67cbf00fc..b77420916 100644 --- a/src/pages/install/components/PermissionRow.tsx +++ b/src/pages/install/components/PermissionRow.tsx @@ -6,8 +6,10 @@ import { isScriptCatOnlyGrant } from "@App/pkg/utils/script_compat"; import { isMarkedValue, type CompatView } from "../compat"; import { catApiDocHref, metadataDocHref } from "../compat_docs"; import { CompatChip, ScriptCatOnlyBadge } from "./CompatChip"; +import { splitUrlForDisplay } from "../url_display"; import { isPermissionChanged, + type PermissionUpdate, type PermissionKind, type PermissionRisk, type PermissionRow as PermissionRowData, @@ -44,30 +46,81 @@ export const RISK_STYLE: Record = { added: "install:perm_change_added", removed: "install:perm_change_removed", unchanged: "install:perm_change_unchanged", + updated: "install:perm_change_updated", }; +const URL_KINDS = new Set(["match", "require"]); + +/** + * 地址类取值单行展示:域名与文件名加参数(版本号通常就在这里)常驻,中段路径优先被省略; + * 点击展开为完整地址。@require 默认的 https:// 不占位置,http:// 等其他协议照常显示——明文加载值得被看见。 + */ +function UrlValue({ + value, + kind, + title = value, + muted, +}: { + value: string; + kind: PermissionKind; + title?: string; + muted?: boolean; +}) { + const [open, setOpen] = useState(false); + const parts = URL_KINDS.has(kind) ? splitUrlForDisplay(value) : null; + if (!parts) return {value}; + const { prefix, scheme, host, middle, tail } = parts; + const showScheme = !(kind === "require" && scheme === "https://"); + + return ( + + ); +} + function Chip({ value, row, change }: { value: string; row: PermissionRowData; change?: ChangeState }) { const { t } = useTranslation(["install", "common"]); const isSensitive = row.sensitive.includes(value); // 装在脚本猫里能用,但换到别的脚本管理器就不行了;已移除的取值不再属于新版本,不标 const isScriptCatOnly = row.kind === "grant" && change !== "removed" && isScriptCatOnlyGrant(value); const base = isSensitive ? "border border-warning-fg bg-muted text-warning-fg" : RISK_STYLE[row.risk].chip; + // 底色与描边优先留给风险等级:新增的 @connect * 若被染成新增色,最该报警的情况就被伪装成了安全的 + const riskTone = isSensitive || row.risk === "danger"; - // 变动状态只用字重、描边与 +/− 记号表达,底色继续留给风险等级—— - // 否则新增的 @connect * 会被染成「新增色」,把最该报警的情况伪装成安全的。 const changeClass = change === "removed" - ? "border border-border bg-transparent text-muted-foreground line-through" + ? "border border-dashed border-border bg-transparent text-muted-foreground" : change === "unchanged" ? "bg-muted border border-border text-muted-foreground" : change === "added" - ? cn(base, "font-semibold", row.risk === "danger" && !isSensitive ? "border-destructive" : "border-current") + ? riskTone + ? cn(base, "font-semibold", row.risk === "danger" && !isSensitive ? "border-destructive" : "border-current") + : "border border-success-fg/55 bg-muted text-foreground" : base; return ( @@ -77,21 +130,68 @@ function Chip({ value, row, change }: { value: string; row: PermissionRowData; c data-sensitive={isSensitive ? "true" : undefined} className={cn("inline-flex max-w-full items-center gap-1 rounded-md px-2 py-0.5 font-mono text-xs", changeClass)} > - {change === "added" && } - {change === "removed" && } + {change === "added" && ( + + )} + {change === "removed" && ( + + )} {isSensitive && change !== "removed" && } {change && {t(CHANGE_LABEL_KEY[change])}} - {value} + + + {isScriptCatOnly && } ); } -function MoreButton({ label, onClick }: { label: string; onClick: () => void }) { +/** 同一资源只换了查询参数:地址只出现一次,旧参数划掉、新参数紧随其后 */ +function UpdatedChip({ update, row }: { update: PermissionUpdate; row: PermissionRowData }) { + const { t } = useTranslation(["install", "common"]); + const base = update.to.split("?")[0]; + const fromQuery = update.from.slice(base.length); + const toQuery = update.to.slice(base.length); + + return ( + + + {t(CHANGE_LABEL_KEY.updated)} + + + {fromQuery && {fromQuery}} + + {toQuery && {toQuery}} + + + ); +} + +function MoreButton({ + label, + onClick, + testId = "permission-more", +}: { + label: string; + onClick: () => void; + testId?: string; +}) { return (