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

92 lines
2.8 KiB
JavaScript
Raw Normal View History

2021-08-14 18:54:32 +02:00
/*!
toot - Cross-instance share page for Mastodon
Copyright (C) 2020-2021 Nikita Karamov <nick@karamoff.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/>.
*/
2021-08-14 21:56:17 +02:00
const INSTANCE_LIST_URL = "https://api.joinmastodon.org/servers";
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
}
const prefillInstance = window.localStorage.getItem("mastodon_instance");
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);