toot-script-condivisione-su.../src/main.js

135 lines
3.9 KiB
JavaScript
Raw Normal View History

2021-08-14 18:54:32 +02:00
/*!
2022-02-12 12:35:53 +01:00
* @source: https://codeberg.org/kytta/toot/src/branch/main/src/main.js
*
* @licstart The following is the entire license notice for the
* JavaScript code in this page.
*
* toot - Cross-instance share page for Mastodon
2022-02-12 12:36:08 +01:00
* Copyright (C) 2020-2022 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
*/
2021-08-14 21:56:17 +02:00
const INSTANCE_LIST_URL = "https://api.joinmastodon.org/servers";
2022-11-20 16:09:11 +01:00
const LOCAL_STORAGE_KEY = "recentInstances";
const RECENT_INSTANCES_SIZE = 5;
2021-08-14 21:56:17 +02:00
const $instance = document.getElementById("instance");
const $instanceDatalist = document.getElementById("instanceDatalist");
/**
* Adds missing "https://" and ending slash to the URL
*
* @param {string} url URL to normalize
* @return {string} normalized URL
*/
function normalizeUrl(url) {
if (url.indexOf("http://") == -1 && url.indexOf("https://") == -1) {
url = "https://" + url;
}
2021-08-14 19:12:42 +02:00
if (url.charAt(url.length - 1) !== "/") {
url = url + "/";
}
return url;
}
2021-08-14 21:56:17 +02:00
function onLoadInstancesError() {
console.error("Couldn't load instance list");
}
2021-08-14 21:56:17 +02:00
function onLoadInstancesSuccess() {
if (this.status >= 400) {
return onLoadInstancesError();
}
2020-09-23 18:10:47 +02:00
2021-08-14 21:56:17 +02:00
const currentInstance = $instance.value;
const instanceDomains = JSON.parse(this.responseText).map((i) => i.domain);
if (currentInstance && instanceDomains.indexOf(currentInstance) < 0) {
instanceDomains.push(currentInstance);
}
2021-08-14 21:56:17 +02:00
instanceDomains.sort();
2020-09-23 18:10:47 +02:00
2021-08-14 21:56:17 +02:00
for (let i = 0; i < instanceDomains.length; i++) {
const $option = document.createElement("option");
$option.value = normalizeUrl(instanceDomains[i]);
$instanceDatalist.appendChild($option);
}
}
2021-08-14 21:56:17 +02:00
function loadInstances() {
if ($instanceDatalist.children.length === 0) {
const request = new XMLHttpRequest();
2021-08-14 21:56:17 +02:00
request.addEventListener("load", onLoadInstancesSuccess);
request.addEventListener("error", onLoadInstancesError);
2021-08-14 21:56:17 +02:00
request.open("GET", INSTANCE_LIST_URL);
request.send();
}
2021-08-14 21:56:17 +02:00
}
2022-11-20 16:09:11 +01: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,
JSON.stringify(recentInstances)
);
}
function onFormSubmit(form) {
const formData = new FormData(form);
if (formData.get("remember")) {
rememberInstance(formData.get("instance"));
}
return true;
}
const prefillInstance = getRecentInstances()[0];
2021-08-14 21:56:17 +02:00
const URLParams = window.location.search.substr(1).split("&");
for (let i = 0; i < URLParams.length; i++) {
const URLParamPair = URLParams[i].split("=");
if (URLParamPair[0] === "text") {
document.getElementById("text").value = decodeURIComponent(URLParamPair[1]);
} else if (URLParamPair[0] === "instance") {
prefillInstance = decodeURIComponent(URLParamPair[1]);
}
}
if (prefillInstance != null) {
2021-08-14 21:56:17 +02:00
$instance.value = normalizeUrl(prefillInstance);
}
2021-08-14 21:56:17 +02:00
$instance.addEventListener("focus", loadInstances);