实现浏览器下载管理和本地下载记录
This commit is contained in:
1 parent
ae04cc9494
commit
9b8c2026ea
8 files changed
+155
-13
No files matched your search
@@ -9,7 +9,7 @@
|
||||
</head>
|
||||
<body>
|
||||
<header>
|
||||
<div class="tabs"><div id="tabs" aria-label="标签页"></div><button data-action="new" aria-label="新建标签页">+</button><span id="mode"></span><button data-action="bookmark">添加书签</button><button data-action="library">书签与历史</button></div>
|
||||
<div class="tabs"><div id="tabs" aria-label="标签页"></div><button data-action="new" aria-label="新建标签页">+</button><span id="mode"></span><button data-action="bookmark">添加书签</button><button data-action="library">书签与历史</button><button id="show-downloads">下载</button></div>
|
||||
<form id="address-form">
|
||||
<button type="button" data-action="back" aria-label="后退">←</button>
|
||||
<button type="button" data-action="forward" aria-label="前进">→</button>
|
||||
@@ -30,5 +30,10 @@
|
||||
<section><h1>书签</h1><ul id="bookmarks"></ul></section>
|
||||
<section><h1>浏览历史</h1><ul id="history"></ul></section>
|
||||
</main>
|
||||
<section id="download-panel" hidden>
|
||||
<h1>下载记录</h1>
|
||||
<button data-action="clearDownloads">清除下载记录</button>
|
||||
<ul id="download-list"></ul>
|
||||
</section>
|
||||
</body>
|
||||
</html>
|
||||
@@ -23,4 +23,9 @@ input { flex: 1; min-width: 0; }
|
||||
#library li { display: flex; gap: 6px; align-items: start; margin-bottom: 6px; }
|
||||
#library li button:first-child { display: grid; flex: 1; text-align: left; white-space: normal; overflow-wrap: anywhere; }
|
||||
#library li span { font-size: 11px; opacity: .78; }
|
||||
#download-panel { padding: 18px; }
|
||||
#download-panel h1 { margin: 0 0 10px; font-size: 18px; }
|
||||
#download-list { list-style: none; padding: 0; }
|
||||
#download-list li { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin-bottom: 8px; }
|
||||
#download-list strong { overflow-wrap: anywhere; }
|
||||
@media (max-width: 750px) { form [data-action="zoomIn"], form [data-action="zoomOut"] { display: none; } }
|
||||
+51
-3
@@ -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
|
||||
|
||||
Reference in new issue
Block a user