修复(半自动注入): 扩展白名单覆盖本机所有端口并显式提示未安装状态
- 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:
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user