I have a youtube playlist embedded in an iframe and was hoping to use the playlist index to cycle through an array of titles on-screen (i.e. when the next video starts playing, the next item in the array will show). Is this possible? It only shows the first in the array and doesn't change so what am I doing wrong? Apologies in advance because I'm very new to JS... this might be gibberish but I felt like I was on to something.
var myArray = ["song 1", "song 2"];
var player;
function onYouTubePlayerAPIReady() {
player = new YT.Player('video', {
events: {
'onReady': onPlayerReady
}
});
}
function onPlayerReady(event) {
console.log(Array.from(document.querySelectorAll('.ytd-playlist-video-list-renderer #video-title')).map((el) => {
return el.textContent.trim()
}).sort().join("\n"))
var Number =
player.getPlaylistIndex();
var print = document.getElementById('print');
print.innerHTML = myArray[Number];
}
<div id="print"></div>
<iframe
id="video"
src="https://www.youtube.com/embed?listType=playlist&list=ID&loop=1&enablejsapi=1&html5=1"
frameborder="0"
allowfullscreen
></iframe>