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 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 ? '隐私窗口' : '' 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')