forked from rDrama/rDrama
1
0
Fork 0
rDrama/files/assets/js/profile_song.js

98 lines
2.0 KiB
JavaScript
Raw Normal View History

2023-10-18 19:07:06 +00:00
const music_playing = localStorage.getItem("music_playing") == 'true'
2022-12-19 02:18:48 +00:00
2023-10-18 19:07:06 +00:00
const audio = document.getElementById('profile-song')
2023-10-18 19:07:06 +00:00
audio.addEventListener('play', () => {
localStorage.setItem("music_playing", true);
})
audio.addEventListener('pause', () => {
localStorage.setItem("music_playing", false);
})
2023-10-18 19:07:06 +00:00
window.addEventListener('beforeunload', () => {
localStorage.setItem("music_playing", false);
})
2022-12-19 02:18:48 +00:00
2023-10-18 19:07:06 +00:00
let u_username = document.getElementById('u_username')
const anthem_button = document.getElementById('toggle-anthem')
const anthem_button_mobile = document.getElementById('toggle-anthem-mobile')
function play_audio(audio) {
audio.play()
if (anthem_button && !audio.paused) {
anthem_button.classList.add('text-primary')
anthem_button_mobile.classList.add('text-primary')
2023-10-17 19:35:00 +00:00
}
2023-10-18 19:07:06 +00:00
}
2023-10-17 19:35:00 +00:00
2023-10-18 19:07:06 +00:00
function pause_audio(audio) {
audio.pause()
if (anthem_button && audio.paused) {
anthem_button.classList.remove('text-primary')
anthem_button_mobile.classList.remove('text-primary')
2022-12-19 02:18:48 +00:00
}
2023-10-18 19:07:06 +00:00
}
2022-12-19 02:18:48 +00:00
2023-10-18 19:07:06 +00:00
if (u_username)
{
function toggle() {
if (audio.paused) {
play_audio(audio);
}
else {
pause_audio(audio);
2023-10-17 19:35:00 +00:00
}
2023-10-18 19:07:06 +00:00
}
2023-10-17 19:35:00 +00:00
2023-10-18 19:07:06 +00:00
if (!music_playing) {
addEventListener("load", () => {
play_audio(audio);
document.addEventListener('click', (e) => {
if (e.target.id.startsWith("toggle-anthem"))
return
if (audio.paused) play_audio(audio);
}, {once : true});
2022-12-19 02:18:48 +00:00
prepare_to_pause(audio)
})
2023-10-17 19:35:00 +00:00
}
2023-10-18 19:07:06 +00:00
}
else
{
let v_username = document.getElementById('v_username')
if (v_username)
2022-12-19 02:18:48 +00:00
{
2023-10-18 19:07:06 +00:00
v_username = v_username.innerHTML
2022-12-19 02:18:48 +00:00
2023-10-18 19:07:06 +00:00
const paused = localStorage.getItem("paused")
2022-12-19 02:18:48 +00:00
2023-10-18 19:07:06 +00:00
function toggle() {
if (audio.paused)
{
play_audio(audio)
localStorage.setItem("paused", "")
2022-12-19 02:18:48 +00:00
}
2023-10-18 19:07:06 +00:00
else
2022-12-19 02:18:48 +00:00
{
2023-10-18 19:07:06 +00:00
pause_audio(audio)
localStorage.setItem("paused", "1")
2022-12-19 02:18:48 +00:00
}
2023-10-18 19:07:06 +00:00
}
if (!paused && !music_playing)
{
addEventListener("load", () => {
play_audio(audio);
document.addEventListener('click', (e) => {
if (e.target.id.startsWith("toggle-anthem"))
return
if (audio.paused) play_audio(audio);
}, {once : true});
2022-12-19 02:18:48 +00:00
prepare_to_pause(audio)
})
2022-12-19 02:18:48 +00:00
}
}
}