This commit is contained in:
www
2026-05-26 17:02:57 +08:00
parent 7f910ee22f
commit 0096805a49
14 changed files with 830 additions and 58 deletions

View File

@@ -5,7 +5,7 @@
* -----------------------------------------------------
* - 不使用全局变量
* - 支持一个页面多个播放器
* - 初始化 data-engine="dplayer" 的模块
* - 优先初始化 data-engine="dplayer" 的模块,兼容旧模板里的 #dplayer
* =====================================================
*/
@@ -22,8 +22,16 @@
}
function resolveContainer(root) {
if (root && root.querySelector) {
return root.querySelector('.dplayer, [id="dplayer"]');
if (root) {
if (root.matches && (root.matches('.dplayer') || root.id === 'dplayer')) {
return root;
}
if (root.querySelector) {
var scoped = root.querySelector('.dplayer, [id="dplayer"]');
if (scoped) {
return scoped;
}
}
}
return document.getElementById('dplayer') || document.querySelector('.dplayer');
@@ -35,6 +43,18 @@
showError(root, '播放器容器不存在');
return null;
}
if (!strPlayUrl) {
showError(root, '播放地址不存在');
return null;
}
if (typeof DPlayer === "undefined") {
showError(root, '播放器脚本加载失败');
return null;
}
if (DomDPlayer.dataset.dplayerReady === '1') {
return null;
}
DomDPlayer.dataset.dplayerReady = '1';
// ---- 创建 DPlayer 实例 ----
try {
@@ -54,7 +74,11 @@
},
});
videoPlayer.play()
window.videoPlayer = videoPlayer;
var playResult = videoPlayer.play()
if (playResult && playResult.catch) {
playResult.catch(function () {});
}
// ---- 错误处理(兜底)----
videoPlayer.on('error', function () {
@@ -64,7 +88,8 @@
return videoPlayer;
} catch (e) {
console.log(e)
console.warn(e);
DomDPlayer.dataset.dplayerReady = '0';
showError(root, '播放器初始化失败');
return null;
}
@@ -77,10 +102,11 @@
}
ensurePrefix(root);
var err = root.querySelector('.' + root.dataset.prefix + '-player-status');
var errClass = root.dataset.prefix ? root.dataset.prefix + '-player-status' : 'player-status';
var err = root.querySelector('.' + errClass);
if (!err) {
err = document.createElement('div');
err.className = root.dataset.prefix + '-player-status is-error';
err.className = errClass + ' is-error';
root.appendChild(err);
}
err.innerText = msg;
@@ -95,6 +121,20 @@
});
}
function resolveRoot() {
var root = document.querySelector('[data-engine="dplayer"]');
if (root) {
return root;
}
var container = document.getElementById('dplayer') || document.querySelector('.dplayer');
if (!container) {
return null;
}
return container.closest ? (container.closest('[data-engine="dplayer"]') || container) : container;
}
// 获取第一个线路 key带容错
function getDefaultLine() {
if (typeof arrPlayUrl === "undefined" || !arrPlayUrl || typeof arrPlayUrl !== "object") {
@@ -120,23 +160,36 @@
return arrPlayUrl[playLine][0].url || null;
}
function getIndexedPlayUrl(playLine, playIndex) {
if (!arrPlayUrl || !arrPlayUrl[playLine]) {
console.warn("播放线路不存在:", playLine, arrPlayUrl);
return null;
}
var intIndex = Math.max(1, parseInt(playIndex, 10) || 1);
var activeUrl = arrPlayUrl[playLine][intIndex - 1] || arrPlayUrl[playLine][0];
return activeUrl ? (activeUrl.url || null) : null;
}
// DOM Ready
document.addEventListener("DOMContentLoaded", function () {
try {
if (typeof strVideoId == "undefined") return;
var root = document.querySelector('[data-engine="dplayer"]');
var root = resolveRoot();
if (!root) return;
boot();
ensurePrefix(root);
console.log(strPlayType)
console.log(boolIsPlayPage)
if (typeof strPlayType !== "undefined" && boolIsPlayPage) {
// 用户指定线路
if (strPlayType !== "default") {
const activeUrl = arrPlayUrl[strPlayType][intPlayUrlIndex - 1];
strPlayUrl = activeUrl.url;
strPlayUrl = getIndexedPlayUrl(strPlayType, intPlayUrlIndex);
if (!strPlayUrl) {
const defaultLine = getDefaultLine();
strPlayUrl = getFirstPlayUrl(defaultLine);
}
// checkLine(strPlayType);
// 默认播放第一个线路的第一个 URL
@@ -145,7 +198,6 @@
strPlayUrl = getFirstPlayUrl(defaultLine);
// checkLine(defaultLine);
}
console.log('strPlayUrlstrPlayUrl')
initDPlayer(root, strPlayUrl);
} else if (strPlayType == "default" && !boolIsPlayPage) {
@@ -157,9 +209,7 @@
initDPlayer(root, strPlayUrl);
}
} catch (error) {
console.log(error);
console.warn(error);
}
})
})();