Skip to content

Commit 94fff0d

Browse files
author
Taois
committed
feat: 支持web://弹幕。已知问题,关闭弹幕再开不好使
1 parent e2f3edb commit 94fff0d

File tree

1 file changed

+92
-22
lines changed

1 file changed

+92
-22
lines changed

dashboard/src/components/players/ArtVideoPlayer.vue

Lines changed: 92 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ const proxySettingsVersion = ref(0)
209209
const danmakuEnabled = ref(JSON.parse(localStorage.getItem('danmakuEnabled') || 'true'))
210210
const danmakuData = ref([])
211211
const danmakuLoading = ref(false)
212+
const currentDanmakuUrl = ref('') // 当前弹幕iframe的URL
212213
213214
214215
// 计算属性:是否显示调试按钮
@@ -456,23 +457,8 @@ const parseDanmakuData = (data) => {
456457
}
457458
}
458459
459-
// 创建弹幕iframe层 - 处理web://协议的弹幕链接
460-
const createDanmakuIframeLayer = async (iframeUrl) => {
461-
try {
462-
if (!artPlayerInstance.value) {
463-
console.warn('ArtPlayer实例不存在,无法创建弹幕iframe层')
464-
return
465-
}
466-
467-
console.log('创建弹幕iframe层:', iframeUrl)
468-
469-
// 移除已存在的弹幕iframe层
470-
removeDanmakuIframeLayer()
471-
472-
// 更新iframe层内容
473-
artPlayerInstance.value.layers.update({
474-
name: 'danmaku-iframe',
475-
html: `
460+
const buildDanmakuIframeHtml = (iframeUrl)=>{
461+
return `
476462
<div class="danmaku-iframe-container" style="
477463
position: absolute;
478464
top: 0;
@@ -482,8 +468,8 @@ const createDanmakuIframeLayer = async (iframeUrl) => {
482468
pointer-events: none;
483469
z-index: 10;
484470
">
485-
<iframe
486-
src="${iframeUrl}"
471+
<iframe
472+
src="${iframeUrl}"
487473
style="
488474
width: 100%;
489475
height: 100%;
@@ -495,15 +481,37 @@ const createDanmakuIframeLayer = async (iframeUrl) => {
495481
allowtransparency="true"
496482
></iframe>
497483
</div>
498-
`,
484+
`
485+
}
486+
487+
// 创建弹幕iframe层 - 处理web://协议的弹幕链接
488+
const createDanmakuIframeLayer = async (iframeUrl) => {
489+
try {
490+
if (!artPlayerInstance.value) {
491+
console.warn('ArtPlayer实例不存在,无法创建弹幕iframe层')
492+
return
493+
}
494+
495+
console.log('创建弹幕iframe层:', iframeUrl)
496+
497+
// 存储当前弹幕URL
498+
currentDanmakuUrl.value = iframeUrl
499+
500+
// 移除已存在的弹幕iframe层
501+
removeDanmakuIframeLayer()
502+
503+
// 更新iframe层内容
504+
artPlayerInstance.value.layers.update({
505+
name: 'danmaku-iframe',
506+
html: buildDanmakuIframeHtml(currentDanmakuUrl.value),
499507
style: {
500508
position: 'absolute',
501509
top: '0',
502510
left: '0',
503511
width: '100%',
504512
height: '100%',
505513
pointerEvents: 'none',
506-
display: 'block',
514+
display: danmakuEnabled.value ? 'block' : 'none',
507515
zIndex: '10'
508516
}
509517
})
@@ -514,7 +522,7 @@ const createDanmakuIframeLayer = async (iframeUrl) => {
514522
}
515523
}
516524
517-
// 移除弹幕iframe层
525+
// 移除弹幕iframe层(完全清空内容)
518526
const removeDanmakuIframeLayer = () => {
519527
try {
520528
if (artPlayerInstance.value && artPlayerInstance.value.layers) {
@@ -538,6 +546,59 @@ const removeDanmakuIframeLayer = () => {
538546
}
539547
}
540548
549+
// 隐藏弹幕iframe层(保留内容,只隐藏显示)
550+
const hideDanmakuIframeLayer = () => {
551+
try {
552+
if (artPlayerInstance.value && artPlayerInstance.value.layers) {
553+
const currentLayer = artPlayerInstance.value.layers['danmaku-iframe']
554+
if (currentLayer) {
555+
artPlayerInstance.value.layers.update({
556+
name: 'danmaku-iframe',
557+
html: '',
558+
style: {
559+
position: 'absolute',
560+
top: '0',
561+
left: '0',
562+
width: '100%',
563+
height: '100%',
564+
pointerEvents: 'none',
565+
zIndex: '10',
566+
display: 'none'
567+
}
568+
})
569+
console.log('隐藏弹幕iframe层成功')
570+
}
571+
}
572+
} catch (error) {
573+
console.error('隐藏弹幕iframe层失败:', error)
574+
}
575+
}
576+
577+
// 显示弹幕iframe层
578+
const showDanmakuIframeLayer = () => {
579+
try {
580+
if (artPlayerInstance.value && artPlayerInstance.value.layers && currentDanmakuUrl.value) {
581+
artPlayerInstance.value.layers.update({
582+
name: 'danmaku-iframe',
583+
html: buildDanmakuIframeHtml(currentDanmakuUrl.value),
584+
style: {
585+
position: 'absolute',
586+
top: '0',
587+
left: '0',
588+
width: '100%',
589+
height: '100%',
590+
pointerEvents: 'none',
591+
display: 'block',
592+
zIndex: '10'
593+
}
594+
})
595+
console.log('显示弹幕iframe层成功:', currentDanmakuUrl.value)
596+
}
597+
} catch (error) {
598+
console.error('显示弹幕iframe层失败:', error)
599+
}
600+
}
601+
541602
// 处理弹幕URL - 根据协议类型选择处理方式
542603
const handleDanmakuUrl = async () => {
543604
try {
@@ -1894,6 +1955,15 @@ watch(danmakuEnabled, (newEnabled) => {
18941955
}
18951956
}
18961957
1958+
// 控制iframe弹幕层的显示/隐藏
1959+
if (newEnabled) {
1960+
console.log('showDanmakuIframeLayer');
1961+
showDanmakuIframeLayer()
1962+
} else {
1963+
console.log('hideDanmakuIframeLayer');
1964+
hideDanmakuIframeLayer()
1965+
}
1966+
18971967
// 持久化到 localStorage
18981968
localStorage.setItem('danmakuEnabled', newEnabled.toString())
18991969
})

0 commit comments

Comments
 (0)