This repository has been archived by the owner on Dec 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
96 lines (89 loc) · 3.21 KB
/
app.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
"use strict";
import CompatibilityChecker from "./service/CompatibilityChecker.js";
import YouTubeRequest from "./service/YouTubeRequest.js";
import AudioExtractor from "./service/AudioExtractor.js";
import StreamQuality from "./service/StreamQuality.js";
import TimeFormatter from "./service/TimeFormatter.js";
document.addEventListener("DOMContentLoaded", async () => {
try {
CompatibilityChecker.checkServiceWorker();
} catch (e) {
return alert(e);
}
await navigator.serviceWorker.register("./ServiceWorker.js");
const progressBar = document.querySelector(".song-played-progress");
const qualityToggle = document.querySelector("#toggle");
const playerButton = document.querySelector(".play-pause");
const currentTime = document.querySelector(".current-time");
const changePlayerButtonStatus = () => {
if (playerButton.classList.contains('paused')) {
playerButton.classList.add("playing");
playerButton.classList.remove("paused");
} else {
playerButton.classList.add("paused");
playerButton.classList.remove("playing");
}
};
const updateProgressBar = () => {
let seek = sound.seek();
if (isNaN(seek)) {
progressBar.removeAttribute("value");
currentTime.innerHTML = 'Loading...';
} else {
currentTime.innerHTML = TimeFormatter.format(seek);
progressBar.value = seek / sound.duration();
}
};
let audio;
if (StreamQuality.getStreamQuality() === StreamQuality.high) {
qualityToggle.checked = true;
}
qualityToggle.onclick = StreamQuality.changeStreamQuality;
try {
CompatibilityChecker.checkStandaloneMode();
let body = await YouTubeRequest.getBody();
audio = AudioExtractor.extract(body);
} catch (e) {
audio = {
"title": e,
"author": "Author of the App: LamasFoker",
"streams": {
"128kbps": "assets/sounds/failed.mp4"
},
"thumbnailUrl": "assets/images/404.jpeg"
}
}
document.querySelector(".song-name").innerHTML = audio.title;
document.querySelector(".song-author").innerHTML = audio.author;
document.querySelector(".thumbnail").src = audio.thumbnailUrl;
const sound = new Howl({
src: StreamQuality.getStreamUrl(audio.streams),
html5: true,
onload: () => {
document.querySelector(".duration").innerHTML = TimeFormatter.format(sound.duration());
setInterval(updateProgressBar, 100);
},
onplay: () => {
changePlayerButtonStatus();
},
onend: () => {
changePlayerButtonStatus();
},
onpause: () => {
changePlayerButtonStatus();
}
});
playerButton.onclick = () => {
if (sound.playing()) {
sound.pause();
} else {
sound.play();
}
};
progressBar.onclick = (e) => {
let offset = progressBar.getBoundingClientRect(),
cursor = e.x - offset.left,
seek = parseFloat(cursor) / parseFloat(progressBar.offsetWidth);
sound.seek(seek * sound.duration());
};
});