-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
241 lines (227 loc) · 8.63 KB
/
script.js
File metadata and controls
241 lines (227 loc) · 8.63 KB
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
let songs = []
let folder = []
let currentFolder;
let playPromise = 0;
let currentsong = new Audio();
async function displayFolders(folder){
let x = await fetch(folder+"info.json");
let info = await x.json();
let div = document.createElement('div');
div.innerHTML = `
<div class="picture">
<div class="play">
<img src="images/play2.svg" alt="open">
</div>
<img src="${folder}/cover.png" alt="playist">
</div>
<div class="text">
<h3>${info["title"]}</h3>
<p>${info["description"]}</p>
</div>
`
div.addEventListener('click',async ()=>{
currentFolder = folder;
let songlists = await getSongs();
await play_pause(songlists);
playsong(songs[0],songlists);
})
div.classList.add('card');
div.classList.add('cursor');
document.querySelector('.cards').append(div);
}
async function getfolder(){
let a = await fetch("https://drive.google.com/drive/folders/1efLqjuiwLA2PGmFrdiN1rIBYfY3c9OmP?usp=sharing");
let response = await a.text();
console.log(response);
let div = document.createElement('div');
div.innerHTML = response;
let as = Array.from(div.getElementsByTagName('a'));
as.forEach(e => {
console.log(e);
if(e.href.includes("/folders")){
folder.push(e.href);
}
});
//display folders
folder.forEach(async e => {
await displayFolders(e);
});
}
async function getSongs(){
songs = [];
let a = await fetch(currentFolder);
let response = await a.text();
let div = document.createElement('div');
div.innerHTML = response;
let as = div.getElementsByTagName('a');
for(let i = 0;i < as.length;i++){
if(as[i].href.endsWith('.mp3')){
songs.push(as[i].href);
}
}
let songsdiv = document.querySelector('.songs').firstElementChild;
songsdiv.innerHTML = "";
songs.forEach(e => {
const element = e.split('/')[5].replaceAll("%20"," ").replace(".mp3","");
let sname = element.split('-')[0];
let aname = element.split('-')[1];
songsdiv.innerHTML += `
<li>
<div class="song-info">
<img class="invert" src="images/music.svg" alt="music">
<div class="info">
<p class="song-name">${sname}</p>
<p class="author-name">By ${aname}</p>
</div>
</div>
<div class="play-song cursor">
<p>Play now</p>
<img class="invert" src="images/play.svg" alt="play">
</div>
</li>
`
});
let songlists = songsdiv.querySelectorAll('.play-song');
for(let i = 0;i < songlists.length;i++){
songlists[i].addEventListener('click',async ()=>{
if(songs[i] == currentsong.src){
await play_pause(songlists);
}
else {
await playsong(songs[i], songlists)
};
})
}
document.querySelector('.play-bar>.song-info>p').innerHTML = songs[0].split('/')[5].replaceAll("%20"," ");
currentsong.src = songs[0];
return songlists;
}
function formatTime(time) {
// Convert time to minutes and seconds
const minutes = Math.floor(time / 60);
const seconds = Math.floor(time % 60);
// Add leading zeros if needed
const formattedMinutes = String(minutes).padStart(2, '0');
const formattedSeconds = String(seconds).padStart(2, '0');
// Return formatted time as a string
return `${formattedMinutes}:${formattedSeconds}`;
}
let playsong = async (song, songlists, fromfolder = false)=>{
if(!currentsong.paused){
currentsong.pause();
play.src = "images/play.svg";
let index = songs.indexOf(currentsong.src);
songlists[index].querySelector('img').src = "images/play.svg";
}
let index = songs.indexOf(song);
songlists[index].querySelector('img').src = "images/pause.svg";
currentsong.src = song;
play.src = "images/pause.svg";
document.querySelector('.play-bar>.song-info>p').innerHTML = song.split('/')[5].replaceAll("%20"," ");
playPromise = currentsong.play();
}
let play_pause = async (songlists)=>{
let index = songs.indexOf(currentsong.src);
if(!currentsong.paused){
play.src = "images/play.svg";
songlists[index].querySelector('img').src = "images/play.svg";
currentsong.pause();
} else{
play.src = "images/pause.svg";
songlists[index].querySelector('img').src = "images/pause.svg";
playPromise = currentsong.play();
}
}
async function main(){
await getfolder();
currentFolder = folder[0];
await getSongs(folder[0]);
currentsong.addEventListener('loadedmetadata', function() {
document.querySelector('.play-bar>.info-box>.time-info').innerHTML = formatTime(currentsong.currentTime) + "/" + formatTime(currentsong.duration);
});
play.addEventListener('click',()=>{
play_pause(document.querySelector('.songs').firstElementChild.querySelectorAll('.play-song'));
});
previous.addEventListener('click',()=>{
let index = songs.indexOf(currentsong.src);
index--;
if(index <0){
index = songs.length - 1;
}
playsong(songs[index],document.querySelector('.songs').firstElementChild.querySelectorAll('.play-song'));
})
next.addEventListener('click',()=>{
let index = songs.indexOf(currentsong.src);
index++;
if(index >= songs.length){
index = 0;
}
playsong(songs[index],document.querySelector('.songs').firstElementChild.querySelectorAll('.play-song'));
})
currentsong.addEventListener('timeupdate',()=>{
if(currentsong.currentTime === currentsong.duration) {
play_pause(document.querySelector('.songs').firstElementChild.querySelectorAll('.play-song'));
next.click()
};
document.querySelector('.play-bar>.info-box>.time-info').innerHTML = formatTime(currentsong.currentTime) + "/" + formatTime(currentsong.duration);
document.querySelector('.seeker>.circle').style.left = (currentsong.currentTime/currentsong.duration)*100 + "%";
})
prevList.addEventListener('click',async ()=>{
let index = folder.indexOf(currentFolder);
index--;
if(index < 0) index = folder.length - 1;
currentFolder = folder[index];
let songlists = await getSongs();
await play_pause(songlists);
playsong(songs[0],songlists);
})
nextList.addEventListener('click',async ()=>{
let index = folder.indexOf(currentFolder);
index++;
if(index >= folder.length)index = 0;
currentFolder = folder[index];
let songlists = await getSongs();
await play_pause(songlists);
playsong(songs[0],songlists);
})
document.querySelector('.seeker').addEventListener('click',(e)=>{
let percent = (e.offsetX/e.target.getBoundingClientRect().width) *100;
currentsong.currentTime = (currentsong.duration * percent ) /100;
})
document.querySelector('.hamburger').addEventListener('click',()=>{
document.querySelector('.left').style.left = 0;
document.querySelector('body').classList.add('hidden-overflow');
})
document.querySelector('.cross').addEventListener('click',()=>{
document.querySelector('.left').style.left = "-100vw";
document.querySelector('body').classList.remove('hidden-overflow');
})
volumeRange.addEventListener('input',(e)=>{
currentsong.volume = e.target.value / 100;
if(currentsong.volume == 0){
volumeBtn.src = "images/mute.svg";
} else {
volumeBtn.src = "images/volume.svg";
}
})
let prevol = 1;
volumeBtn.addEventListener('click',()=>{
if(currentsong.volume == 0){
currentsong.volume = prevol;
volumeBtn.src = "images/volume.svg";
volumeRange.value = 100*prevol;
} else{
prevol = currentsong.volume;
currentsong.volume = 0;
volumeBtn.src = "images/mute.svg";
volumeRange.value = 0;
}
})
let pending = Array.from(document.querySelectorAll(".notavailable"));
pending.forEach(e => {
e.addEventListener('click',()=>{
alert("This feature is not yet available");
})
});
}
main()