Add API for Hubspot Video

Status: Idea Submitted

So frustrated with this video player as a developer.
All I want to do is have a video in a modal that is triggered by a button that says ‘view video’. The user would click, the overlay would fade on over the page and the video would start. when the user clicks again the overplay closes and the video stops.

I wrote all the pieces, and added the video. Did a test without autoplay first, obviously when I closed the modal the video kept playing of course. So thats when I do a simple search ‘hubspot video player js api’ - NO RESULTS (oh god this is going to be a nightmare).
I go to the hubspot slack board and search the same. Results are from the past few years and some clearly have solutions for a previous version of the player that are no longer relevant.
I do see one thing though that looks a little promising:

window.addEventListener('message', function(e){ 
 if(e.data.videoId) {
 const stop = document.querySelector('.js-stop');
 const watch = document.querySelector('.js-watchButton');
 watch.addEventListener('click', openModal);
 stop.addEventListener('click', pauseMedia);
 let hsPlayers = hsVideoApi.getPlayers();
 let hsPlayer = hsPlayers[0];
 function pauseMedia() {
 hsPlayer.triggerPause();
 }
 function openModal() {
 hsPlayer.postMessageToPlayer("SET_PLAYER_STATUS", {status: "PLAYING"})
 }
 }
});

Its a pretty crappy way to handle it, but at this point I’m just like ‘fine whatever’. So I try to replicate this, but run into a problem immediately. Because the overlay is set to ‘display:none’ the player never actually inits until it becomes visible. So that means I can’t add the click eventlisteners for the ‘view video’ button inside the message event listener, because unless the button is clicked, the overlay is never visible.
I move the click event outside and run into the next problem. My click event runs before the ‘hsPlayer’ is ready. I do a few more tests with setTimeout (which is obviously a big no-no, but I just wanted to see). I’m console logging to check the value of hsPlayer and I finally get it to be available.
I then send the message:

hsPlayer.postMessageToPlayer("SET_PLAYER_STATUS", {status: "PLAYING"})

and… NOTHING HAPPENS! in fact I’ve been messing with this for hours and this postMessageToPlayer function has never worked once.

Hubspot, RESPOND!