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() { document.getElementById('player').addEventListener('ended', function() {
// shuffle // shuffle
if (document.getElementById('shuffle').checked) { if (document.getElementById('shuffle').checked) {
var httpRequest; fetch(api_uri + '/random').then(function(response) {
httpRequest = new XMLHttpRequest(); return response.json();
httpRequest.onreadystatechange = function () { }).then(function(json) {
if (httpRequest.readyState !== XMLHttpRequest.DONE) { return; } change_track(json);
if (httpRequest.status !== 200) { return; } });
let track = JSON.parse(httpRequest.responseText);
change_track(track);
};
httpRequest.open('GET', api_uri + '/random', true);
httpRequest.send();
} }
}); });
} }
function select_artist(select) { 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 = { let params = {
artist: select.value, artist: select.value,
}; };
let query = Object.keys(params) let query = Object.keys(params)
.map(k => encodeURIComponent(k) + '=' + encodeURIComponent(params[k])) .map(k => encodeURIComponent(k) + '=' + encodeURIComponent(params[k]))
.join('&'); .join('&');
httpRequest.open('GET', api_uri + '?' + query, true); fetch(api_uri + '?' + query).then(function(response) {
httpRequest.send(); 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) { 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 = { let params = {
artist: document.getElementById('artistList').value, artist: document.getElementById('artistList').value,
album: select.value, album: select.value,
@ -62,20 +39,18 @@ function select_album(select) {
let query = Object.keys(params) let query = Object.keys(params)
.map(k => encodeURIComponent(k) + '=' + encodeURIComponent(params[k])) .map(k => encodeURIComponent(k) + '=' + encodeURIComponent(params[k]))
.join('&'); .join('&');
httpRequest.open('GET', api_uri + '?' + query, true); fetch(api_uri + '?' + query).then(function(response) {
httpRequest.send(); 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) { 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 = { let params = {
artist: document.getElementById('artistList').value, artist: document.getElementById('artistList').value,
album: document.getElementById('albumList').value, album: document.getElementById('albumList').value,
@ -85,8 +60,11 @@ function select_track(select) {
let query = Object.keys(params) let query = Object.keys(params)
.map(k => encodeURIComponent(k) + '=' + encodeURIComponent(params[k])) .map(k => encodeURIComponent(k) + '=' + encodeURIComponent(params[k]))
.join('&'); .join('&');
httpRequest.open('GET', api_uri + '?' + query, true); fetch(api_uri + '?' + query).then(function(response) {
httpRequest.send(); return response.json();
}).then(function(json) {
change_track(json);
});
} }
function change_track(track) { function change_track(track) {