Archived
66 lines
1.8 KiB
JavaScript
66 lines
1.8 KiB
JavaScript
const api = window.settingsAPI
|
|
const windowsInput = document.getElementById('windows')
|
|
const flashInput = document.getElementById('flash')
|
|
const soundInput = document.getElementById('sound')
|
|
const configPathText = document.getElementById('configPath')
|
|
const statusText = document.getElementById('status')
|
|
const saveButton = document.getElementById('save')
|
|
const testButton = document.getElementById('test')
|
|
const openConfigButton = document.getElementById('openConfig')
|
|
|
|
function values() {
|
|
return {
|
|
windows: !!windowsInput.checked,
|
|
flash: !!flashInput.checked,
|
|
sound: !!soundInput.checked,
|
|
}
|
|
}
|
|
|
|
function setStatus(text, error = false) {
|
|
statusText.textContent = text
|
|
statusText.classList.toggle('error', error)
|
|
}
|
|
|
|
async function load() {
|
|
try {
|
|
const config = await api.get()
|
|
windowsInput.checked = !!config.windows
|
|
flashInput.checked = !!config.flash
|
|
soundInput.checked = !!config.sound
|
|
configPathText.textContent = config.configPath
|
|
} catch (err) {
|
|
setStatus(`读取配置失败:${err && err.message ? err.message : err}`, true)
|
|
}
|
|
}
|
|
|
|
saveButton.addEventListener('click', async () => {
|
|
saveButton.disabled = true
|
|
try {
|
|
const config = await api.save(values())
|
|
configPathText.textContent = config.configPath
|
|
setStatus('已保存,立即生效。')
|
|
} catch (err) {
|
|
setStatus(`保存失败:${err && err.message ? err.message : err}`, true)
|
|
} finally {
|
|
saveButton.disabled = false
|
|
}
|
|
})
|
|
|
|
testButton.addEventListener('click', async () => {
|
|
testButton.disabled = true
|
|
try {
|
|
await api.test(values())
|
|
setStatus('测试通知已触发。')
|
|
} catch (err) {
|
|
setStatus(`测试失败:${err && err.message ? err.message : err}`, true)
|
|
} finally {
|
|
testButton.disabled = false
|
|
}
|
|
})
|
|
|
|
openConfigButton.addEventListener('click', () => {
|
|
api.openConfig()
|
|
})
|
|
|
|
load()
|