maotube/public/watch/script.js

52 lines
1.5 KiB
JavaScript
Raw Permalink Normal View History

2024-01-19 03:22:22 +00:00
function error(message) {
const errorContainer = document.getElementById("mao-error");
const errorElement = document.getElementById("mao-error-message");
document.getElementById("video-result").style.display = "none";
errorElement.innerText = message || "unknown error";
2024-01-19 03:22:22 +00:00
errorContainer.classList.remove("hidden");
}
async function main() {
2024-01-19 03:22:22 +00:00
const params = new URLSearchParams(window.location.search);
const id = params.get("id");
if (!id) {
error("invalid id parameter");
return;
}
const info = await fetch(`/api/video-info?id=${id}`);
if (!info.ok) {
error("error fetching video info");
return;
}
2024-01-19 03:22:22 +00:00
const json = await info.json();
if (!json.ok) {
error(json.error);
return;
2024-01-19 03:22:22 +00:00
}
const video = json.video;
const player = document.getElementById("video-player");
player.controls = true;
player.src = video.path;
player.onerror = () => {
error("unable to play video");
}
player.onload = () => {
player.style.height = player.clientWidth / 16 * 9 + "px";
}
document.getElementById("video-title").innerText = video.title;
document.getElementById("video-author").innerText = "by " + video.author + " - published " + new Date(video.created_at).toLocaleDateString();
document.getElementById("video-description").innerText = video.description;
document.getElementById("video-result").style.display = "block";
2024-01-19 03:22:22 +00:00
}
main();