Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -360,80 +360,93 @@ button.lg.secondary.svelte-1gz44hr span {
|
|
360 |
</style>
|
361 |
|
362 |
<script>
|
363 |
-
//
|
364 |
-
|
365 |
-
|
366 |
-
|
367 |
-
|
368 |
-
|
369 |
-
|
370 |
-
//
|
371 |
-
|
372 |
-
|
373 |
-
|
374 |
-
|
375 |
-
|
376 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
377 |
}
|
378 |
-
};
|
379 |
|
380 |
-
|
381 |
-
|
382 |
-
|
383 |
-
}
|
|
|
384 |
|
385 |
-
|
386 |
-
|
387 |
-
|
388 |
-
|
389 |
-
}, 1000000); // 10秒后停止检查
|
390 |
-
|
391 |
-
// 另外一种方法:在 DOM 加载完成后检查
|
392 |
-
document.addEventListener('DOMContentLoaded', () => {
|
393 |
-
const targetNode = document.getElementById('targetElement');
|
394 |
-
if (targetNode) {
|
395 |
-
const observer = new MutationObserver(observerCallback);
|
396 |
-
observer.observe(targetNode, { childList: true, subtree: true });
|
397 |
-
console.log('开始观察目标元素');
|
398 |
}
|
399 |
-
});
|
400 |
|
401 |
-
//
|
402 |
-
|
403 |
-
|
404 |
-
|
405 |
-
|
406 |
-
|
407 |
-
|
408 |
-
|
409 |
-
|
410 |
-
|
411 |
-
|
412 |
-
|
413 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
414 |
}
|
415 |
});
|
416 |
}
|
417 |
});
|
418 |
-
};
|
419 |
-
|
420 |
-
const containerObserver = new MutationObserver(containerObserverCallback);
|
421 |
-
containerObserver.observe(containerNode, { childList: true, subtree: true });
|
422 |
-
|
423 |
-
// 使用 requestAnimationFrame 高效检查目标元素是否存在
|
424 |
-
const waitForElement = () => {
|
425 |
-
const targetNode = document.getElementById('targetElement');
|
426 |
-
if (targetNode) {
|
427 |
-
const observer = new MutationObserver(observerCallback);
|
428 |
-
observer.observe(targetNode, { childList: true, subtree: true });
|
429 |
-
console.log('开始观察目标元素');
|
430 |
-
} else {
|
431 |
-
requestAnimationFrame(waitForElement);
|
432 |
-
}
|
433 |
-
};
|
434 |
|
435 |
-
|
|
|
|
|
|
|
|
|
436 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
437 |
</script>
|
438 |
"""
|
439 |
|
|
|
360 |
</style>
|
361 |
|
362 |
<script>
|
363 |
+
// 新版同步控制代码
|
364 |
+
function syncMediaElements() {
|
365 |
+
// 获取视频和音频元素
|
366 |
+
const video = document.querySelector('[data-testid="Video-player"] video');
|
367 |
+
const waveform = document.querySelector('#waveform');
|
368 |
+
const audio = waveform?.querySelector('audio') || waveform?.shadowRoot?.querySelector('audio');
|
369 |
+
|
370 |
+
// 如果任一元素不存在,则退出
|
371 |
+
if (!video || !audio) return;
|
372 |
+
|
373 |
+
// 解除旧的事件监听(避免重复绑定)
|
374 |
+
video.removeEventListener('play', syncPlay);
|
375 |
+
audio.removeEventListener('play', syncPlay);
|
376 |
+
video.removeEventListener('timeupdate', syncVideoTime);
|
377 |
+
audio.removeEventListener('timeupdate', syncAudioTime);
|
378 |
+
|
379 |
+
// 定义同步函数
|
380 |
+
function syncPlay(e) {
|
381 |
+
if(e.target === video && audio.paused) audio.play();
|
382 |
+
if(e.target === audio && video.paused) video.play();
|
383 |
}
|
|
|
384 |
|
385 |
+
function syncVideoTime() {
|
386 |
+
if(!audio.seeking && Math.abs(video.currentTime - audio.currentTime) > 0.1){
|
387 |
+
audio.currentTime = video.currentTime;
|
388 |
+
}
|
389 |
+
}
|
390 |
|
391 |
+
function syncAudioTime() {
|
392 |
+
if(!video.seeking && Math.abs(audio.currentTime - video.currentTime) > 0.1){
|
393 |
+
video.currentTime = audio.currentTime;
|
394 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
395 |
}
|
|
|
396 |
|
397 |
+
// 绑定新的事件监听
|
398 |
+
video.addEventListener('play', syncPlay);
|
399 |
+
audio.addEventListener('play', syncPlay);
|
400 |
+
video.addEventListener('timeupdate', syncVideoTime);
|
401 |
+
audio.addEventListener('timeupdate', syncAudioTime);
|
402 |
+
|
403 |
+
// 同步暂停事件
|
404 |
+
video.addEventListener('pause', () => audio.pause());
|
405 |
+
audio.addEventListener('pause', () => video.pause());
|
406 |
+
|
407 |
+
console.log('Media elements synced successfully!');
|
408 |
+
}
|
409 |
+
|
410 |
+
// 智能DOM观察器
|
411 |
+
const observer = new MutationObserver((mutations) => {
|
412 |
+
mutations.forEach((mutation) => {
|
413 |
+
if (mutation.addedNodes.length) {
|
414 |
+
mutation.addedNodes.forEach((node) => {
|
415 |
+
// 深度检查新增节点
|
416 |
+
if (node.nodeType === 1) { // Element node
|
417 |
+
// 检查是否包含视频组件
|
418 |
+
if (node.querySelector?.('[data-testid="Video-player"]')) {
|
419 |
+
// 当视频组件出现时,开始查找音频
|
420 |
+
const audioObserver = new MutationObserver(() => {
|
421 |
+
if(document.querySelector('#waveform audio')) {
|
422 |
+
audioObserver.disconnect();
|
423 |
+
setTimeout(syncMediaElements, 500); // 等待组件完全加载
|
424 |
+
}
|
425 |
+
});
|
426 |
+
audioObserver.observe(document.body, {
|
427 |
+
childList: true,
|
428 |
+
subtree: true
|
429 |
+
});
|
430 |
+
}
|
431 |
}
|
432 |
});
|
433 |
}
|
434 |
});
|
435 |
+
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
436 |
|
437 |
+
// 开始观察整个文档
|
438 |
+
observer.observe(document.body, {
|
439 |
+
childList: true,
|
440 |
+
subtree: true
|
441 |
+
});
|
442 |
|
443 |
+
// 初始检查(应对组件已存在的情况)
|
444 |
+
setTimeout(() => {
|
445 |
+
if(document.querySelector('[data-testid="Video-player"] video') &&
|
446 |
+
document.querySelector('#waveform audio')){
|
447 |
+
syncMediaElements();
|
448 |
+
}
|
449 |
+
}, 1000);
|
450 |
</script>
|
451 |
"""
|
452 |
|