Re-implement prefilling instance and text

This commit is contained in:
Nikita Karamov 2023-03-18 00:34:18 +01:00
parent 378351a5d8
commit 47c498ad2e
No known key found for this signature in database
GPG key ID: 41D6F71EE78E77CD
3 changed files with 11 additions and 74 deletions

View file

@ -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 <me@kytta.dev>
*
* 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 <https://www.gnu.org/licenses/>.
*
* @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);
}

View file

@ -7,6 +7,8 @@ try {
console.error("Couln't fetch instances:", error);
instances = [];
}
const { prefilledInstance } = Astro.props;
---
<datalist id="instanceDatalist">
@ -24,6 +26,7 @@ try {
list="instanceDatalist"
required
aria-describedby="https-label"
value={prefilledInstance}
/>
</div>
</label>
@ -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");

View file

@ -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");
---
<!--
@ -103,10 +107,11 @@ import "../styles/main.scss";
rows="7"
placeholder="What's on your mind?"
required
></textarea>
>{prefilledText}</textarea
>
</label>
<InstanceSelect />
<InstanceSelect prefilledInstance={prefilledInstance} />
<input
type="submit"