Files

155 lines
6.5 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
const api = window.xhchatBrowser
const tabs = document.querySelector('#tabs')
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)
const rows = entries.map((entry) => {
const row = document.createElement('li')
const open = document.createElement('button')
const title = document.createElement('strong')
const url = document.createElement('span')
title.textContent = entry.title || entry.url
url.textContent = entry.url
open.append(title, url)
open.onclick = () => void command('openEntry', entry.id)
row.append(open)
if (removable) {
const remove = document.createElement('button')
remove.textContent = '移除'
remove.onclick = () => void command('removeBookmark', entry.id)
row.append(remove)
}
return row
})
if (!rows.length) {
const empty = document.createElement('li')
empty.textContent = removable ? '暂无书签' : '暂无历史记录'
rows.push(empty)
}
container.replaceChildren(...rows)
}
function render(state) {
current = state
tabs.replaceChildren(...state.tabs.map((tab) => {
const element = document.createElement('span')
element.className = tab.id === state.active ? 'tab active' : 'tab'
const title = document.createElement('button')
title.textContent = tab.title
title.setAttribute('aria-pressed', String(tab.id === state.active))
title.onclick = () => { address.blur(); void command('activate', tab.id) }
const close = document.createElement('button')
close.textContent = '×'
close.setAttribute('aria-label', '关闭 ' + tab.title)
close.onclick = () => void command('close', tab.id)
element.append(title, close)
return element
}))
if (document.activeElement !== address) address.value = state.url
button('back').disabled = !state.back
button('forward').disabled = !state.forward
button('reload').textContent = state.loading ? '×' : '↻'
button('reload').setAttribute('aria-label', state.loading ? '停止加载' : '刷新')
for (const action of ['copy', 'external', 'zoomIn', 'zoomOut']) button(action).disabled = !state.url
document.querySelector('#mode').textContent = state.privateMode ? '隐私窗口' : ''
button('bookmark').hidden = state.privateMode
button('bookmark').disabled = !state.url
button('bookmark').textContent = state.bookmarked ? '取消书签' : '添加书签'
button('library').hidden = state.privateMode
document.querySelector('#show-downloads').hidden = state.privateMode
document.querySelector('#show-permissions').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('#show-permissions').setAttribute('aria-pressed', String(state.libraryMode && panel === 'permissions'))
document.querySelector('#library').hidden = !(state.libraryMode && panel === 'library')
document.querySelector('#download-panel').hidden = !(state.libraryMode && panel === 'downloads')
document.querySelector('#permission-panel').hidden = !(state.libraryMode && panel === 'permissions')
document.querySelector('#permission-origin').textContent = state.origin || '请先打开一个网站。'
document.querySelector('#site-grants').disabled = !state.origin
for (const input of document.querySelectorAll('#site-grants input')) input.checked = state.grants[input.value] === true
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
}
async function command(action, value) {
try {
render(await api.command(action, value))
} catch (error) {
if (current) render(current)
status.textContent = error.message
}
}
for (const element of document.querySelectorAll('[data-action]')) {
element.onclick = () => {
const action = element.dataset.action
void command(action === 'reload' && current?.loading ? 'stop' : 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('#show-permissions').onclick = () => showPanel('permissions')
for (const input of document.querySelectorAll('#site-grants input')) {
input.onchange = () => void command('siteGrant', [current.origin, input.value, input.checked ? 'allow' : 'block'].join('\n'))
}
document.querySelector('#address-form').onsubmit = (event) => {
event.preventDefault()
const value = address.value
address.blur()
void command('go', value)
}
api.onState(render)
void command('state')