From 12a733f55661856c8a597fecb9d049308e1ab95e Mon Sep 17 00:00:00 2001 From: Arthur Lu Date: Mon, 12 Dec 2022 16:18:40 -0800 Subject: [PATCH] remove repeated code --- elements.js | 2 +- utils.js | 36 +++++++----------------------------- 2 files changed, 8 insertions(+), 30 deletions(-) diff --git a/elements.js b/elements.js index c4c4a73..132b5f5 100644 --- a/elements.js +++ b/elements.js @@ -75,7 +75,7 @@ class Instance extends HTMLElement { let instanceParagraph = document.createElement("p"); instanceParagraph.innerText = `CT | ${data.vmid} | ${data.name} | ${data.status}`; - instanceParagraph.style.color = data.status == "running" ? "#00ff00" : "#ff0000"; + instanceParagraph.style.color = data.status === "running" ? "#00ff00" : "#ff0000"; instanceDiv.append(instanceParagraph); } } diff --git a/utils.js b/utils.js index 0a73118..c4e5354 100644 --- a/utils.js +++ b/utils.js @@ -13,31 +13,7 @@ class NetworkError extends Error { } export async function requestTicket (username, password) { - let prms = new URLSearchParams({username: `${username}@pve`, password: password}); - - let content = { - method: "POST", - mode: "cors", - credentials: "include", - headers: { - "Content-Type": "application/x-www-form-urlencoded" - }, - body: prms.toString() - } - - let response = await fetch("https://pve.tronnet.net/api2/json/access/ticket", content) - .then((response) => { - if (!response.ok) { - throw new ResponseError(`recieved response status code ${response.status}`); - } - return response; - }) - .catch((error) => { - if (error instanceof ResponseError) { - throw error; - } - throw new NetworkError(error); - }); + let response = await request("/access/ticket", "POST", {username: `${username}@pve`, password: password}, auth = false); let data = await response.json(); return data; @@ -49,7 +25,7 @@ export function setTicket (ticket) { document.cookie = `PVEAuthCookie=${ticket}; path=/; expires=${d.toUTCString()}; domain=.tronnet.net`; } -export async function request (path, method, body) { +export async function request (path, method, body, auth = true) { let prms = new URLSearchParams(body); let content = { @@ -57,13 +33,15 @@ export async function request (path, method, body) { mode: "cors", credentials: "include", headers: { - "Content-Type": "application/x-www-form-urlencoded", - "Cookie": document.cookie + "Content-Type": "application/x-www-form-urlencoded" } } - if(method == "POST") { + if(method === "POST") { content.body = prms.toString(); } + if(auth) { + content.headers.Cookie = document.cookie; + } let response = await fetch(`https://pve.tronnet.net/api2/json${path}`, content) .then((response) => {