修复(半自动注入): 扩展白名单覆盖本机所有端口并显式提示未安装状态

- manifest content_scripts.matches 改为 http://localhost/* 与 http://127.0.0.1/*,
  覆盖本机 Caddy(80/443) 与 Vite(5173) 等所有端口;此前只白名单 5173,
  本地从 http://localhost/ 打开后台时内容脚本不加载,注入既不写 Cookie 也不跳转。
- background 的来源校验从「精确 origin 集合」改为「主机名集合」,
  避免同一后台在不同端口下被判定为未授权来源。
- 版本升到 1.0.1;README 补充本地端口与重新构建/重新加载的说明。
- 后台页在扩展未检测到时展示常驻提示,并在点击注入时说明当前 origin、
  构建与安装步骤,明确「不会写入也不会跳转」,不再静默无反应。
This commit is contained in:
yml
2026-11-30 23:57:32 +08:00
parent eaf6296430
commit f9e50c499c
3 changed files with 57 additions and 14 deletions
@@ -12,25 +12,29 @@ const PLATFORM_DEFAULT_DOMAIN = {
weibo: '.weibo.com', weibo: '.weibo.com',
} }
// 允许触发注入的来源站点:直接从 manifest 内容脚本白名单推导,避免两处配置漂移。 // 允许触发注入的来源:直接从 manifest 内容脚本白名单推导主机,避免两处配置漂移。
const ALLOWED_ORIGINS = (() => { // 只比主机不比端口:同一台机器上后台可能跑在 Caddy(80/443)、Vite(5173) 或隧道域名上。
const ALLOWED_HOSTS = (() => {
const manifest = chrome.runtime.getManifest() const manifest = chrome.runtime.getManifest()
const patterns = (manifest.content_scripts || []).flatMap((entry) => entry.matches || []) const patterns = (manifest.content_scripts || []).flatMap((entry) => entry.matches || [])
const origins = new Set() const hosts = []
for (const pattern of patterns) { for (const pattern of patterns) {
try { try {
origins.add(new URL(pattern.replace(/\/\*$/, '/')).origin) const { hostname } = new URL(pattern.replace(/\/\*$/, '/'))
if (hostname) hosts.push(hostname.toLowerCase())
} catch (e) { } catch (e) {
// 形如 *://*.example.com/* 的通配主机无法解析,忽略 // 形如 *://*.example.com/* 的通配主机无法解析,忽略
} }
} }
return origins return hosts
})() })()
function originAllowed(url) { function originAllowed(url) {
if (!url) return false if (!url) return false
try { 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) { } catch (e) {
return false return false
} }
@@ -1,9 +1,13 @@
{ {
"manifest_version": 3, "manifest_version": 3,
"name": "半自动兑换助手", "name": "半自动兑换助手",
"version": "1.0.0", "version": "1.0.1",
"description": "配合 order_site 后台使用:把所选虎牙/微博账号的登录态写入本机浏览器,兑换操作仍由人工完成。", "description": "配合 order_site 后台使用:把所选虎牙/微博账号的登录态写入本机浏览器,兑换操作仍由人工完成。",
"permissions": ["cookies", "scripting", "activeTab"], "permissions": [
"cookies",
"scripting",
"activeTab"
],
"host_permissions": [ "host_permissions": [
"*://*.huya.com/*", "*://*.huya.com/*",
"*://huya.com/*", "*://huya.com/*",
@@ -15,10 +19,12 @@
"content_scripts": [ "content_scripts": [
{ {
"matches": [ "matches": [
"http://localhost:5173/*", "http://localhost/*",
"http://127.0.0.1:5173/*" "http://127.0.0.1/*"
],
"js": [
"relay.js"
], ],
"js": ["relay.js"],
"run_at": "document_start" "run_at": "document_start"
} }
], ],
@@ -163,6 +163,12 @@ export default function AdminSemiAutoTasksPage() {
const [completeFiles, setCompleteFiles] = useState<UploadedFile[]>([]) const [completeFiles, setCompleteFiles] = useState<UploadedFile[]>([])
const [completeForm] = Form.useForm<CompleteFormValues>() const [completeForm] = Form.useForm<CompleteFormValues>()
const pendingInjectionRef = useRef<PendingInjection | null>(null) const pendingInjectionRef = useRef<PendingInjection | null>(null)
// 扩展内容脚本只在白名单页面上打标记;没有标记说明未安装或当前站点不在白名单。
const [helperReady, setHelperReady] = useState(false)
useEffect(() => {
setHelperReady(isSemiAutoExchangeHelperReady(document.documentElement?.dataset))
}, [])
// tab 即平台:URL 上没有 tab 或值非法时落到虎牙,刷新/分享链接后仍是同一个列表。 // tab 即平台:URL 上没有 tab 或值非法时落到虎牙,刷新/分享链接后仍是同一个列表。
const tab = resolveSemiAutoPlatformTab(searchParams.get('tab')) const tab = resolveSemiAutoPlatformTab(searchParams.get('tab'))
@@ -319,9 +325,21 @@ export default function AdminSemiAutoTasksPage() {
// 扩展内容脚本会在白名单页面打标记;没有标记时直接提示 + 提供复制 CK 兜底,不静默失败。 // 扩展内容脚本会在白名单页面打标记;没有标记时直接提示 + 提供复制 CK 兜底,不静默失败。
if (!isSemiAutoExchangeHelperReady(document.documentElement?.dataset)) { if (!isSemiAutoExchangeHelperReady(document.documentElement?.dataset)) {
modal.warning({ modal.warning({
title: '未检测到「半自动兑换助手」扩展', title: '未检测到「半自动兑换助手」扩展,本次不会写入也不会跳转',
content: content: (
'请先在 Chrome / Edge 安装并启用「半自动兑换助手」,然后刷新本页再注入;也可以直接复制账号 CK 手工兑换。', <div>
<p>
当前页面地址 <Text code>{window.location.origin}</Text>{' '}
不在扩展白名单内,或扩展未安装/未启用。
</p>
<p>
处理方式:执行 <Text code>node build.mjs</Text>(生产加{' '}
<Text code>--env-file .env</Text>)生成 <Text code>dist/</Text>,在{' '}
<Text code>chrome://extensions</Text> 加载并启用后刷新本页。
</p>
<p>也可以直接复制账号 CK 手工兑换。</p>
</div>
),
okText: '复制 CK', okText: '复制 CK',
cancelText: '我知道了', cancelText: '我知道了',
onOk: () => credentialExportMutation.mutateAsync(task.id), onOk: () => credentialExportMutation.mutateAsync(task.id),
@@ -734,6 +752,21 @@ export default function AdminSemiAutoTasksPage() {
title="半自动履约" title="半自动履约"
description="人工客服渠道下的平台账号授权、扫码绑定和浏览器辅助履约。" description="人工客服渠道下的平台账号授权、扫码绑定和浏览器辅助履约。"
/> />
{helperReady ? null : (
<Alert
type="warning"
showIcon
message="未检测到「半自动兑换助手」扩展,注入浏览器不会写入 Cookie,也不会跳转平台页"
description={
<span>
当前地址 <Text code>{window.location.origin}</Text> 需在扩展白名单内:执行{' '}
<Text code>node build.mjs</Text> 生成 <Text code>dist/</Text> 后在{' '}
<Text code>chrome://extensions</Text> 加载,然后刷新本页。临时可用「复制
CK」手工兑换。
</span>
}
/>
)}
<Tabs <Tabs
activeKey={tab} activeKey={tab}
onChange={changeTab} onChange={changeTab}