replaced XMLHttpRequest with fetch()

This commit is contained in:
iou1name 2019-06-04 16:11:43 -04:00
parent a7ae72c82a
commit 77e8fd2c8b

View File

@ -3,58 +3,35 @@ function load() {
document.getElementById('player').addEventListener('ended', function() {
// shuffle
if (document.getElementById('shuffle').checked) {
var httpRequest;
httpRequest = new XMLHttpRequest();
httpRequest.onreadystatechange = function () {
if (httpRequest.readyState !== XMLHttpRequest.DONE) { return; }
if (httpRequest.status !== 200) { return; }
let track = JSON.parse(httpRequest.responseText);
change_track(track);
};
httpRequest.open('GET', api_uri + '/random', true);
httpRequest.send();
fetch(api_uri + '/random').then(function(response) {
return response.json();
}).then(function(json) {
change_track(json);
});
}
});
}
function select_artist(select) {
var httpRequest;
httpRequest = new XMLHttpRequest();
httpRequest.onreadystatechange = function () {
if (httpRequest.readyState !== XMLHttpRequest.DONE) { return; }
if (httpRequest.status !== 200) { return; }
nav_items = JSON.parse(httpRequest.responseText);
let html_str = '';
for (let i = 0; i < nav_items.length; i++) {
html_str += '<option value="' + nav_items[i] + '">' + nav_items[i] + '</option>';
}
document.getElementById('albumList').innerHTML = html_str;
document.getElementById('trackList').innerHTML = '';
};
let params = {
artist: select.value,
};
let query = Object.keys(params)
.map(k => encodeURIComponent(k) + '=' + encodeURIComponent(params[k]))
.join('&');
httpRequest.open('GET', api_uri + '?' + query, true);
httpRequest.send();
fetch(api_uri + '?' + query).then(function(response) {
return response.json();
}).then(function(json) {
let html_str = '';
for (let i = 0; i < json.length; i++) {
html_str += '<option value="' + json[i] + '">' + json[i] + '</option>';
}
document.getElementById('albumList').innerHTML = html_str;
document.getElementById('trackList').innerHTML = '';
});
}
function select_album(select) {
var httpRequest;
httpRequest = new XMLHttpRequest();
httpRequest.onreadystatechange = function () {
if (httpRequest.readyState !== XMLHttpRequest.DONE) { return; }
if (httpRequest.status !== 200) { return; }
nav_items = JSON.parse(httpRequest.responseText);
let html_str = '';
for (let i = 0; i < nav_items.length; i++) {
html_str += '<option data-discnumber="' + nav_items[i][0] + '" data-tracknumber="' + nav_items[i][1]+ '" value="' + nav_items[i][2] + '">' + nav_items[i][0] + '.' + nav_items[i][1] + ' - ' + nav_items[i][2] + '</option>';
}
document.getElementById('trackList').innerHTML = html_str;
};
let params = {
artist: document.getElementById('artistList').value,
album: select.value,
@ -62,20 +39,18 @@ function select_album(select) {
let query = Object.keys(params)
.map(k => encodeURIComponent(k) + '=' + encodeURIComponent(params[k]))
.join('&');
httpRequest.open('GET', api_uri + '?' + query, true);
httpRequest.send();
fetch(api_uri + '?' + query).then(function(response) {
return response.json();
}).then(function(json) {
let html_str = '';
for (let i = 0; i < json.length; i++) {
html_str += '<option data-discnumber="' + json[i][0] + '" data-tracknumber="' + json[i][1]+ '" value="' + json[i][2] + '">' + json[i][0] + '.' + json[i][1] + ' - ' + json[i][2] + '</option>';
}
document.getElementById('trackList').innerHTML = html_str;
});
}
function select_track(select) {
var httpRequest;
httpRequest = new XMLHttpRequest();
httpRequest.onreadystatechange = function () {
if (httpRequest.readyState !== XMLHttpRequest.DONE) { return; }
if (httpRequest.status !== 200) { return; }
let track = JSON.parse(httpRequest.responseText);
change_track(track);
};
let params = {
artist: document.getElementById('artistList').value,
album: document.getElementById('albumList').value,
@ -85,8 +60,11 @@ function select_track(select) {
let query = Object.keys(params)
.map(k => encodeURIComponent(k) + '=' + encodeURIComponent(params[k]))
.join('&');
httpRequest.open('GET', api_uri + '?' + query, true);
httpRequest.send();
fetch(api_uri + '?' + query).then(function(response) {
return response.json();
}).then(function(json) {
change_track(json);
});
}
function change_track(track) {