2021-08-14 11:54:32 -05:00
|
|
|
/*!
|
2021-08-29 06:54:07 -05:00
|
|
|
* @source: https://github.com/NickKaramoff/toot/blob/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
|
|
|
|
* 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/>.
|
|
|
|
*
|
|
|
|
* @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
|
|
|
|
2021-08-14 14:56:17 -05: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
|
|
|
|
*/
|
2020-09-23 11:39:32 -05:00
|
|
|
function normalizeUrl(url) {
|
2021-01-29 16:56:37 -05:00
|
|
|
if (url.indexOf("http://") == -1 && url.indexOf("https://") == -1) {
|
|
|
|
url = "https://" + url;
|
|
|
|
}
|
2021-08-14 12:12:42 -05:00
|
|
|
if (url.charAt(url.length - 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;
|
|
|
|
const instanceDomains = JSON.parse(this.responseText).map((i) => i.domain);
|
|
|
|
if (currentInstance && instanceDomains.indexOf(currentInstance) < 0) {
|
|
|
|
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
|
|
|
|
2021-08-14 14:56:17 -05:00
|
|
|
for (let i = 0; i < instanceDomains.length; i++) {
|
|
|
|
const $option = document.createElement("option");
|
|
|
|
$option.value = normalizeUrl(instanceDomains[i]);
|
|
|
|
$instanceDatalist.appendChild($option);
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
|
|
|
const prefillInstance = window.localStorage.getItem("mastodon_instance");
|
2020-09-24 03:28:46 -05:00
|
|
|
|
2021-08-14 14:56:17 -05: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]);
|
2021-01-29 16:56:37 -05:00
|
|
|
}
|
2020-09-24 03:28:46 -05:00
|
|
|
}
|
|
|
|
|
2020-09-23 11:32:29 -05:00
|
|
|
if (prefillInstance != null) {
|
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);
|