In JavaScript I need to detect if YouTube ad is currently playing, my code is not working for me. This is a single page personal project no need to use YouTube Data API.
function onPlayerStateChange(event) {
if (event.data == YT.PlayerState.PLAYING) {
if (document.querySelector(".video-ads .ad-container, .ytp-ad-player-overlay")) {
YouTubeAdChannel.postMessage('ad_playing');
}
}
}