129 lines
2.7 KiB
Svelte
129 lines
2.7 KiB
Svelte
<script>
|
|
import { Link } from "svelte-navigator";
|
|
import { onMount } from "svelte";
|
|
import Spinner from "./components/Spinner.svelte";
|
|
|
|
export let audioId;
|
|
export let videoId;
|
|
|
|
let videoUrl = "";
|
|
let audioUrl = "";
|
|
|
|
$: videoCode = extract(videoUrl);
|
|
$: audioCode = extract(audioUrl);
|
|
|
|
let videoReady = false;
|
|
let audioReady = false;
|
|
|
|
let videoPlayer;
|
|
let audioPlayer;
|
|
|
|
function extract(url) {
|
|
const match = url.match(/(youtu\.be\/|v=)([\w-]+)/);
|
|
if (match) {
|
|
return match[2];
|
|
} else {
|
|
return "???";
|
|
}
|
|
}
|
|
|
|
onMount(async () => {
|
|
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");
|
|
if (!currentResult.ok) {
|
|
alert(currentResult.status);
|
|
return;
|
|
}
|
|
const current = await currentResult.json();
|
|
videoUrl = current.find((link) => link.id == videoId).url;
|
|
audioUrl = current.find((link) => link.id == audioId).url;
|
|
|
|
// @ts-ignore
|
|
window.onYouTubeIframeAPIReady = () => {
|
|
// @ts-ignore
|
|
videoPlayer = new YT.Player("videoPlayer", {
|
|
videoId: videoCode,
|
|
events: {
|
|
onReady: () => {
|
|
videoReady = true;
|
|
},
|
|
},
|
|
});
|
|
|
|
// @ts-ignore
|
|
audioPlayer = new YT.Player("audioPlayer", {
|
|
videoId: audioCode,
|
|
events: {
|
|
onReady: () => {
|
|
audioReady = true;
|
|
},
|
|
},
|
|
});
|
|
};
|
|
});
|
|
|
|
function go() {
|
|
videoPlayer.playVideo();
|
|
videoPlayer.mute();
|
|
audioPlayer.playVideo();
|
|
}
|
|
</script>
|
|
|
|
<main>
|
|
<a
|
|
class="link"
|
|
class:disabled={!videoReady || !audioReady}
|
|
href="javascript:;"
|
|
on:click={go}>START</a
|
|
>
|
|
|
|
<div id="player">
|
|
<h2 title={videoUrl}>Video</h2>
|
|
{#if !videoReady}<Spinner />{/if}
|
|
<div id="videoPlayer" />
|
|
<h2 title={audioUrl}>Audio</h2>
|
|
{#if !audioReady}<Spinner />{/if}
|
|
<div id="audioPlayer" />
|
|
</div>
|
|
|
|
<Link class="link" to="/">BACK</Link>
|
|
</main>
|
|
|
|
<style>
|
|
main {
|
|
text-align: center;
|
|
display: flex;
|
|
flex-direction: column;
|
|
justify-content: space-between;
|
|
align-items: center;
|
|
}
|
|
|
|
:global(.link) {
|
|
font-family: inherit;
|
|
font-size: inherit;
|
|
padding: 1em 2em;
|
|
margin: 2rem;
|
|
color: white;
|
|
background-color: rgba(255, 255, 255, 0.25);
|
|
border-radius: 2em;
|
|
border: 2px solid rgba(255, 255, 255, 0);
|
|
outline: none;
|
|
width: 200px;
|
|
font-variant-numeric: tabular-nums;
|
|
cursor: pointer;
|
|
text-decoration: none;
|
|
}
|
|
|
|
#audioPlayer {
|
|
height: 128px;
|
|
}
|
|
|
|
.disabled {
|
|
color: gray;
|
|
cursor: not-allowed;
|
|
}
|
|
</style>
|