2021-08-14 11:54:32 -05:00
|
|
|
/*!
|
2023-03-16 05:47:59 -05:00
|
|
|
* @source: https://github.com/kytta/share2fedi/blob/main/lib/main.js
|
2021-08-29 06:54:07 -05:00
|
|
|
*
|
|
|
|
* @licstart The following is the entire license notice for the
|
|
|
|
* JavaScript code in this page.
|
|
|
|
*
|
2023-03-16 04:16:01 -05:00
|
|
|
* share2fedi - Instance-agnostic share page for the Fediverse.
|
|
|
|
* Copyright (C) 2020-2023 Nikita Karamov <me@kytta.dev>
|
2021-08-29 06:54:07 -05:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*/
|
2021-01-29 18:48:40 -05:00
|
|
|
|
2023-03-16 05:47:59 -05:00
|
|
|
import "./scss/style.scss";
|
|
|
|
|
2021-08-14 14:56:17 -05:00
|
|
|
const INSTANCE_LIST_URL = "https://api.joinmastodon.org/servers";
|
2022-11-20 10:09:11 -05:00
|
|
|
const LOCAL_STORAGE_KEY = "recentInstances";
|
|
|
|
const RECENT_INSTANCES_SIZE = 5;
|
2021-08-14 14:56:17 -05:00
|
|
|
|
2023-03-16 05:48:38 -05:00
|
|
|
const $form = document.querySelector("#js-s2f-form");
|
2023-03-01 06:42:33 -05:00
|
|
|
const $instance = document.querySelector("#instance");
|
|
|
|
const $instanceDatalist = document.querySelector("#instanceDatalist");
|
2021-08-14 14:56:17 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds missing "https://" and ending slash to the URL
|
|
|
|
*
|
|
|
|
* @param {string} url URL to normalize
|
|
|
|
* @return {string} normalized URL
|
|
|
|
*/
|
2020-09-23 11:39:32 -05:00
|
|
|
function normalizeUrl(url) {
|
2023-03-01 06:42:33 -05:00
|
|
|
if (!url.includes("http://") && !url.includes("https://")) {
|
2021-01-29 16:56:37 -05:00
|
|
|
url = "https://" + url;
|
|
|
|
}
|
2023-06-17 07:08:15 -05:00
|
|
|
if (url.at(-1) !== "/") {
|
2021-01-29 16:56:37 -05:00
|
|
|
url = url + "/";
|
|
|
|
}
|
|
|
|
return url;
|
2020-09-23 11:39:32 -05:00
|
|
|
}
|
|
|
|
|
2021-08-14 14:56:17 -05:00
|
|
|
function onLoadInstancesError() {
|
|
|
|
console.error("Couldn't load instance list");
|
|
|
|
}
|
2020-09-24 04:03:42 -05:00
|
|
|
|
2021-08-14 14:56:17 -05:00
|
|
|
function onLoadInstancesSuccess() {
|
|
|
|
if (this.status >= 400) {
|
|
|
|
return onLoadInstancesError();
|
|
|
|
}
|
2020-09-23 11:10:47 -05:00
|
|
|
|
2021-08-14 14:56:17 -05:00
|
|
|
const currentInstance = $instance.value;
|
2023-03-01 06:42:33 -05:00
|
|
|
const instanceDomains = JSON.parse(this.responseText).map(
|
2023-03-17 08:06:30 -05:00
|
|
|
(index) => index.domain,
|
2023-03-01 06:42:33 -05:00
|
|
|
);
|
|
|
|
if (currentInstance && !instanceDomains.includes(currentInstance)) {
|
2021-08-14 14:56:17 -05:00
|
|
|
instanceDomains.push(currentInstance);
|
2021-01-29 16:56:37 -05:00
|
|
|
}
|
2021-08-14 14:56:17 -05:00
|
|
|
instanceDomains.sort();
|
2020-09-23 11:10:47 -05:00
|
|
|
|
2023-03-01 06:42:33 -05:00
|
|
|
for (const instanceDomain of instanceDomains) {
|
2021-08-14 14:56:17 -05:00
|
|
|
const $option = document.createElement("option");
|
2023-03-01 06:42:33 -05:00
|
|
|
$option.value = normalizeUrl(instanceDomain);
|
|
|
|
$instanceDatalist.append($option);
|
2021-08-14 14:56:17 -05:00
|
|
|
}
|
2020-09-24 03:28:46 -05:00
|
|
|
}
|
|
|
|
|
2021-08-14 14:56:17 -05:00
|
|
|
function loadInstances() {
|
|
|
|
if ($instanceDatalist.children.length === 0) {
|
|
|
|
const request = new XMLHttpRequest();
|
2020-09-24 03:28:46 -05:00
|
|
|
|
2021-08-14 14:56:17 -05:00
|
|
|
request.addEventListener("load", onLoadInstancesSuccess);
|
|
|
|
request.addEventListener("error", onLoadInstancesError);
|
2020-09-24 03:28:46 -05:00
|
|
|
|
2021-08-14 14:56:17 -05:00
|
|
|
request.open("GET", INSTANCE_LIST_URL);
|
|
|
|
request.send();
|
2021-01-29 16:56:37 -05:00
|
|
|
}
|
2021-08-14 14:56:17 -05:00
|
|
|
}
|
|
|
|
|
2022-11-20 10:09:11 -05:00
|
|
|
function getRecentInstances() {
|
|
|
|
const storedValue = window.localStorage.getItem(LOCAL_STORAGE_KEY);
|
|
|
|
if (!storedValue) return [];
|
|
|
|
return JSON.parse(storedValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
function rememberInstance(instance) {
|
|
|
|
const recentInstances = getRecentInstances();
|
|
|
|
|
|
|
|
const index = recentInstances.indexOf(instance);
|
|
|
|
if (index >= 0) {
|
|
|
|
recentInstances.splice(index, 1);
|
|
|
|
}
|
|
|
|
recentInstances.unshift(instance);
|
|
|
|
recentInstances.length = RECENT_INSTANCES_SIZE;
|
|
|
|
|
|
|
|
window.localStorage.setItem(
|
|
|
|
LOCAL_STORAGE_KEY,
|
2023-03-17 08:06:30 -05:00
|
|
|
JSON.stringify(recentInstances),
|
2022-11-20 10:09:11 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-03-16 05:48:38 -05:00
|
|
|
function onFormSubmit(event) {
|
|
|
|
const formData = new FormData(event.target);
|
2022-11-20 10:09:11 -05:00
|
|
|
|
|
|
|
if (formData.get("remember")) {
|
|
|
|
rememberInstance(formData.get("instance"));
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-03-01 05:50:43 -05:00
|
|
|
let prefillInstance = getRecentInstances()[0];
|
2020-09-24 03:28:46 -05:00
|
|
|
|
2023-03-01 06:42:33 -05:00
|
|
|
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(
|
2023-03-17 08:06:30 -05:00
|
|
|
URLParameterPair[1],
|
2023-03-01 06:42:33 -05:00
|
|
|
);
|
|
|
|
} else if (URLParameterPair[0] === "instance") {
|
|
|
|
prefillInstance = decodeURIComponent(URLParameterPair[1]);
|
2021-01-29 16:56:37 -05:00
|
|
|
}
|
2020-09-24 03:28:46 -05:00
|
|
|
}
|
|
|
|
|
2023-03-01 06:42:33 -05:00
|
|
|
if (prefillInstance != undefined) {
|
2021-08-14 14:56:17 -05:00
|
|
|
$instance.value = normalizeUrl(prefillInstance);
|
2020-09-23 11:32:29 -05:00
|
|
|
}
|
|
|
|
|
2021-08-14 14:56:17 -05:00
|
|
|
$instance.addEventListener("focus", loadInstances);
|
2023-03-16 05:48:38 -05:00
|
|
|
$form.addEventListener("submit", onFormSubmit);
|