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

update js in app.py

Browse files
Files changed (1) hide show
  1. app.py +65 -82
app.py CHANGED
@@ -360,96 +360,79 @@ button.lg.secondary.svelte-1gz44hr span {
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>
455
  """
 
360
  </style>
361
 
362
  <script>
363
+ // 观察回调函数
364
+ const observerCallback = (mutationsList, observer) => {
365
+ mutationsList.forEach(mutation => {
366
+ console.log(mutation);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
367
  });
368
+ };
369
+
370
+ // 检查目标元素是否存在,如果存在则开始观察
371
+ const checkAndObserve = () => {
372
+ const targetNode = document.getElementById('targetElement');
373
+ if (targetNode) {
374
+ const observer = new MutationObserver(observerCallback);
375
+ observer.observe(targetNode, { childList: true, subtree: true });
376
+ console.log('开始观察目标元素');
377
+ }
378
+ };
379
 
380
+ // 使用 setInterval 定期检查目标节点是否存在
381
+ const intervalId = setInterval(() => {
382
+ checkAndObserve();
383
+ }, 100);
 
 
384
 
385
+ // 可选:设置最大等待时间,防止无限检查
386
+ setTimeout(() => {
387
+ clearInterval(intervalId); // 停止检查
388
+ console.log('停止检查目标元素');
389
+ }, 10000); // 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
+ // 如果目标元素是动态生成的,可以使用 MutationObserver 来监听其父节点的变化
402
+ const containerNode = document.getElementById('container'); // 包含目标元素的父节点
403
+
404
+ const containerObserverCallback = (mutationsList, observer) => {
405
+ mutationsList.forEach(mutation => {
406
+ if (mutation.type === 'childList') {
407
+ mutation.addedNodes.forEach(addedNode => {
408
+ if (addedNode.id === 'targetElement') {
409
+ console.log('目标元素已添加');
410
+ observer.disconnect(); // 停止监听
411
+ // 开始观察目标元素
412
+ const observer = new MutationObserver(observerCallback);
413
+ observer.observe(addedNode, { childList: true, subtree: true });
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
+ waitForElement(); // 启动检查直到元素出现
 
 
 
 
436
 
437
  </script>
438
  """