Skip to content

Commit 2d24504

Browse files
cyfung1031CodFrm
andauthored
✨ 把 fetchIconByDomain 改成 scriptcat.org (#1268)
* 把 fetchIconByDomain 改成 scriptcat.org * scriptcat.org -> ext.scriptcat.org * ✨ 支持配置化 Favicon 服务提供者 #1268 新增图标服务设置项,支持 ScriptCat / 本地获取 / Google 三种 provider, 默认使用 ScriptCat API。切换 provider 时自动清除 favicon 缓存。 同时增加了对接口返回异常状态码(>=300)的处理。 * 🐛 修复图标服务设置项布局,标题与选择框放在同一行 * 🐛 修复 local 模式下 favicon 获取逻辑,还原本地抓取实现 * 调整图标服务位置 * ✅ 为 #1268 favicon 服务增加单元测试 * 🐛 修复 favicon 服务切换相关问题 - fetchIconByService 添加 default 分支回退到 local - 切换服务时清除内存中的 blob URL 缓存 - 使用 deletes() 批量删除替代逐条删除 - setFaviconService 添加 return - 修复 pre-push 正则匹配 release/ 分支 * 🎨 修复 favicons 相关文件的 prettier 格式问题 --------- Co-authored-by: 王一之 <yz@ggnb.top>
1 parent 50cd845 commit 2d24504

13 files changed

Lines changed: 420 additions & 10 deletions

File tree

.husky/pre-push

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ need_check=0
1313
while read local_ref local_sha remote_ref remote_sha; do
1414
branch=$(echo "$remote_ref" | sed 's|refs/heads/||')
1515

16-
if [ "$branch" = "main" ] || echo "$branch" | grep -q "^release"; then
16+
if [ "$branch" = "main" ] || echo "$branch" | grep -q "^release/"; then
1717
need_check=1
1818
echo "🔍 Detected push target: $branch"
1919
fi

src/locales/de-DE/translation.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -580,6 +580,11 @@
580580
"maybe_later": "Vielleicht später",
581581
"settings_hint": "Sie können diese Option jederzeit in den Einstellungen ändern."
582582
},
583+
"favicon_service": "Favicon-Dienst",
584+
"favicon_service_desc": "Dienst zum Abrufen von Website-Symbolen auswählen",
585+
"favicon_service_scriptcat": "ScriptCat",
586+
"favicon_service_local": "Lokal abrufen",
587+
"favicon_service_google": "Google",
583588
"editor": {
584589
"show_script_list": "Skriptliste anzeigen",
585590
"hide_script_list": "Skriptliste ausblenden"

src/locales/en-US/translation.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -580,6 +580,11 @@
580580
"maybe_later": "Maybe Later",
581581
"settings_hint": "You can change this option in settings at any time."
582582
},
583+
"favicon_service": "Favicon Service",
584+
"favicon_service_desc": "Choose the service for fetching website icons",
585+
"favicon_service_scriptcat": "ScriptCat",
586+
"favicon_service_local": "Local Fetch",
587+
"favicon_service_google": "Google",
583588
"editor": {
584589
"show_script_list": "Show Script List",
585590
"hide_script_list": "Hide Script List"

src/locales/ja-JP/translation.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -580,6 +580,11 @@
580580
"maybe_later": "後で",
581581
"settings_hint": "設定ページでいつでも変更できます。"
582582
},
583+
"favicon_service": "Favicon サービス",
584+
"favicon_service_desc": "ウェブサイトアイコンの取得サービスを選択",
585+
"favicon_service_scriptcat": "ScriptCat",
586+
"favicon_service_local": "ローカル取得",
587+
"favicon_service_google": "Google",
583588
"editor": {
584589
"show_script_list": "スクリプトリストを表示",
585590
"hide_script_list": "スクリプトリストを非表示"

src/locales/ru-RU/translation.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -580,6 +580,11 @@
580580
"maybe_later": "Может быть позже",
581581
"settings_hint": "Вы можете изменить эту опцию в настройках в любое время."
582582
},
583+
"favicon_service": "Сервис Favicon",
584+
"favicon_service_desc": "Выберите сервис для получения значков сайтов",
585+
"favicon_service_scriptcat": "ScriptCat",
586+
"favicon_service_local": "Локальное получение",
587+
"favicon_service_google": "Google",
583588
"editor": {
584589
"show_script_list": "Показать список скриптов",
585590
"hide_script_list": "Скрыть список скриптов"

src/locales/vi-VN/translation.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -580,6 +580,11 @@
580580
"maybe_later": "Để sau",
581581
"settings_hint": "Bạn có thể thay đổi tùy chọn này trong cài đặt bất kỳ lúc nào."
582582
},
583+
"favicon_service": "Dịch vụ Favicon",
584+
"favicon_service_desc": "Chọn dịch vụ để lấy biểu tượng trang web",
585+
"favicon_service_scriptcat": "ScriptCat",
586+
"favicon_service_local": "Lấy cục bộ",
587+
"favicon_service_google": "Google",
583588
"editor": {
584589
"show_script_list": "Hiển thị danh sách script",
585590
"hide_script_list": "Ẩn danh sách script"

src/locales/zh-CN/translation.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -580,6 +580,11 @@
580580
"maybe_later": "暂不启用",
581581
"settings_hint": "你可以随时在设置中修改此选项。"
582582
},
583+
"favicon_service": "图标服务",
584+
"favicon_service_desc": "选择获取网站图标的服务",
585+
"favicon_service_scriptcat": "ScriptCat",
586+
"favicon_service_local": "本地获取",
587+
"favicon_service_google": "Google",
583588
"editor": {
584589
"show_script_list": "显示脚本列表",
585590
"hide_script_list": "隐藏脚本列表"

src/locales/zh-TW/translation.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -580,6 +580,11 @@
580580
"maybe_later": "暫不啟用",
581581
"settings_hint": "你可以隨時在設定中修改此選項。"
582582
},
583+
"favicon_service": "圖示服務",
584+
"favicon_service_desc": "選擇取得網站圖示的服務",
585+
"favicon_service_scriptcat": "ScriptCat",
586+
"favicon_service_local": "本地取得",
587+
"favicon_service_google": "Google",
583588
"editor": {
584589
"show_script_list": "顯示腳本列表",
585590
"hide_script_list": "隱藏腳本列表"

src/pages/options/routes/ScriptList/hooks.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
} from "@App/app/repo/scripts";
1111
import { fetchScript, fetchScriptList } from "@App/pages/store/features/script";
1212
import { loadScriptFavicons } from "@App/pages/store/favicons";
13+
import { systemConfig } from "@App/pages/store/global";
1314
import { parseTags } from "@App/app/repo/metadata";
1415
import { getCombinedMeta } from "@App/app/service/service_worker/utils";
1516
import { cacheInstance } from "@App/app/cache";
@@ -76,7 +77,8 @@ export function useScriptDataManagement() {
7677
setLoadingList(false);
7778
cacheInstance.tx("faviconOPFSControl", async () => {
7879
if (!mounted) return;
79-
for await (const { chunkResults } of loadScriptFavicons(list)) {
80+
const faviconService = await systemConfig.getFaviconService();
81+
for await (const { chunkResults } of loadScriptFavicons(list, faviconService)) {
8082
if (!mounted) return;
8183
setScriptList((prev) => {
8284
const favMap = new Map(chunkResults.map((r) => [r.uuid, r]));

src/pages/options/routes/Setting.tsx

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import CustomTrans from "@App/pages/components/CustomTrans";
1515
import { useSystemConfig } from "./utils";
1616
import { subscribeMessage } from "@App/pages/store/global";
1717
import { SystemConfigChange, type SystemConfigKey } from "@App/pkg/config/config";
18+
import { FaviconDAO } from "@App/app/repo/favicon";
19+
import { clearFaviconMemoryCache } from "@App/pages/store/favicons";
1820
import { type TKeyValue } from "@Packages/message/message_queue";
1921
import { useEffect, useMemo } from "react";
2022
import { systemConfig } from "@App/pages/store/global";
@@ -41,6 +43,7 @@ function Setting() {
4143
const [badgeTextColor, setBadgeTextColor, submitBadgeTextColor] = useSystemConfig("badge_text_color");
4244
const [scriptMenuDisplayType, setScriptMenuDisplayType, submitScriptMenuDisplayType] =
4345
useSystemConfig("script_menu_display_type");
46+
const [faviconService, setFaviconService, submitFaviconService] = useSystemConfig("favicon_service");
4447

4548
const [editorTypeDefinition, setEditorTypeDefinition, submitEditorTypeDefinition] =
4649
useSystemConfig("editor_type_definition");
@@ -81,6 +84,7 @@ function Setting() {
8184
badge_background_color: setBadgeBackgroundColor,
8285
badge_text_color: setBadgeTextColor,
8386
script_menu_display_type: setScriptMenuDisplayType,
87+
favicon_service: setFaviconService,
8488
editor_type_definition: setEditorTypeDefinition,
8589
} as const;
8690
const hookMgr = new HookManager();
@@ -306,6 +310,39 @@ function Setting() {
306310
</div>
307311
</Space>
308312
</div>
313+
314+
{/* Favicon 服务 */}
315+
<div className="tw-flex tw-items-center tw-justify-between tw-min-h-9">
316+
<div className="tw-flex tw-items-center tw-gap-4 tw-flex-1">
317+
<span className="tw-min-w-20">{t("favicon_service")}</span>
318+
<Select
319+
value={faviconService}
320+
className="tw-w-40 tw-max-w-50"
321+
onChange={async (value) => {
322+
submitFaviconService(value);
323+
// 清除 favicon 缓存
324+
try {
325+
const faviconDAO = new FaviconDAO();
326+
const allFavicons = await faviconDAO.find();
327+
await faviconDAO.deletes(allFavicons.map((f) => f.uuid));
328+
// 清除 OPFS 缓存:删除并重建目录
329+
const opfsRoot = await navigator.storage.getDirectory();
330+
await opfsRoot.removeEntry("cached_favicons", { recursive: true }).catch(() => {});
331+
// 清除内存中的 blob URL 缓存
332+
clearFaviconMemoryCache();
333+
} catch {
334+
// 忽略清除缓存的错误
335+
}
336+
Message.success(t("save_success")!);
337+
}}
338+
>
339+
<Select.Option value="scriptcat">{t("favicon_service_scriptcat")}</Select.Option>
340+
<Select.Option value="google">{t("favicon_service_google")}</Select.Option>
341+
<Select.Option value="local">{t("favicon_service_local")}</Select.Option>
342+
</Select>
343+
</div>
344+
<span className="tw-text-xs tw-ml-6 tw-flex-shrink-0">{t("favicon_service_desc")}</span>
345+
</div>
309346
</Space>
310347
</Card>
311348

0 commit comments

Comments
 (0)