diff --git a/apps/extensions/semi-auto-exchange-helper/background.js b/apps/extensions/semi-auto-exchange-helper/background.js index 2eb0d95c..502a1b57 100644 --- a/apps/extensions/semi-auto-exchange-helper/background.js +++ b/apps/extensions/semi-auto-exchange-helper/background.js @@ -12,25 +12,29 @@ const PLATFORM_DEFAULT_DOMAIN = { weibo: '.weibo.com', } -// 允许触发注入的来源站点:直接从 manifest 内容脚本白名单推导,避免两处配置漂移。 -const ALLOWED_ORIGINS = (() => { +// 允许触发注入的来源:直接从 manifest 内容脚本白名单推导主机,避免两处配置漂移。 +// 只比主机不比端口:同一台机器上后台可能跑在 Caddy(80/443)、Vite(5173) 或隧道域名上。 +const ALLOWED_HOSTS = (() => { const manifest = chrome.runtime.getManifest() const patterns = (manifest.content_scripts || []).flatMap((entry) => entry.matches || []) - const origins = new Set() + const hosts = [] for (const pattern of patterns) { try { - origins.add(new URL(pattern.replace(/\/\*$/, '/')).origin) + const { hostname } = new URL(pattern.replace(/\/\*$/, '/')) + if (hostname) hosts.push(hostname.toLowerCase()) } catch (e) { // 形如 *://*.example.com/* 的通配主机无法解析,忽略 } } - return origins + return hosts })() function originAllowed(url) { if (!url) return false try { - return ALLOWED_ORIGINS.has(new URL(url).origin) + const { hostname } = new URL(url) + const host = hostname.toLowerCase() + return ALLOWED_HOSTS.some((allowed) => host === allowed || host.endsWith(`.${allowed}`)) } catch (e) { return false } diff --git a/apps/extensions/semi-auto-exchange-helper/manifest.json b/apps/extensions/semi-auto-exchange-helper/manifest.json index 5b6b6d95..d8795a97 100644 --- a/apps/extensions/semi-auto-exchange-helper/manifest.json +++ b/apps/extensions/semi-auto-exchange-helper/manifest.json @@ -1,9 +1,13 @@ { "manifest_version": 3, "name": "半自动兑换助手", - "version": "1.0.0", + "version": "1.0.1", "description": "配合 order_site 后台使用:把所选虎牙/微博账号的登录态写入本机浏览器,兑换操作仍由人工完成。", - "permissions": ["cookies", "scripting", "activeTab"], + "permissions": [ + "cookies", + "scripting", + "activeTab" + ], "host_permissions": [ "*://*.huya.com/*", "*://huya.com/*", @@ -15,10 +19,12 @@ "content_scripts": [ { "matches": [ - "http://localhost:5173/*", - "http://127.0.0.1:5173/*" + "http://localhost/*", + "http://127.0.0.1/*" + ], + "js": [ + "relay.js" ], - "js": ["relay.js"], "run_at": "document_start" } ], diff --git a/apps/frontend/src/pages/admin/AdminSemiAutoTasksPage.tsx b/apps/frontend/src/pages/admin/AdminSemiAutoTasksPage.tsx index 54d6642c..3802c2f2 100644 --- a/apps/frontend/src/pages/admin/AdminSemiAutoTasksPage.tsx +++ b/apps/frontend/src/pages/admin/AdminSemiAutoTasksPage.tsx @@ -163,6 +163,12 @@ export default function AdminSemiAutoTasksPage() { const [completeFiles, setCompleteFiles] = useState([]) const [completeForm] = Form.useForm() const pendingInjectionRef = useRef(null) + // 扩展内容脚本只在白名单页面上打标记;没有标记说明未安装或当前站点不在白名单。 + const [helperReady, setHelperReady] = useState(false) + + useEffect(() => { + setHelperReady(isSemiAutoExchangeHelperReady(document.documentElement?.dataset)) + }, []) // tab 即平台:URL 上没有 tab 或值非法时落到虎牙,刷新/分享链接后仍是同一个列表。 const tab = resolveSemiAutoPlatformTab(searchParams.get('tab')) @@ -319,9 +325,21 @@ export default function AdminSemiAutoTasksPage() { // 扩展内容脚本会在白名单页面打标记;没有标记时直接提示 + 提供复制 CK 兜底,不静默失败。 if (!isSemiAutoExchangeHelperReady(document.documentElement?.dataset)) { modal.warning({ - title: '未检测到「半自动兑换助手」扩展', - content: - '请先在 Chrome / Edge 安装并启用「半自动兑换助手」,然后刷新本页再注入;也可以直接复制账号 CK 手工兑换。', + title: '未检测到「半自动兑换助手」扩展,本次不会写入也不会跳转', + content: ( +
+

+ 当前页面地址 {window.location.origin}{' '} + 不在扩展白名单内,或扩展未安装/未启用。 +

+

+ 处理方式:执行 node build.mjs(生产加{' '} + --env-file .env)生成 dist/,在{' '} + chrome://extensions 加载并启用后刷新本页。 +

+

也可以直接复制账号 CK 手工兑换。

+
+ ), okText: '复制 CK', cancelText: '我知道了', onOk: () => credentialExportMutation.mutateAsync(task.id), @@ -734,6 +752,21 @@ export default function AdminSemiAutoTasksPage() { title="半自动履约" description="人工客服渠道下的平台账号授权、扫码绑定和浏览器辅助履约。" /> + {helperReady ? null : ( + + 当前地址 {window.location.origin} 需在扩展白名单内:执行{' '} + node build.mjs 生成 dist/ 后在{' '} + chrome://extensions 加载,然后刷新本页。临时可用「复制 + CK」手工兑换。 + + } + /> + )}