Skip to content

Commit 87eac92

Browse files
authored
⚡️ 优化 获取脚本 的 多平台搜索引擎选择 (#1379)
* 修正 获取脚本 的 多平台搜索引擎选择 * lint
1 parent 982f09e commit 87eac92

1 file changed

Lines changed: 54 additions & 14 deletions

File tree

src/pages/popup/App.tsx

Lines changed: 54 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import { subscribeMessage } from "@App/pages/store/global";
2727
import type { TDeleteScript, TEnableScript, TScriptRunStatus } from "@App/app/service/queue";
2828
import { SCRIPT_RUN_STATUS_RUNNING } from "@App/app/repo/scripts";
2929
import { HookManager } from "@App/pkg/utils/hookManager";
30+
import { cacheInstance } from "@App/app/cache";
3031

3132
const CollapseItem = Collapse.Item;
3233

@@ -69,6 +70,43 @@ const updateList = (list: ScriptMenu[], update: TUpdateEntryFn, options: TUpdate
6970
return changed ? newList : list; // 如子项没任何变化,则返回原list参考
7071
};
7172

73+
type ScriptProvider = "scriptcat" | "greasyfork" | "openuserjs";
74+
75+
const getMoreScriptWindowOpen = (currentUrl: string, provider: ScriptProvider) => {
76+
let urlHost = "";
77+
if (currentUrl) {
78+
try {
79+
const url = new URL(currentUrl);
80+
if (url.hostname && url.protocol.startsWith("http")) {
81+
urlHost = url.hostname;
82+
}
83+
} catch (e: any) {
84+
console.warn(e); // 容错:URL 解析失败时忽略错误(不影响后续 UI)
85+
}
86+
}
87+
let link = "";
88+
if (provider === "greasyfork") {
89+
// www.google.com -> google.com
90+
urlHost = /[^.]+\.[^.]+$/.exec(urlHost)?.[0] || urlHost;
91+
}
92+
switch (provider) {
93+
case "scriptcat":
94+
link = !urlHost
95+
? "https://scriptcat.org/search"
96+
: `https://scriptcat.org/search?domain=${encodeURIComponent(urlHost)}`;
97+
break;
98+
case "greasyfork":
99+
link = !urlHost
100+
? "https://greasyfork.org/scripts/"
101+
: `https://greasyfork.org/scripts/by-site/${encodeURI(urlHost)}`;
102+
break;
103+
case "openuserjs":
104+
link = !urlHost ? "https://openuserjs.org/" : `https://openuserjs.org/?q=${encodeURIComponent(urlHost)}`;
105+
break;
106+
}
107+
window.open(link, "_blank");
108+
};
109+
72110
function App() {
73111
const [loading, setLoading] = useState(true);
74112
const [scriptList, setScriptList] = useState<(ScriptMenu & { menuUpdated?: number })[]>([]);
@@ -84,6 +122,12 @@ function App() {
84122
const [isEnableScript, setIsEnableScript] = useState(true);
85123
const [isBlacklist, setIsBlacklist] = useState(false);
86124
const [collapseActiveKey, setCollapseActiveKey] = useState<string[]>(["script"]);
125+
const [defaultScriptProvider, setDefaultScriptProvider] = useState<ScriptProvider>(() => {
126+
cacheInstance.get<ScriptProvider>("default_script_provider").then((value) => {
127+
setDefaultScriptProvider(value || "scriptcat");
128+
});
129+
return "scriptcat";
130+
});
87131
const { t } = useTranslation();
88132
const pageTabIdRef = useRef(0);
89133

@@ -130,16 +174,6 @@ function App() {
130174
};
131175
}, [backEnables]);
132176

133-
const urlHost = useMemo(() => {
134-
let url: URL | undefined;
135-
try {
136-
url = new URL(currentUrl);
137-
} catch (_: any) {
138-
// 容错:URL 解析失败时忽略错误(不影响后续 UI)
139-
}
140-
return url?.hostname ?? "";
141-
}, [currentUrl]);
142-
143177
useEffect(() => {
144178
const hookMgr = new HookManager();
145179

@@ -336,6 +370,12 @@ function App() {
336370
};
337371

338372
const handleMenuClick = async (key: string) => {
373+
if (key.startsWith("get_more_script_")) {
374+
const provider = key.replace("get_more_script_", "") as ScriptProvider;
375+
await cacheInstance.set<ScriptProvider>("default_script_provider", provider);
376+
setDefaultScriptProvider(provider);
377+
return getMoreScriptWindowOpen(currentUrl, provider);
378+
}
339379
switch (key) {
340380
case "newScript":
341381
await chrome.storage.local.set({
@@ -446,17 +486,17 @@ function App() {
446486
className="tw-flex tw-flex-row tw-items-center"
447487
onClick={(e) => {
448488
e.stopPropagation();
449-
window.open(`https://scriptcat.org/search?domain=${urlHost}`, "_blank");
489+
getMoreScriptWindowOpen(currentUrl, defaultScriptProvider);
450490
}}
451491
>
452492
<IconSearch style={iconStyle} />
453493
{t("get_script")}
454494
</span>
455495
}
456496
>
457-
<Menu.Item key={`https://scriptcat.org/search?domain=${urlHost}`}>ScriptCat</Menu.Item>
458-
<Menu.Item key={`https://greasyfork.org/scripts/by-site/${urlHost}`}>Greasy Fork</Menu.Item>
459-
<Menu.Item key={`https://openuserjs.org/?q=${urlHost}`}>OpenUserJS</Menu.Item>
497+
<Menu.Item key="get_more_script_scriptcat">{"ScriptCat"}</Menu.Item>
498+
<Menu.Item key="get_more_script_greasyfork">{"Greasy Fork"}</Menu.Item>
499+
<Menu.Item key="get_more_script_openuserjs">{"OpenUserJS"}</Menu.Item>
460500
</Menu.SubMenu>
461501
<Menu.Item key={"checkUpdate"} className="tw-flex tw-flex-row tw-items-center">
462502
<IconSync style={iconStyle} />

0 commit comments

Comments
 (0)