web/Omnibox: fix prefilled link parsing & refactor to svelte 5 style

This commit is contained in:
wukko 2025-05-07 19:45:32 +06:00
parent 0e836fa4fc
commit 393d60ef7a
No known key found for this signature in database
GPG Key ID: 3E30B3F26C7B4AA2

View File

@ -2,7 +2,7 @@
import env, { officialApiURL } from "$lib/env"; import env, { officialApiURL } from "$lib/env";
import { tick } from "svelte"; import { tick } from "svelte";
import { page } from "$app/stores"; import { page } from "$app/state";
import { goto } from "$app/navigation"; import { goto } from "$app/navigation";
import { browser } from "$app/environment"; import { browser } from "$app/environment";
@ -34,31 +34,36 @@
let linkInput: Optional<HTMLInputElement>; let linkInput: Optional<HTMLInputElement>;
let isFocused = false;
let isDisabled = false;
let isLoading = false;
$: isBotCheckOngoing = $turnstileEnabled && !$turnstileSolved;
const validLink = (url: string) => { const validLink = (url: string) => {
try { try {
return /^https?\:/i.test(new URL(url).protocol); return /^https?\:/i.test(new URL(url).protocol);
} catch {} } catch {}
}; };
$: linkFromHash = $page.url.hash.replace("#", "") || ""; let isFocused = $state(false);
$: linkFromQuery = (browser ? $page.url.searchParams.get("u") : 0) || ""; let isDisabled = $state(false);
let isLoading = $state(false);
$: if (linkFromHash || linkFromQuery) { let isBotCheckOngoing = $derived($turnstileEnabled && !$turnstileSolved);
if (validLink(linkFromHash)) {
$link = linkFromHash; let linkPrefill = $derived(page.url.hash.replace("#", "") || page.url.searchParams.get("u") || "");
} else if (validLink(linkFromQuery)) {
$link = linkFromQuery; let downloadable = $derived(validLink($link));
let clearVisible = $derived($link && !isLoading);
$effect (() => {
if (linkPrefill) {
// prefilled link may be uri encoded
linkPrefill = decodeURIComponent(linkPrefill);
if (validLink(linkPrefill)) {
$link = linkPrefill;
}
// clear hash and query to prevent bookmarking unwanted links
if (browser) goto("/", { replaceState: true });
} }
});
// clear hash and query to prevent bookmarking unwanted links
goto("/", { replaceState: true });
}
const pasteClipboard = async () => { const pasteClipboard = async () => {
if ($dialogs.length > 0 || isDisabled || isLoading) { if ($dialogs.length > 0 || isDisabled || isLoading) {
@ -124,12 +129,9 @@
break; break;
} }
}; };
$: downloadable = validLink($link);
$: clearVisible = $link && !isLoading;
</script> </script>
<svelte:window on:keydown={handleKeydown} /> <svelte:window onkeydown={handleKeydown} />
<!-- <!--
if you want to remove the community instance label, if you want to remove the community instance label,
@ -153,9 +155,9 @@
id="link-area" id="link-area"
bind:value={$link} bind:value={$link}
bind:this={linkInput} bind:this={linkInput}
on:input={() => (isFocused = true)} oninput={() => (isFocused = true)}
on:focus={() => (isFocused = true)} onfocus={() => (isFocused = true)}
on:blur={() => (isFocused = false)} onblur={() => (isFocused = false)}
spellcheck="false" spellcheck="false"
autocomplete="off" autocomplete="off"
autocapitalize="off" autocapitalize="off"