Created
February 4, 2023 00:56
-
-
Save rileyrichter/7326fbb50fca5ba23698d7872c7142e7 to your computer and use it in GitHub Desktop.
YouTube Facade with YouTube API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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