细节调整及优化(26.8.3)

This commit is contained in:
zhongluofeng
2026-08-21 17:25:47 +08:00
parent 0a19b4b38a
commit 4dd60f42a1
72 changed files with 4779 additions and 1395 deletions
@@ -358,6 +358,11 @@ async function addDownload(url, filename, referer, cookies, headers) {
}
// ===== 下载拦截 =====
// 处理中的 URL(防止同一 URL 并发/重入,也避免与引擎去重检查竞态)
const processingUrls = new Set()
// 我们自己用 chrome.downloads.download 回退创建的下载 URL(短时间内跳过,防止再次被拦截形成死循环)
const fallbackUrls = new Map() // url -> 过期时间戳
async function shouldIntercept(downloadItem) {
const config = await getConfig()
if (!config.interceptDownload) return false
@@ -373,27 +378,94 @@ async function shouldIntercept(downloadItem) {
return true
}
async function handleDownloadCreated(downloadItem) {
if (!await shouldIntercept(downloadItem)) return
/**
* 判断是否为"历史下载"(安装插件之前就已存在、随后被浏览器恢复的旧下载)
* 这类下载一律不接管,交给浏览器原生处理,实现"只接管以后的下载,历史都不管"
*/
async function isHistoricalDownload(item) {
// 1) canResume=true 表示已存在有效的部分文件,说明浏览器在恢复旧下载
if (item.canResume) return true
// 2) 开始时间早于插件首次安装时间(浏览器重启后恢复的旧下载会保留原来的开始时间)
try {
await chrome.downloads.cancel(downloadItem.id)
await chrome.downloads.erase({ id: downloadItem.id })
const start = item.startTime ? new Date(item.startTime).getTime() : 0
if (start > 0) {
const stored = await chrome.storage.local.get('installTime')
const installTime = stored.installTime || 0
if (installTime > 0 && start < installTime) return true
}
} catch { /* ignore */ }
return false
}
const url = downloadItem.finalUrl || downloadItem.url
const filename = downloadItem.filename || ''
/**
* 查询引擎是否已有同 URL 的非终态任务(活跃/排队/暂停)
* 用于防止同一下载被重复转发、重复下载
*/
async function hasExistingTask(url) {
try {
await addDownload(url, filename, downloadItem.referrer, '')
} catch (e) {
// 添加失败:回退到浏览器自带下载,不弹通知
try { await chrome.downloads.download({ url }) } catch { /* ignore */ }
const tasks = await apiRequest('/api/downloads')
return tasks.some(t => {
const s = t.status
if (s === 'complete' || s === 'error') return false
return t.url === url
})
} catch {
return false
}
}
// ===== 右键菜单 =====
chrome.runtime.onInstalled.addListener(() => {
async function handleDownloadCreated(downloadItem) {
const url = downloadItem.finalUrl || downloadItem.url
// 回退下载:我们自己用 chrome.downloads.download 创建的,直接跳过,避免无限循环
const fbExp = fallbackUrls.get(url)
if (fbExp && Date.now() < fbExp) {
fallbackUrls.delete(url)
return
}
// 历史下载(安装前的旧下载被浏览器恢复)一律不接管
if (await isHistoricalDownload(downloadItem)) return
if (!await shouldIntercept(downloadItem)) return
// 同一 URL 已在处理中,跳过(防并发/防重复转发)
if (processingUrls.has(url)) return
// 引擎不可达时不接管(保留浏览器原生下载),避免取消后下载无处可去
let connected = false
try { connected = await testConnection() } catch { connected = false }
if (!connected) return
// 引擎已有同 URL 的非终态任务,不重复转发
if (await hasExistingTask(url)) return
processingUrls.add(url)
try {
try {
await chrome.downloads.cancel(downloadItem.id)
await chrome.downloads.erase({ id: downloadItem.id })
} catch { /* ignore */ }
const filename = downloadItem.filename || ''
try {
await addDownload(url, filename, downloadItem.referrer, '')
} catch (e) {
// 添加失败:回退为浏览器自带下载,并标记该 URL 短时间内跳过,防止再次被拦截形成死循环
fallbackUrls.set(url, Date.now() + 3000)
try { await chrome.downloads.download({ url }) } catch { /* ignore */ }
}
} finally {
processingUrls.delete(url)
}
}
// ===== 右键菜单 & 安装标记 =====
chrome.runtime.onInstalled.addListener(async (details) => {
// 记录首次安装时间:用于区分"安装前的历史下载"(被浏览器恢复的旧下载)与"安装后的新下载"
if (details.reason === 'install') {
await chrome.storage.local.set({ installTime: Date.now() })
}
chrome.contextMenus.create({
id: 'thing-download-link',
title: '使用 Thing 下载此链接',
@@ -1,7 +1,7 @@
{
"manifest_version": 3,
"name": "Thing Extension",
"version": "0.2.0",
"version": "0.30",
"description": "发送浏览器下载到 Thing 下载引擎,嗅探网页资源。",
"icons": {
"16": "icons/icon-16.png",