实现浏览器下载管理和本地下载记录
This commit is contained in:
1 parent
ae04cc9494
commit
9b8c2026ea
8 files changed
+155
-13
No files matched your search
@@ -4,7 +4,8 @@ import { readFileSync, renameSync, writeFileSync } from 'node:fs'
|
||||
import { join } from 'node:path'
|
||||
|
||||
export type Entry = { id: string; url: string; title: string; time: number }
|
||||
type Library = { bookmarks: Entry[]; history: Entry[] }
|
||||
export type DownloadRecord = { id: string; name: string; path: string; state: 'completed' | 'cancelled' | 'interrupted'; time: number }
|
||||
type Library = { bookmarks: Entry[]; history: Entry[]; downloads: DownloadRecord[] }
|
||||
|
||||
let cache: Library | undefined
|
||||
const file = () => join(app.getPath('userData'), 'browser-library.json')
|
||||
@@ -13,9 +14,12 @@ export function library(): Library {
|
||||
if (cache) return cache
|
||||
try {
|
||||
const saved = JSON.parse(readFileSync(file(), 'utf8')) as Partial<Library>
|
||||
cache = { bookmarks: parseEntries(saved.bookmarks), history: parseEntries(saved.history) }
|
||||
cache = { bookmarks: parseEntries(saved.bookmarks), history: parseEntries(saved.history),
|
||||
downloads: Array.isArray(saved.downloads) ? saved.downloads.slice(0, 150).filter((item) =>
|
||||
typeof item?.id === 'string' && typeof item.name === 'string' && typeof item.path === 'string' &&
|
||||
typeof item.time === 'number' && ['completed', 'cancelled', 'interrupted'].includes(item.state)) : [] }
|
||||
} catch {
|
||||
cache = { bookmarks: [], history: [] }
|
||||
cache = { bookmarks: [], history: [], downloads: [] }
|
||||
}
|
||||
return cache
|
||||
}
|
||||
@@ -64,3 +68,12 @@ export function removeBookmark(id: string) {
|
||||
export function clearHistory() {
|
||||
persist({ ...library(), history: [] })
|
||||
}
|
||||
|
||||
export function recordDownload(record: DownloadRecord) {
|
||||
const previous = library()
|
||||
persist({ ...previous, downloads: [record, ...previous.downloads].slice(0, 150) })
|
||||
}
|
||||
|
||||
export function clearDownloads() {
|
||||
persist({ ...library(), downloads: [] })
|
||||
}
|
||||
+49
-3
@@ -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()
|
||||
|
||||
Reference in new issue
Block a user