2020-07-04 06:22:28 +00:00
|
|
|
var requestsDiv = document.getElementById("requests");
|
2021-02-26 02:52:57 +00:00
|
|
|
var cronJobs = ['processBans'];
|
|
|
|
var currentPage = 1;
|
|
|
|
var totalPages = 1;
|
2020-07-04 06:22:28 +00:00
|
|
|
|
2021-02-26 02:52:57 +00:00
|
|
|
function getRequests(count,offset,allRequests) {
|
|
|
|
if (allRequests) var reqUrl = "/api/getAllRequests";
|
|
|
|
else var reqUrl = "/api/getRequests";
|
|
|
|
reqUrl += `?count=${count}&offset=${offset}`;
|
2020-07-04 06:22:28 +00:00
|
|
|
fetch(reqUrl)
|
|
|
|
.then(response => response.json())
|
2020-11-01 20:07:16 +00:00
|
|
|
.then(requests => {
|
2021-02-26 02:52:57 +00:00
|
|
|
buildTable(requests);
|
2020-11-01 20:07:16 +00:00
|
|
|
});
|
2020-07-04 06:22:28 +00:00
|
|
|
}
|
|
|
|
|
2021-02-26 02:52:57 +00:00
|
|
|
function buildTable(requests) {
|
|
|
|
totalPages = Math.ceil(requests.total/document.getElementById("count").value);
|
|
|
|
document.getElementById("totalPages").innerText = totalPages;
|
|
|
|
if (currentPage <= 1) {
|
|
|
|
currentPage = 1;
|
|
|
|
document.getElementById("pageBtnFirst").disabled = true;
|
|
|
|
document.getElementById("pageBtnPrev").disabled = true;
|
|
|
|
} else {
|
|
|
|
document.getElementById("pageBtnFirst").disabled = false;
|
|
|
|
document.getElementById("pageBtnPrev").disabled = false;
|
|
|
|
}
|
|
|
|
if (currentPage >= totalPages) {
|
|
|
|
currentPage = totalPages;
|
|
|
|
document.getElementById("pageBtnLast").disabled = true;
|
|
|
|
document.getElementById("pageBtnNext").disabled = true;
|
|
|
|
} else {
|
|
|
|
document.getElementById("pageBtnLast").disabled = false;
|
|
|
|
document.getElementById("pageBtnNext").disabled = false;
|
|
|
|
}
|
|
|
|
document.getElementById("page").innerHTML = "";
|
|
|
|
for (i = 1; i <= totalPages; i++) {
|
|
|
|
document.getElementById("page").innerHTML += `<option value=${i}>${i}</option>`;
|
|
|
|
}
|
|
|
|
document.getElementById("page").value = currentPage;
|
2020-11-10 06:43:16 +00:00
|
|
|
var requestsDivHTML = '<table><tr><th class="request-link">Song</th><th class="request-requester">Requester</th><th class="request-score">Score</th>';
|
2020-11-01 20:07:16 +00:00
|
|
|
requestsDivHTML += '<th class="request-state">State</td>';
|
|
|
|
if (window.loggedIn) requestsDivHTML += '<th class="request-vote">Vote</td>';
|
|
|
|
if (window.isStreamer) requestsDivHTML += '<th class="request-update">Update</th>'
|
2020-07-04 06:22:28 +00:00
|
|
|
requestsDivHTML += "</tr>";
|
2021-02-26 02:52:57 +00:00
|
|
|
for (request of requests.requests) {
|
2020-11-10 06:43:16 +00:00
|
|
|
requestsDivHTML += `<tr><td class="request-link"><a href="${request.url}" target="_blank">${request.title}</a></td>\
|
2020-08-12 19:43:30 +00:00
|
|
|
<td class="request-requester">${request.imageurl ? `<img src="${request.imageurl}" class="table-userpic"/>` : ''}${request.requester}</td>\
|
2020-07-04 06:22:28 +00:00
|
|
|
<td class="request-score">${request.score}</td>`;
|
2020-11-01 20:07:16 +00:00
|
|
|
requestsDivHTML += `<td class="request-state">${request.state}</td>`;
|
2020-08-08 04:32:06 +00:00
|
|
|
if (window.loggedIn) {
|
|
|
|
if (request.voted) {
|
|
|
|
requestsDivHTML += `<td class="request-vote"><button onclick="deleteVote('${request.url}')">Unvote</button></td>`;
|
|
|
|
} else {
|
|
|
|
requestsDivHTML += `<td class="request-vote"><button onclick="addVote('${request.url}')">Vote</button></td>`;
|
|
|
|
}
|
2020-11-01 20:07:16 +00:00
|
|
|
if (window.isStreamer) {
|
|
|
|
requestsDivHTML += '<td class="request-update"><button onclick="openUpdateRequestModal(this.parentElement.parentElement)">Update</button></td>'
|
|
|
|
}
|
2020-08-08 04:32:06 +00:00
|
|
|
}
|
2020-07-04 06:22:28 +00:00
|
|
|
requestsDivHTML += "</tr>";
|
|
|
|
}
|
|
|
|
requestsDivHTML += "</table>";
|
|
|
|
requestsDiv.innerHTML = requestsDivHTML;
|
|
|
|
}
|
|
|
|
|
|
|
|
function updateTable() {
|
2021-02-26 02:52:57 +00:00
|
|
|
var count = document.getElementById("count").value;
|
|
|
|
var offset = (currentPage - 1) * count;
|
|
|
|
var allRequests = document.getElementById("allRequests").checked;
|
|
|
|
getRequests(count,offset,allRequests);
|
2020-07-04 06:22:28 +00:00
|
|
|
}
|
|
|
|
|
2020-11-08 20:04:34 +00:00
|
|
|
function applyUrlTransforms(url) {
|
|
|
|
console.log("Begin applyUrlTransforms:" + url);
|
|
|
|
if (url.match(/^https?:\/\/(www\.)?youtu(\.be|be\.com)\//)) { // Youtube
|
|
|
|
console.log("Youtube");
|
|
|
|
var videoid = "";
|
|
|
|
// youtu.be share url - Do not anchor to $ due to parameters such as ?t=
|
|
|
|
if (url.match(/^https?:\/\/youtu.be\/[A-Za-z0-9_-]{11}/)) {
|
|
|
|
videoid = url.match(/(?<=^https:\/\/youtu.be\/)[A-Za-z0-9_-]{11}/)[0];
|
|
|
|
} else if (url.match(/^https?:\/\/(www\.)?youtube\.com\//)) {
|
|
|
|
videoid = url.match(/(?<=^https?:\/\/(www\.)?youtube\.com\/watch.*[?&]v=)[A-Za-z0-9_-]{11}/)[0];
|
|
|
|
}
|
|
|
|
var result = 'https://www.youtube.com/watch?v=' + videoid;
|
|
|
|
console.log("Result: " + result);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-26 02:52:57 +00:00
|
|
|
function goToPage(page) {
|
|
|
|
currentPage = parseInt(page,10);
|
|
|
|
updateTable();
|
|
|
|
}
|
|
|
|
|
2021-02-22 06:25:23 +00:00
|
|
|
function getColorObject() {
|
|
|
|
return {
|
|
|
|
bg: {
|
|
|
|
primary: document.getElementById('color-bg-primary').value,
|
|
|
|
table: document.getElementById('color-bg-table').value,
|
|
|
|
navbar: document.getElementById('color-bg-navbar').value,
|
|
|
|
error: document.getElementById('color-bg-error').value,
|
|
|
|
},
|
|
|
|
fg: {
|
|
|
|
primary: document.getElementById('color-fg-primary').value,
|
|
|
|
ahover: document.getElementById('color-fg-ahover').value,
|
|
|
|
title: document.getElementById('color-fg-title').value,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-07 04:24:04 +00:00
|
|
|
function addRequestErr(msg) {
|
|
|
|
document.getElementById('addRequestError').style.display = "inline-block";
|
|
|
|
document.getElementById('addRequestError').innerText = msg;
|
|
|
|
}
|
|
|
|
|
|
|
|
function addRequestErrReset() {
|
|
|
|
document.getElementById('addRequestError').style.display = "none";
|
|
|
|
document.getElementById('addRequestError').innerText = "";
|
|
|
|
}
|
|
|
|
|
2020-11-01 20:07:16 +00:00
|
|
|
function updateRequestErr(msg) {
|
|
|
|
document.getElementById('updateRequestError').style.display = "inline-block";
|
|
|
|
document.getElementById('updateRequestError').innerText = msg;
|
|
|
|
}
|
|
|
|
|
|
|
|
function updateRequestErrReset() {
|
|
|
|
document.getElementById('updateRequestError').style.display = "none";
|
|
|
|
document.getElementById('updateRequestError').innerText = "";
|
|
|
|
}
|
|
|
|
|
2021-02-22 06:25:23 +00:00
|
|
|
function streamerSettingsErr(msg) {
|
|
|
|
document.getElementById('streamerSettingsError').style.display = "inline-block";
|
|
|
|
document.getElementById('streamerSettingsError').innerText = msg;
|
|
|
|
}
|
|
|
|
|
|
|
|
function streamerSettingsErrReset() {
|
|
|
|
document.getElementById('streamerSettingsError').style.display = "none";
|
|
|
|
document.getElementById('streamerSettingsError').innerText = "";
|
|
|
|
}
|
|
|
|
|
|
|
|
// Hides all modals in preparation to show a one or to close out of
|
|
|
|
// all modals and return to the page. Does NOT hide modalBackground.
|
|
|
|
function hideModals() {
|
|
|
|
document.getElementById("messageModal").style.display = "none";
|
2020-11-01 20:07:16 +00:00
|
|
|
document.getElementById("addRequestModal").style.display = "none";
|
|
|
|
document.getElementById("updateRequestModal").style.display = "none";
|
|
|
|
document.getElementById("deleteRequestModal").style.display = "none";
|
2021-02-22 06:25:23 +00:00
|
|
|
document.getElementById("streamerSettingsModal").style.display = "none";
|
2020-07-07 04:24:04 +00:00
|
|
|
}
|
|
|
|
|
2021-02-22 06:25:23 +00:00
|
|
|
function closeAllModals() {
|
|
|
|
hideModals();
|
2020-07-07 04:24:04 +00:00
|
|
|
document.getElementById("modalBackground").style.display = "none";
|
|
|
|
}
|
|
|
|
|
2021-02-22 06:25:23 +00:00
|
|
|
function showMessage(msg) {
|
|
|
|
hideModals();
|
|
|
|
document.getElementById("messageModalText").innerText = msg;
|
2020-07-07 04:24:04 +00:00
|
|
|
document.getElementById("modalBackground").style.display = "flex";
|
2021-02-22 06:25:23 +00:00
|
|
|
document.getElementById("messageModal").style.display = "block";
|
2020-07-05 22:20:22 +00:00
|
|
|
}
|
|
|
|
|
2021-02-22 06:25:23 +00:00
|
|
|
function openAddRequestModal() {
|
|
|
|
hideModals();
|
|
|
|
document.getElementById("modalBackground").style.display = "flex";
|
2021-02-23 04:10:34 +00:00
|
|
|
document.getElementById("addRequestModal").style.display = "block";
|
2020-07-07 04:24:04 +00:00
|
|
|
}
|
|
|
|
|
2020-11-01 20:07:16 +00:00
|
|
|
function openUpdateRequestModal(tr) {
|
2021-02-22 06:25:23 +00:00
|
|
|
hideModals();
|
2020-11-10 06:43:16 +00:00
|
|
|
var url = tr.getElementsByClassName('request-link')[0].firstChild.href;
|
2020-11-01 20:07:16 +00:00
|
|
|
var score = tr.getElementsByClassName('request-score')[0].innerText;
|
|
|
|
var state = tr.getElementsByClassName('request-state')[0].innerText;
|
|
|
|
document.getElementById("updateRequestUrl").href = url;
|
|
|
|
document.getElementById("updateRequestUrl").innerText = url;
|
|
|
|
document.getElementById("updateRequestModalCurrentScore").innerText = score;
|
|
|
|
document.querySelector(`#updateRequestStateSelect [value="${state}"]`).selected = true;
|
|
|
|
document.getElementById("scoreModifierInput").value = 0;
|
|
|
|
document.getElementById("modalBackground").style.display = "flex";
|
|
|
|
document.getElementById("updateRequestModal").style.display = "block";
|
|
|
|
}
|
|
|
|
|
|
|
|
function openDeleteRequestModal(url) {
|
2021-02-22 06:25:23 +00:00
|
|
|
hideModals();
|
2020-11-01 20:07:16 +00:00
|
|
|
document.getElementById("updateRequestUrl").href = url;
|
|
|
|
document.getElementById("updateRequestUrl").innerText = url;
|
|
|
|
document.getElementById("messageModal").style.display = "none";
|
|
|
|
document.getElementById("addRequestModal").style.display = "none";
|
|
|
|
document.getElementById("updateRequestModal").style.display = "none";
|
|
|
|
document.getElementById("modalBackground").style.display = "flex";
|
|
|
|
document.getElementById("deleteRequestModal").style.display = "block";
|
|
|
|
}
|
|
|
|
|
2021-02-22 06:25:23 +00:00
|
|
|
// Returns to update request modal
|
2020-11-01 20:07:16 +00:00
|
|
|
function closeDeleteRequestModal() {
|
2021-02-22 06:25:23 +00:00
|
|
|
hideModals();
|
2020-11-01 20:07:16 +00:00
|
|
|
document.getElementById("deleteRequestModal").style.display = "none";
|
|
|
|
document.getElementById("updateRequestModal").style.display = "block";
|
|
|
|
}
|
|
|
|
|
2021-02-22 06:25:23 +00:00
|
|
|
function openStreamerSettingsModal() {
|
|
|
|
hideModals();
|
|
|
|
document.getElementById("modalBackground").style.display = "flex";
|
|
|
|
document.getElementById("streamerSettingsModal").style.display = "block";
|
|
|
|
}
|
|
|
|
|
|
|
|
function cronRequest(job) {
|
|
|
|
if (!cronJobs.includes(job)) throw new Error("Request for invalid job");
|
2020-11-01 20:07:16 +00:00
|
|
|
}
|
|
|
|
|
2020-07-07 04:24:04 +00:00
|
|
|
const validUrlRegexes = [
|
|
|
|
/^https:\/\/www\.youtube\.com\/watch\?v=[a-zA-Z0-9_-]{11}$/
|
|
|
|
];
|
|
|
|
function validateAndSubmitRequest() {
|
|
|
|
addRequestErrReset();
|
2020-11-08 20:04:34 +00:00
|
|
|
var url = applyUrlTransforms(document.getElementById("addRequestUrl").value);
|
2020-07-07 04:24:04 +00:00
|
|
|
var validUrl = false;
|
|
|
|
for (var regex of validUrlRegexes) {
|
|
|
|
if (regex.test(url)) {
|
|
|
|
validUrl = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!validUrl) {
|
|
|
|
addRequestErr("Invalid URL");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
fetch("/api/addRequest", { method: 'POST', body: new URLSearchParams({
|
|
|
|
url: url
|
2020-08-08 04:32:06 +00:00
|
|
|
})})
|
|
|
|
.then(response => {
|
|
|
|
updateTable();
|
|
|
|
document.getElementById("addRequestUrl").value = "";
|
|
|
|
response.text().then((message) => {
|
|
|
|
closeAddRequestModal();
|
|
|
|
showMessage(message);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function addVote(url) {
|
|
|
|
fetch("/api/addVote", { method: 'POST', body: new URLSearchParams({
|
|
|
|
url: url
|
|
|
|
})})
|
|
|
|
.then(response => {
|
|
|
|
if (!response.ok) {
|
2020-09-20 01:14:58 +00:00
|
|
|
response.text().then(showMessage);
|
2020-08-08 04:32:06 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
updateTable();
|
|
|
|
response.text().then(showMessage);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function deleteVote(url) {
|
|
|
|
fetch("/api/deleteVote", { method: 'POST', body: new URLSearchParams({
|
|
|
|
url: url
|
2020-07-07 04:24:04 +00:00
|
|
|
})})
|
|
|
|
.then(response => {
|
|
|
|
if (!response.ok) {
|
|
|
|
response.text().then(addRequestErr);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
updateTable();
|
|
|
|
response.text().then(showMessage);
|
|
|
|
});
|
2020-07-05 22:20:22 +00:00
|
|
|
}
|
|
|
|
|
2020-11-01 20:07:16 +00:00
|
|
|
function updateRequestState(url,state) {
|
|
|
|
updateRequestErrReset();
|
|
|
|
fetch("/api/updateRequestState", { method: 'POST', body: new URLSearchParams({
|
|
|
|
url: url,
|
|
|
|
state: state
|
|
|
|
})})
|
|
|
|
.then(response => {
|
|
|
|
if (!response.ok) {
|
|
|
|
response.text().then(updateRequestErr);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
updateTable();
|
|
|
|
response.text().then(showMessage);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function updateRequestScoreModifier(url,scoreDiff) {
|
|
|
|
updateRequestErrReset();
|
|
|
|
fetch("/api/updateRequestScoreModifier", { method: 'POST', body: new URLSearchParams({
|
|
|
|
url: url,
|
|
|
|
scoreDiff: scoreDiff
|
|
|
|
})})
|
|
|
|
.then(response => {
|
|
|
|
if (!response.ok) {
|
|
|
|
response.text().then(updateRequestErr);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
updateTable();
|
|
|
|
response.text().then(showMessage);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-11-10 06:43:16 +00:00
|
|
|
function updateRequestMetadata(url) {
|
|
|
|
fetch("/api/updateRequestMetadata?url=" + url)
|
|
|
|
.then(response => {
|
|
|
|
updateTable();
|
|
|
|
response.text().then(showMessage);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-11-01 20:07:16 +00:00
|
|
|
function deleteRequest(url) {
|
|
|
|
fetch("/api/deleteRequest", { method: 'POST', body: new URLSearchParams({
|
|
|
|
url: url
|
|
|
|
})})
|
|
|
|
.then(response => {
|
|
|
|
updateTable();
|
|
|
|
response.text().then(showMessage);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-02-22 06:25:23 +00:00
|
|
|
function updatePageTitle(pageTitle) {
|
|
|
|
streamerSettingsErrReset();
|
|
|
|
fetch("/api/updatePageTitle", { method: 'POST', body: new URLSearchParams({
|
|
|
|
pageTitle: pageTitle
|
|
|
|
})})
|
|
|
|
.then(response => {
|
|
|
|
if (response.ok) {
|
|
|
|
location.reload();
|
|
|
|
} else {
|
|
|
|
response.text().then(streamerSettingsErr);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2020-11-01 20:07:16 +00:00
|
|
|
|
2021-02-22 06:25:23 +00:00
|
|
|
function updateColors(colors) {
|
|
|
|
streamerSettingsErrReset();
|
|
|
|
fetch("/api/updateColors", { method: 'POST', headers: {
|
|
|
|
'Content-Type': 'application/json'
|
|
|
|
}, body: JSON.stringify(colors)})
|
|
|
|
.then(response => {
|
|
|
|
if (response.ok) {
|
|
|
|
location.reload();
|
|
|
|
} else {
|
|
|
|
response.text().then(streamerSettingsErr);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2020-11-01 20:07:16 +00:00
|
|
|
|
2021-02-23 05:53:03 +00:00
|
|
|
function updateVotePoints(user,follower,subscriber) {
|
|
|
|
streamerSettingsErrReset();
|
|
|
|
fetch("/api/updateVotePoints", { method: 'POST', body: new URLSearchParams({
|
|
|
|
user: user,
|
|
|
|
follower: follower,
|
|
|
|
subscriber: subscriber
|
|
|
|
})})
|
|
|
|
.then(response => {
|
|
|
|
if (response.ok) {
|
|
|
|
location.reload();
|
|
|
|
} else {
|
|
|
|
response.text().then(streamerSettingsErr);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-07-04 06:22:28 +00:00
|
|
|
updateTable();
|
2020-07-06 03:41:42 +00:00
|
|
|
|
|
|
|
document.addEventListener("keydown", function onEvent(event) {
|
|
|
|
if (event.key === "Escape") {
|
2021-02-23 05:53:03 +00:00
|
|
|
closeAllModals();
|
2020-07-06 03:41:42 +00:00
|
|
|
}
|
|
|
|
});
|
2020-11-01 20:07:16 +00:00
|
|
|
document.getElementById("modalBackground").addEventListener("click", (e) => { if (e.target === e.currentTarget) closeAllModals();});
|
|
|
|
|
|
|
|
var updateRequestStateSelect = document.getElementById("updateRequestStateSelect");
|
|
|
|
for(state of validStates) {
|
|
|
|
var opt = document.createElement("option");
|
|
|
|
opt.text = state;
|
|
|
|
opt.value = state;
|
|
|
|
updateRequestStateSelect.add(opt)
|
|
|
|
}
|