Small js fixes

This commit is contained in:
2021-05-24 15:46:02 +02:00
parent 9987086bf0
commit 2b2a9a6758
2 changed files with 27 additions and 29 deletions

View File

@ -76,8 +76,6 @@ export class App {
console.warn(`Invalid url hash #${hash}`); console.warn(`Invalid url hash #${hash}`);
return defaultCase(); return defaultCase();
} }
//this.setHash(hash);
} }
async login(accountName, domainName, password) { async login(accountName, domainName, password) {
@ -161,7 +159,7 @@ export class App {
<button type="submit">Login</button> <button type="submit">Login</button>
</form>`; </form>`;
win.getElementsByTagName("form")[0].addEventListener("submit", async (event) => { win.getElementsByTagName("form")[0].addEventListener("submit", async event => {
event.preventDefault(); event.preventDefault();
const form = event.target; const form = event.target;
for (const e of form) { for (const e of form) {
@ -169,28 +167,24 @@ export class App {
e.removeAttribute("invalid"); e.removeAttribute("invalid");
} }
for (const d of form.getElementsByTagName("div")) {
form.removeChild(d);
}
function formError(msg) {
const div = document.createElement("div");
div.classList.add("error");
div.innerText = msg;
form.appendChild(div);
}
try { try {
await this.login(form.account.value, form.domain.value, form.password.value); try {
await this.login(form.account.value, form.domain.value, form.password.value);
} finally {
for (const d of form.getElementsByTagName("div")) form.removeChild(d);
for (const e of form) e.disabled = false;
}
} catch (error) { } catch (error) {
if (error.toString() === "Error: unable to authenticate") { if (error.toString() === "Error: unable to authenticate") {
document.getElementsByName("account")[0].setAttribute("invalid", "invalid"); document.getElementsByName("account")[0].setAttribute("invalid", "invalid");
} else { } else {
document.getElementsByName("domain")[0].setAttribute("invalid", "invalid"); document.getElementsByName("domain")[0].setAttribute("invalid", "invalid");
} }
formError(error.toString());
} finally { const div = document.createElement("div");
for (const e of form) e.disabled = false; div.classList.add("error");
div.innerText = error.toString();
form.appendChild(div);
} }
}); });
@ -217,13 +211,14 @@ export class App {
</div>`; </div>`;
const input = document.getElementsByName("message")[0]; const input = document.getElementsByName("message")[0];
input.addEventListener("keyup", (event) => { input.addEventListener("keyup", async event => {
if (event.key === "Enter" && input.value.length > 0) { if (event.key === "Enter" && input.value.length > 0) {
this.session.sendEvent("60nc0XXDIYUh6QzX4p0rMpCdzDmxghZLZk8dLuQh628", {
message: input.value,
});
this.addMessage(input.value); this.addMessage(input.value);
const val = input.value;
input.value = ""; input.value = "";
await this.session.sendEvent("60nc0XXDIYUh6QzX4p0rMpCdzDmxghZLZk8dLuQh628", {
message: val,
});
} }
}); });

View File

@ -25,7 +25,7 @@ export class Domain {
const controller = new AbortController(); const controller = new AbortController();
const timer = setTimeout(() => controller.abort(), 2000); const timer = setTimeout(() => controller.abort(), 2000);
let response = await fetch(`https://${domainName}/.well-known/usimp.json`, { const response = await fetch(`https://${domainName}/.well-known/usimp.json`, {
redirect: "manual", redirect: "manual",
signal: controller.signal, signal: controller.signal,
}); });
@ -166,7 +166,7 @@ export class Session {
} }
const startTime = new Date(); const startTime = new Date();
let response = await fetch(`${this.httpBaseUrl}/${endpoint}`, { const response = await fetch(`${this.httpBaseUrl}/${endpoint}`, {
method: "POST", method: "POST",
headers: headers, headers: headers,
body: JSON.stringify(data), body: JSON.stringify(data),
@ -175,7 +175,7 @@ export class Session {
const endTime = new Date(); const endTime = new Date();
clearTimeout(timer); clearTimeout(timer);
let responseData = await response.json(); const responseData = await response.json();
responseData.duration = endTime - startTime; responseData.duration = endTime - startTime;
return responseData; return responseData;
} }
@ -183,16 +183,19 @@ export class Session {
async ping() { async ping() {
let result = {"http": null, "ws": null}; let result = {"http": null, "ws": null};
const res = await this.send("ping", {}, undefined, true); const resHttp = await this.send("ping", {}, undefined, true);
result.http = res.duration; result.http = resHttp.duration;
if (this.wsConnection) { if (this.wsConnection) {
await this.wsConnection.ping(); const resWs = await this.send("ping", {});
result.ws = resWs.duration;
} }
return result; return result;
} }
async authenticate(accountName, password) { async authenticate(accountName, password) {
let response = await this.send("authenticate", { const response = await this.send("authenticate", {
type: "password", type: "password",
name: accountName, name: accountName,
password: password, password: password,