refactor(captcha): extract URL constants and improve wording

This commit is contained in:
WittF 2025-06-29 16:49:34 +08:00
parent 19b0968c8d
commit 732d7ee5f3
2 changed files with 11 additions and 5 deletions

View File

@ -441,7 +441,7 @@
"capAssetServerDes": "Choose the source for loading Cap captcha static assets. Using self-deployed server requires setting environment variables on the server side, please refer to <0>enable asset server</0>.",
"capAssetServerJsdelivr": "jsDelivr CDN",
"capAssetServerUnpkg": "unpkg CDN",
"capAssetServerInstance": "Self-deployed server",
"capAssetServerInstance": "Self-hosted server",
"captchaProvider": "Captcha provider",
"captchaWidth": "Width",
"captchaHeight": "Height",

View File

@ -4,6 +4,12 @@ import { useAppSelector } from "../../../redux/hooks.ts";
import { CaptchaParams } from "./Captcha.tsx";
import { Box, useTheme } from "@mui/material";
// Cap Widget URLs
const CAP_WASM_UNPKG_URL = "https://unpkg.com/@cap.js/wasm@0.0.4/browser/cap_wasm.js";
const CAP_WASM_JSDELIVR_URL = "https://cdn.jsdelivr.net/npm/@cap.js/wasm@0.0.4/browser/cap_wasm.min.js";
const CAP_WIDGET_UNPKG_URL = "https://unpkg.com/@cap.js/widget";
const CAP_WIDGET_JSDELIVR_URL = "https://cdn.jsdelivr.net/npm/@cap.js/widget";
export interface CapProps {
onStateChange: (state: CaptchaParams) => void;
generation: number;
@ -138,10 +144,10 @@ const CapCaptcha = ({ onStateChange, generation, fullWidth, ...rest }: CapProps
if (capAssetServer === "instance") {
(window as any).CAP_CUSTOM_WASM_URL = `${capInstanceURL.replace(/\/$/, "")}/assets/cap_wasm.min.js`;
} else if (capAssetServer === "unpkg") {
(window as any).CAP_CUSTOM_WASM_URL = "https://unpkg.com/@cap.js/wasm@0.0.4/browser/cap_wasm.js";
(window as any).CAP_CUSTOM_WASM_URL = CAP_WASM_UNPKG_URL;
} else {
// jsdelivr - 默认CDN
(window as any).CAP_CUSTOM_WASM_URL = "https://cdn.jsdelivr.net/npm/@cap.js/wasm@0.0.4/browser/cap_wasm.min.js";
(window as any).CAP_CUSTOM_WASM_URL = CAP_WASM_JSDELIVR_URL;
}
// Add a small delay to ensure DOM is ready
@ -159,10 +165,10 @@ const CapCaptcha = ({ onStateChange, generation, fullWidth, ...rest }: CapProps
if (capAssetServer === "instance") {
assetSource = `${capInstanceURL.replace(/\/$/, "")}/assets/widget.js`;
} else if (capAssetServer === "unpkg") {
assetSource = "https://unpkg.com/@cap.js/widget";
assetSource = CAP_WIDGET_UNPKG_URL;
} else {
// jsdelivr - 默认CDN
assetSource = "https://cdn.jsdelivr.net/npm/@cap.js/widget";
assetSource = CAP_WIDGET_JSDELIVR_URL;
}
script.src = assetSource;