实现浏览器下载管理和本地下载记录

This commit is contained in:
xhgit committed 2026-11-13 19:23:20 +08:00
1 parent ae04cc9494
commit 9b8c2026ea
8 files changed
+155 -13

No files matched your search

+49 -3
View File
@@ -1,8 +1,8 @@
import { app, BrowserWindow, clipboard, dialog, ipcMain, Menu, session, shell, WebContentsView } from 'electron'
import type { Session } from 'electron'
import type { DownloadItem, Session } from 'electron'
import { randomUUID } from 'node:crypto'
import { join } from 'node:path'
import { clearHistory, library, removeBookmark, toggleBookmark, visit } from './browser-library'
import { clearDownloads, clearHistory, library, recordDownload, removeBookmark, toggleBookmark, visit } from './browser-library'
const browserURL = 'xhchat://app/browser'
const height = 102
@@ -19,6 +19,7 @@ type BrowserWindowState = {
}
const windows = new Map<number, BrowserWindowState>()
const downloads = new Map<string, DownloadItem>()
export function website(value: string): URL | undefined {
try {
@@ -41,7 +42,7 @@ function address(value: string): URL | undefined {
function state(browser: BrowserWindowState) {
const contents = browser.active?.view.webContents
const collection = browser.privateMode ? { bookmarks: [], history: [] } : library()
const collection = browser.privateMode ? { bookmarks: [], history: [], downloads: [] } : library()
return {
privateMode: browser.privateMode,
active: browser.active?.id,
@@ -54,6 +55,13 @@ function state(browser: BrowserWindowState) {
libraryMode: browser.libraryMode,
bookmarked: collection.bookmarks.some((item) => item.url === contents?.getURL()),
...collection,
downloads: browser.privateMode ? [] : [
...[...downloads].map(([id, item]) => ({
id, name: item.getFilename(), state: item.isPaused() ? 'paused' : item.getState(),
received: item.getReceivedBytes(), total: item.getTotalBytes(), resumable: item.canResume(),
})),
...collection.downloads,
],
}
}
@@ -188,6 +196,21 @@ function configureSites(sites: Session, privateMode: boolean) {
return
}
item.setSaveDialogOptions({ title: '保存网页下载', buttonLabel: '保存' })
const id = randomUUID()
downloads.set(id, item)
item.on('updated', updateNormalWindows)
item.once('done', (_event, result) => {
downloads.delete(id)
if (result === 'completed' || result === 'cancelled' || result === 'interrupted') {
try {
recordDownload({ id, name: item.getFilename(), path: item.getSavePath(), state: result, time: Date.now() })
} catch {
if (browser.active) browser.active.error = '无法保存下载记录'
}
}
updateNormalWindows()
})
updateNormalWindows()
})
}
@@ -280,6 +303,29 @@ export function registerBrowserHandlers(trusted: Session) {
updateNormalWindows()
break
}
case 'downloadPause':
case 'downloadResume':
case 'downloadCancel': {
if (browser.privateMode || typeof value !== 'string') break
const item = downloads.get(value)
if (action === 'downloadPause' && item?.getState() === 'progressing') item.pause()
if (action === 'downloadResume' && item?.canResume()) item.resume()
if (action === 'downloadCancel') item?.cancel()
updateNormalWindows()
break
}
case 'showDownload': {
if (browser.privateMode || typeof value !== 'string') break
const item = library().downloads.find((entry) => entry.id === value && entry.state === 'completed')
if (item?.path) shell.showItemInFolder(item.path)
break
}
case 'clearDownloads': {
if (browser.privateMode) break
clearDownloads()
updateNormalWindows()
break
}
case 'openEntry': {
if (browser.privateMode || typeof value !== 'string') break
const { bookmarks, history } = library()