From 7d83916f92a2985deee998a86f4d39494cefd961 Mon Sep 17 00:00:00 2001 From: WardPearce Date: Sun, 4 May 2025 11:47:06 +1200 Subject: [PATCH] Added instance URL validation --- .../lib/components/Settings/Interface.svelte | 45 ++++++++++++++----- materialious/src/lib/i18n/locales/en.json | 3 +- 2 files changed, 36 insertions(+), 12 deletions(-) diff --git a/materialious/src/lib/components/Settings/Interface.svelte b/materialious/src/lib/components/Settings/Interface.svelte index ea8d0d92..e53723ca 100644 --- a/materialious/src/lib/components/Settings/Interface.svelte +++ b/materialious/src/lib/components/Settings/Interface.svelte @@ -36,6 +36,8 @@ let forceCase = $state(get(interfaceForceCase)); let defaultPage = $state(get(interfaceDefaultPage)); + let invalidInstance = $state(false); + async function setColor(color: any) { const target = color.target; const hex = (target as { value: string }).value; @@ -55,21 +57,44 @@ darkModeStore.set(true); } } + + async function setInstance(event: Event) { + event.preventDefault(); + + invalidInstance = false; + + const instance = ensureNoTrailingSlash(invidiousInstance); + + try { + new URL(instance); + } catch { + invalidInstance = true; + } + + if (invalidInstance) return; + + const resp = await fetch(`${instance}/api/v1/trending`); + if (!resp.ok) { + invalidInstance = true; + return; + } + + instanceStore.set(instance); + authStore.set(null); + goto('/', { replaceState: true }); + ui('#dialog-settings'); + } {#if Capacitor.isNativePlatform()} -
{ - instanceStore.set(ensureNoTrailingSlash(invidiousInstance)); - authStore.set(null); - goto('/', { replaceState: true }); - ui('#dialog-settings'); - })} - > +