Skip to content

Instantly share code, notes, and snippets.

@rileyrichter
Created February 4, 2023 00:56
Show Gist options
  • Save rileyrichter/7326fbb50fca5ba23698d7872c7142e7 to your computer and use it in GitHub Desktop.
Save rileyrichter/7326fbb50fca5ba23698d7872c7142e7 to your computer and use it in GitHub Desktop.
YouTube Facade with YouTube API
const facade = document.querySelector(".yt-facade");
window.onload = (event) => {
const tag = document.createElement("script");
tag.src = "https://www.youtube.com/iframe_api";
const firstScriptTag = document.getElementsByTagName("script")[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
facade.addEventListener("mouseover", replaceFacade);
};
function replaceFacade() {
let newPlayer = document.createElement("div");
newPlayer.id = "player";
newPlayer.style.width = "100%";
newPlayer.style.height = "100%";
newPlayer.style.aspectRatio = "16/9";
let player;
function onYouTubeIframeAPIReady() {
player = new YT.Player(newPlayer, {
videoId: "-dhsDrSLovk",
playerVars: {
playsinline: 1,
},
});
}
facade.replaceWith(newPlayer);
onYouTubeIframeAPIReady();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment