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