Is this YouTube code able to use addPlayer instead of window?
I was trying to figure out how to do that.
Does window have to be used with loadPlayer()?
Working code using window, trying to use addPlayer instead with loadPlayer().
https://jsfiddle.net/xdrtuj5e/
const videoPlayer = (function makeVideoPlayer() {
let player = null;
function loadPlayer() {
const tag = document.createElement("script");
tag.src = "https://www.youtube.com/iframe_api";
const firstScriptTag = document.getElementsByTagName("script")[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
}
function onPlayerReady(event) {
player = event.target;
player.setVolume(100);
}
window.onYouTubePlayerAPIReady = function() {
const video = document.querySelector(".video");
/* function addPlayer(video) { */
new YT.Player(video, {
width: 606,
height: 344,
videoId: video.dataset.id,
playerVars: {
autoplay: 0,
controls: 1,
showinfo: 1,
rel: 0,
iv_load_policy: 3,
cc_load_policy: 0,
fs: 0,
disablekb: 1
},
events: {
"onReady": onPlayerReady
}
});
};
function init() {
loadPlayer();
}
return {
/* addPlayer,*/
init
};
}());
(function iife() {
function coverClickHandler(evt) {
videoPlayer.init();
}
const cover = document.querySelector(".playa");
cover.addEventListener("click", coverClickHandler);
}());
/*
function onYouTubeIframeAPIReady() {
const frameContainer = document.querySelector(".video");
videoPlayer.addPlayer(frameContainer);
}*/
/*
function onYouTubeIframeAPIReady() {
const cover = document.querySelector(".playa");
const wrapper = cover.parentElement;
const frameContainer = wrapper.querySelector(".video");
videoPlayer.addPlayer(frameContainer);
}*/