fix URL changes, re-gimmes, etc.
This commit is contained in:
		
							parent
							
								
									5023235a2a
								
							
						
					
					
						commit
						5725ace006
					
				
					 1 changed files with 55 additions and 31 deletions
				
			
		| 
						 | 
					@ -6,15 +6,18 @@
 | 
				
			||||||
  export let audioId;
 | 
					  export let audioId;
 | 
				
			||||||
  export let videoId;
 | 
					  export let videoId;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  // @ts-ignore
 | 
				
			||||||
 | 
					  let ytReady = Boolean(window.YT);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  let videoUrl = "";
 | 
					  let videoUrl = "";
 | 
				
			||||||
  let audioUrl = "";
 | 
					  let audioUrl = "";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  $: videoCode = extract(videoUrl);
 | 
					 | 
				
			||||||
  $: audioCode = extract(audioUrl);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  let videoReady = false;
 | 
					  let videoReady = false;
 | 
				
			||||||
  let audioReady = false;
 | 
					  let audioReady = false;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  let videoEl;
 | 
				
			||||||
 | 
					  let audioEl;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  let videoPlayer;
 | 
					  let videoPlayer;
 | 
				
			||||||
  let audioPlayer;
 | 
					  let audioPlayer;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -27,12 +30,7 @@
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  onMount(async () => {
 | 
					  async function init() {
 | 
				
			||||||
    const tag = document.createElement("script");
 | 
					 | 
				
			||||||
    tag.src = "https://www.youtube.com/iframe_api";
 | 
					 | 
				
			||||||
    const firstScriptTag = document.getElementsByTagName("script")[0];
 | 
					 | 
				
			||||||
    firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    const currentResult = await fetch("grid.php");
 | 
					    const currentResult = await fetch("grid.php");
 | 
				
			||||||
    if (!currentResult.ok) {
 | 
					    if (!currentResult.ok) {
 | 
				
			||||||
      alert(currentResult.status);
 | 
					      alert(currentResult.status);
 | 
				
			||||||
| 
						 | 
					@ -42,30 +40,56 @@
 | 
				
			||||||
    videoUrl = current.find((link) => link.id == videoId).url;
 | 
					    videoUrl = current.find((link) => link.id == videoId).url;
 | 
				
			||||||
    audioUrl = current.find((link) => link.id == audioId).url;
 | 
					    audioUrl = current.find((link) => link.id == audioId).url;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    videoEl.innerHTML = "";
 | 
				
			||||||
 | 
					    let videoPlayerEl = document.createElement("div");
 | 
				
			||||||
 | 
					    videoEl.appendChild(videoPlayerEl);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    // @ts-ignore
 | 
				
			||||||
 | 
					    videoPlayer = new YT.Player(videoPlayerEl, {
 | 
				
			||||||
 | 
					      videoId: extract(videoUrl),
 | 
				
			||||||
 | 
					      events: {
 | 
				
			||||||
 | 
					        onReady: () => {
 | 
				
			||||||
 | 
					          videoReady = true;
 | 
				
			||||||
 | 
					        },
 | 
				
			||||||
 | 
					      },
 | 
				
			||||||
 | 
					    });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    audioEl.innerHTML = "";
 | 
				
			||||||
 | 
					    let audioPlayerEl = document.createElement("div");
 | 
				
			||||||
 | 
					    audioEl.appendChild(audioPlayerEl);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    // @ts-ignore
 | 
				
			||||||
 | 
					    audioPlayer = new YT.Player(audioPlayerEl, {
 | 
				
			||||||
 | 
					      videoId: extract(audioUrl),
 | 
				
			||||||
 | 
					      events: {
 | 
				
			||||||
 | 
					        onReady: () => {
 | 
				
			||||||
 | 
					          audioReady = true;
 | 
				
			||||||
 | 
					        },
 | 
				
			||||||
 | 
					      },
 | 
				
			||||||
 | 
					    });
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  onMount(() => {
 | 
				
			||||||
 | 
					    const tag = document.createElement("script");
 | 
				
			||||||
 | 
					    tag.src = "https://www.youtube.com/iframe_api";
 | 
				
			||||||
 | 
					    const firstScriptTag = document.getElementsByTagName("script")[0];
 | 
				
			||||||
 | 
					    firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // @ts-ignore
 | 
					    // @ts-ignore
 | 
				
			||||||
    window.onYouTubeIframeAPIReady = () => {
 | 
					    window.onYouTubeIframeAPIReady = () => {
 | 
				
			||||||
      // @ts-ignore
 | 
					      ytReady = true;
 | 
				
			||||||
      videoPlayer = new YT.Player("videoPlayer", {
 | 
					 | 
				
			||||||
        videoId: videoCode,
 | 
					 | 
				
			||||||
        events: {
 | 
					 | 
				
			||||||
          onReady: () => {
 | 
					 | 
				
			||||||
            videoReady = true;
 | 
					 | 
				
			||||||
          },
 | 
					 | 
				
			||||||
        },
 | 
					 | 
				
			||||||
      });
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
      // @ts-ignore
 | 
					 | 
				
			||||||
      audioPlayer = new YT.Player("audioPlayer", {
 | 
					 | 
				
			||||||
        videoId: audioCode,
 | 
					 | 
				
			||||||
        events: {
 | 
					 | 
				
			||||||
          onReady: () => {
 | 
					 | 
				
			||||||
            audioReady = true;
 | 
					 | 
				
			||||||
          },
 | 
					 | 
				
			||||||
        },
 | 
					 | 
				
			||||||
      });
 | 
					 | 
				
			||||||
    };
 | 
					    };
 | 
				
			||||||
  });
 | 
					  });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  $: {
 | 
				
			||||||
 | 
					    audioId;
 | 
				
			||||||
 | 
					    videoId;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    if (ytReady) {
 | 
				
			||||||
 | 
					      init();
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  function go() {
 | 
					  function go() {
 | 
				
			||||||
    videoPlayer.playVideo();
 | 
					    videoPlayer.playVideo();
 | 
				
			||||||
    videoPlayer.mute();
 | 
					    videoPlayer.mute();
 | 
				
			||||||
| 
						 | 
					@ -84,10 +108,10 @@
 | 
				
			||||||
  <div id="player">
 | 
					  <div id="player">
 | 
				
			||||||
    <h2 title={videoUrl}>Video</h2>
 | 
					    <h2 title={videoUrl}>Video</h2>
 | 
				
			||||||
    {#if !videoReady}<Spinner />{/if}
 | 
					    {#if !videoReady}<Spinner />{/if}
 | 
				
			||||||
    <div id="videoPlayer" />
 | 
					    <div id="videoPlayer" bind:this={videoEl} />
 | 
				
			||||||
    <h2 title={audioUrl}>Audio</h2>
 | 
					    <h2 title={audioUrl}>Audio</h2>
 | 
				
			||||||
    {#if !audioReady}<Spinner />{/if}
 | 
					    {#if !audioReady}<Spinner />{/if}
 | 
				
			||||||
    <div id="audioPlayer" />
 | 
					    <div id="audioPlayer" bind:this={audioEl} />
 | 
				
			||||||
  </div>
 | 
					  </div>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  <Link class="link" to="/">BACK</Link>
 | 
					  <Link class="link" to="/">BACK</Link>
 | 
				
			||||||
| 
						 | 
					@ -118,7 +142,7 @@
 | 
				
			||||||
    text-decoration: none;
 | 
					    text-decoration: none;
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  #audioPlayer {
 | 
					  :global(#audioPlayer > *) {
 | 
				
			||||||
    height: 128px;
 | 
					    height: 128px;
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		
		Reference in a new issue