Files
vjs-player/player-init.js
2026-03-27 23:21:31 -07:00

115 lines
3.1 KiB
JavaScript

/**
* This script is an init script designed to load ajs or vjs if the specific elements exist
* Copyright oxmc. <https://oxmc.is-a.dev/>
*/
window.addEventListener('DOMContentLoaded', async (event) => {
console.log('DOM fully loaded and parsed, Adding player scripts and styles');
console.log("Player-init.js, dynamically including CSS and JS");
// Load script dynamically
function loadScript(scriptUrl) {
const script = document.createElement('script');
script.src = scriptUrl;
document.body.appendChild(script);
return new Promise((res, rej) => {
script.onload = res;
script.onerror = rej;
});
}
// Remove specified file from DOM (JS/CSS)
function removeFile(filename, filetype) {
const targetElement = filetype === "js" ? "script" : filetype === "css" ? "link" : null;
const targetAttr = filetype === "js" ? "src" : filetype === "css" ? "href" : null;
if (!targetElement || !targetAttr) return;
const allElements = document.getElementsByTagName(targetElement);
for (let i = allElements.length - 1; i >= 0; i--) {
if (allElements[i] && allElements[i].getAttribute(targetAttr)?.includes(filename)) {
allElements[i].parentNode.removeChild(allElements[i]);
console.log(`Removed: ${filename}`);
}
}
}
// Remove elements by class name
function removeElementsByClass(className) {
const elements = document.getElementsByClassName(className);
while (elements.length > 0) {
elements[0].parentNode.removeChild(elements[0]);
}
}
// List of CSS and JS to load
const includeList = {
vjs: {
css: [
"bundle/vjs-player-bundle.min.css"
],
js: [
"bundle/vjs-player-bundle.min.js",
]
},
ajs: {
css: [
"assets/lib/ajs/audiojs-playertheme.css"
],
js: [
"assets/lib/ajs/audio.min.js",
"assets/lib/ajs/player.js"
]
}
};
// Load Video.js if there are video elements
const videoPlayers = document.querySelectorAll("video");
if (videoPlayers.length > 0) {
console.log("Adding VJS CSS");
// Add VJS CSS
for (const url of includeList.vjs.css) {
const link = document.createElement('link');
link.rel = "stylesheet";
link.type = "text/css";
link.href = url;
document.head.appendChild(link);
}
console.log("Adding VJS JS");
// Load VJS JS
try {
await loadScript('https://vjs.zencdn.net/7.20.3/video.min.js');
for (const url of includeList.vjs.js) {
await loadScript(url);
}
// Finally, load player.js
await loadScript('assets/lib/vjs/player.js');
console.log('VJS Player loaded!');
} catch (err) {
console.error('Failed to load VJS scripts:', err);
}
}
// Load Audio.js if there are audio elements
const audioPlayers = document.querySelectorAll("audio");
if (audioPlayers.length > 0) {
console.log("Adding AJS CSS");
// Add AJS CSS
for (const url of includeList.ajs.css) {
const link = document.createElement('link');
link.rel = "stylesheet";
link.type = "text/css";
link.href = url;
document.head.appendChild(link);
}
console.log("Adding AJS JS");
// Load AJS JS
for (const url of includeList.ajs.js) {
await loadScript(url);
}
}
console.log("End Player-init.js");
});