stream-api/frontend/script.js

114 lines
4.2 KiB
JavaScript

(() => {
const info = {% raw info %};
const handle = () => {
const [body] = document.getElementsByTagName("body");
const video = document.createElement("video");
video.className = "video-js vjs-big-play-centered";
body.appendChild(video);
const options = {};
options.controls = true;
options.liveui = true;
options.responsive = true;
options.fill = true;
options.techOrder = ["chromecast", "html5"];
options.plugins = {};
options.plugins.chromecast = {};
options.plugins.chromecast.addButtonToControlBar = false;
const player = videojs(video, options);
if(info.poster) {
player.poster(info.poster);
}
const source = {};
source.type = info.ctype;
source.src = info.upstream;
player.src(source);
const dl = document.createElement("a");
dl.href = info.download;
const canPlayTypeRaw = player.canPlayType(info.ctype);
const canPlayType = (canPlayTypeRaw === "maybe") || (canPlayTypeRaw === "probably");
if(canPlayType) {
const Button = videojs.getComponent("Button");
const cbutton = {};
cbutton.clickHandler = (ev) => {
player.trigger("chromecastRequested");
}
cbutton.text = "Chromecast";
const dbutton = {};
dbutton.clickHandler = (ev) => {
document.body.appendChild(dl);
dl.click();
document.body.removeChild(dl);
}
dbutton.text = "Download";
const bigCastButtonWrapper = new Button(player, cbutton);
const bigDownloadButtonWrapper = new Button(player, dbutton);
const bigCastButton = bigCastButtonWrapper.el();
const bigDownloadButton = bigDownloadButtonWrapper.el();
for(const elem of bigCastButton.children) {
bigCastButton.removeChild(elem);
}
for(const elem of bigDownloadButton.children) {
bigDownloadButton.removeChild(elem);
}
bigCastButton.classList.add("vjs-control");
bigCastButton.classList.add("vjs-button");
bigCastButton.classList.add("fab");
bigCastButton.classList.add("fa-chromecast");
bigCastButton.classList.add("vjs-big-chromecast");
player.addChild(bigCastButtonWrapper);
if(info.download) {
bigDownloadButton.classList.add("vjs-control");
bigDownloadButton.classList.add("vjs-button");
bigDownloadButton.classList.add("fas");
bigDownloadButton.classList.add("fa-download");
bigDownloadButton.classList.add("vjs-big-download");
player.addChild(bigDownloadButtonWrapper);
}
player.one("error", () => {
player.removeChild(bigCastButtonWrapper);
if(info.download) {
player.removeChild(bigDownloadButtonWrapper);
}
});
player.one("play", () => {
player.removeChild(bigCastButtonWrapper);
if(!info.ctype.toLowerCase().includes("mpegurl")) {
player.removeChild(bigDownloadButtonWrapper);
}
});
const smallCastButtonWrapper = new Button(player, cbutton);
const smallCastButton = smallCastButtonWrapper.el();
smallCastButton.classList.add("vjs-control");
smallCastButton.classList.add("vjs-button");
for(const elem of smallCastButton.children) {
if(elem.classList.contains("vjs-icon-placeholder")) {
elem.classList.add("fab");
elem.classList.add("fa-chromecast");
}
}
player.controlBar.addChild(smallCastButtonWrapper);
player.on("chromecastConnected", () => {
if(player.volume() > 0.3) {
player.volume(0.2);
}
});
}
}
document.addEventListener("DOMContentLoaded", handle);
})();