dndmusicbot/src/script.js

233 lines
6.5 KiB
JavaScript
Raw Normal View History

2022-11-19 01:28:11 +00:00
import ReconnectingWebSocket from 'reconnecting-websocket';
import Sortable from 'sortablejs';
window.onload = function () {
2022-11-18 21:18:12 +00:00
const ws = new ReconnectingWebSocket(((window.location.protocol === "https:") ? "wss://" : "ws://") + window.location.host + "/ws");
const items = document.querySelector("#items");
const amb = document.querySelector("#ambiance")
const submit = document.querySelector("#addplaylist")
const output = document.querySelector(".container2 p#output")
const info = document.querySelector("#info")
2022-11-19 01:28:11 +00:00
const link = document.querySelector("#link")
const time = document.querySelector("#time")
2022-11-18 21:18:12 +00:00
const waiting = document.querySelector("#waiting")
document.querySelector("input#next").addEventListener("click", (e) => {
ws.send(JSON.stringify({
"event": "next"
}))
})
document.querySelector("input#prev").addEventListener("click", (e) => {
ws.send(JSON.stringify({
"event": "prev"
}))
})
Sortable.create(items, {
delayOnTouchOnly: true,
2022-11-18 21:18:12 +00:00
group: "dndmusicbot-playlists",
filter: ".locked",
onMove: (e) => {
2022-11-18 21:26:28 +00:00
if (e.related) {
return !e.related.classList.contains('locked');
}
2022-11-18 21:18:12 +00:00
},
invertSwap: true,
store: {
2022-11-18 21:26:28 +00:00
get: function (sortable) {
2022-11-18 21:18:12 +00:00
var order = localStorage.getItem(sortable.options.group.name);
return order ? order.split('|') : [];
},
2022-11-18 21:26:28 +00:00
set: function (sortable) {
2022-11-18 21:18:12 +00:00
var order = sortable.toArray();
localStorage.setItem(sortable.options.group.name, order.join('|'));
}
}
})
Sortable.create(amb, {
delayOnTouchOnly: true,
2022-11-18 21:18:12 +00:00
group: "dndmusicbot-ambiance",
filter: ".locked",
onMove: (e) => {
2022-11-18 21:26:28 +00:00
if (e.related) {
return !e.related.classList.contains('locked');
}
2022-11-18 21:18:12 +00:00
},
invertSwap: true,
store: {
2022-11-18 21:26:28 +00:00
get: function (sortable) {
2022-11-18 21:18:12 +00:00
var order = localStorage.getItem(sortable.options.group.name);
return order ? order.split('|') : [];
},
2022-11-18 21:26:28 +00:00
set: function (sortable) {
2022-11-18 21:18:12 +00:00
var order = sortable.toArray();
localStorage.setItem(sortable.options.group.name, order.join('|'));
}
}
})
ws.onopen = (e) => {
2022-11-19 01:28:11 +00:00
waiting.classList.add("u-hidden")
2022-11-18 21:18:12 +00:00
}
ws.onclose = (e) => {
2022-11-19 01:28:11 +00:00
waiting.classList.remove("u-hidden")
2022-11-18 21:18:12 +00:00
}
ws.onmessage = (e) => {
data = JSON.parse(e.data)
switch (data.event) {
case "ambiance_play":
document.querySelectorAll("#ambiance > div").forEach((e) => {e.style.removeProperty("background-color")})
document.querySelector(`#ambiance > div[data-id='${data.payload.type}']`).style.backgroundColor = "burlywood"
ambiance.style.pointerEvents = 'auto'
break
case "ambiance_stop":
document.querySelectorAll("#ambiance > div").forEach((el) => {
el.style.removeProperty("background-color")
})
break
case "song_info":
document.querySelectorAll("#items > div").forEach((el) => {
el.style.removeProperty("background-color")
})
if (data.payload.pause) {
2022-11-19 01:28:11 +00:00
info.classList.add("u-hidden")
2022-11-18 21:18:12 +00:00
} else {
2022-11-19 01:28:11 +00:00
info.classList.remove("u-hidden")
link.children.channel.innerText = data.payload.channel
link.children.title.innerText = data.payload.current
link.href = "https://youtu.be/" + data.payload.song
link.target = "_blank"
time.innerText = `${msToTime(data.payload.position)} / ${msToTime(data.payload.len)}`
2022-11-18 21:18:12 +00:00
document.querySelector(`#items > div[data-id='${data.payload.playlist}']`).style.backgroundColor = "burlywood"
}
setTimeout(() => {
items.style.pointerEvents = 'auto'
}, 1000);
break
case "song_position":
2022-11-19 01:28:11 +00:00
time.innerText = `${msToTime(data.payload.position)} / ${msToTime(data.payload.len)}`
info.classList.remove("u-hidden")
2022-11-18 21:18:12 +00:00
break
case "stop":
setTimeout(() => {
items.style.pointerEvents = 'auto'
}, 2000);
2022-11-19 01:28:11 +00:00
info.classList.add("u-hidden")
2022-11-18 21:18:12 +00:00
break
case "new_playlist":
addPlaylist(data.payload);
break;
default:
}
}
document.querySelector("#addambiance").addEventListener("click", (e) => {
e.preventDefault()
//output.innerText = ""
2022-11-18 21:18:12 +00:00
var title = document.querySelector("#inputambiance > input[name='title']")
var url = document.querySelector("#inputambiance > input[name='url']")
if (title.value == "" || url.value == "") {
console.log("Title or Url is empty!")
2022-11-18 21:18:12 +00:00
return
}
ws.send(JSON.stringify({
"event": "ambiance_add",
"payload": {
"title": title.value,
"url": url.value
}
}))
})
submit.addEventListener("click", (e) => {
e.preventDefault()
//output.innerText = ""
2022-11-18 21:18:12 +00:00
var title = document.querySelector("#inputplaylist > input[name='title']")
var url = document.querySelector("#inputplaylist > input[name='url']")
if (title.value == "" || url.value == "") {
output.innerText = "Title or Url is empty!"
return
}
ws.send(JSON.stringify({
"event": "add_playlist",
"payload": {
"title": title.value,
"url": url.value
}
}))
title.innerText = ""
url.innerText = ""
})
document.querySelectorAll("#items .item").forEach(item => item.addEventListener("click", (e) => {
2022-11-18 21:18:12 +00:00
e.preventDefault()
e.target.parentElement.style.pointerEvents = 'none'
const disableui = setTimeout((t) => {
t.style.pointerEvents = 'auto'
}, 3000, e.target.parentElement);
var id = e.target.dataset.id
ws.send(JSON.stringify({
"event": ((id === "reset") ? "stop" : "load_playlist"),
"payload": id
}))
}));
document.querySelectorAll("#ambiance .item").forEach(item => item.addEventListener("click", (e) => {
2022-11-18 21:18:12 +00:00
e.preventDefault()
e.target.parentElement.style.pointerEvents = 'none'
const disableui = setTimeout((t) => {
t.style.pointerEvents = 'auto'
}, 3000, e.target.parentElement);
var id = e.target.dataset.id
ws.send(JSON.stringify({
"event": ((id === "reset") ? "ambiance_stop" : "ambiance_play"),
"payload": id
}))
}));
};
function addPlaylist(payload) {
const container = document.querySelector("body > div.container")
var newdiv = document.createElement('div');
newdiv.className = "item"
newdiv.dataset.id = payload.url
newdiv.innerText = payload.title
newdiv.addEventListener("click", (e) => {
e.preventDefault()
var id = e.target.dataset.id
ws.send(JSON.stringify({
"event": ((id === "reset") ? "stop" : "load_playlist"),
"payload": id
}))
})
container.insertBefore(newdiv, document.querySelector("body > div.container div:last-child"))
output.innerText = "New playlist was added: " + payload.title
}
function msToTime(duration) {
var milliseconds = parseInt((duration % 1000) / 100),
seconds = Math.floor((duration / 1000) % 60),
minutes = Math.floor((duration / (1000 * 60)) % 60),
minutes = (minutes < 10) ? "0" + minutes : minutes;
seconds = (seconds < 10) ? "0" + seconds : seconds;
return minutes + ":" + seconds
}