-
Notifications
You must be signed in to change notification settings - Fork 66
/
swetify.js
732 lines (705 loc) · 26.4 KB
/
swetify.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
// Below are the variables of JS, which are used in the code for the functionality of the website.
const music = document.querySelector('audio');
const songimg = document.getElementById('songimg');
const play = document.getElementById('play');
const artist = document.getElementById('artist');
const title = document.getElementById('title');
const likeToggle = document.getElementById('likeToggle');
const prev = document.getElementById('prev');
const next = document.getElementById('next');
const back = document.getElementById('back');
const shuffle = document.getElementById('shuffle');
const loop = document.getElementById('loop');
const download = document.getElementById('download');
let progress = document.getElementById('progress');
let song_duration = document.getElementById('duration');
let current_time = document.getElementById('current_time');
const progress_div = document.getElementById('progress_div');
const homepage_content = document.getElementById('content');
// const liked_div = document.getElementById("liked_div");
const main_div = document.getElementById('main_div');
let cat_images = document.getElementsByClassName('image');
let category_title = document.getElementsByClassName('small-category');
let home = document.getElementById('home');
let categories = document.getElementById('popular categories');
let artists = document.getElementById('artists');
const favlist = document.getElementById('fav-list');
const jsonPaths = [
'./playlists/atifsongs.json',
'./playlists/arijitsongs.json',
'./playlists/nehasongs.json',
'./playlists/jubinsongs.json',
'./playlists/ritvizsongs.json',
'./playlists/rocksongs.json',
'./playlists/dancesongs.json',
'./playlists/collegesongs.json',
'./playlists/garbasongs.json',
'./playlists/Hits90s.json',
'./playlists/patriotic.json',
'./playlists/holispecial.json',
'./playlists/bollywoodsongs.json',
'./playlists/hollywoodsongs.json',
'./playlists/sonusongs.json',
'./playlists/shreyasongs.json',
];
function toggleNavbar() {
var navbar = document.getElementById("navbar");
if (navbar.style.left === "0px") {
navbar.style.left = "-250px";
} else {
navbar.style.left = "0px";
}
}
let globalSong, allsongs;
const fetchData = async (path) => {
try {
const response = await fetch(path);
const data = await response.json();
return data;
} catch (error) {
console.error('Error fetching data:', error);
return null;
}
};
const fetchAndLogSongs = async () => {
try {
const atifsongs = await fetchData(jsonPaths[0]);
// Below is the array of songs by Arijit Singh, in which each song is an object with the name, title and artist properties.
const arijitsongs = await fetchData(jsonPaths[1]);
// Below is the array of songs by Neha Kakkar, in which each song is an object with the name, title and artist properties.
const nehasongs = await fetchData(jsonPaths[2]);
// Below is the array of songs by Jubin Nautiyal, in which each song is an object with the name, title and artist properties.
const jubinsongs = await fetchData(jsonPaths[3]);
// Below is the array of songs by Ritviz, in which each song is an object with the name, title and artist properties.
const ritvizsongs = await fetchData(jsonPaths[4]);
// Below is the array of songs in Rock genre, in which each song is an object with the name, title and artist properties.
const rocksongs = await fetchData(jsonPaths[5]);
// Below is the array of songs for dance, in which each song is an object with the name, title and artist properties.
const dancesongs = await fetchData(jsonPaths[6]);
// Below is the array of songs for college, in which each song is an object with the name, title and artist properties.
const collegesongs = await fetchData(jsonPaths[7]);
// Below is the array of songs for garba, in which each song is an object with the name, title and artist properties.
const garbasongs = await fetchData(jsonPaths[8]);
// Below is the array of songs for 90's Bollywood songs, in which each song is an object with the name, title and artist properties.
const Hits90s = await fetchData(jsonPaths[9]);
// Below is the array of Bollywood patriotic songs , in which each song is an object with the name, title and artist properties.
const patriotic = await fetchData(jsonPaths[10]);
// Below is the array of Holi special songs , in which each song is an object with the name, title and artist properties.
const holispecial = await fetchData(jsonPaths[11]);
// Array of Bollywood songs, in which each song is an object with the name, title and artist properties.
const bollywoodsongs = await fetchData(jsonPaths[12]);
// Array of Hollywood songs, in which each song is an object with the name, title and artist properties.
const hollywoodsongs = await fetchData(jsonPaths[13]);
// Below is the array of songs by Sonu Nigam, in which each song is an object with the name, title and artist properties.
const sonusongs = await fetchData(jsonPaths[14]);
// Below is the array of songs by Shreya Ghosal, in which each song is an object with the name, title and artist properties.
const shreyasongs = await fetchData(jsonPaths[15]);
globalSong = [
hollywoodsongs,
holispecial,
bollywoodsongs,
patriotic,
Hits90s,
garbasongs,
dancesongs,
arijitsongs,
atifsongs,
nehasongs,
jubinsongs,
ritvizsongs,
rocksongs,
collegesongs,
sonusongs,
shreyasongs,
];
// Wrapping all the songs array in one array by Type of songs.
allsongs = [
hollywoodsongs,
bollywoodsongs,
rocksongs,
dancesongs,
collegesongs,
garbasongs,
Hits90s,
patriotic,
holispecial,
nehasongs,
arijitsongs,
ritvizsongs,
jubinsongs,
atifsongs,
sonusongs,
shreyasongs,
];
} catch (error) {
console.error('Error fetching or logging songs:', error);
}
};
fetchAndLogSongs();
let likedSongs = [];
function toastMessage(msg) {
var x = document.getElementById('snackbar');
x.className = 'show';
x.innerHTML = msg;
setTimeout(function () {
x.className = x.className.replace('show', '');
}, 3000);
}
//code for dynamic table
const refreshLikedList = () => {
// console.log("refreshLikedList function called");
const container_liked_list = document.getElementById('container-liked-list');
let count = 0;
// for(var i in localStorage){
// console.log(i," ",localStorage.getItem(i))
// }
for (var i in localStorage) {
if (localStorage.getItem(i)) count++;
}
// console.log("count: "+count);
if (count !== 0) {
container_liked_list.remove();
// if (container_liked_list)
// container_liked_list.remove();
let no = 1;
var containerLL = document.createElement('div');
containerLL.id = 'container-liked-list';
containerLL.className = 'container-liked-list';
var containerScroll = document.createElement('div');
containerScroll.className = 'container-scroll';
var table = document.createElement('table');
var headerRow = table.insertRow();
headerRow.className = 'headT';
var headers = ['#', 'Title', 'Artist'];
headers.forEach(function (headerText) {
var th = document.createElement('th');
th.appendChild(document.createTextNode(headerText));
headerRow.appendChild(th);
});
for (var i in localStorage) {
if (localStorage.getItem(i) !== false) {
var searchTerm = i;
// console.log(i)
if (
globalSong.flat().find((song) => song.title === searchTerm) ===
undefined
) {
localStorage.removeItem(i);
count--;
// console.log("Removing: "+i+" from local storage");
continue;
}
var results = globalSong
.flat()
.find((song) => song.title === searchTerm);
// console.log("Results: "+results);
var name = results.name;
var title = i;
var artist = results.artist;
var newSong = { name, title, artist };
likedSongs.push(newSong);
// console.log(newSong);
var row = table.insertRow();
row.setAttribute('id', 'likelist');
row.setAttribute('onclick', 'loadlikedsong(this)');
var cell1 = row.insertCell(0);
var cell2 = row.insertCell(1);
var cell3 = row.insertCell(2);
cell1.innerHTML = no;
cell2.innerHTML = i;
cell3.innerHTML = results.artist;
no++;
}
}
containerScroll.appendChild(table);
containerLL.appendChild(containerScroll);
favlist.appendChild(containerLL);
}
};
changeimagewidth(); // calling the function to change the width of the images according to the screen size.
function changeimagewidth() {
var w =
window.innerWidth ||
document.documentElement.clientWidth ||
document.body.clientWidth; // getting the width of the screen.
if (w < 480) {
// console.log("Y");
for (let i = 0; i < cat_images.length; i++) {
cat_images[i].width = '300'; // changing the width of the images.
cat_images[i].height = '300'; // changing the height of the images.
category_title[i].style.width = '300px'; // changing the width of the category title.
category_title[i].style.height = '300px'; // changing the height of the category title.
}
}
}
const hidevisible = () => {
// function to hide the homepage content and show the main content.
homepage_content.classList.remove('hidden'); // removing the hidden class from the homepage content.
main_div.classList.add('hidden');
};
// Wrapping all the categories in one array.
const allcategories = [
'TopHollywoodHits',
'TopBollywoodHits',
'RockOn',
'DanceHits',
'CollegeDeYaar',
'GujjuGarba',
"90'sHits",
'PatrioticBollywood',
'HoliSpecial',
'NehaKakkar',
'ArijitSingh',
'Ritviz',
'JubinNautiyal',
'AtifAslam',
'SonuNigam',
'ShreyaGhosal',
];
let songs = [];
let category;
let islikedplaying = false;
const loadfirstsong = (
arrno // function to load the first song of the category.
) => {
// console.log("loadfirstsong function called");
songs = allsongs[arrno];
category = allcategories[arrno];
loadSong(songs[0]);
homepage_content.classList.add('hidden');
main_div.classList.remove('hidden');
// console.log(title)
playmusic();
};
function findSongCategory(songTitle) {
// Loop through each category in the globalSongs array
for (var i = 0; i < allsongs.length; i++) {
for (const song of allsongs[i]) {
if (song.title === songTitle) {
return allcategories[i];
}
}
}
}
const loadlikedsong = (obj, i = 0) => {
// console.log(obj);
var index = obj.children[0].innerHTML;
category = findSongCategory(obj.children[1].innerHTML);
islikedplaying = true;
loadSong(likedSongs[index - 1]);
homepage_content.classList.add('hidden');
main_div.classList.remove('hidden');
playmusic();
};
let isplaying = false; // variable to check if the song is playing or not.
const playmusic = () => {
// function to play the song.
isplaying = true;
music.play();
play.classList.replace('fa-play', 'fa-pause'); // replacing the play icon with pause icon.
//songimg.classList.add("anime");
};
const pausemusic = () => {
// function to pause the song.
isplaying = false;
music.pause();
play.classList.replace('fa-pause', 'fa-play'); // replacing the pause icon with play icon.
//songimg.classList.remove("anime");
};
currSong = 0; // variable to store the current song number.
const loadSong = (song) => {
// function to load the song.
// console.log("loadSong function called");
title.textContent = song.title;
category = findSongCategory(title.innerHTML);
// console.log(title.textContent); // changing the title of the song.
artist.innerHTML = `<marquee>${song.artist}</marquee>`; // changing the artist of the song.
music.src = 'songs-images/' + category + '/' + song.name + '.mp3';
// console.log(music.src); // changing the source of the song.
songimg.src = 'songs-images/' + category + '/' + song.name + '.jpg';
// console.log(songimg.src); // changing the source of the image.
const likedState = localStorage.getItem(song.title);
if (likedState === null) {
likeToggle.checked = false;
} else if (likedState === 'true') {
likeToggle.checked = true;
}
};
const nextSong = (e) => {
// function to play the next song.
if (loopActive && e.type === 'ended') {
loadSong(songs[currSong]);
playmusic();
} else if (islikedplaying) {
currSong = (currSong + 1) % likedSongs.length;
category = findSongCategory(title.innerHTML);
// console.log(category);
loadSong(likedSongs[currSong]);
playmusic();
} else {
currSong = (currSong + 1) % songs.length; // changing the current song number.
loadSong(songs[currSong]); // calling the loadSong function to load the next song.
playmusic(); // calling the playmusic function to play the next song.
}
// console.log("hii")
};
const prevSong = () => {
// function to play the previous song.
// console.log(songs[currSong]);
if (islikedplaying) {
currSong = (currSong - 1 + likedSongs.length) % likedSongs.length;
category = findSongCategory(title.innerHTML);
loadSong(likedSongs[currSong]);
playmusic();
} else {
currSong = (currSong - 1 + songs.length) % songs.length; // changing the current song number.
loadSong(songs[currSong]); // calling the loadSong function to load the previous song.
playmusic();
}
};
const shuffleSong = () => {
// console.log("shuffleSong function called");
prevsong = currSong;
if (islikedplaying) {
currSong =
(currSong + Math.floor((Math.random() + 1) * (likedSongs.length + 1))) %
likedSongs.length;
if (currSong == prevsong) {
shuffleSong();
}
// console.log(currSong);
category = findSongCategory(title.innerHTML);
loadSong(likedSongs[currSong]);
playmusic();
} else {
currSong =
(currSong + Math.floor((Math.random() + 1) * (songs.length + 1))) %
songs.length;
if (currSong == prevsong) {
shuffleSong();
}
// console.log(currSong);
loadSong(songs[currSong]);
playmusic();
}
};
let loopActive = false;
const loopSong = () => {
// console.log("Inside loopSong");
// function to check if the song is looping or not.
if (loopActive) {
loopActive = false;
loop.style.color = '#3333ff';
} else {
loopActive = true;
loop.style.color = 'white';
}
};
const downloadCurrentSong = () => {
const currentSong = songs[currSong];
const downloadUrl = `songs-images/${category}/${currentSong.name}.mp3`;
fetch(downloadUrl)
.then((response) => response.blob())
.then((blob) => {
const blobUrl = window.URL.createObjectURL(blob);
const downloadLink = document.createElement('a');
downloadLink.href = blobUrl;
downloadLink.download = `${currentSong.title}.mp3`;
document.body.appendChild(downloadLink);
downloadLink.click();
document.body.removeChild(downloadLink);
})
.catch((error) => {
console.error('Error fetching the file:', error);
});
};
play.addEventListener('click', () => {
// event listener to play or pause the song.
if (isplaying) pausemusic();
else playmusic();
});
home.onclick = function () {
if (isplaying) pausemusic();
islikedplaying = false;
};
categories.onclick = function () {
if (isplaying) pausemusic();
islikedplaying = false;
};
artists.onclick = function () {
if (isplaying) pausemusic();
islikedplaying = false;
};
function skipBack() {
// console.log("skipback called");
if (music.currentTime >= 10) {
music.currentTime -= 10; // Skip back 5 seconds
} else {
music.currentTime = 0; // If less than 5 seconds have passed, set currentTime to 0
}
}
// Update the liked state in local storage
const updateLikedState = () => {
// console.log("updateLikedState function called");
song = title.textContent;
// console.log("Song: "+song);
if (localStorage.getItem(song) === 'true') {
localStorage.removeItem(song);
toastMessage(song + ' removed from Liked list');
} else {
localStorage.setItem(song, true);
toastMessage(song + ' added in Liked list');
}
refreshLikedList();
};
// Add event listener for the like toggle button
likeToggle.addEventListener('change', updateLikedState);
function updateDuration() {
// updating duration time for each song
const min_duration = Math.floor(music.duration / 60); // calculating the minutes of the song.
const sec_duration = Math.floor(music.duration % 60)
.toString()
.padStart(2, '0'); // calculating the seconds of the song.
if (min_duration && sec_duration) {
song_duration.innerHTML = `${min_duration}:${sec_duration}`;
// console.log(`${min_duration}:${sec_duration}`);
} // changing the duration of the song.
}
music.addEventListener('timeupdate', () => {
// console.log("timeupdate event fired");
// event listener to update the progress bar of the song.
// const { currentTime, duration } = event.target; // getting the current time and duration of the song.
// const progress_time = (currentTime / duration) * 100; // calculating the progress time of the song.
// progress.style.width = `${progress_time}%`; // changing the width of the progress bar.
/*
Updating duration of song is not needed when updating the progress bar
because the duration of the song is fixed and the progress bar is updated
// updating duration time for each song
const min_duration = Math.floor(duration / 60); // calculating the minutes of the song.
const sec_duration = Math.floor(duration % 60).toString().padStart(2, '0'); // calculating the seconds of the song.
if (duration) {
song_duration.innerHTML = `${min_duration}:${sec_duration}`;
} // changing the duration of the song.
*/
let time = parseInt((music.currentTime / music.duration) * 100);
progress.value = time;
// updating current time for a song currently playing
const min_currtime = Math.floor(music.currentTime / 60); // calculating the minutes of the song.
const sec_currtime = Math.floor(music.currentTime % 60)
.toString()
.padStart(2, '0'); // calculating the seconds of the song.
current_time.textContent = `${min_currtime}:${sec_currtime}`; // changing the current time of the song.
updateDuration();
});
// window.addEventListener("load", () => {
// refreshLikedList();
// });
// Modify the window event listener to use async/await
window.addEventListener('load', async () => {
await fetchAndLogSongs();
refreshLikedList();
});
progress.addEventListener('change', () => {
// console.log("progress_div click event fired");
// event listener to change the progress bar of the song.
music.currentTime = (progress.value * music.duration) / 100;
// const { duration } = music.duration; // getting the duration of the song.
// let move_progress = (event.offsetX / event.target.clientWidth) * duration;
// console.log(move_progress);
// music.currentTime = move_progress; // changing the current time of the song.
});
music.addEventListener('ended', nextSong); // Event listener to play the next song when the current song ends.
next.addEventListener('click', nextSong); // Event listener to play the next song when the next button is clicked.
prev.addEventListener('click', prevSong); // Event listener to play the previous song when the previous button is clicked.
shuffle.addEventListener('click', shuffleSong);
loop.addEventListener('click', loopSong);
back.addEventListener('click', skipBack);
download.addEventListener('click', downloadCurrentSong); // Adding an event listener to the download button
// spotify functionality
//var redirect_uri = "https://deploy-preview-40--swetify.netlify.app/";
var redirect_uri = 'https://swetify.netlify.app/';
//var redirect_uri= 'http://localhost:5501/';
var client_id = 'e5a392471667465499be5e9bc54c24dc';
var client_secret = '3a7df71fe8554b5faaa1bd69c11265c9';
var stateKey = 'spotify_auth_state';
function generateRandomString(length) {
var text = '';
var possible =
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
for (var i = 0; i < length; i++) {
text += possible.charAt(Math.floor(Math.random() * possible.length));
}
return text;
}
function getHashParams() {
var hashParams = {};
var e,
r = /([^&;=]+)=?([^&;]*)/g,
q = window.location.hash.substring(1);
while ((e = r.exec(q))) {
hashParams[e[1]] = decodeURIComponent(e[2]);
}
return hashParams;
}
function authorize() {
var params = getHashParams();
var access_token = params.access_token,
state = params.state,
storedState = localStorage.getItem(stateKey);
if (access_token && (state == null || state !== storedState)) {
alert('There was an error during the authentication');
} else {
console.log('access token recieved');
}
var state = generateRandomString(16);
localStorage.setItem(stateKey, state);
var scope = 'user-read-private user-read-email user-top-read';
var url = 'https://accounts.spotify.com/authorize';
url += '?response_type=token';
url += '&client_id=' + encodeURIComponent(client_id);
url += '&scope=' + encodeURIComponent(scope);
url += '&redirect_uri=' + encodeURIComponent(redirect_uri);
url += '&state=' + encodeURIComponent(state);
window.location = url;
}
function scrollToTarget() {
var element = document.getElementById('your spotify music');
element.scrollIntoView({ behavior: 'smooth' });
}
function toggleVisibility() {
var login_container = document.getElementById('login_container');
login_container.style.display =
login_container.style.display === 'none' ? 'block' : 'none';
var spotify_window = document.getElementById('spotify-window');
spotify_window.style.display =
spotify_window.style.display === 'none' ||
spotify_window.style.display === ''
? 'block'
: 'none';
}
if (window.location.href.includes('access_token')) {
// If it does, call the scrollToTarget function
scrollToTarget();
toggleVisibility();
getProfile();
getTopSongs();
}
async function getProfile() {
var params = getHashParams();
var access_token = params.access_token;
const response = await fetch('https://api.spotify.com/v1/me', {
headers: {
Authorization: 'Bearer ' + access_token,
},
});
const data = await response.json();
// console.log(data)
setProfileData(data);
}
async function getTopSongs() {
var params = getHashParams();
var access_token = params.access_token;
const response = await fetch(
'https://api.spotify.com/v1/me/top/tracks?offset=0&limit=10&time_range=long_term',
{
headers: {
Authorization: 'Bearer ' + access_token,
},
}
);
const data = await response.json();
renderTopSongs(data);
}
async function getTopArtists() {
var params = getHashParams();
var access_token = params.access_token;
const response = await fetch(
'https://api.spotify.com/v1/me/top/artists?offset=0&limit=10&time_range=long_term',
{
headers: {
Authorization: 'Bearer ' + access_token,
},
}
);
const data = await response.json();
renderTopArtists(data);
}
function setProfileData(data) {
const display_name = document.getElementById('display_name');
display_name.innerHTML = data.display_name;
const user_email = document.getElementById('user-email');
user_email.innerHTML = data.email;
const user_country = document.getElementById('user-country');
user_country.innerHTML = data.country;
const user_external_uri = document.getElementById('user-external-uri');
user_external_uri.innerHTML = `<a href="${data.external_urls.spotify}" target="_blank">${data.external_urls.spotify}</a>`;
}
function renderTopArtists(data) {
//hide top songs list
var topSongsListElement = document.getElementById('top_songs_list');
// Remove all child elements (songs) from the top songs list
while (topSongsListElement.firstChild) {
topSongsListElement.removeChild(topSongsListElement.firstChild);
}
var topSongsListElement = document.getElementById('top_artists_list');
// Loop through the top songs data and create HTML elements for each song
data.items.forEach(function (song) {
var songItemElement = document.createElement('div');
songItemElement.classList.add('song-item');
// Add song information to the HTML
songItemElement.innerHTML = `
<div class="list-displayer">
<div class="innerimage" >
<img src="${song.images[0].url}" alt="${song.name}" width="200" height="100" >
</div>
<div class="textdiv" >
<label>Artist name: ${song.name}</label>
<label>Popularity: ${song.popularity}</label>
<label>URI: <a href="${song.uri}" target="_blank">${song.uri}</a></label>
</div>
</div>
`;
// Append the song item to the top songs list
topSongsListElement.appendChild(songItemElement);
});
}
function renderTopSongs(data) {
//hide top artists list
var topSongsListElement = document.getElementById('top_artists_list');
// Remove all child elements (songs) from the top songs list
while (topSongsListElement.firstChild) {
topSongsListElement.removeChild(topSongsListElement.firstChild);
}
var topSongsListElement = document.getElementById('top_songs_list');
// Loop through the top songs data and create HTML elements for each song
data.items.forEach(function (song) {
var songItemElement = document.createElement('div');
songItemElement.classList.add('song-item');
// Add song information to the HTML
songItemElement.innerHTML = `
<div class="list-displayer">
<div class="innerimage" >
<img src="${song.album.images[0].url}" alt="${song.name}" width="200" height="100" >
</div>
<div class="textdiv" >
<label><ran>Song name:</ran> ${song.name}</label>
<label><ran>Popularity:</ran> ${song.popularity}</label>
<label><ran>URI:</ran> <a href="${song.uri}" target="_blank">${song.uri}</a></label>
</div>
</div>
`;
topSongsListElement.appendChild(songItemElement);
});
}
function userLogout() {
let comfirm = confirm('logout??');
if (comfirm) {
// Redirect the user to the login page or perform any other necessary actions
window.location.href = '/';
}
}
function toggleProfile() {
var profileData = document.getElementById('profile-data');
profileData.style.display =
profileData.style.display === 'none' ? 'flex' : 'none';
}