forked from wilpar4813/musicPlus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlyrix.js
44 lines (38 loc) · 1.95 KB
/
lyrix.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
function getLyrix(artist, song) {
//console.log("getLyrix function was called")
if (artist != '' || song != '') {
$.ajax({ // AJAX call for current conditions
//https://api.musixmatch.com/ws/1.1/matcher.lyrics.get?format=jsonp&callback=callback&q_track=alone&q_artist=blues%20traveler
url: "https://cors-anywhere.herokuapp.com/https://api.musixmatch.com/ws/1.1/matcher.lyrics.get?format=json&callback=callback&q_track=" + song + "&q_artist=" + artist + "&apikey=057fcae6cc1599a783e98bf3f2153ced",
method: "GET"
}).then(function (response) {
if (JSON.parse(response).message.header.status_code != 404) {
$('#lyrix').show();
var data = JSON.parse(response);
postToHtml(data);
} else {
// $('#lyrix').hide();
var data = { message: { body: { lyrics: { lyrics_body: "Lyrics Not Available" } } } };
postToHtml(data);
}
});
} else { // Error message if user doesn't enter anything
$("#error").html('Field cannot be empty');
}
}
function postToHtml(response) {
//console.log(response);
//console.log("post to html called")
//console.log(response.body);
$("#songLyrix").empty;
//https://stackoverflow.com/questions/4253367/how-to-escape-a-json-string-containing-newline-characters-using-javascript
var myLyrixString = JSON.stringify(response.message.body.lyrics.lyrics_body);//response.message.body.lyrics.lyrics_body not working
//console.log(myLyrixString);
var myEscapedLyrixString = myLyrixString.replace(/\\n/g, "<br>")
.replace(/\\'|\\"|\\&|\\r|\\t|\\b|\\f/g, function (x) {
return x.slice(1);
})
//console.log(myEscapedLyrixString);
$("#songLyrix").html(myEscapedLyrixString.slice(1, myEscapedLyrixString.length - 1));
//$("#songLyrix").append(response.message.body.lyrics_copyright)
}