JSFiddle - React, Tailwind, and code Playground

by nocircleno

JavaScript

(function() {
        var videoEl, iFrame, iFrameDocument, iFrameWindow, smartPlayerConfig;

        var onVideoEvent = function(e) {
            var video = e.currentTarget;
            // use the video src as an id
            var videoSrc = video.getElementsByTagName("source")[0].src;
            var eventType = e.type;
            // save the event somewhere
            console.log("eventType: " + eventType);
            console.log("videoSrc: " + videoSrc);
        }

        var onCustomSmartPlayerEvent = function(eventInfo) {
            if(eventInfo.category === "HTML5-Video" && eventInfo.action === "Video_Started") {
                // remove event listener once video has started.  Now the video tag is in the iframe document.
                smartPlayerConfig.useCustomEventTracking(false, undefined);

                // grab the video tag from the iframe document
                var videos = iFrameDocument.getElementsByTagName('video');
                if(videos.length > 0) {
                    videoEl = videos[0];

                    // add events you want to listen
                    videoEl.addEventListener("play", onVideoEvent);
                    videoEl.addEventListener("pause", onVideoEvent);
                }
            }
        }

        var onIFrameLoad = function(e) {
            iFrameDocument =  iFrame.contentDocument;
            iFrameWindow = iFrame.contentWindow;

            // grab the smart player configuration JS and use the custom event tracking to listen
            // for the user to press the big play button.  The video tag will be available then.
            smartPlayerConfig = iFrameWindow.TSC.playerConfiguration;
            smartPlayerConfig.useCustomEventTracking(true, onCustomSmartPlayerEvent);
        }

        function watchForIFrameLoadedEvent() {
            iFrame = document.getElementsByTagName("iframe")[0];
            iFrame.onload = onIFrameLoad;
        }

        watchForIFrameLoadedEvent();
    }());