diff --git a/src/pages/popup/App.tsx b/src/pages/popup/App.tsx index 5f9eb3676..03ee57a00 100644 --- a/src/pages/popup/App.tsx +++ b/src/pages/popup/App.tsx @@ -27,6 +27,7 @@ import { subscribeMessage } from "@App/pages/store/global"; import type { TDeleteScript, TEnableScript, TScriptRunStatus } from "@App/app/service/queue"; import { SCRIPT_RUN_STATUS_RUNNING } from "@App/app/repo/scripts"; import { HookManager } from "@App/pkg/utils/hookManager"; +import { cacheInstance } from "@App/app/cache"; const CollapseItem = Collapse.Item; @@ -69,6 +70,43 @@ const updateList = (list: ScriptMenu[], update: TUpdateEntryFn, options: TUpdate return changed ? newList : list; // 如子项没任何变化,则返回原list参考 }; +type ScriptProvider = "scriptcat" | "greasyfork" | "openuserjs"; + +const getMoreScriptWindowOpen = (currentUrl: string, provider: ScriptProvider) => { + let urlHost = ""; + if (currentUrl) { + try { + const url = new URL(currentUrl); + if (url.hostname && url.protocol.startsWith("http")) { + urlHost = url.hostname; + } + } catch (e: any) { + console.warn(e); // 容错:URL 解析失败时忽略错误(不影响后续 UI) + } + } + let link = ""; + if (provider === "greasyfork") { + // www.google.com -> google.com + urlHost = /[^.]+\.[^.]+$/.exec(urlHost)?.[0] || urlHost; + } + switch (provider) { + case "scriptcat": + link = !urlHost + ? "https://scriptcat.org/search" + : `https://scriptcat.org/search?domain=${encodeURIComponent(urlHost)}`; + break; + case "greasyfork": + link = !urlHost + ? "https://greasyfork.org/scripts/" + : `https://greasyfork.org/scripts/by-site/${encodeURI(urlHost)}`; + break; + case "openuserjs": + link = !urlHost ? "https://openuserjs.org/" : `https://openuserjs.org/?q=${encodeURIComponent(urlHost)}`; + break; + } + window.open(link, "_blank"); +}; + function App() { const [loading, setLoading] = useState(true); const [scriptList, setScriptList] = useState<(ScriptMenu & { menuUpdated?: number })[]>([]); @@ -84,6 +122,12 @@ function App() { const [isEnableScript, setIsEnableScript] = useState(true); const [isBlacklist, setIsBlacklist] = useState(false); const [collapseActiveKey, setCollapseActiveKey] = useState(["script"]); + const [defaultScriptProvider, setDefaultScriptProvider] = useState(() => { + cacheInstance.get("default_script_provider").then((value) => { + setDefaultScriptProvider(value || "scriptcat"); + }); + return "scriptcat"; + }); const { t } = useTranslation(); const pageTabIdRef = useRef(0); @@ -130,16 +174,6 @@ function App() { }; }, [backEnables]); - const urlHost = useMemo(() => { - let url: URL | undefined; - try { - url = new URL(currentUrl); - } catch (_: any) { - // 容错:URL 解析失败时忽略错误(不影响后续 UI) - } - return url?.hostname ?? ""; - }, [currentUrl]); - useEffect(() => { const hookMgr = new HookManager(); @@ -336,6 +370,12 @@ function App() { }; const handleMenuClick = async (key: string) => { + if (key.startsWith("get_more_script_")) { + const provider = key.replace("get_more_script_", "") as ScriptProvider; + await cacheInstance.set("default_script_provider", provider); + setDefaultScriptProvider(provider); + return getMoreScriptWindowOpen(currentUrl, provider); + } switch (key) { case "newScript": await chrome.storage.local.set({ @@ -446,7 +486,7 @@ function App() { className="tw-flex tw-flex-row tw-items-center" onClick={(e) => { e.stopPropagation(); - window.open(`https://scriptcat.org/search?domain=${urlHost}`, "_blank"); + getMoreScriptWindowOpen(currentUrl, defaultScriptProvider); }} > @@ -454,9 +494,9 @@ function App() { } > - ScriptCat - Greasy Fork - OpenUserJS + {"ScriptCat"} + {"Greasy Fork"} + {"OpenUserJS"}