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,24 +60,20 @@ function parseUrl(url) {
return parser; return parser;
} }
if (prefillInstance != null) { function instances_loading_error() {
const url = normalizeUrl(prefillInstance); choose_instance.innerHTML = "";
instance.value = url; add_instance("-- LOADING FAILED! --", true, false, "");
add_instance(parseUrl(url).hostname, false, true); console.error('Failed to fetch servers list from joinmastodon.');
} }
choose_instance.addEventListener('focus', function (e) { function instances_loaded() {
if (choose_instance.children.length < 3) { if (this.status !== 200) {
remove_loading_instance(); instances_loading_error();
add_loading_instance(); return;
fetch('https://api.joinmastodon.org/servers')
.then(response => {
if (!response.ok) {
throw new Error('Failure response from joinmastodon.');
} }
return response.json();
}) const servers = JSON.parse(this.responseText);
.then(servers => {
const chosen_instance = choose_instance.value; const chosen_instance = choose_instance.value;
const domains = servers.map(obj => obj.domain); const domains = servers.map(obj => obj.domain);
if (domains.indexOf(chosen_instance) === -1) { if (domains.indexOf(chosen_instance) === -1) {
@ -91,13 +87,24 @@ choose_instance.addEventListener('focus', function (e) {
const selected = (domain === chosen_instance); const selected = (domain === chosen_instance);
add_instance(domain, false, selected); add_instance(domain, false, selected);
} }
}) }
.catch(error => {
choose_instance.innerHTML = ""; if (prefillInstance != null) {
add_instance("-- LOADING FAILED! --", true, false, ""); const url = normalizeUrl(prefillInstance);
console.error( instance.value = url;
'Failed to fetch servers list from joinmastodon.', error); add_instance(parseUrl(url).hostname, false, true);
}); }
choose_instance.addEventListener('focus', function (e) {
if (choose_instance.children.length < 3) {
remove_loading_instance();
add_loading_instance();
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();
} }
}) })