2021-08-14 12:54:32 -04:00
|
|
|
/*!
|
2021-01-29 18:48:40 -05: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/>.
|
|
|
|
*/
|
|
|
|
|
2020-09-23 12:39:32 -04: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 13:12:42 -04:00
|
|
|
if (url.charAt(url.length - 1) !== "/") {
|
2021-01-29 16:56:37 -05:00
|
|
|
url = url + "/";
|
|
|
|
}
|
|
|
|
return url;
|
2020-09-23 12:39:32 -04:00
|
|
|
}
|
|
|
|
|
2021-08-14 13:12:42 -04:00
|
|
|
const instance = document.getElementById("instance");
|
|
|
|
const instances_list = document.getElementById("instances_list");
|
2020-09-24 05:03:42 -04:00
|
|
|
|
2021-08-14 13:12:42 -04:00
|
|
|
var prefillInstance = window.localStorage.getItem("mastodon_instance");
|
2020-09-23 12:10:47 -04:00
|
|
|
|
2021-08-14 13:12:42 -04:00
|
|
|
var paramPairs = window.location.search.substr(1).split("&");
|
2020-09-23 12:10:47 -04:00
|
|
|
var paramPairsLength = paramPairs.length;
|
|
|
|
for (var i = 0; i < paramPairsLength; i++) {
|
2021-08-14 13:12:42 -04:00
|
|
|
var paramPair = paramPairs[i].split("=");
|
|
|
|
if (paramPair[0] === "text") {
|
|
|
|
document.getElementById("text").value = decodeURIComponent(paramPair[1]);
|
|
|
|
} else if (paramPair[0] === "instance") {
|
2021-01-29 16:56:37 -05:00
|
|
|
prefillInstance = decodeURIComponent(paramPair[1]);
|
|
|
|
}
|
2020-09-23 12:10:47 -04:00
|
|
|
}
|
|
|
|
|
2020-09-24 04:28:46 -04:00
|
|
|
function instances_loading_error() {
|
2021-08-14 13:12:42 -04:00
|
|
|
console.error("Failed to fetch servers list from joinmastodon.");
|
2020-09-24 04:28:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function instances_loaded() {
|
2021-01-29 16:56:37 -05:00
|
|
|
if (this.status !== 200) {
|
|
|
|
instances_loading_error();
|
|
|
|
return;
|
|
|
|
}
|
2020-09-24 04:28:46 -04:00
|
|
|
|
2021-01-29 16:56:37 -05:00
|
|
|
const servers = JSON.parse(this.responseText);
|
2020-09-24 04:28:46 -04:00
|
|
|
|
2021-01-29 16:56:37 -05:00
|
|
|
const chosen_instance = instance.value;
|
2021-08-14 13:12:42 -04:00
|
|
|
const domains = servers.map((obj) => obj.domain);
|
2021-01-29 16:56:37 -05:00
|
|
|
if (chosen_instance && domains.indexOf(chosen_instance) === -1) {
|
|
|
|
domains.push(chosen_instance);
|
|
|
|
}
|
|
|
|
domains.sort();
|
2020-09-24 04:28:46 -04:00
|
|
|
|
2021-01-29 16:56:37 -05:00
|
|
|
for (const domain of domains) {
|
2021-08-14 13:12:42 -04:00
|
|
|
const opt = document.createElement("option");
|
2021-01-29 16:56:37 -05:00
|
|
|
opt.value = normalizeUrl(domain);
|
|
|
|
instances_list.appendChild(opt);
|
|
|
|
}
|
2020-09-24 04:28:46 -04:00
|
|
|
}
|
|
|
|
|
2020-09-23 12:32:29 -04:00
|
|
|
if (prefillInstance != null) {
|
2021-01-29 16:56:37 -05:00
|
|
|
instance.value = normalizeUrl(prefillInstance);
|
2020-09-23 12:32:29 -04:00
|
|
|
}
|
|
|
|
|
2021-08-14 13:12:42 -04:00
|
|
|
instance.addEventListener("focus", function (e) {
|
2021-01-29 16:56:37 -05:00
|
|
|
if (instances_list.children.length === 0) {
|
|
|
|
const req = new XMLHttpRequest();
|
2021-08-14 13:12:42 -04:00
|
|
|
req.addEventListener("load", instances_loaded);
|
|
|
|
req.addEventListener("error", instances_loading_error);
|
|
|
|
req.open("GET", "https://api.joinmastodon.org/servers");
|
2021-01-29 16:56:37 -05:00
|
|
|
req.send();
|
|
|
|
}
|
2021-08-14 13:12:42 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
document.getElementById("form").addEventListener("submit", function (e) {
|
|
|
|
e.preventDefault();
|
|
|
|
var text = e.target.elements["text"].value;
|
|
|
|
var instance = normalizeUrl(e.target.elements["instance"].value);
|
|
|
|
var remember = e.target.elements["remember"].checked;
|
|
|
|
|
|
|
|
if (remember) {
|
|
|
|
window.localStorage.setItem("mastodon_instance", instance);
|
|
|
|
}
|
|
|
|
|
|
|
|
var shareUrl = instance + "share?text=" + encodeURIComponent(text);
|
|
|
|
window.location.href = shareUrl;
|
|
|
|
});
|