Instance host & secure fields

This commit is contained in:
CenTdemeern1 2025-01-14 13:10:29 +01:00
parent 0f935f5453
commit e68a498cb3
5 changed files with 58 additions and 14 deletions

View file

@ -4,6 +4,7 @@ export function parseHost(host: string): { host: string, secure: boolean } | nul
let parsedInstance = URL.parse(host); let parsedInstance = URL.parse(host);
parsedInstance ??= URL.parse("https://" + host); parsedInstance ??= URL.parse("https://" + host);
if (!parsedInstance?.host) return null; if (!parsedInstance?.host) return null;
if (!/https?:/.test(parsedInstance.protocol)) return null;
return { return {
host: parsedInstance.host, host: parsedInstance.host,
secure: parsedInstance.protocol === "https:" secure: parsedInstance.protocol === "https:"

View file

@ -9,6 +9,8 @@ export function initializeInstanceDetailsDialog(dialog: HTMLDialogElement): {
hideInstanceDetailsDialog: () => void, hideInstanceDetailsDialog: () => void,
populateInstanceDetailsDialog: ( populateInstanceDetailsDialog: (
instanceNameValue: string, instanceNameValue: string,
instanceHostValue: string,
instanceHostSecureValue: boolean,
instanceSoftwareValue: string, instanceSoftwareValue: string,
instanceIconValue: string | null instanceIconValue: string | null
) => void, ) => void,
@ -24,6 +26,14 @@ export function initializeInstanceDetailsDialog(dialog: HTMLDialogElement): {
if (!(instanceName instanceof HTMLInputElement)) if (!(instanceName instanceof HTMLInputElement))
throw new Error("#instanceName isn't an input"); throw new Error("#instanceName isn't an input");
const instanceHost = form.querySelector("#instanceHost");
if (!(instanceHost instanceof HTMLInputElement))
throw new Error("#instanceHost isn't an input");
const instanceHostSecure = form.querySelector("#instanceHostSecure");
if (!(instanceHostSecure instanceof HTMLInputElement))
throw new Error("#instanceHostSecure isn't an input");
const instanceSoftware = form.querySelector("#instanceSoftware"); const instanceSoftware = form.querySelector("#instanceSoftware");
if (!(instanceSoftware instanceof HTMLSelectElement)) if (!(instanceSoftware instanceof HTMLSelectElement))
throw new Error("#instanceSoftware isn't a select"); throw new Error("#instanceSoftware isn't a select");
@ -41,12 +51,17 @@ export function initializeInstanceDetailsDialog(dialog: HTMLDialogElement): {
const populateInstanceDetailsDialog = ( const populateInstanceDetailsDialog = (
instanceNameValue: string, instanceNameValue: string,
instanceHostValue: string,
instanceHostSecureValue: boolean,
instanceSoftwareValue: string, instanceSoftwareValue: string,
instanceIconValue: string | null instanceIconValue: string | null
) => { ) => {
instanceName.value = instanceNameValue; instanceName.value = instanceNameValue;
instanceHost.value = instanceHostValue;
instanceHostSecure.checked = instanceHostSecureValue;
instanceSoftware.value = instanceSoftwareValue; instanceSoftware.value = instanceSoftwareValue;
instanceIcon.src = instanceIconValue ?? blankImage; instanceIcon.src = instanceIconValue ?? blankImage;
}; };
form.addEventListener("submit", e => { form.addEventListener("submit", e => {

View file

@ -65,7 +65,19 @@ We do not track or save any requests or data.">
<div class="half-width"> <div class="half-width">
<label for="instanceName">Instance name</label> <label for="instanceName">Instance name</label>
<br> <br>
<input id="instanceName" type="text" name="instanceName" /> <input id="instanceName" type="text" name="instanceName" required />
<br><br>
<label for="instanceHost">Instance hostname</label>
<br>
<input id="instanceHost" type="text" name="instanceHost" required />
<br>
<input type="checkbox" name="instanceHostSecure" id="instanceHostSecure" checked />
<label for="instanceHostSecure">
<abbr title="Whether to use HTTPS (as opposed to HTTP).
Unchecking this is not recommended, and this option only exists for exceptional cases">
Secure?
</abbr>
</label>
<br><br> <br><br>
<label for="instanceSoftware">Instance software</label> <label for="instanceSoftware">Instance software</label>
<br> <br>

View file

@ -24,10 +24,8 @@ const addInstanceDialogCallback = async (
secure: boolean, secure: boolean,
autoQueryMetadata: boolean, autoQueryMetadata: boolean,
) => { ) => {
if (!autoQueryMetadata) { try {
showInstanceDetailsDialog(); if (!autoQueryMetadata) throw new Error("Don't");
return;
}
const { name, software, iconURL } = const { name, software, iconURL } =
await fetch(`/api/instance_info/${secure}/${encodeURI(host)}`) await fetch(`/api/instance_info/${secure}/${encodeURI(host)}`)
.then(r => r.json()); .then(r => r.json());
@ -37,8 +35,11 @@ const addInstanceDialogCallback = async (
|| !(typeof iconURL === "string" || iconURL === null) || !(typeof iconURL === "string" || iconURL === null)
) )
throw new Error("Invalid API response"); throw new Error("Invalid API response");
populateInstanceDetailsDialog(name, software, iconURL as string | null); populateInstanceDetailsDialog(name, host, secure, software, iconURL as string | null);
} finally {
populateInstanceDetailsDialog("", host, secure, "", null);
showInstanceDetailsDialog(); showInstanceDetailsDialog();
}
} }
const addDialog = document.querySelector("#addInstance"); const addDialog = document.querySelector("#addInstance");

15
static/image.mts Normal file
View file

@ -0,0 +1,15 @@
export function resize(image: HTMLImageElement, width: number = 16, height: number = 16): string {
const canvas = document.createElement('canvas');
canvas.width = width;
canvas.height = height;
canvas.style.display = "none";
document.body.appendChild(canvas);
const ctx = canvas.getContext("2d");
if (ctx === null) throw Error("Resize failed");
const w = Math.min(image.width / image.height, 1) * width;
const h = Math.min(image.height / image.width, 1) * height;
ctx.drawImage(image, (width - w) / 2, (height - h) / 2, w, h);
const url = canvas.toDataURL();
document.body.removeChild(canvas);
return url;
}