jadefin/JadefinUtils.js
2024-03-01 21:01:22 +01:00

94 lines
2.4 KiB
JavaScript

//@ts-check
import JadefinIntegrity from "./JadefinIntegrity.js";
import JadefinModules from "./JadefinModules.js";
export default JadefinIntegrity("JadefinUtils", import.meta.url, () => window["JadefinUtils"] = new (class JadefinUtils {
events = new EventTarget();
eventTypes = {
WORKER_CREATING: "workerCreating",
WORKER_CREATED: "workerCreated",
HTML_VIDEO_PLAYER_CHANGED: "htmlVideoPlayerChanged"
};
htmlVideoPlayers = new Set();
/** @type {any} */
htmlVideoPlayer = null;
get isInMovie() {
return !!document.querySelector(".osdHeader");
}
get video() {
return /** @type {HTMLVideoElement} */ (document.querySelector(".videoPlayerContainer > video.htmlvideoplayer"));
}
get assCanvas() {
return /** @type {HTMLCanvasElement} */ (document.querySelector(".videoPlayerContainer > .libassjs-canvas-parent > canvas.libassjs-canvas"));
}
get routePath() {
return JadefinModules.Emby.Page.currentRouteInfo.route.path;
}
get routePathIsVideo() {
return this.routePath == "playback/video/index.html";
}
get currentPlayer() {
return JadefinModules.playbackManager._currentPlayer;
}
/**
* @param {() => any} cb
* @param {number} [tries]
* @param {number} [time]
*/
waitUntil(cb, tries, time) {
let triesLeft = tries || Number.POSITIVE_INFINITY;
return new Promise((resolve, reject) => {
const interval = setInterval(() => {
const v = cb();
if (v) {
resolve(v);
clearInterval(interval);
} else if ((--triesLeft) <= 0) {
reject(v);
clearInterval(interval);
}
}, time || 100);
});
}
/**
* @template T
* @param {T | null | undefined} value
* @returns {T}
*/
notnull(value) {
if (value === null || value === undefined) {
throw new Error("notnull encountered null");
}
return value;
}
/**
* @param {Node | null} el
* @param {Node} check
*/
hasParent(el, check) {
for (let curr = el; curr; curr = curr?.parentNode) {
if (curr == check) {
return true;
}
}
return false;
}
})());