-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscript.js
More file actions
188 lines (167 loc) · 7.46 KB
/
Copy pathscript.js
File metadata and controls
188 lines (167 loc) · 7.46 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
let currSong = new Audio();
console.log(window.innerWidth)
async function getSongs() {
let a = await fetch('/Apple%20Music%20Clone/songs/');
let response = await a.text();
let div = document.createElement('div');
div.innerHTML = response;
let as = div.getElementsByTagName('a');
let songs = [];
for (let i = 0; i < as.length; i++) {
const element = as[i];
if (element.href.endsWith('.m4a')) {
songs.push(element.href);
}
}
return songs;
}
async function getCovers() {
let a = await fetch('/Apple%20Music%20Clone/covers/')
let response = await a.text();
let div = document.createElement('div');
div.innerHTML = response;
let as = div.getElementsByTagName('a');
let covers = [];
for (let i = 0; i < as.length; i++) {
const element = as[i];
if (element.href.endsWith('.webp')) {
covers.push(element.href);
}
}
return covers;
}
function createCard(song, cover) {
let html = `<div class="card">
<svg class="playBtn" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100">
<circle cx="50" cy="50" r="45" fill="rgba(77, 77, 77, 0.8)" />
<polygon points="35,25 35,75 75,50" fill="#fff" />
</svg>
<img src=${cover} class="albumCover">
<h3 class="cardTitle">${song.split("/songs/")[1].replace(/%20/g, " ").slice(0, -4).split(' - ')[1]}</h3>
<p class="cardDesc">${song.split("/songs/")[1].replace(/%20/g, " ").slice(0, -4).split(' - ')[0]}</p>
</div>`;
return html;
}
function formatTime(seconds) {
if (isNaN(seconds) || seconds < 0) {
return "Invalid Input";
}
const mins = Math.floor(seconds / 60);
const remainingSecs = Math.floor(seconds % 60);
const formattedMins = String(mins).padStart(2, "0");
const formattedSecs = String(remainingSecs).padStart(2, "0");
return `${formattedMins}:${formattedSecs}`;
}
function playSong(song, songs, covers) {
let songName = song.replace(/ /g, "%20");
for (let i = 0; i < songs.length; i++) {
if (songs[i].includes(songName)) {
currSong.src = songs[i];
document.querySelector('.Track').innerHTML =
`<div class="trackImage" style="padding: 0">
<img src=${covers[i]} class="trackicn">
</div>
<div class="trackInfo justify-content align-items" style="padding-top: 2%;">
<div class="TrackName" style="display: flex; justify-content: center; align-items: center;font-size: 0.8vw;">${songs[i].split("/songs/")[1].replace(/%20/g, " ").slice(0, -4).split(' - ')[1]}</div>
<div class="artsistName" style="color: rgb(165, 165, 165); width: 100%;">
<ul type="none" style="display: flex;justify-content: space-between;padding-left:10px;padding-right:10px;height: 2px;font-size: 0.7vw;">
<li class = "currTime">00:00</li>
<li style ="display: inline-block;">${songs[i].split("/songs/")[1].replace(/%20/g, " ").slice(0, -4).split(' - ')[0]}</li>
<li class = "totalDuration">00:00</li>
</ul>
</div>
</div>`
currSong.play();
}
}
}
async function main() {
let songs = await getSongs();
console.log(songs);
let covers = await getCovers();
console.log(covers);
let cards = document.querySelector('.cardContainer');
for (let i = 0; i < songs.length; i++) {
cards.innerHTML += createCard(songs[i], covers[i]);
}
Array.from(document.getElementsByClassName('playBtn')).forEach(e => {
e.addEventListener('click', element => {
console.log(element.target.closest('.card').querySelector('.cardTitle').innerText);
playSong(element.target.closest('.card').querySelector('.cardTitle').innerText, songs, covers);
})
})
let play = document.getElementById('play');
play.addEventListener('click', () => {
if (currSong.paused) {
currSong.play();
}
else {
currSong.pause();
}
})
let next = document.getElementById('next');
next.addEventListener('click', () => {
let songName = currSong.src;
let songIndex = songs.indexOf(songName);
if (songIndex == songs.length - 1) {
songIndex = 0;
}
else {
songIndex++;
}
playSong(songs[songIndex], songs, covers);
})
let prev = document.getElementById('previous');
prev.addEventListener('click', () => {
let songName = currSong.src;
let songIndex = songs.indexOf(songName);
if (songIndex == 0) {
songIndex = songs.length - 1;
}
else {
songIndex--;
}
playSong(songs[songIndex], songs, covers);
})
currSong.addEventListener("timeupdate", () => {
let played = (currSong.currentTime / currSong.duration) * 100;
let circle = document.querySelector('.circle');
let currSeek = document.querySelector('.currSeek');
document.querySelector('.currTime').innerText = formatTime(currSong.currentTime);
document.querySelector('.totalDuration').innerText = formatTime(currSong.duration);
circle.style.left = `${played}%`;
currSeek.style.width = `${played}%`;
})
document.querySelector('.seekbar').addEventListener('click', e => {
document.querySelector('.circle').style.left = ((e.offsetX / e.target.getBoundingClientRect().width) * 100) + "%";
document.querySelector('.currSeek').style.width = (e.offsetX / e.target.getBoundingClientRect().width) * 100 + "%";
currSong.currentTime = (e.offsetX / e.target.getBoundingClientRect().width) * currSong.duration;
})
document.querySelector('.volume').getElementsByTagName('input')[0].addEventListener('change', e => {
currSong.volume = parseInt(e.target.value) / 100;
})
document.querySelector('.hamburger').addEventListener('click', e => {
document.querySelector('.sidebar').style.left = "0%";
})
document.querySelector('.crossBtn').addEventListener('click', e => {
document.querySelector('.sidebar').style.left = "-100%";
})
if (window.innerWidth <= 550) {
document.querySelector('.volumeIcon').addEventListener('click', function () {
let volumeRange = document.querySelector('.volumeRange');
if (volumeRange.style.display === 'none' || volumeRange.style.display === '') {
volumeRange.style = `position: absolute;
min-width: 80px;
max-width: 90px;
appearance: slider-vertical;
z-index: 1;
display: block;
top: 6%;
right: -5%;`
} else {
volumeRange.style.display = 'none';
}
});
}
}
main();