///
// 打开设置界面时触发
async function onSettingWindowCreated (view: Element) {
window.llonebot.log('setting window created')
const isEmpty = (value: any) => value === undefined || value === null || value === ''
const config = await window.llonebot.getConfig()
const httpClass = 'http'
const httpPostClass = 'http-post'
const wsClass = 'ws'
const reverseWSClass = 'reverse-ws'
const llonebotError = await window.llonebot.getError()
window.llonebot.log('获取error' + JSON.stringify(llonebotError))
function createHttpHostEleStr (host: string) {
const eleStr = `
HTTP事件上报地址(http)
`
return eleStr
}
function createWsHostEleStr (host: string) {
const eleStr = `
反向websocket地址:
`
return eleStr
}
let httpHostsEleStr = ''
for (const host of config.ob11.httpHosts) {
httpHostsEleStr += createHttpHostEleStr(host)
}
let wsHostsEleStr = ''
for (const host of config.ob11.wsHosts) {
wsHostsEleStr += createWsHostEleStr(host)
}
const html = `
`
const parser = new DOMParser()
const doc = parser.parseFromString(html, 'text/html')
const getError = async () => {
const llonebotError = await window.llonebot.getError()
console.log(llonebotError)
const llonebotErrorEle = document.getElementById('llonebotError')
const ffmpegErrorEle = document.getElementById('ffmpegError')
const otherErrorEle = document.getElementById('otherError')
if (llonebotError.otherError || llonebotError.ffmpegError) {
llonebotErrorEle.style.display = ''
} else {
llonebotErrorEle.style.display = 'none'
}
if (llonebotError.ffmpegError) {
const errContentEle = doc.querySelector('#ffmpegError .err-content')
// const errContent = ffmpegErrorEle.getElementsByClassName("err-content")[0];
errContentEle.textContent = llonebotError.ffmpegError;
(ffmpegErrorEle).style.display = ''
} else {
ffmpegErrorEle.style.display = ''
}
if (llonebotError.otherError) {
const errContentEle = doc.querySelector('#otherError .err-content')
errContentEle.textContent = llonebotError.otherError
otherErrorEle.style.display = ''
} else {
otherErrorEle.style.display = 'none'
}
}
function addHostEle (type: string, initValue: string = '') {
let addressEle, hostItemsEle
if (type === 'ws') {
const addressDoc = parser.parseFromString(createWsHostEleStr(initValue), 'text/html')
addressEle = addressDoc.querySelector('setting-item')
hostItemsEle = document.getElementById('wsHostItems')
} else {
const addressDoc = parser.parseFromString(createHttpHostEleStr(initValue), 'text/html')
addressEle = addressDoc.querySelector('setting-item')
hostItemsEle = document.getElementById('httpHostItems')
}
hostItemsEle.appendChild(addressEle)
}
doc.getElementById('addHttpHost').addEventListener('click', () => {
addHostEle('http')
})
doc.getElementById('addWsHost').addEventListener('click', () => {
addHostEle('ws')
})
doc.getElementById('messagePostFormat').addEventListener('selected', (e: CustomEvent) => {
config.ob11.messagePostFormat = e.detail && !isEmpty(e.detail.value) ? e.detail.value : 'array'
window.llonebot.setConfig(config)
})
function switchClick (eleId: string, configKey: string, _config = null) {
if (!_config) {
_config = config
}
doc.getElementById(eleId)?.addEventListener('click', (e) => {
const switchEle = e.target as HTMLInputElement
if (_config[configKey]) {
_config[configKey] = false
switchEle.removeAttribute('is-active')
} else {
_config[configKey] = true
switchEle.setAttribute('is-active', '')
}
// 妈蛋,手动操作DOM越写越麻烦,要不用vue算了
const keyClassMap = {
enableHttp: httpClass,
enableHttpPost: httpPostClass,
enableWs: wsClass,
enableWsReverse: reverseWSClass
}
for (const e of document.getElementsByClassName(keyClassMap[configKey])) {
(e as HTMLElement).style.display = _config[configKey] ? '' : 'none'
}
window.llonebot.setConfig(config)
})
}
switchClick('http', 'enableHttp', config.ob11)
switchClick('httpPost', 'enableHttpPost', config.ob11)
switchClick('websocket', 'enableWs', config.ob11)
switchClick('websocketReverse', 'enableWsReverse', config.ob11)
switchClick('debug', 'debug')
switchClick('switchFileUrl', 'enableLocalFile2Url')
switchClick('reportSelfMessage', 'reportSelfMessage')
switchClick('log', 'log')
switchClick('autoDeleteFile', 'autoDeleteFile')
doc.getElementById('save')?.addEventListener('click',
() => {
const httpPortEle: HTMLInputElement = document.getElementById('httpPort') as HTMLInputElement
const httpHostEles: HTMLCollectionOf = document.getElementsByClassName('httpHost') as HTMLCollectionOf
const wsPortEle: HTMLInputElement = document.getElementById('wsPort') as HTMLInputElement
const wsHostEles: HTMLCollectionOf = document.getElementsByClassName('wsHost') as HTMLCollectionOf
const tokenEle = document.getElementById('token') as HTMLInputElement
const ffmpegPathEle = document.getElementById('ffmpegPath') as HTMLInputElement
// 获取端口和host
const httpPort = httpPortEle.value
const httpHosts: string[] = []
for (const hostEle of httpHostEles) {
const value = hostEle.value.trim()
value && httpHosts.push(value)
}
const wsPort = wsPortEle.value
const token = tokenEle.value.trim()
const wsHosts: string[] = []
for (const hostEle of wsHostEles) {
const value = hostEle.value.trim()
value && wsHosts.push(value)
}
config.ob11.httpPort = parseInt(httpPort)
config.ob11.httpHosts = httpHosts
config.ob11.wsPort = parseInt(wsPort)
config.ob11.wsHosts = wsHosts
config.token = token
config.ffmpeg = ffmpegPathEle.value.trim()
window.llonebot.setConfig(config)
setTimeout(() => {
getError().then()
}, 1000)
alert('保存成功')
})
doc.getElementById('selectFFMPEG')?.addEventListener('click', () => {
window.llonebot.selectFile().then(selectPath => {
if (selectPath) {
config.ffmpeg = (document.getElementById('ffmpegPath') as HTMLInputElement).value = selectPath
// window.llonebot.setConfig(config);
}
})
})
// 自动保存删除文件延时时间
const autoDeleteMinEle = doc.getElementById('autoDeleteMin') as HTMLInputElement
let st = null
autoDeleteMinEle.addEventListener('change', () => {
if (st) {
clearTimeout(st)
}
st = setTimeout(() => {
console.log('auto delete file minute change')
config.autoDeleteFileSecond = parseInt(autoDeleteMinEle.value) || 1
window.llonebot.setConfig(config)
}, 1000)
})
doc.body.childNodes.forEach(node => {
view.appendChild(node)
})
}
function init () {
const hash = location.hash
if (hash === '#/blank') {
}
}
if (location.hash === '#/blank') {
(window as any).navigation.addEventListener('navigatesuccess', init, { once: true })
} else {
init()
}
export {
onSettingWindowCreated
}