This repository has been archived on 2024-05-13. You can view files and clone it, but cannot push or open issues or pull requests.
share2fedi/lib/main.js

143 lines
4 KiB
JavaScript
Raw Normal View History

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
*
* @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
*/
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
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
*/
function normalizeUrl(url) {
2023-03-01 06:42:33 -05:00
if (!url.includes("http://") && !url.includes("https://")) {
url = "https://" + url;
}
2023-06-17 07:08:15 -05:00
if (url.at(-1) !== "/") {
url = url + "/";
}
return url;
}
2021-08-14 14:56:17 -05:00
function onLoadInstancesError() {
console.error("Couldn't load instance list");
}
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-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
}
}
2021-08-14 14:56:17 -05:00
function loadInstances() {
if ($instanceDatalist.children.length === 0) {
const request = new XMLHttpRequest();
2021-08-14 14:56:17 -05:00
request.addEventListener("load", onLoadInstancesSuccess);
request.addEventListener("error", onLoadInstancesError);
2021-08-14 14:56:17 -05:00
request.open("GET", INSTANCE_LIST_URL);
request.send();
}
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
);
}
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;
}
let prefillInstance = getRecentInstances()[0];
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]);
}
}
2023-03-01 06:42:33 -05:00
if (prefillInstance != undefined) {
2021-08-14 14:56:17 -05:00
$instance.value = normalizeUrl(prefillInstance);
}
2021-08-14 14:56:17 -05:00
$instance.addEventListener("focus", loadInstances);
$form.addEventListener("submit", onFormSubmit);