Use XMLHttpRequest (for IE 11 compatibility)

This commit is contained in:
Andy Balaam 2020-09-24 09:28:46 +01:00
parent 4d36c872ca
commit 7850cece52

View file

@ -60,6 +60,35 @@ function parseUrl(url) {
return parser; return parser;
} }
function instances_loading_error() {
choose_instance.innerHTML = "";
add_instance("-- LOADING FAILED! --", true, false, "");
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);
const chosen_instance = choose_instance.value;
const domains = servers.map(obj => obj.domain);
if (domains.indexOf(chosen_instance) === -1) {
domains.push(chosen_instance);
}
domains.sort();
choose_instance.innerHTML = "";
add_instance("-- Choose an instance --", false, false, "")
for (const domain of domains) {
const selected = (domain === chosen_instance);
add_instance(domain, false, selected);
}
}
if (prefillInstance != null) { if (prefillInstance != null) {
const url = normalizeUrl(prefillInstance); const url = normalizeUrl(prefillInstance);
instance.value = url; instance.value = url;
@ -70,34 +99,12 @@ choose_instance.addEventListener('focus', function (e) {
if (choose_instance.children.length < 3) { if (choose_instance.children.length < 3) {
remove_loading_instance(); remove_loading_instance();
add_loading_instance(); add_loading_instance();
fetch('https://api.joinmastodon.org/servers')
.then(response => {
if (!response.ok) {
throw new Error('Failure response from joinmastodon.');
}
return response.json();
})
.then(servers => {
const chosen_instance = choose_instance.value;
const domains = servers.map(obj => obj.domain);
if (domains.indexOf(chosen_instance) === -1) {
domains.push(chosen_instance);
}
domains.sort();
choose_instance.innerHTML = ""; const req = new XMLHttpRequest();
add_instance("-- Choose an instance --", false, false, "") req.addEventListener('load', instances_loaded);
for (const domain of domains) { req.addEventListener('error', instances_loading_error);
const selected = (domain === chosen_instance); req.open('GET', 'https://api.joinmastodon.org/servers');
add_instance(domain, false, selected); req.send();
}
})
.catch(error => {
choose_instance.innerHTML = "";
add_instance("-- LOADING FAILED! --", true, false, "");
console.error(
'Failed to fetch servers list from joinmastodon.', error);
});
} }
}) })