Skip to content
Merged
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
68 changes: 54 additions & 14 deletions src/pages/popup/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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 })[]>([]);
Expand All @@ -84,6 +122,12 @@ function App() {
const [isEnableScript, setIsEnableScript] = useState(true);
const [isBlacklist, setIsBlacklist] = useState(false);
const [collapseActiveKey, setCollapseActiveKey] = useState<string[]>(["script"]);
const [defaultScriptProvider, setDefaultScriptProvider] = useState<ScriptProvider>(() => {
cacheInstance.get<ScriptProvider>("default_script_provider").then((value) => {
setDefaultScriptProvider(value || "scriptcat");
});
return "scriptcat";
});
const { t } = useTranslation();
const pageTabIdRef = useRef(0);

Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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<ScriptProvider>("default_script_provider", provider);
setDefaultScriptProvider(provider);
return getMoreScriptWindowOpen(currentUrl, provider);
}
switch (key) {
case "newScript":
await chrome.storage.local.set({
Expand Down Expand Up @@ -446,17 +486,17 @@ 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);
}}
>
<IconSearch style={iconStyle} />
{t("get_script")}
</span>
}
>
<Menu.Item key={`https://scriptcat.org/search?domain=${urlHost}`}>ScriptCat</Menu.Item>
<Menu.Item key={`https://greasyfork.org/scripts/by-site/${urlHost}`}>Greasy Fork</Menu.Item>
<Menu.Item key={`https://openuserjs.org/?q=${urlHost}`}>OpenUserJS</Menu.Item>
<Menu.Item key="get_more_script_scriptcat">{"ScriptCat"}</Menu.Item>
<Menu.Item key="get_more_script_greasyfork">{"Greasy Fork"}</Menu.Item>
<Menu.Item key="get_more_script_openuserjs">{"OpenUserJS"}</Menu.Item>
</Menu.SubMenu>
<Menu.Item key={"checkUpdate"} className="tw-flex tw-flex-row tw-items-center">
<IconSync style={iconStyle} />
Expand Down
Loading