Nicous commited on
Commit
8a9c12a
·
verified ·
1 Parent(s): 7b59b1a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -54
app.py CHANGED
@@ -360,91 +360,95 @@ button.lg.secondary.svelte-1gz44hr span {
360
  </style>
361
 
362
  <script>
363
- // Function to initialize controls once elements are found
364
  function initializeControls() {
365
  const video = document.querySelector('[data-testid="Video-player"]');
366
  const waveform = document.getElementById('waveform');
367
 
368
- // 如果元素还没准备好,直接返回
369
  if (!video || !waveform) {
 
370
  return;
371
  }
372
-
373
- // 尝试获取音频元素
374
  const audio = waveform.querySelector('div')?.shadowRoot?.querySelector('audio');
375
  if (!audio) {
 
376
  return;
377
  }
378
 
379
  console.log('Elements found:', { video, audio });
380
-
381
- // 监听视频播放进度
382
- video.addEventListener("play", () => {
383
- if (audio.paused) {
384
- audio.play(); // 如果音频暂停,开始播放
385
- }
386
- });
387
 
388
- // 监听音频播放进度
389
- audio.addEventListener("play", () => {
390
- if (video.paused) {
391
- video.play(); // 如果视频暂停,开始播放
392
- }
393
- });
394
 
395
- // 同步视频和音频的播放进度
396
- video.addEventListener("timeupdate", () => {
397
- if (Math.abs(video.currentTime - audio.currentTime) > 0.1) {
398
- audio.currentTime = video.currentTime; // 如果时间差超过0.1秒,同步
399
- }
400
- });
401
 
402
- audio.addEventListener("timeupdate", () => {
403
- if (Math.abs(audio.currentTime - video.currentTime) > 0.1) {
404
- video.currentTime = audio.currentTime; // 如果时间差超过0.1秒,同步
405
- }
406
- });
 
407
 
408
- // 监听暂停事件,确保视频和音频都暂停
409
- video.addEventListener("pause", () => {
410
- if (!audio.paused) {
411
- audio.pause(); // 如果音频未暂停,暂停音频
412
- }
413
- });
414
 
415
- audio.addEventListener("pause", () => {
416
- if (!video.paused) {
417
- video.pause(); // 如果视频未暂停,暂停视频
418
- }
419
- });
 
 
 
 
 
 
 
420
  }
421
 
422
- // 创建观察器监听DOM变化
423
  const observer = new MutationObserver((mutations) => {
424
- for (const mutation of mutations) {
425
- if (mutation.addedNodes.length) {
426
- // 当有新节点添加时,尝试初始化
427
- const waveform = document.getElementById('waveform');
428
- if (waveform?.querySelector('div')?.shadowRoot?.querySelector('audio')) {
429
- console.log('Audio element detected');
430
- initializeControls();
431
- // 可选:如果不需要继续监听,可以断开观察器
432
- // observer.disconnect();
433
  }
434
- }
435
- }
436
  });
437
 
438
- // 开始观察
439
  observer.observe(document.body, {
440
  childList: true,
441
  subtree: true
442
  });
443
 
444
- // 页面加载完成时也尝试初始化
445
  document.addEventListener('DOMContentLoaded', () => {
446
  console.log('DOM Content Loaded');
447
- initializeControls();
 
 
 
 
 
 
448
  });
449
 
450
  </script>
 
360
  </style>
361
 
362
  <script>
 
363
  function initializeControls() {
364
  const video = document.querySelector('[data-testid="Video-player"]');
365
  const waveform = document.getElementById('waveform');
366
 
367
+ // Ensure the video and waveform elements are available
368
  if (!video || !waveform) {
369
+ console.log("Video or waveform element not found.");
370
  return;
371
  }
372
+
373
+ // Try to get the audio element from the shadow DOM
374
  const audio = waveform.querySelector('div')?.shadowRoot?.querySelector('audio');
375
  if (!audio) {
376
+ console.log("Audio element not found.");
377
  return;
378
  }
379
 
380
  console.log('Elements found:', { video, audio });
 
 
 
 
 
 
 
381
 
382
+ // Event listener to synchronize the video and audio play states
383
+ video.addEventListener("play", () => {
384
+ if (audio.paused) {
385
+ audio.play(); // If audio is paused, start playing it
386
+ }
387
+ });
388
 
389
+ audio.addEventListener("play", () => {
390
+ if (video.paused) {
391
+ video.play(); // If video is paused, start playing it
392
+ }
393
+ });
 
394
 
395
+ // Sync the play progress between video and audio
396
+ video.addEventListener("timeupdate", () => {
397
+ if (Math.abs(video.currentTime - audio.currentTime) > 0.1) {
398
+ audio.currentTime = video.currentTime; // Sync time if the difference is more than 0.1s
399
+ }
400
+ });
401
 
402
+ audio.addEventListener("timeupdate", () => {
403
+ if (Math.abs(audio.currentTime - video.currentTime) > 0.1) {
404
+ video.currentTime = audio.currentTime; // Sync time if the difference is more than 0.1s
405
+ }
406
+ });
 
407
 
408
+ // Pause both video and audio when one of them is paused
409
+ video.addEventListener("pause", () => {
410
+ if (!audio.paused) {
411
+ audio.pause();
412
+ }
413
+ });
414
+
415
+ audio.addEventListener("pause", () => {
416
+ if (!video.paused) {
417
+ video.pause();
418
+ }
419
+ });
420
  }
421
 
422
+ // Create a MutationObserver to watch for DOM changes
423
  const observer = new MutationObserver((mutations) => {
424
+ mutations.forEach((mutation) => {
425
+ mutation.addedNodes.forEach((node) => {
426
+ // Check if the audio element has been added
427
+ if (node.tagName === 'AUDIO') {
428
+ console.log("Audio element detected");
429
+ initializeControls(); // Call initialize when the audio element is found
430
+ observer.disconnect(); // Disconnect the observer once we've found the audio element
 
 
431
  }
432
+ });
433
+ });
434
  });
435
 
436
+ // Start observing the DOM for new nodes
437
  observer.observe(document.body, {
438
  childList: true,
439
  subtree: true
440
  });
441
 
442
+ // Ensure the initialization happens after DOM is fully loaded
443
  document.addEventListener('DOMContentLoaded', () => {
444
  console.log('DOM Content Loaded');
445
+ initializeControls(); // Initialize controls once the DOM is loaded
446
+ });
447
+
448
+ // In case the page takes longer to load, use the load event
449
+ window.addEventListener('load', () => {
450
+ console.log('Window loaded');
451
+ initializeControls(); // Ensure everything is loaded before initializing
452
  });
453
 
454
  </script>