@tycoonsystems/tycoon-modules
Version:
www.tv.tycoon.systems/documentation
1 lines • 3.7 kB
JavaScript
let vast=null;import{throttleFunctionCall}from"@tycoonsystems/tycoon-modules/util";const handleLoadIMA=(e,n)=>{if(vast=n)console.log("Window",window,e),"undefined"!=typeof window&&window?.google?loadIMA(e):setTimeout(()=>{handleLoadIMA(e)},1e3);else try{window.imaAdManagerDidLoad=!0}catch(e){}},loadIMA=e=>{var n,o,t,i,s=!1,a=document.getElementById(e);function d(e){console.log("ad container clicked"),console.log(i,a),a.paused?i.resume():a.pause()}function l(e){_(),window.imaPlaying=!0,console.log("Ad Started!"),n?.classList.contains("no_pointer_events")&&n.classList.remove("no_pointer_events"),a.classList.contains("no_pointer_events")||a.classList.add("no_pointer_events")}function r(e){window.imaPlaying=!1,n?.classList.contains("no_pointer_events")||n.classList.add("no_pointer_events"),a.classList.contains("no_pointer_events")&&a.classList.remove("no_pointer_events")}function c(e){window.imaPlaying=!1,n?.classList.contains("no_pointer_events")||n.classList.add("no_pointer_events"),a.classList.contains("no_pointer_events")&&a.classList.remove("no_pointer_events")}function g(e){try{throttleFunctionCall(window,"_onAdProgress",1500,e=>{try{_(),A(),window.imaPlaying=!0,a.pause()}catch(e){}},[e])}catch(e){}}function v(e){console.log("Loaded",e,a),window.imaAdManagerDidLoad=!0,(i=e.getAdsManager(a)).addEventListener(google.ima.AdErrorEvent.Type.AD_ERROR,E),i.addEventListener(google.ima.AdEvent.Type.CONTENT_PAUSE_REQUESTED,L),i.addEventListener(google.ima.AdEvent.Type.CONTENT_RESUME_REQUESTED,m),i.addEventListener(google.ima.AdEvent.Type.LOADED,u),i.addEventListener(google.ima.AdEvent.Type.AD_STARTED,l),i.addEventListener(google.ima.AdEvent.Type.AD_STOPPED,r),i.addEventListener(google.ima.AdEvent.Type.ALL_ADS_COMPLETED,c),i.addEventListener(google.ima.AdEvent.Type.AD_PROGRESS,g)}function E(e){console.log(e?.getError()),i?.destroy&&i.destroy()}function A(){var e,n;i?.resize&&(e=a.clientWidth,n=a.clientHeight,i.resize(e,n,google.ima.ViewMode.NORMAL))}function L(){window.imaPlaying=!0,n?.classList.contains("no_pointer_events")&&n.classList.remove("no_pointer_events"),_(),a.pause()}function m(){window.imaPlaying=!1,n?.classList.contains("no_pointer_events")||n.classList.add("no_pointer_events"),_(),console.log("VideoEleme",a),window.isAuthorized&&a.play()}function _(){try{let e;null!==a?.volume&&(e=a.volume),null!=(e=a.muted?0:e)&&i.setVolume(e)}catch(e){}}function u(e){e=e.getAd();console.log("Ad!",e,e?.isLinear()),_(),e.isLinear()||window.isAuthorized&&a.play()}console.log("Video Element",a,e),console.log("Initializing IMA",a),google?.ima?.AdDisplayContainer&&((n=document.getElementById("ad-container")).addEventListener("click",d),o=new google.ima.AdDisplayContainer(n,a),(t=new google.ima.AdsLoader(o)).addEventListener(google.ima.AdsManagerLoadedEvent.Type.ADS_MANAGER_LOADED,v,!1),t.addEventListener(google.ima.AdErrorEvent.Type.AD_ERROR,E,!1),a.addEventListener("ended",function(){t.contentComplete()}),(e=new google.ima.AdsRequest).adTagUrl=vast,e.linearAdSlotWidth=a.clientWidth,e.linearAdSlotHeight=a.clientHeight,e.nonLinearAdSlotWidth=a.clientWidth,e.nonLinearAdSlotHeight=a.clientHeight/3,console.log("Running request for ads",e),t.requestAds(e)),a.addEventListener("play",function(e){if(!s){s=!0,e.preventDefault(),console.log("loading ads",a),a.load(),o?.initialize&&o.initialize();var e=a.clientWidth,n=a.clientHeight;try{i.init(e,n,google.ima.ViewMode.NORMAL),i.start(),console.log(i),window.imaPlaying=!0,_()}catch(e){console.log("AdsManager could not be started"),window.isAuthorized&&a.play()}}}),window.addEventListener("resize",function(e){console.log("window resized")}),window.addEventListener("resize",function(e){A()})};export{handleLoadIMA};