Files
XHChat/Desktop/browser/ui.js
T

96 lines
3.5 KiB
JavaScript
Raw 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
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
button('library').setAttribute('aria-pressed', String(state.libraryMode))
document.querySelector('#library').hidden = !state.libraryMode
if (state.libraryMode) {
renderEntries('#bookmarks', state.bookmarks, true)
renderEntries('#history', state.history, false)
}
status.textContent = state.error
}
async function command(action, value) {
try {
render(await api.command(action, value))
} catch (error) {
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)
}
}
document.querySelector('#address-form').onsubmit = (event) => {
event.preventDefault()
const value = address.value
address.blur()
void command('go', value)
}
api.onState(render)
void command('state')