Skip to content

Instantly share code, notes, and snippets.

@NickDeckerDevs
Last active March 1, 2023 13:55
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NickDeckerDevs/5213df3ff7e79161bbd15c175e6c6726 to your computer and use it in GitHub Desktop.
Save NickDeckerDevs/5213df3ff7e79161bbd15c175e6c6726 to your computer and use it in GitHub Desktop.
hubspot video status handling? via Stuart Grant - Dev @ King Post Studio, LLC (hubspot dev slack)
const SET_PLAYER_STATUS = 'SET_PLAYER_STATUS';
const hsVideos = window.hsVideoApi?.getPlayers();
const videoEls = document.querySelectorAll('.video-container');
function manageVideoStatus() {
videoEls.forEach((videoEl) => {
const iframe = videoEl.querySelector('iframe');
if (iframe) {
const videoId = iframe.getAttribute('id')?.replace('hs_player_', '');
const hsVideo = hsVideos.find(({ id }) => id === videoId);
if (hsVideo) {
if (isElInView(iframe)) {
if (hsVideo.status !== statuses.playing) {
hsVideo.postMessageToPlayer(SET_PLAYER_STATUS, { status: statuses.playing });
}
} else if (hsVideo.status !== statuses.paused) {
hsVideo.postMessageToPlayer(SET_PLAYER_STATUS, { status: statuses.paused });
}
}
}
});
}
window.addEventListener('scroll', manageVideoStatus);
manageVideoStatus();
@alexandru-burca
Copy link

thank you, it works just fine

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment