///
// 打开设置界面时触发
async function onSettingWindowCreated(view: Element) {
window.llonebot.log("setting window created");
let config = await window.llonebot.getConfig()
function creatHostEleStr(host: string) {
let eleStr = `
事件上报地址(http)
`
return eleStr
}
let hostsEleStr = ""
for (const host of config.hosts) {
hostsEleStr += creatHostEleStr(host);
}
let html = `
`
const parser = new DOMParser();
const doc = parser.parseFromString(html, "text/html");
function addHostEle(initValue: string = "") {
let addressDoc = parser.parseFromString(creatHostEleStr(initValue), "text/html");
let addressEle = addressDoc.querySelector("setting-item")
let hostItemsEle = document.getElementById("hostItems");
hostItemsEle.appendChild(addressEle);
}
doc.getElementById("addHost").addEventListener("click", () => addHostEle())
function switchClick(eleId: string, configKey: string) {
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", "")
}
window.llonebot.setConfig(config)
})
}
switchClick("debug", "debug");
switchClick("switchBase64", "enableBase64");
switchClick("reportSelfMessage", "reportSelfMessage");
switchClick("log", "log");
doc.getElementById("save")?.addEventListener("click",
() => {
const portEle: HTMLInputElement = document.getElementById("port") as HTMLInputElement
const wsPortEle: HTMLInputElement = document.getElementById("wsPort") as HTMLInputElement
const hostEles: HTMLCollectionOf = document.getElementsByClassName("host") as HTMLCollectionOf;
const tokenEle = document.getElementById("token") as HTMLInputElement;
// const port = doc.querySelector("input[type=number]")?.value
// const host = doc.querySelector("input[type=text]")?.value
// 获取端口和host
const port = portEle.value
const wsPort = wsPortEle.value
const token = tokenEle.value
let hosts: string[] = [];
for (const hostEle of hostEles) {
if (hostEle.value) {
hosts.push(hostEle.value.trim());
}
}
config.port = parseInt(port);
config.wsPort = parseInt(wsPort);
config.hosts = hosts;
config.token = token.trim();
window.llonebot.setConfig(config);
alert("保存成功");
})
doc.body.childNodes.forEach(node => {
view.appendChild(node);
});
}
function init() {
let hash = location.hash;
if (hash === "#/blank") {
return;
}
}
if (location.hash === "#/blank") {
(window as any).navigation.addEventListener("navigatesuccess", init, {once: true});
} else {
init();
}
export {
onSettingWindowCreated
}