From 47c498ad2eb008f3339e739d979068457c671f76 Mon Sep 17 00:00:00 2001 From: Nikita Karamov Date: Sat, 18 Mar 2023 00:34:18 +0100 Subject: [PATCH] Re-implement prefilling instance and text --- lib/main.js | 71 ---------------------------- src/components/instance-select.astro | 5 +- src/pages/index.astro | 9 +++- 3 files changed, 11 insertions(+), 74 deletions(-) delete mode 100644 lib/main.js diff --git a/lib/main.js b/lib/main.js deleted file mode 100644 index bbbe886..0000000 --- a/lib/main.js +++ /dev/null @@ -1,71 +0,0 @@ -/*! - * @source: https://github.com/kytta/share2fedi/blob/main/lib/main.js - * - * @licstart The following is the entire license notice for the - * JavaScript code in this page. - * - * share2fedi - Instance-agnostic share page for the Fediverse. - * Copyright (C) 2020-2023 Nikita Karamov - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published - * by the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - * - * @licend The above is the entire license notice - * for the JavaScript code in this page. - * - * SPDX-License-Identifier: AGPL-3.0-or-later - */ - -const LOCAL_STORAGE_KEY = "recentInstances"; - -const $instance = document.querySelector("#instance"); - -/** - * Adds missing "https://" and ending slash to the URL - * - * @param {string} url URL to normalize - * @return {string} normalized URL - */ -function normalizeUrl(url) { - if (!url.includes("http://") && !url.includes("https://")) { - url = "https://" + url; - } - if (url.at(-1) !== "/") { - url = url + "/"; - } - return url; -} - -function getRecentInstances() { - const storedValue = window.localStorage.getItem(LOCAL_STORAGE_KEY); - if (!storedValue) return []; - return JSON.parse(storedValue); -} - -let prefillInstance = getRecentInstances()[0]; - -const URLParameters = window.location.search.slice(1).split("&"); -for (const URLParameter of URLParameters) { - const URLParameterPair = URLParameter.split("="); - if (URLParameterPair[0] === "text") { - document.querySelector("#text").value = decodeURIComponent( - URLParameterPair[1], - ); - } else if (URLParameterPair[0] === "instance") { - prefillInstance = decodeURIComponent(URLParameterPair[1]); - } -} - -if (prefillInstance != undefined) { - $instance.value = normalizeUrl(prefillInstance); -} diff --git a/src/components/instance-select.astro b/src/components/instance-select.astro index 10f72d1..c2ea5b2 100644 --- a/src/components/instance-select.astro +++ b/src/components/instance-select.astro @@ -7,6 +7,8 @@ try { console.error("Couln't fetch instances:", error); instances = []; } + +const { prefilledInstance } = Astro.props; --- @@ -24,6 +26,7 @@ try { list="instanceDatalist" required aria-describedby="https-label" + value={prefilledInstance} /> @@ -98,7 +101,7 @@ try { } const host = extractHost(instance); - if (index == 0) { + if (index == 0 && !$instance.value) { $instance.value = host; } const element = document.createElement("span"); diff --git a/src/pages/index.astro b/src/pages/index.astro index 259f1b5..db02368 100644 --- a/src/pages/index.astro +++ b/src/pages/index.astro @@ -1,6 +1,10 @@ --- import InstanceSelect from "../components/instance-select.astro"; import "../styles/main.scss"; + +const searchParameters = new URL(Astro.request.url).searchParams; +const prefilledText = searchParameters.get("text"); +const prefilledInstance = searchParameters.get("instance"); ---