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');
|
|
|
|
const choose_instance = document.getElementById('choose_instance');
|
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:07:17 -05:00
|
|
|
function add_instance(text, disabled, selected, value) {
|
2020-09-23 20:08:07 -05:00
|
|
|
const opt = document.createElement('option');
|
|
|
|
opt.innerText = text;
|
|
|
|
if (disabled) {
|
|
|
|
opt.setAttribute('disabled', true);
|
|
|
|
}
|
|
|
|
if (selected) {
|
|
|
|
opt.setAttribute('selected', true);
|
|
|
|
}
|
2020-09-24 03:07:17 -05:00
|
|
|
if (value !== undefined) {
|
|
|
|
opt.value = value;
|
|
|
|
}
|
2020-09-23 20:08:07 -05:00
|
|
|
choose_instance.appendChild(opt);
|
|
|
|
}
|
|
|
|
|
2020-09-24 02:39:45 -05:00
|
|
|
function add_loading_instance() {
|
|
|
|
add_instance("... loading ...", true);
|
|
|
|
}
|
|
|
|
|
|
|
|
function remove_loading_instance() {
|
|
|
|
const last_index = choose_instance.children.length - 1;
|
|
|
|
const last_entry = choose_instance.children[last_index];
|
|
|
|
|
|
|
|
if (last_entry.innerText === "... loading ...") {
|
|
|
|
last_entry.remove();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-24 02:27:27 -05:00
|
|
|
function parseUrl(url) {
|
|
|
|
const parser = document.createElement('a');
|
|
|
|
parser.href = url;
|
|
|
|
return parser;
|
|
|
|
}
|
|
|
|
|
2020-09-23 11:32:29 -05:00
|
|
|
if (prefillInstance != null) {
|
2020-09-23 20:08:07 -05:00
|
|
|
const url = normalizeUrl(prefillInstance);
|
|
|
|
instance.value = url;
|
2020-09-24 02:27:27 -05:00
|
|
|
add_instance(parseUrl(url).hostname, false, true);
|
2020-09-23 11:32:29 -05:00
|
|
|
}
|
|
|
|
|
2020-09-23 20:08:07 -05:00
|
|
|
choose_instance.addEventListener('focus', function (e) {
|
|
|
|
if (choose_instance.children.length < 3) {
|
2020-09-24 02:39:45 -05:00
|
|
|
remove_loading_instance();
|
|
|
|
add_loading_instance();
|
2020-09-23 20:08:07 -05:00
|
|
|
fetch('https://api.joinmastodon.org/servers')
|
|
|
|
.then(response => {
|
|
|
|
if (!response.ok) {
|
|
|
|
throw new Error('Failure response from joinmastodon.');
|
|
|
|
}
|
|
|
|
return response.json();
|
|
|
|
})
|
|
|
|
.then(servers => {
|
2020-09-24 03:07:17 -05:00
|
|
|
const chosen_instance = choose_instance.value;
|
2020-09-23 20:08:07 -05:00
|
|
|
const domains = servers.map(obj => obj.domain);
|
2020-09-24 03:07:17 -05:00
|
|
|
if (domains.indexOf(chosen_instance) === -1) {
|
|
|
|
domains.push(chosen_instance);
|
|
|
|
}
|
2020-09-23 20:08:07 -05:00
|
|
|
domains.sort();
|
2020-09-24 03:07:17 -05:00
|
|
|
|
|
|
|
choose_instance.innerHTML = "";
|
|
|
|
add_instance("-- Choose an instance --", false, false, "")
|
2020-09-23 20:08:07 -05:00
|
|
|
for (const domain of domains) {
|
2020-09-24 03:07:17 -05:00
|
|
|
const selected = (domain === chosen_instance);
|
|
|
|
add_instance(domain, false, selected);
|
2020-09-23 20:08:07 -05:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(error => {
|
2020-09-24 03:07:17 -05:00
|
|
|
choose_instance.innerHTML = "";
|
|
|
|
add_instance("-- LOADING FAILED! --", true, false, "");
|
2020-09-23 20:08:07 -05:00
|
|
|
console.error(
|
|
|
|
'Failed to fetch servers list from joinmastodon.', error);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
choose_instance.addEventListener('change', function (e) {
|
2020-09-24 02:42:23 -05:00
|
|
|
instance.value = normalizeUrl(choose_instance.value);
|
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-23 11:10:47 -05:00
|
|
|
window.open(shareUrl, '_blank', 'noopener,noreferrer')
|
2020-09-23 20:08:07 -05:00
|
|
|
})
|