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

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

+51 -3
View File
@@ -4,6 +4,37 @@ const address = document.querySelector('#address')
const status = document.querySelector('#status')
const button = (action) => document.querySelector(`[data-action="${action}"]`)
let current
let panel = 'library'
function renderDownloads(entries) {
const rows = entries.map((item) => {
const row = document.createElement('li')
const name = document.createElement('strong')
const state = document.createElement('span')
name.textContent = item.name
const labels = { progressing: '下载中', paused: '已暂停', completed: '已完成', cancelled: '已取消', interrupted: '已中断' }
state.textContent = labels[item.state] ?? item.state
if (item.total > 0 && item.state === 'progressing') state.textContent += ' · ' + Math.floor(100 * item.received / item.total) + '%'
row.append(name, state)
function add(label, action) {
const button = document.createElement('button')
button.textContent = label
button.onclick = () => void command(action, item.id)
row.append(button)
}
if (item.state === 'progressing') add('暂停', 'downloadPause')
if (item.resumable) add('继续', 'downloadResume')
if (['progressing', 'paused'].includes(item.state)) add('取消', 'downloadCancel')
if (item.state === 'completed') add('在文件夹中显示', 'showDownload')
return row
})
if (!rows.length) {
const empty = document.createElement('li')
empty.textContent = '暂无下载记录'
rows.push(empty)
}
document.querySelector('#download-list').replaceChildren(...rows)
}
function renderEntries(selector, entries, removable) {
const container = document.querySelector(selector)
@@ -60,12 +91,16 @@ function render(state) {
button('bookmark').disabled = !state.url
button('bookmark').textContent = state.bookmarked ? '取消书签' : '添加书签'
button('library').hidden = state.privateMode
button('library').setAttribute('aria-pressed', String(state.libraryMode))
document.querySelector('#library').hidden = !state.libraryMode
if (state.libraryMode) {
document.querySelector('#show-downloads').hidden = state.privateMode
button('library').setAttribute('aria-pressed', String(state.libraryMode && panel === 'library'))
document.querySelector('#show-downloads').setAttribute('aria-pressed', String(state.libraryMode && panel === 'downloads'))
document.querySelector('#library').hidden = !(state.libraryMode && panel === 'library')
document.querySelector('#download-panel').hidden = !(state.libraryMode && panel === 'downloads')
if (state.libraryMode && panel === 'library') {
renderEntries('#bookmarks', state.bookmarks, true)
renderEntries('#history', state.history, false)
}
if (state.libraryMode && panel === 'downloads') renderDownloads(state.downloads)
status.textContent = state.error
}
@@ -84,6 +119,19 @@ for (const element of document.querySelectorAll('[data-action]')) {
}
}
function showPanel(next) {
if (current?.libraryMode && panel !== next) {
panel = next
render(current)
} else {
panel = next
void command('library')
}
}
button('library').onclick = () => showPanel('library')
document.querySelector('#show-downloads').onclick = () => showPanel('downloads')
document.querySelector('#address-form').onsubmit = (event) => {
event.preventDefault()
const value = address.value