From 5aca99f9038cc134cd70b04648c64173e72ead1f Mon Sep 17 00:00:00 2001 From: Basil Goldman Date: Tue, 31 Jan 2023 15:32:11 +0200 Subject: [PATCH] fixed ad stopped before ad started then ad error --- dist/ima-wrapper.es.js | 2 +- dist/ima-wrapper.js | 2 +- package.json | 2 +- public/js/ima-wrapper.js | 6 +++--- src/ima-wrapper.js | 15 ++++++++++++--- 5 files changed, 18 insertions(+), 9 deletions(-) diff --git a/dist/ima-wrapper.es.js b/dist/ima-wrapper.es.js index 08437f2..0402e45 100644 --- a/dist/ima-wrapper.es.js +++ b/dist/ima-wrapper.es.js @@ -1 +1 @@ -var t={d:(e,i)=>{for(var n in i)t.o(i,n)&&!t.o(e,n)&&Object.defineProperty(e,n,{enumerable:!0,get:i[n]})},o:(t,e)=>Object.prototype.hasOwnProperty.call(t,e)},e={};t.d(e,{Z:()=>n});const i=function(t,e,i){if(!(t&&(t instanceof HTMLElement||t.getRootNode)&&e&&(e instanceof HTMLElement||e.getRootNode)))throw new Error("ad container and/or video element not defined");this._adContainer=t,this._videoElement=e,this._callback=i,this.EVENTS={AdsManagerLoaded:"AdsManagerLoaded",AdStarted:"AdStarted",AdStopped:"AdStopped",AdSkipped:"AdSkipped",AdLoaded:"AdLoaded",AdLinearChange:"AdLinearChange",AdSizeChange:"AdSizeChange",AdExpandedChange:"AdExpandedChange",AdSkippableStateChange:"AdSkippableStateChange",AdDurationChange:"AdDurationChange",AdRemainingTimeChange:"AdRemainingTimeChange",AdVolumeChange:"AdVolumeChange",AdImpression:"AdImpression",AdClickThru:"AdClickThru",AdInteraction:"AdInteraction",AdVideoStart:"AdVideoStart",AdVideoFirstQuartile:"AdVideoFirstQuartile",AdVideoMidpoint:"AdVideoMidpoint",AdVideoThirdQuartile:"AdVideoThirdQuartile",AdVideoComplete:"AdVideoComplete",AdUserAcceptInvitation:"AdUserAcceptInvitation",AdUserMinimize:"AdUserMinimize",AdUserClose:"AdUserClose",AdPaused:"AdPaused",AdPlaying:"AdPlaying",AdError:"AdError",AdLog:"AdLog",AllAdsCompleted:"AllAdsCompleted"},this._eventCallbacks={},this._adsLoader=null,this._adDisplayContainer=null,this._adDisplayContainerInitialized=!1,this._adsManager=null,this._currentAd=null,this._adPodInfo=null,this._attributes={version:"1.0.6"},this._options={autoplay:!0,muted:!0,secure:!1,vastLoadTimeout:23e3,loadVideoTimeout:8e3},this.IMA_SDK_SRC="//imasdk.googleapis.com/js/sdkloader/ima3.js",window.google&&google.ima&&google.ima.AdsLoader?this.setupIMA():function(t,e=!1,i){const n=document.getElementsByTagName("head")[0]||document.documentElement,o=document.createElement("script");o.type="text/javascript",o.src=t,o.async=e,o.addEventListener("load",(function(){i&&"function"==typeof i&&i(!0,window)}),!1),o.addEventListener("error",(function(t){n.removeChild(o),i&&"function"==typeof i&&i(!1,t)}),!1),n.insertBefore(o,n.firstChild)}(this.IMA_SDK_SRC,!0,(t=>{t?this.setupIMA():this._callback&&"function"==typeof this._callback&&this._callback("ima3.js - failed to load")}))};i.prototype.setupIMA=function(){google.ima.settings.setDisableCustomPlaybackForIOS10Plus(!0),this._adDisplayContainer=new google.ima.AdDisplayContainer(this._adContainer,this._videoElement),this._adsLoader=new google.ima.AdsLoader(this._adDisplayContainer),this._adsLoader.addEventListener(google.ima.AdsManagerLoadedEvent.Type.ADS_MANAGER_LOADED,this.onIMAAdsManagerLoaded.bind(this),!1),this._adsLoader.addEventListener(google.ima.AdErrorEvent.Type.AD_ERROR,this.onIMAAdsManagerAdError.bind(this),!1),this._callback&&"function"==typeof this._callback&&this._callback()},i.prototype.doContentComplete=function(){this._adsLoader&&this._adsLoader.contentComplete()},i.prototype.maybeInitializeAdDisplayContainer=function(){this._adDisplayContainerInitialized||(this._adDisplayContainer.initialize(),this._adDisplayContainerInitialized=!0)},i.prototype.init=function(t,e,i){this.maybeInitializeAdDisplayContainer(),this._adsManager&&this._adsManager.init(t,e,i)},i.prototype.start=function(){this._adsManager&&this._adsManager.start()},i.prototype.getDuration=function(){return this._currentAd?this._currentAd.getDuration():-2},i.prototype.getRemainingTime=function(){return this._adsManager?this._adsManager.getRemainingTime():-2},i.prototype.resume=function(){this._adsManager&&this._adsManager.resume()},i.prototype.pause=function(){this._adsManager&&this._adsManager.pause()},i.prototype.stop=function(){this._adsManager&&this._adsManager.stop()},i.prototype.skip=function(){this._adsManager&&(this._adsManager.getAdSkippableState()&&this._adsManager.skip(),this.onAdSkipped())},i.prototype.resize=function(t,e,i){this._adsManager&&(this._adsManager.resize(t,e,i),this.onAdSizeChange())},i.prototype.getVolume=function(){return this._adsManager&&this._adsManager.getVolume()},i.prototype.setVolume=function(t){this._adsManager&&this._adsManager.setVolume(t)},i.prototype.getSkippableState=function(){return this._adsManager&&this._adsManager.getAdSkippableState()},i.prototype.collapse=function(){this._adsManager&&this._adsManager.collapse()},i.prototype.expand=function(){this._adsManager&&this._adsManager.expand()},i.prototype.requestAds=function(t,e){Object.assign(this._options,e);const i=new google.ima.AdsRequest;let n=!1;try{new URL(t),n=!0}catch(t){}n?i.adTagUrl=t:i.adsResponse=t,this._options.pageUrl&&(i.pageUrl=this._options.pageUrl),i.vastLoadTimeout=this._options.vastLoadTimeout,i.linearAdSlotWidth=this._videoElement.width,i.linearAdSlotHeight=this._videoElement.height,i.nonLinearAdSlotWidth=this._videoElement.width,i.nonLinearAdSlotHeight=this._videoElement.height,i.setAdWillAutoPlay(this._options.autoplay),this._options.muted&&i.setAdWillPlayMuted(this._options.muted),this._adsLoader.getSettings().setVpaidMode(this._options.secure?google.ima.ImaSdkSettings.VpaidMode.ENABLED:google.ima.ImaSdkSettings.VpaidMode.INSECURE),this._adsLoader.requestAds(i)},i.prototype.abort=function(){this._adsManager&&(this._adsManager.destroy(),this._adsManager=null),this.doContentComplete()},i.prototype._abort=function(){this.abort(),this.onAllAdsCompleted()},i.prototype.onIMAAdsManagerLoaded=function(t){const e=new google.ima.AdsRenderingSettings;e.restoreCustomPlaybackStateOnAdBreakComplete=!0,e.loadVideoTimeout=this._options.loadVideoTimeout,this._adsManager=t.getAdsManager(this._videoElement,e),this._adsManager.addEventListener(google.ima.AdErrorEvent.Type.AD_ERROR,this.onIMAAdError.bind(this)),this._adsManager.addEventListener(google.ima.AdEvent.Type.AD_BREAK_READY,(function(){})),this._adsManager.addEventListener(google.ima.AdEvent.Type.AD_METADATA,(function(){})),this._adsManager.addEventListener(google.ima.AdEvent.Type.ALL_ADS_COMPLETED,this.onAllAdsCompleted.bind(this)),this._adsManager.addEventListener(google.ima.AdEvent.Type.CLICK,this.onIMAAdClickThru.bind(this)),this._adsManager.addEventListener(google.ima.AdEvent.Type.COMPLETE,this.onIMAAdVideoComplete.bind(this)),this._adsManager.addEventListener(google.ima.AdEvent.Type.CONTENT_PAUSE_REQUESTED,this.onAdStarted.bind(this)),this._adsManager.addEventListener(google.ima.AdEvent.Type.CONTENT_RESUME_REQUESTED,this.onAdStopped.bind(this)),this._adsManager.addEventListener(google.ima.AdEvent.Type.DURATION_CHANGE,this.onAdDurationChange.bind(this)),this._adsManager.addEventListener(google.ima.AdEvent.Type.FIRST_QUARTILE,this.onAdVideoFirstQuartile.bind(this)),this._adsManager.addEventListener(google.ima.AdEvent.Type.IMPRESSION,this.onAdImpression.bind(this)),this._adsManager.addEventListener(google.ima.AdEvent.Type.INTERACTION,this.onAdInteraction.bind(this)),this._adsManager.addEventListener(google.ima.AdEvent.Type.LINEAR_CHANGED,this.onAdLinearChange.bind(this)),this._adsManager.addEventListener(google.ima.AdEvent.Type.LOADED,this.onIMAAdLoaded.bind(this)),this._adsManager.addEventListener(google.ima.AdEvent.Type.LOG,this.onIMAAdLog.bind(this)),this._adsManager.addEventListener(google.ima.AdEvent.Type.MIDPOINT,this.onAdVideoMidpoint.bind(this)),this._adsManager.addEventListener(google.ima.AdEvent.Type.PAUSED,this.onAdPaused.bind(this)),this._adsManager.addEventListener(google.ima.AdEvent.Type.RESUMED,this.onAdPlaying.bind(this)),this._adsManager.addEventListener(google.ima.AdEvent.Type.SKIPPABLE_STATE_CHANGED,this.onAdSkippableStateChange.bind(this)),this._adsManager.addEventListener(google.ima.AdEvent.Type.SKIPPED,this.onAdSkipped.bind(this)),this._adsManager.addEventListener(google.ima.AdEvent.Type.STARTED,this.onAdVideoStart.bind(this)),this._adsManager.addEventListener(google.ima.AdEvent.Type.THIRD_QUARTILE,this.onAdVideoThirdQuartile.bind(this)),this._adsManager.addEventListener(google.ima.AdEvent.Type.USER_CLOSE,this.onAdUserClose.bind(this)),this._adsManager.addEventListener(google.ima.AdEvent.Type.VOLUME_CHANGED,this.onAdVolumeChange.bind(this)),this._adsManager.addEventListener(google.ima.AdEvent.Type.VOLUME_MUTED,this.onAdVolumeChange.bind(this)),this.onAdsManagerLoaded()},i.prototype.onIMAAdsManagerAdError=function(t){this.onAdError(t.getError())},i.prototype.onIMAAdClickThru=function(t){this.onAdClickThru("",t.getAd().getAdId(),!1)},i.prototype.onIMAAdVideoComplete=function(){this.onAdVideoComplete()},i.prototype.onIMAAdLoaded=function(t){this._currentAd=t.getAd(),this.onAdLoaded(this._currentAd)},i.prototype.onIMAAdError=function(t){this.onAdError(t.getError())},i.prototype.onIMAAdLog=function(t){(t=t.getAdData()).hasOwnProperty("adError")?this.onAdLog(t.adError.getMessage()):this.onAdLog("IMA Ad Log")},i.prototype.onAdsManagerLoaded=function(){this._callEvent(this.EVENTS.AdsManagerLoaded)},i.prototype.onAdLoaded=function(t){this.EVENTS.AdLoaded in this._eventCallbacks&&this._eventCallbacks[this.EVENTS.AdLoaded](t)},i.prototype.onAdStarted=function(){this._callEvent(this.EVENTS.AdStarted)},i.prototype.onAdDurationChange=function(){this._callEvent(this.EVENTS.AdDurationChange)},i.prototype.onAdLinearChange=function(){this._callEvent(this.EVENTS.AdLinearChange)},i.prototype.onAdSkippableStateChange=function(){this._callEvent(this.EVENTS.AdSkippableStateChange)},i.prototype.onAdSizeChange=function(){this._callEvent(this.EVENTS.AdSizeChange)},i.prototype.onAdVolumeChange=function(){this._callEvent(this.EVENTS.AdVolumeChange)},i.prototype.onAdVideoStart=function(){this._callEvent(this.EVENTS.AdVideoStart)},i.prototype.onAdImpression=function(){this._callEvent(this.EVENTS.AdImpression)},i.prototype.onAdVideoFirstQuartile=function(){this._callEvent(this.EVENTS.AdVideoFirstQuartile)},i.prototype.onAdVideoMidpoint=function(){this._callEvent(this.EVENTS.AdVideoMidpoint)},i.prototype.onAdVideoThirdQuartile=function(){this._callEvent(this.EVENTS.AdVideoThirdQuartile)},i.prototype.onAdVideoComplete=function(){this._callEvent(this.EVENTS.AdVideoComplete)},i.prototype.onAdPaused=function(){this._callEvent(this.EVENTS.AdPaused)},i.prototype.onAdPlaying=function(){this._callEvent(this.EVENTS.AdPlaying)},i.prototype.onAdSkipped=function(){this._callEvent(this.EVENTS.AdSkipped),this._abort()},i.prototype.onAdStopped=function(){this._callEvent(this.EVENTS.AdStopped),this._abort()},i.prototype.onAdClickThru=function(t,e,i){this.EVENTS.AdClickThru in this._eventCallbacks&&this._eventCallbacks[this.EVENTS.AdClickThru](t,e,i)},i.prototype.onAdInteraction=function(){this._callEvent(this.EVENTS.AdInteraction)},i.prototype.onAdUserClose=function(){this._callEvent(this.EVENTS.AdUserClose)},i.prototype.onAllAdsCompleted=function(){this.abort(),this._callEvent(this.EVENTS.AllAdsCompleted)},i.prototype.onAdError=function(t){this.abort(),this.EVENTS.AdError in this._eventCallbacks&&this._eventCallbacks[this.EVENTS.AdError](t)},i.prototype.onAdLog=function(t){this.EVENTS.AdLog in this._eventCallbacks&&this._eventCallbacks[this.EVENTS.AdLog](t)},i.prototype._callEvent=function(t){t in this._eventCallbacks&&this._eventCallbacks[t]()},i.prototype.addEventListener=function(t,e,i){const n=e.bind(i);this._eventCallbacks[t]=n},i.prototype.removeEventListener=function(t){this._eventCallbacks[t]=null},i.prototype.getVersion=function(){return this._attributes.version};const n=i;var o=e.Z;export{o as default}; \ No newline at end of file +var t={d:(e,i)=>{for(var n in i)t.o(i,n)&&!t.o(e,n)&&Object.defineProperty(e,n,{enumerable:!0,get:i[n]})},o:(t,e)=>Object.prototype.hasOwnProperty.call(t,e)},e={};t.d(e,{Z:()=>n});const i=function(t,e,i){if(!(t&&(t instanceof HTMLElement||t.getRootNode)&&e&&(e instanceof HTMLElement||e.getRootNode)))throw new Error("ad container and/or video element not defined");this._adContainer=t,this._videoElement=e,this._callback=i,this.EVENTS={AdsManagerLoaded:"AdsManagerLoaded",AdStarted:"AdStarted",AdStopped:"AdStopped",AdSkipped:"AdSkipped",AdLoaded:"AdLoaded",AdLinearChange:"AdLinearChange",AdSizeChange:"AdSizeChange",AdExpandedChange:"AdExpandedChange",AdSkippableStateChange:"AdSkippableStateChange",AdDurationChange:"AdDurationChange",AdRemainingTimeChange:"AdRemainingTimeChange",AdVolumeChange:"AdVolumeChange",AdImpression:"AdImpression",AdClickThru:"AdClickThru",AdInteraction:"AdInteraction",AdVideoStart:"AdVideoStart",AdVideoFirstQuartile:"AdVideoFirstQuartile",AdVideoMidpoint:"AdVideoMidpoint",AdVideoThirdQuartile:"AdVideoThirdQuartile",AdVideoComplete:"AdVideoComplete",AdUserAcceptInvitation:"AdUserAcceptInvitation",AdUserMinimize:"AdUserMinimize",AdUserClose:"AdUserClose",AdPaused:"AdPaused",AdPlaying:"AdPlaying",AdError:"AdError",AdLog:"AdLog",AllAdsCompleted:"AllAdsCompleted"},this._eventCallbacks={},this._adsLoader=null,this._adDisplayContainer=null,this._adDisplayContainerInitialized=!1,this._adsManager=null,this._currentAd=null,this._adPodInfo=null,this._attributes={version:"1.0.7"},this._options={autoplay:!0,muted:!0,secure:!1,vastLoadTimeout:23e3,loadVideoTimeout:8e3},this._hasLoaded=!1,this._hasStarted=!1,this.IMA_SDK_SRC="//imasdk.googleapis.com/js/sdkloader/ima3.js",window.google&&google.ima&&google.ima.AdsLoader?this.setupIMA():function(t,e=!1,i){const n=document.getElementsByTagName("head")[0]||document.documentElement,o=document.createElement("script");o.type="text/javascript",o.src=t,o.async=e,o.addEventListener("load",(function(){i&&"function"==typeof i&&i(!0,window)}),!1),o.addEventListener("error",(function(t){n.removeChild(o),i&&"function"==typeof i&&i(!1,t)}),!1),n.insertBefore(o,n.firstChild)}(this.IMA_SDK_SRC,!0,(t=>{t?this.setupIMA():this._callback&&"function"==typeof this._callback&&this._callback("ima3.js - failed to load")}))};i.prototype.setupIMA=function(){google.ima.settings.setDisableCustomPlaybackForIOS10Plus(!0),this._adDisplayContainer=new google.ima.AdDisplayContainer(this._adContainer,this._videoElement),this._adsLoader=new google.ima.AdsLoader(this._adDisplayContainer),this._adsLoader.addEventListener(google.ima.AdsManagerLoadedEvent.Type.ADS_MANAGER_LOADED,this.onIMAAdsManagerLoaded.bind(this),!1),this._adsLoader.addEventListener(google.ima.AdErrorEvent.Type.AD_ERROR,this.onIMAAdsManagerAdError.bind(this),!1),this._callback&&"function"==typeof this._callback&&this._callback()},i.prototype.doContentComplete=function(){this._adsLoader&&this._adsLoader.contentComplete()},i.prototype.maybeInitializeAdDisplayContainer=function(){this._adDisplayContainerInitialized||(this._adDisplayContainer.initialize(),this._adDisplayContainerInitialized=!0)},i.prototype.init=function(t,e,i){this.maybeInitializeAdDisplayContainer(),this._adsManager&&this._adsManager.init(t,e,i)},i.prototype.start=function(){this._adsManager&&this._adsManager.start()},i.prototype.getDuration=function(){return this._currentAd?this._currentAd.getDuration():-2},i.prototype.getRemainingTime=function(){return this._adsManager?this._adsManager.getRemainingTime():-2},i.prototype.resume=function(){this._adsManager&&this._adsManager.resume()},i.prototype.pause=function(){this._adsManager&&this._adsManager.pause()},i.prototype.stop=function(){this._adsManager&&this._adsManager.stop()},i.prototype.skip=function(){this._adsManager&&(this._adsManager.getAdSkippableState()&&this._adsManager.skip(),this.onAdSkipped())},i.prototype.resize=function(t,e,i){this._adsManager&&(this._adsManager.resize(t,e,i),this.onAdSizeChange())},i.prototype.getVolume=function(){return this._adsManager&&this._adsManager.getVolume()},i.prototype.setVolume=function(t){this._adsManager&&this._adsManager.setVolume(t)},i.prototype.getSkippableState=function(){return this._adsManager&&this._adsManager.getAdSkippableState()},i.prototype.collapse=function(){this._adsManager&&this._adsManager.collapse()},i.prototype.expand=function(){this._adsManager&&this._adsManager.expand()},i.prototype.requestAds=function(t,e){Object.assign(this._options,e);const i=new google.ima.AdsRequest;let n=!1;try{new URL(t),n=!0}catch(t){}n?i.adTagUrl=t:i.adsResponse=t,this._options.pageUrl&&(i.pageUrl=this._options.pageUrl),i.vastLoadTimeout=this._options.vastLoadTimeout,i.linearAdSlotWidth=this._videoElement.width,i.linearAdSlotHeight=this._videoElement.height,i.nonLinearAdSlotWidth=this._videoElement.width,i.nonLinearAdSlotHeight=this._videoElement.height,i.setAdWillAutoPlay(this._options.autoplay),this._options.muted&&i.setAdWillPlayMuted(this._options.muted),this._adsLoader.getSettings().setVpaidMode(this._options.secure?google.ima.ImaSdkSettings.VpaidMode.ENABLED:google.ima.ImaSdkSettings.VpaidMode.INSECURE),this._adsLoader.requestAds(i)},i.prototype.abort=function(){this._adsManager&&(this._adsManager.destroy(),this._adsManager=null),this.doContentComplete()},i.prototype._abort=function(){this.abort(),this.onAllAdsCompleted()},i.prototype.onIMAAdsManagerLoaded=function(t){const e=new google.ima.AdsRenderingSettings;e.restoreCustomPlaybackStateOnAdBreakComplete=!0,e.loadVideoTimeout=this._options.loadVideoTimeout,this._adsManager=t.getAdsManager(this._videoElement,e),this._adsManager.addEventListener(google.ima.AdErrorEvent.Type.AD_ERROR,this.onIMAAdError.bind(this)),this._adsManager.addEventListener(google.ima.AdEvent.Type.AD_BREAK_READY,(function(){})),this._adsManager.addEventListener(google.ima.AdEvent.Type.AD_METADATA,(function(){})),this._adsManager.addEventListener(google.ima.AdEvent.Type.ALL_ADS_COMPLETED,this.onAllAdsCompleted.bind(this)),this._adsManager.addEventListener(google.ima.AdEvent.Type.CLICK,this.onIMAAdClickThru.bind(this)),this._adsManager.addEventListener(google.ima.AdEvent.Type.COMPLETE,this.onIMAAdVideoComplete.bind(this)),this._adsManager.addEventListener(google.ima.AdEvent.Type.CONTENT_PAUSE_REQUESTED,this.onAdStarted.bind(this)),this._adsManager.addEventListener(google.ima.AdEvent.Type.CONTENT_RESUME_REQUESTED,this.onAdStopped.bind(this)),this._adsManager.addEventListener(google.ima.AdEvent.Type.DURATION_CHANGE,this.onAdDurationChange.bind(this)),this._adsManager.addEventListener(google.ima.AdEvent.Type.FIRST_QUARTILE,this.onAdVideoFirstQuartile.bind(this)),this._adsManager.addEventListener(google.ima.AdEvent.Type.IMPRESSION,this.onAdImpression.bind(this)),this._adsManager.addEventListener(google.ima.AdEvent.Type.INTERACTION,this.onAdInteraction.bind(this)),this._adsManager.addEventListener(google.ima.AdEvent.Type.LINEAR_CHANGED,this.onAdLinearChange.bind(this)),this._adsManager.addEventListener(google.ima.AdEvent.Type.LOADED,this.onIMAAdLoaded.bind(this)),this._adsManager.addEventListener(google.ima.AdEvent.Type.LOG,this.onIMAAdLog.bind(this)),this._adsManager.addEventListener(google.ima.AdEvent.Type.MIDPOINT,this.onAdVideoMidpoint.bind(this)),this._adsManager.addEventListener(google.ima.AdEvent.Type.PAUSED,this.onAdPaused.bind(this)),this._adsManager.addEventListener(google.ima.AdEvent.Type.RESUMED,this.onAdPlaying.bind(this)),this._adsManager.addEventListener(google.ima.AdEvent.Type.SKIPPABLE_STATE_CHANGED,this.onAdSkippableStateChange.bind(this)),this._adsManager.addEventListener(google.ima.AdEvent.Type.SKIPPED,this.onAdSkipped.bind(this)),this._adsManager.addEventListener(google.ima.AdEvent.Type.STARTED,this.onAdVideoStart.bind(this)),this._adsManager.addEventListener(google.ima.AdEvent.Type.THIRD_QUARTILE,this.onAdVideoThirdQuartile.bind(this)),this._adsManager.addEventListener(google.ima.AdEvent.Type.USER_CLOSE,this.onAdUserClose.bind(this)),this._adsManager.addEventListener(google.ima.AdEvent.Type.VOLUME_CHANGED,this.onAdVolumeChange.bind(this)),this._adsManager.addEventListener(google.ima.AdEvent.Type.VOLUME_MUTED,this.onAdVolumeChange.bind(this)),this.onAdsManagerLoaded()},i.prototype.onIMAAdsManagerAdError=function(t){this.onAdError(t.getError())},i.prototype.onIMAAdClickThru=function(t){this.onAdClickThru("",t.getAd().getAdId(),!1)},i.prototype.onIMAAdVideoComplete=function(){this.onAdVideoComplete()},i.prototype.onIMAAdLoaded=function(t){this._hasLoaded=!0,this._currentAd=t.getAd(),this.onAdLoaded(this._currentAd)},i.prototype.onIMAAdError=function(t){this.onAdError(t.getError())},i.prototype.onIMAAdLog=function(t){(t=t.getAdData()).hasOwnProperty("adError")?this.onAdLog(t.adError.getMessage()):this.onAdLog("IMA Ad Log")},i.prototype.onAdsManagerLoaded=function(){this._callEvent(this.EVENTS.AdsManagerLoaded)},i.prototype.onAdLoaded=function(t){this.EVENTS.AdLoaded in this._eventCallbacks&&this._eventCallbacks[this.EVENTS.AdLoaded](t)},i.prototype.onAdStarted=function(){this._hasStarted=!0,this._callEvent(this.EVENTS.AdStarted)},i.prototype.onAdDurationChange=function(){this._callEvent(this.EVENTS.AdDurationChange)},i.prototype.onAdLinearChange=function(){this._callEvent(this.EVENTS.AdLinearChange)},i.prototype.onAdSkippableStateChange=function(){this._callEvent(this.EVENTS.AdSkippableStateChange)},i.prototype.onAdSizeChange=function(){this._callEvent(this.EVENTS.AdSizeChange)},i.prototype.onAdVolumeChange=function(){this._callEvent(this.EVENTS.AdVolumeChange)},i.prototype.onAdVideoStart=function(){this._callEvent(this.EVENTS.AdVideoStart)},i.prototype.onAdImpression=function(){this._callEvent(this.EVENTS.AdImpression)},i.prototype.onAdVideoFirstQuartile=function(){this._callEvent(this.EVENTS.AdVideoFirstQuartile)},i.prototype.onAdVideoMidpoint=function(){this._callEvent(this.EVENTS.AdVideoMidpoint)},i.prototype.onAdVideoThirdQuartile=function(){this._callEvent(this.EVENTS.AdVideoThirdQuartile)},i.prototype.onAdVideoComplete=function(){this._callEvent(this.EVENTS.AdVideoComplete)},i.prototype.onAdPaused=function(){this._callEvent(this.EVENTS.AdPaused)},i.prototype.onAdPlaying=function(){this._callEvent(this.EVENTS.AdPlaying)},i.prototype.onAdSkipped=function(){this._callEvent(this.EVENTS.AdSkipped),this._abort()},i.prototype.onAdStopped=function(){this._hasStarted?(this._callEvent(this.EVENTS.AdStopped),this._abort()):this.onAdError("ad stopped before ad started")},i.prototype.onAdClickThru=function(t,e,i){this.EVENTS.AdClickThru in this._eventCallbacks&&this._eventCallbacks[this.EVENTS.AdClickThru](t,e,i)},i.prototype.onAdInteraction=function(){this._callEvent(this.EVENTS.AdInteraction)},i.prototype.onAdUserClose=function(){this._callEvent(this.EVENTS.AdUserClose)},i.prototype.onAllAdsCompleted=function(){this.abort(),this._callEvent(this.EVENTS.AllAdsCompleted)},i.prototype.onAdError=function(t){this.abort(),this.EVENTS.AdError in this._eventCallbacks&&this._eventCallbacks[this.EVENTS.AdError](t)},i.prototype.onAdLog=function(t){this.EVENTS.AdLog in this._eventCallbacks&&this._eventCallbacks[this.EVENTS.AdLog](t)},i.prototype._callEvent=function(t){t in this._eventCallbacks&&this._eventCallbacks[t]()},i.prototype.addEventListener=function(t,e,i){const n=e.bind(i);this._eventCallbacks[t]=n},i.prototype.removeEventListener=function(t){this._eventCallbacks[t]=null},i.prototype.getVersion=function(){return this._attributes.version};const n=i;var o=e.Z;export{o as default}; \ No newline at end of file diff --git a/dist/ima-wrapper.js b/dist/ima-wrapper.js index 1676f1e..5f8db47 100644 --- a/dist/ima-wrapper.js +++ b/dist/ima-wrapper.js @@ -1 +1 @@ -var adserve;(()=>{"use strict";var t={d:(e,i)=>{for(var n in i)t.o(i,n)&&!t.o(e,n)&&Object.defineProperty(e,n,{enumerable:!0,get:i[n]})},o:(t,e)=>Object.prototype.hasOwnProperty.call(t,e),r:t=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})}},e={};t.r(e),t.d(e,{tv:()=>n});const i=function(t,e,i){if(!(t&&(t instanceof HTMLElement||t.getRootNode)&&e&&(e instanceof HTMLElement||e.getRootNode)))throw new Error("ad container and/or video element not defined");this._adContainer=t,this._videoElement=e,this._callback=i,this.EVENTS={AdsManagerLoaded:"AdsManagerLoaded",AdStarted:"AdStarted",AdStopped:"AdStopped",AdSkipped:"AdSkipped",AdLoaded:"AdLoaded",AdLinearChange:"AdLinearChange",AdSizeChange:"AdSizeChange",AdExpandedChange:"AdExpandedChange",AdSkippableStateChange:"AdSkippableStateChange",AdDurationChange:"AdDurationChange",AdRemainingTimeChange:"AdRemainingTimeChange",AdVolumeChange:"AdVolumeChange",AdImpression:"AdImpression",AdClickThru:"AdClickThru",AdInteraction:"AdInteraction",AdVideoStart:"AdVideoStart",AdVideoFirstQuartile:"AdVideoFirstQuartile",AdVideoMidpoint:"AdVideoMidpoint",AdVideoThirdQuartile:"AdVideoThirdQuartile",AdVideoComplete:"AdVideoComplete",AdUserAcceptInvitation:"AdUserAcceptInvitation",AdUserMinimize:"AdUserMinimize",AdUserClose:"AdUserClose",AdPaused:"AdPaused",AdPlaying:"AdPlaying",AdError:"AdError",AdLog:"AdLog",AllAdsCompleted:"AllAdsCompleted"},this._eventCallbacks={},this._adsLoader=null,this._adDisplayContainer=null,this._adDisplayContainerInitialized=!1,this._adsManager=null,this._currentAd=null,this._adPodInfo=null,this._attributes={version:"1.0.6"},this._options={autoplay:!0,muted:!0,secure:!1,vastLoadTimeout:23e3,loadVideoTimeout:8e3},this.IMA_SDK_SRC="//imasdk.googleapis.com/js/sdkloader/ima3.js",window.google&&google.ima&&google.ima.AdsLoader?this.setupIMA():function(t,e=!1,i){const n=document.getElementsByTagName("head")[0]||document.documentElement,o=document.createElement("script");o.type="text/javascript",o.src=t,o.async=e,o.addEventListener("load",(function(){i&&"function"==typeof i&&i(!0,window)}),!1),o.addEventListener("error",(function(t){n.removeChild(o),i&&"function"==typeof i&&i(!1,t)}),!1),n.insertBefore(o,n.firstChild)}(this.IMA_SDK_SRC,!0,(t=>{t?this.setupIMA():this._callback&&"function"==typeof this._callback&&this._callback("ima3.js - failed to load")}))};i.prototype.setupIMA=function(){google.ima.settings.setDisableCustomPlaybackForIOS10Plus(!0),this._adDisplayContainer=new google.ima.AdDisplayContainer(this._adContainer,this._videoElement),this._adsLoader=new google.ima.AdsLoader(this._adDisplayContainer),this._adsLoader.addEventListener(google.ima.AdsManagerLoadedEvent.Type.ADS_MANAGER_LOADED,this.onIMAAdsManagerLoaded.bind(this),!1),this._adsLoader.addEventListener(google.ima.AdErrorEvent.Type.AD_ERROR,this.onIMAAdsManagerAdError.bind(this),!1),this._callback&&"function"==typeof this._callback&&this._callback()},i.prototype.doContentComplete=function(){this._adsLoader&&this._adsLoader.contentComplete()},i.prototype.maybeInitializeAdDisplayContainer=function(){this._adDisplayContainerInitialized||(this._adDisplayContainer.initialize(),this._adDisplayContainerInitialized=!0)},i.prototype.init=function(t,e,i){this.maybeInitializeAdDisplayContainer(),this._adsManager&&this._adsManager.init(t,e,i)},i.prototype.start=function(){this._adsManager&&this._adsManager.start()},i.prototype.getDuration=function(){return this._currentAd?this._currentAd.getDuration():-2},i.prototype.getRemainingTime=function(){return this._adsManager?this._adsManager.getRemainingTime():-2},i.prototype.resume=function(){this._adsManager&&this._adsManager.resume()},i.prototype.pause=function(){this._adsManager&&this._adsManager.pause()},i.prototype.stop=function(){this._adsManager&&this._adsManager.stop()},i.prototype.skip=function(){this._adsManager&&(this._adsManager.getAdSkippableState()&&this._adsManager.skip(),this.onAdSkipped())},i.prototype.resize=function(t,e,i){this._adsManager&&(this._adsManager.resize(t,e,i),this.onAdSizeChange())},i.prototype.getVolume=function(){return this._adsManager&&this._adsManager.getVolume()},i.prototype.setVolume=function(t){this._adsManager&&this._adsManager.setVolume(t)},i.prototype.getSkippableState=function(){return this._adsManager&&this._adsManager.getAdSkippableState()},i.prototype.collapse=function(){this._adsManager&&this._adsManager.collapse()},i.prototype.expand=function(){this._adsManager&&this._adsManager.expand()},i.prototype.requestAds=function(t,e){Object.assign(this._options,e);const i=new google.ima.AdsRequest;let n=!1;try{new URL(t),n=!0}catch(t){}n?i.adTagUrl=t:i.adsResponse=t,this._options.pageUrl&&(i.pageUrl=this._options.pageUrl),i.vastLoadTimeout=this._options.vastLoadTimeout,i.linearAdSlotWidth=this._videoElement.width,i.linearAdSlotHeight=this._videoElement.height,i.nonLinearAdSlotWidth=this._videoElement.width,i.nonLinearAdSlotHeight=this._videoElement.height,i.setAdWillAutoPlay(this._options.autoplay),this._options.muted&&i.setAdWillPlayMuted(this._options.muted),this._adsLoader.getSettings().setVpaidMode(this._options.secure?google.ima.ImaSdkSettings.VpaidMode.ENABLED:google.ima.ImaSdkSettings.VpaidMode.INSECURE),this._adsLoader.requestAds(i)},i.prototype.abort=function(){this._adsManager&&(this._adsManager.destroy(),this._adsManager=null),this.doContentComplete()},i.prototype._abort=function(){this.abort(),this.onAllAdsCompleted()},i.prototype.onIMAAdsManagerLoaded=function(t){const e=new google.ima.AdsRenderingSettings;e.restoreCustomPlaybackStateOnAdBreakComplete=!0,e.loadVideoTimeout=this._options.loadVideoTimeout,this._adsManager=t.getAdsManager(this._videoElement,e),this._adsManager.addEventListener(google.ima.AdErrorEvent.Type.AD_ERROR,this.onIMAAdError.bind(this)),this._adsManager.addEventListener(google.ima.AdEvent.Type.AD_BREAK_READY,(function(){})),this._adsManager.addEventListener(google.ima.AdEvent.Type.AD_METADATA,(function(){})),this._adsManager.addEventListener(google.ima.AdEvent.Type.ALL_ADS_COMPLETED,this.onAllAdsCompleted.bind(this)),this._adsManager.addEventListener(google.ima.AdEvent.Type.CLICK,this.onIMAAdClickThru.bind(this)),this._adsManager.addEventListener(google.ima.AdEvent.Type.COMPLETE,this.onIMAAdVideoComplete.bind(this)),this._adsManager.addEventListener(google.ima.AdEvent.Type.CONTENT_PAUSE_REQUESTED,this.onAdStarted.bind(this)),this._adsManager.addEventListener(google.ima.AdEvent.Type.CONTENT_RESUME_REQUESTED,this.onAdStopped.bind(this)),this._adsManager.addEventListener(google.ima.AdEvent.Type.DURATION_CHANGE,this.onAdDurationChange.bind(this)),this._adsManager.addEventListener(google.ima.AdEvent.Type.FIRST_QUARTILE,this.onAdVideoFirstQuartile.bind(this)),this._adsManager.addEventListener(google.ima.AdEvent.Type.IMPRESSION,this.onAdImpression.bind(this)),this._adsManager.addEventListener(google.ima.AdEvent.Type.INTERACTION,this.onAdInteraction.bind(this)),this._adsManager.addEventListener(google.ima.AdEvent.Type.LINEAR_CHANGED,this.onAdLinearChange.bind(this)),this._adsManager.addEventListener(google.ima.AdEvent.Type.LOADED,this.onIMAAdLoaded.bind(this)),this._adsManager.addEventListener(google.ima.AdEvent.Type.LOG,this.onIMAAdLog.bind(this)),this._adsManager.addEventListener(google.ima.AdEvent.Type.MIDPOINT,this.onAdVideoMidpoint.bind(this)),this._adsManager.addEventListener(google.ima.AdEvent.Type.PAUSED,this.onAdPaused.bind(this)),this._adsManager.addEventListener(google.ima.AdEvent.Type.RESUMED,this.onAdPlaying.bind(this)),this._adsManager.addEventListener(google.ima.AdEvent.Type.SKIPPABLE_STATE_CHANGED,this.onAdSkippableStateChange.bind(this)),this._adsManager.addEventListener(google.ima.AdEvent.Type.SKIPPED,this.onAdSkipped.bind(this)),this._adsManager.addEventListener(google.ima.AdEvent.Type.STARTED,this.onAdVideoStart.bind(this)),this._adsManager.addEventListener(google.ima.AdEvent.Type.THIRD_QUARTILE,this.onAdVideoThirdQuartile.bind(this)),this._adsManager.addEventListener(google.ima.AdEvent.Type.USER_CLOSE,this.onAdUserClose.bind(this)),this._adsManager.addEventListener(google.ima.AdEvent.Type.VOLUME_CHANGED,this.onAdVolumeChange.bind(this)),this._adsManager.addEventListener(google.ima.AdEvent.Type.VOLUME_MUTED,this.onAdVolumeChange.bind(this)),this.onAdsManagerLoaded()},i.prototype.onIMAAdsManagerAdError=function(t){this.onAdError(t.getError())},i.prototype.onIMAAdClickThru=function(t){this.onAdClickThru("",t.getAd().getAdId(),!1)},i.prototype.onIMAAdVideoComplete=function(){this.onAdVideoComplete()},i.prototype.onIMAAdLoaded=function(t){this._currentAd=t.getAd(),this.onAdLoaded(this._currentAd)},i.prototype.onIMAAdError=function(t){this.onAdError(t.getError())},i.prototype.onIMAAdLog=function(t){(t=t.getAdData()).hasOwnProperty("adError")?this.onAdLog(t.adError.getMessage()):this.onAdLog("IMA Ad Log")},i.prototype.onAdsManagerLoaded=function(){this._callEvent(this.EVENTS.AdsManagerLoaded)},i.prototype.onAdLoaded=function(t){this.EVENTS.AdLoaded in this._eventCallbacks&&this._eventCallbacks[this.EVENTS.AdLoaded](t)},i.prototype.onAdStarted=function(){this._callEvent(this.EVENTS.AdStarted)},i.prototype.onAdDurationChange=function(){this._callEvent(this.EVENTS.AdDurationChange)},i.prototype.onAdLinearChange=function(){this._callEvent(this.EVENTS.AdLinearChange)},i.prototype.onAdSkippableStateChange=function(){this._callEvent(this.EVENTS.AdSkippableStateChange)},i.prototype.onAdSizeChange=function(){this._callEvent(this.EVENTS.AdSizeChange)},i.prototype.onAdVolumeChange=function(){this._callEvent(this.EVENTS.AdVolumeChange)},i.prototype.onAdVideoStart=function(){this._callEvent(this.EVENTS.AdVideoStart)},i.prototype.onAdImpression=function(){this._callEvent(this.EVENTS.AdImpression)},i.prototype.onAdVideoFirstQuartile=function(){this._callEvent(this.EVENTS.AdVideoFirstQuartile)},i.prototype.onAdVideoMidpoint=function(){this._callEvent(this.EVENTS.AdVideoMidpoint)},i.prototype.onAdVideoThirdQuartile=function(){this._callEvent(this.EVENTS.AdVideoThirdQuartile)},i.prototype.onAdVideoComplete=function(){this._callEvent(this.EVENTS.AdVideoComplete)},i.prototype.onAdPaused=function(){this._callEvent(this.EVENTS.AdPaused)},i.prototype.onAdPlaying=function(){this._callEvent(this.EVENTS.AdPlaying)},i.prototype.onAdSkipped=function(){this._callEvent(this.EVENTS.AdSkipped),this._abort()},i.prototype.onAdStopped=function(){this._callEvent(this.EVENTS.AdStopped),this._abort()},i.prototype.onAdClickThru=function(t,e,i){this.EVENTS.AdClickThru in this._eventCallbacks&&this._eventCallbacks[this.EVENTS.AdClickThru](t,e,i)},i.prototype.onAdInteraction=function(){this._callEvent(this.EVENTS.AdInteraction)},i.prototype.onAdUserClose=function(){this._callEvent(this.EVENTS.AdUserClose)},i.prototype.onAllAdsCompleted=function(){this.abort(),this._callEvent(this.EVENTS.AllAdsCompleted)},i.prototype.onAdError=function(t){this.abort(),this.EVENTS.AdError in this._eventCallbacks&&this._eventCallbacks[this.EVENTS.AdError](t)},i.prototype.onAdLog=function(t){this.EVENTS.AdLog in this._eventCallbacks&&this._eventCallbacks[this.EVENTS.AdLog](t)},i.prototype._callEvent=function(t){t in this._eventCallbacks&&this._eventCallbacks[t]()},i.prototype.addEventListener=function(t,e,i){const n=e.bind(i);this._eventCallbacks[t]=n},i.prototype.removeEventListener=function(t){this._eventCallbacks[t]=null},i.prototype.getVersion=function(){return this._attributes.version};const n={IMAWrapper:i};adserve=e})(); \ No newline at end of file +var adserve;(()=>{"use strict";var t={d:(e,i)=>{for(var n in i)t.o(i,n)&&!t.o(e,n)&&Object.defineProperty(e,n,{enumerable:!0,get:i[n]})},o:(t,e)=>Object.prototype.hasOwnProperty.call(t,e),r:t=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})}},e={};t.r(e),t.d(e,{tv:()=>n});const i=function(t,e,i){if(!(t&&(t instanceof HTMLElement||t.getRootNode)&&e&&(e instanceof HTMLElement||e.getRootNode)))throw new Error("ad container and/or video element not defined");this._adContainer=t,this._videoElement=e,this._callback=i,this.EVENTS={AdsManagerLoaded:"AdsManagerLoaded",AdStarted:"AdStarted",AdStopped:"AdStopped",AdSkipped:"AdSkipped",AdLoaded:"AdLoaded",AdLinearChange:"AdLinearChange",AdSizeChange:"AdSizeChange",AdExpandedChange:"AdExpandedChange",AdSkippableStateChange:"AdSkippableStateChange",AdDurationChange:"AdDurationChange",AdRemainingTimeChange:"AdRemainingTimeChange",AdVolumeChange:"AdVolumeChange",AdImpression:"AdImpression",AdClickThru:"AdClickThru",AdInteraction:"AdInteraction",AdVideoStart:"AdVideoStart",AdVideoFirstQuartile:"AdVideoFirstQuartile",AdVideoMidpoint:"AdVideoMidpoint",AdVideoThirdQuartile:"AdVideoThirdQuartile",AdVideoComplete:"AdVideoComplete",AdUserAcceptInvitation:"AdUserAcceptInvitation",AdUserMinimize:"AdUserMinimize",AdUserClose:"AdUserClose",AdPaused:"AdPaused",AdPlaying:"AdPlaying",AdError:"AdError",AdLog:"AdLog",AllAdsCompleted:"AllAdsCompleted"},this._eventCallbacks={},this._adsLoader=null,this._adDisplayContainer=null,this._adDisplayContainerInitialized=!1,this._adsManager=null,this._currentAd=null,this._adPodInfo=null,this._attributes={version:"1.0.7"},this._options={autoplay:!0,muted:!0,secure:!1,vastLoadTimeout:23e3,loadVideoTimeout:8e3},this._hasLoaded=!1,this._hasStarted=!1,this.IMA_SDK_SRC="//imasdk.googleapis.com/js/sdkloader/ima3.js",window.google&&google.ima&&google.ima.AdsLoader?this.setupIMA():function(t,e=!1,i){const n=document.getElementsByTagName("head")[0]||document.documentElement,o=document.createElement("script");o.type="text/javascript",o.src=t,o.async=e,o.addEventListener("load",(function(){i&&"function"==typeof i&&i(!0,window)}),!1),o.addEventListener("error",(function(t){n.removeChild(o),i&&"function"==typeof i&&i(!1,t)}),!1),n.insertBefore(o,n.firstChild)}(this.IMA_SDK_SRC,!0,(t=>{t?this.setupIMA():this._callback&&"function"==typeof this._callback&&this._callback("ima3.js - failed to load")}))};i.prototype.setupIMA=function(){google.ima.settings.setDisableCustomPlaybackForIOS10Plus(!0),this._adDisplayContainer=new google.ima.AdDisplayContainer(this._adContainer,this._videoElement),this._adsLoader=new google.ima.AdsLoader(this._adDisplayContainer),this._adsLoader.addEventListener(google.ima.AdsManagerLoadedEvent.Type.ADS_MANAGER_LOADED,this.onIMAAdsManagerLoaded.bind(this),!1),this._adsLoader.addEventListener(google.ima.AdErrorEvent.Type.AD_ERROR,this.onIMAAdsManagerAdError.bind(this),!1),this._callback&&"function"==typeof this._callback&&this._callback()},i.prototype.doContentComplete=function(){this._adsLoader&&this._adsLoader.contentComplete()},i.prototype.maybeInitializeAdDisplayContainer=function(){this._adDisplayContainerInitialized||(this._adDisplayContainer.initialize(),this._adDisplayContainerInitialized=!0)},i.prototype.init=function(t,e,i){this.maybeInitializeAdDisplayContainer(),this._adsManager&&this._adsManager.init(t,e,i)},i.prototype.start=function(){this._adsManager&&this._adsManager.start()},i.prototype.getDuration=function(){return this._currentAd?this._currentAd.getDuration():-2},i.prototype.getRemainingTime=function(){return this._adsManager?this._adsManager.getRemainingTime():-2},i.prototype.resume=function(){this._adsManager&&this._adsManager.resume()},i.prototype.pause=function(){this._adsManager&&this._adsManager.pause()},i.prototype.stop=function(){this._adsManager&&this._adsManager.stop()},i.prototype.skip=function(){this._adsManager&&(this._adsManager.getAdSkippableState()&&this._adsManager.skip(),this.onAdSkipped())},i.prototype.resize=function(t,e,i){this._adsManager&&(this._adsManager.resize(t,e,i),this.onAdSizeChange())},i.prototype.getVolume=function(){return this._adsManager&&this._adsManager.getVolume()},i.prototype.setVolume=function(t){this._adsManager&&this._adsManager.setVolume(t)},i.prototype.getSkippableState=function(){return this._adsManager&&this._adsManager.getAdSkippableState()},i.prototype.collapse=function(){this._adsManager&&this._adsManager.collapse()},i.prototype.expand=function(){this._adsManager&&this._adsManager.expand()},i.prototype.requestAds=function(t,e){Object.assign(this._options,e);const i=new google.ima.AdsRequest;let n=!1;try{new URL(t),n=!0}catch(t){}n?i.adTagUrl=t:i.adsResponse=t,this._options.pageUrl&&(i.pageUrl=this._options.pageUrl),i.vastLoadTimeout=this._options.vastLoadTimeout,i.linearAdSlotWidth=this._videoElement.width,i.linearAdSlotHeight=this._videoElement.height,i.nonLinearAdSlotWidth=this._videoElement.width,i.nonLinearAdSlotHeight=this._videoElement.height,i.setAdWillAutoPlay(this._options.autoplay),this._options.muted&&i.setAdWillPlayMuted(this._options.muted),this._adsLoader.getSettings().setVpaidMode(this._options.secure?google.ima.ImaSdkSettings.VpaidMode.ENABLED:google.ima.ImaSdkSettings.VpaidMode.INSECURE),this._adsLoader.requestAds(i)},i.prototype.abort=function(){this._adsManager&&(this._adsManager.destroy(),this._adsManager=null),this.doContentComplete()},i.prototype._abort=function(){this.abort(),this.onAllAdsCompleted()},i.prototype.onIMAAdsManagerLoaded=function(t){const e=new google.ima.AdsRenderingSettings;e.restoreCustomPlaybackStateOnAdBreakComplete=!0,e.loadVideoTimeout=this._options.loadVideoTimeout,this._adsManager=t.getAdsManager(this._videoElement,e),this._adsManager.addEventListener(google.ima.AdErrorEvent.Type.AD_ERROR,this.onIMAAdError.bind(this)),this._adsManager.addEventListener(google.ima.AdEvent.Type.AD_BREAK_READY,(function(){})),this._adsManager.addEventListener(google.ima.AdEvent.Type.AD_METADATA,(function(){})),this._adsManager.addEventListener(google.ima.AdEvent.Type.ALL_ADS_COMPLETED,this.onAllAdsCompleted.bind(this)),this._adsManager.addEventListener(google.ima.AdEvent.Type.CLICK,this.onIMAAdClickThru.bind(this)),this._adsManager.addEventListener(google.ima.AdEvent.Type.COMPLETE,this.onIMAAdVideoComplete.bind(this)),this._adsManager.addEventListener(google.ima.AdEvent.Type.CONTENT_PAUSE_REQUESTED,this.onAdStarted.bind(this)),this._adsManager.addEventListener(google.ima.AdEvent.Type.CONTENT_RESUME_REQUESTED,this.onAdStopped.bind(this)),this._adsManager.addEventListener(google.ima.AdEvent.Type.DURATION_CHANGE,this.onAdDurationChange.bind(this)),this._adsManager.addEventListener(google.ima.AdEvent.Type.FIRST_QUARTILE,this.onAdVideoFirstQuartile.bind(this)),this._adsManager.addEventListener(google.ima.AdEvent.Type.IMPRESSION,this.onAdImpression.bind(this)),this._adsManager.addEventListener(google.ima.AdEvent.Type.INTERACTION,this.onAdInteraction.bind(this)),this._adsManager.addEventListener(google.ima.AdEvent.Type.LINEAR_CHANGED,this.onAdLinearChange.bind(this)),this._adsManager.addEventListener(google.ima.AdEvent.Type.LOADED,this.onIMAAdLoaded.bind(this)),this._adsManager.addEventListener(google.ima.AdEvent.Type.LOG,this.onIMAAdLog.bind(this)),this._adsManager.addEventListener(google.ima.AdEvent.Type.MIDPOINT,this.onAdVideoMidpoint.bind(this)),this._adsManager.addEventListener(google.ima.AdEvent.Type.PAUSED,this.onAdPaused.bind(this)),this._adsManager.addEventListener(google.ima.AdEvent.Type.RESUMED,this.onAdPlaying.bind(this)),this._adsManager.addEventListener(google.ima.AdEvent.Type.SKIPPABLE_STATE_CHANGED,this.onAdSkippableStateChange.bind(this)),this._adsManager.addEventListener(google.ima.AdEvent.Type.SKIPPED,this.onAdSkipped.bind(this)),this._adsManager.addEventListener(google.ima.AdEvent.Type.STARTED,this.onAdVideoStart.bind(this)),this._adsManager.addEventListener(google.ima.AdEvent.Type.THIRD_QUARTILE,this.onAdVideoThirdQuartile.bind(this)),this._adsManager.addEventListener(google.ima.AdEvent.Type.USER_CLOSE,this.onAdUserClose.bind(this)),this._adsManager.addEventListener(google.ima.AdEvent.Type.VOLUME_CHANGED,this.onAdVolumeChange.bind(this)),this._adsManager.addEventListener(google.ima.AdEvent.Type.VOLUME_MUTED,this.onAdVolumeChange.bind(this)),this.onAdsManagerLoaded()},i.prototype.onIMAAdsManagerAdError=function(t){this.onAdError(t.getError())},i.prototype.onIMAAdClickThru=function(t){this.onAdClickThru("",t.getAd().getAdId(),!1)},i.prototype.onIMAAdVideoComplete=function(){this.onAdVideoComplete()},i.prototype.onIMAAdLoaded=function(t){this._hasLoaded=!0,this._currentAd=t.getAd(),this.onAdLoaded(this._currentAd)},i.prototype.onIMAAdError=function(t){this.onAdError(t.getError())},i.prototype.onIMAAdLog=function(t){(t=t.getAdData()).hasOwnProperty("adError")?this.onAdLog(t.adError.getMessage()):this.onAdLog("IMA Ad Log")},i.prototype.onAdsManagerLoaded=function(){this._callEvent(this.EVENTS.AdsManagerLoaded)},i.prototype.onAdLoaded=function(t){this.EVENTS.AdLoaded in this._eventCallbacks&&this._eventCallbacks[this.EVENTS.AdLoaded](t)},i.prototype.onAdStarted=function(){this._hasStarted=!0,this._callEvent(this.EVENTS.AdStarted)},i.prototype.onAdDurationChange=function(){this._callEvent(this.EVENTS.AdDurationChange)},i.prototype.onAdLinearChange=function(){this._callEvent(this.EVENTS.AdLinearChange)},i.prototype.onAdSkippableStateChange=function(){this._callEvent(this.EVENTS.AdSkippableStateChange)},i.prototype.onAdSizeChange=function(){this._callEvent(this.EVENTS.AdSizeChange)},i.prototype.onAdVolumeChange=function(){this._callEvent(this.EVENTS.AdVolumeChange)},i.prototype.onAdVideoStart=function(){this._callEvent(this.EVENTS.AdVideoStart)},i.prototype.onAdImpression=function(){this._callEvent(this.EVENTS.AdImpression)},i.prototype.onAdVideoFirstQuartile=function(){this._callEvent(this.EVENTS.AdVideoFirstQuartile)},i.prototype.onAdVideoMidpoint=function(){this._callEvent(this.EVENTS.AdVideoMidpoint)},i.prototype.onAdVideoThirdQuartile=function(){this._callEvent(this.EVENTS.AdVideoThirdQuartile)},i.prototype.onAdVideoComplete=function(){this._callEvent(this.EVENTS.AdVideoComplete)},i.prototype.onAdPaused=function(){this._callEvent(this.EVENTS.AdPaused)},i.prototype.onAdPlaying=function(){this._callEvent(this.EVENTS.AdPlaying)},i.prototype.onAdSkipped=function(){this._callEvent(this.EVENTS.AdSkipped),this._abort()},i.prototype.onAdStopped=function(){this._hasStarted?(this._callEvent(this.EVENTS.AdStopped),this._abort()):this.onAdError("ad stopped before ad started")},i.prototype.onAdClickThru=function(t,e,i){this.EVENTS.AdClickThru in this._eventCallbacks&&this._eventCallbacks[this.EVENTS.AdClickThru](t,e,i)},i.prototype.onAdInteraction=function(){this._callEvent(this.EVENTS.AdInteraction)},i.prototype.onAdUserClose=function(){this._callEvent(this.EVENTS.AdUserClose)},i.prototype.onAllAdsCompleted=function(){this.abort(),this._callEvent(this.EVENTS.AllAdsCompleted)},i.prototype.onAdError=function(t){this.abort(),this.EVENTS.AdError in this._eventCallbacks&&this._eventCallbacks[this.EVENTS.AdError](t)},i.prototype.onAdLog=function(t){this.EVENTS.AdLog in this._eventCallbacks&&this._eventCallbacks[this.EVENTS.AdLog](t)},i.prototype._callEvent=function(t){t in this._eventCallbacks&&this._eventCallbacks[t]()},i.prototype.addEventListener=function(t,e,i){const n=e.bind(i);this._eventCallbacks[t]=n},i.prototype.removeEventListener=function(t){this._eventCallbacks[t]=null},i.prototype.getVersion=function(){return this._attributes.version};const n={IMAWrapper:i};adserve=e})(); \ No newline at end of file diff --git a/package.json b/package.json index a149176..03178f2 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ima-wrapper", - "version": "1.0.6", + "version": "1.0.7", "description": "Wrapper for IMA SDK. IMA Wrapper is a convenience layer around Google Interactive Media Ads (IMA SDK for HTML5) which tries to make using IMA less cumbersome for common monetization use cases.", "publishConfig": { "registry": "https://registry.npmjs.org" diff --git a/public/js/ima-wrapper.js b/public/js/ima-wrapper.js index f898c94..cea7600 100644 --- a/public/js/ima-wrapper.js +++ b/public/js/ima-wrapper.js @@ -1,6 +1,6 @@ /*! - * ima-wrapper v1.0.6 development - * Updated : 2023-01-27 + * ima-wrapper v1.0.7 development + * Updated : 2023-01-31 */ /* * ATTENTION: The "eval" devtool has been used (maybe by default in mode: "development"). @@ -21,7 +21,7 @@ var adserve; \****************************/ /***/ ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => { -eval("__webpack_require__.r(__webpack_exports__);\n/* harmony export */ __webpack_require__.d(__webpack_exports__, {\n/* harmony export */ \"default\": () => (__WEBPACK_DEFAULT_EXPORT__)\n/* harmony export */ });\n/* harmony import */ var _utils__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./utils */ \"./src/utils.js\");\n/*global google*/\n\n\nconst IMAWrapper = function(adContainer, videoElement, callback) {\n\n if(!(adContainer && (adContainer instanceof HTMLElement\n || adContainer.getRootNode))\n || !(videoElement && (videoElement instanceof HTMLElement\n || videoElement.getRootNode))) {\n throw new Error('ad container and/or video element not defined');\n }\n\n this._adContainer = adContainer;\n this._videoElement = videoElement;\n this._callback = callback;\n\n // Events\n this.EVENTS = {\n AdsManagerLoaded: 'AdsManagerLoaded', // After success ad request, when vast xml is parsed and ready\n AdStarted: 'AdStarted',\n AdStopped: 'AdStopped',\n AdSkipped: 'AdSkipped',\n AdLoaded: 'AdLoaded',\n AdLinearChange: 'AdLinearChange',\n AdSizeChange: 'AdSizeChange',\n AdExpandedChange: 'AdExpandedChange',\n AdSkippableStateChange: 'AdSkippableStateChange',\n AdDurationChange: 'AdDurationChange',\n AdRemainingTimeChange: 'AdRemainingTimeChange',\n AdVolumeChange: 'AdVolumeChange',\n AdImpression: 'AdImpression',\n AdClickThru: 'AdClickThru',\n AdInteraction: 'AdInteraction',\n AdVideoStart: 'AdVideoStart',\n AdVideoFirstQuartile: 'AdVideoFirstQuartile',\n AdVideoMidpoint: 'AdVideoMidpoint',\n AdVideoThirdQuartile: 'AdVideoThirdQuartile',\n AdVideoComplete: 'AdVideoComplete',\n AdUserAcceptInvitation: 'AdUserAcceptInvitation',\n AdUserMinimize: 'AdUserMinimize',\n AdUserClose: 'AdUserClose',\n AdPaused: 'AdPaused',\n AdPlaying: 'AdPlaying',\n AdError: 'AdError',\n AdLog: 'AdLog',\n AllAdsCompleted: 'AllAdsCompleted' // After all ads completed, vast, vpaid, vmap\n };\n // An object containing all registered events.\n // These events are all callbacks.\n this._eventCallbacks = {};\n\n // IMA global variables\n this._adsLoader = null;\n this._adDisplayContainer = null;\n this._adDisplayContainerInitialized = false;\n this._adsManager = null;\n this._currentAd = null;\n this._adPodInfo = null;\n\n // Attributes\n this._attributes = {\n version: '1.0.6'\n };\n\n // Options\n this._options = {\n autoplay: true,\n muted: true,\n secure: false, // default false, google.ima.ImaSdkSettings.VpaidMode.INSECURE\n vastLoadTimeout: 23000,\n loadVideoTimeout: 8000, // default value is 8000 ms = 8 sec, timeout to load video of the ad\n };\n\n // IMA SDK ima3.js\n this.IMA_SDK_SRC = '//imasdk.googleapis.com/js/sdkloader/ima3.js';\n // Check that Client Side IMA SDK has been included\n // NOTE: (window['google'] && google.ima) check for any\n // IMA SDK, including SDK for Server Side ads.\n // The 3rd check insures we have the right SDK:\n // {google.ima.AdsLoader} is an object that's part of Client Side IMA SDK\n // but not Server Side SDK.\n if (!window['google'] || !google.ima || !google.ima.AdsLoader) {\n console.log('ima3.js is not included');\n (0,_utils__WEBPACK_IMPORTED_MODULE_0__.loadScript)(this.IMA_SDK_SRC, true, (isIMALoaded) => {\n if(isIMALoaded) {\n console.log('ima3.js is loaded, setup IMA');\n this.setupIMA();\n } else {\n console.log('ima3.js is not loaded');\n if(this._callback\n && typeof this._callback === 'function') {\n this._callback('ima3.js - failed to load');\n }\n }\n });\n } else {\n console.log('ima3.js is included, use it');\n this.setupIMA();\n }\n\n};\nIMAWrapper.prototype.setupIMA = function() {\n console.log('setup IMA');\n google.ima.settings.setDisableCustomPlaybackForIOS10Plus(true);\n\n this._adDisplayContainer = new google.ima.AdDisplayContainer(this._adContainer, this._videoElement);\n this._adsLoader = new google.ima.AdsLoader(this._adDisplayContainer);\n\n // Listen and respond to ads loaded and error events.\n this._adsLoader.addEventListener(google.ima.AdsManagerLoadedEvent.Type.ADS_MANAGER_LOADED, this.onIMAAdsManagerLoaded.bind(this), false);\n this._adsLoader.addEventListener(google.ima.AdErrorEvent.Type.AD_ERROR, this.onIMAAdsManagerAdError.bind(this), false);\n\n if(this._callback\n && typeof this._callback === 'function') {\n this._callback();\n }\n};\nIMAWrapper.prototype.doContentComplete = function() {\n this._adsLoader && this._adsLoader.contentComplete();\n};\nIMAWrapper.prototype.maybeInitializeAdDisplayContainer = function() {\n this._adDisplayContainerInitialized || (this._adDisplayContainer.initialize(), this._adDisplayContainerInitialized = true);\n};\nIMAWrapper.prototype.init = function(width, height, viewMode) {\n console.log('ima init');\n this.maybeInitializeAdDisplayContainer();\n this._adsManager && this._adsManager.init(width, height, viewMode);\n};\nIMAWrapper.prototype.start = function() {\n console.log('ima start');\n this._adsManager && this._adsManager.start();\n};\nIMAWrapper.prototype.getDuration = function() {\n return this._currentAd ? this._currentAd.getDuration() : -2\n};\nIMAWrapper.prototype.getRemainingTime = function() {\n return this._adsManager ? this._adsManager.getRemainingTime() : -2\n};\nIMAWrapper.prototype.resume = function() {\n this._adsManager && this._adsManager.resume();\n};\nIMAWrapper.prototype.pause = function() {\n this._adsManager && this._adsManager.pause();\n};\nIMAWrapper.prototype.stop = function() {\n this._adsManager && this._adsManager.stop();\n};\nIMAWrapper.prototype.skip = function() {\n if(this._adsManager) {\n if (this._adsManager.getAdSkippableState()) {\n this._adsManager.skip();\n }\n // Force skip\n //this.onIMAAdSkipped();\n this.onAdSkipped();\n }\n};\nIMAWrapper.prototype.resize = function(width, height, viewMode) {\n this._adsManager && (this._adsManager.resize(width, height, viewMode), this.onAdSizeChange());\n};\nIMAWrapper.prototype.getVolume = function() {\n return this._adsManager && this._adsManager.getVolume()\n};\nIMAWrapper.prototype.setVolume = function(volume) {\n this._adsManager && this._adsManager.setVolume(volume);\n};\nIMAWrapper.prototype.getSkippableState = function() {\n return this._adsManager && this._adsManager.getAdSkippableState();\n};\nIMAWrapper.prototype.collapse = function() {\n this._adsManager && this._adsManager.collapse();\n};\nIMAWrapper.prototype.expand = function() {\n this._adsManager && this._adsManager.expand();\n};\nIMAWrapper.prototype.requestAds = function(vastUrl, options) {\n\n console.log('ima requestAds', vastUrl, options);\n\n // Assign options\n Object.assign(this._options, options);\n\n // Create adsRequest\n const adsRequest = new google.ima.AdsRequest();\n // Check if vastUrl is URL\n let isURL = false;\n try {\n new URL(vastUrl);\n isURL = true;\n } catch (e) {}\n\n // Use VAST URL or VAST XML\n isURL ? adsRequest.adTagUrl = vastUrl : adsRequest.adsResponse = vastUrl;\n\n // Get/Set options\n this._options.pageUrl && (adsRequest.pageUrl = this._options.pageUrl);\n\n adsRequest.vastLoadTimeout = this._options.vastLoadTimeout;\n\n adsRequest.linearAdSlotWidth = this._videoElement.width;\n adsRequest.linearAdSlotHeight = this._videoElement.height;\n adsRequest.nonLinearAdSlotWidth = this._videoElement.width;\n adsRequest.nonLinearAdSlotHeight = this._videoElement.height;\n\n adsRequest.setAdWillAutoPlay(this._options.autoplay);\n console.log('ima ad will play muted', this._options.muted);\n if(this._options.muted) {\n adsRequest.setAdWillPlayMuted(this._options.muted);\n }\n\n this._adsLoader.getSettings().setVpaidMode(this._options.secure ?\n google.ima.ImaSdkSettings.VpaidMode.ENABLED :\n google.ima.ImaSdkSettings.VpaidMode.INSECURE\n );\n // Request ads\n this._adsLoader.requestAds(adsRequest);\n};\nIMAWrapper.prototype.abort = function() {\n console.log('ima destroy adsManager');\n // Destroy\n this._adsManager && (this._adsManager.destroy(), this._adsManager = null);\n this.doContentComplete();\n};\nIMAWrapper.prototype._abort = function() {\n this.abort();\n // Dispatch AllAdsCompleted\n this.onAllAdsCompleted();\n}\n\n// IMA Events\nIMAWrapper.prototype.onIMAAdsManagerLoaded = function(adsManagerLoadedEvent) {\n\n console.log('ima adsManager loaded');\n\n const adsRenderingSettings = new google.ima.AdsRenderingSettings;\n adsRenderingSettings.restoreCustomPlaybackStateOnAdBreakComplete = true;\n adsRenderingSettings.loadVideoTimeout = this._options.loadVideoTimeout;\n\n this._adsManager = adsManagerLoadedEvent.getAdsManager(this._videoElement, adsRenderingSettings);\n\n this._adsManager.addEventListener(google.ima.AdErrorEvent.Type.AD_ERROR, this.onIMAAdError.bind(this));\n this._adsManager.addEventListener(google.ima.AdEvent.Type.AD_BREAK_READY, function() {});\n this._adsManager.addEventListener(google.ima.AdEvent.Type.AD_METADATA, function() {});\n this._adsManager.addEventListener(google.ima.AdEvent.Type.ALL_ADS_COMPLETED, this.onAllAdsCompleted.bind(this));\n this._adsManager.addEventListener(google.ima.AdEvent.Type.CLICK, this.onIMAAdClickThru.bind(this));\n this._adsManager.addEventListener(google.ima.AdEvent.Type.COMPLETE, this.onIMAAdVideoComplete.bind(this));\n this._adsManager.addEventListener(google.ima.AdEvent.Type.CONTENT_PAUSE_REQUESTED, this.onAdStarted.bind(this));\n this._adsManager.addEventListener(google.ima.AdEvent.Type.CONTENT_RESUME_REQUESTED, this.onAdStopped.bind(this));\n this._adsManager.addEventListener(google.ima.AdEvent.Type.DURATION_CHANGE, this.onAdDurationChange.bind(this));\n this._adsManager.addEventListener(google.ima.AdEvent.Type.FIRST_QUARTILE, this.onAdVideoFirstQuartile.bind(this));\n this._adsManager.addEventListener(google.ima.AdEvent.Type.IMPRESSION, this.onAdImpression.bind(this));\n this._adsManager.addEventListener(google.ima.AdEvent.Type.INTERACTION, this.onAdInteraction.bind(this));\n this._adsManager.addEventListener(google.ima.AdEvent.Type.LINEAR_CHANGED, this.onAdLinearChange.bind(this));\n this._adsManager.addEventListener(google.ima.AdEvent.Type.LOADED, this.onIMAAdLoaded.bind(this));\n this._adsManager.addEventListener(google.ima.AdEvent.Type.LOG, this.onIMAAdLog.bind(this));\n this._adsManager.addEventListener(google.ima.AdEvent.Type.MIDPOINT, this.onAdVideoMidpoint.bind(this));\n this._adsManager.addEventListener(google.ima.AdEvent.Type.PAUSED, this.onAdPaused.bind(this));\n this._adsManager.addEventListener(google.ima.AdEvent.Type.RESUMED, this.onAdPlaying.bind(this));\n this._adsManager.addEventListener(google.ima.AdEvent.Type.SKIPPABLE_STATE_CHANGED, this.onAdSkippableStateChange.bind(this));\n this._adsManager.addEventListener(google.ima.AdEvent.Type.SKIPPED, this.onAdSkipped.bind(this));\n this._adsManager.addEventListener(google.ima.AdEvent.Type.STARTED, this.onAdVideoStart.bind(this));\n this._adsManager.addEventListener(google.ima.AdEvent.Type.THIRD_QUARTILE, this.onAdVideoThirdQuartile.bind(this));\n this._adsManager.addEventListener(google.ima.AdEvent.Type.USER_CLOSE, this.onAdUserClose.bind(this));\n this._adsManager.addEventListener(google.ima.AdEvent.Type.VOLUME_CHANGED, this.onAdVolumeChange.bind(this));\n this._adsManager.addEventListener(google.ima.AdEvent.Type.VOLUME_MUTED, this.onAdVolumeChange.bind(this));\n\n this.onAdsManagerLoaded();\n};\nIMAWrapper.prototype.onIMAAdsManagerAdError = function(adsManagerAdErrorEvent) {\n this.onAdError(adsManagerAdErrorEvent.getError()); //.getError().getErrorCode() + ' ' + adsManagerAdErrorEvent.getError().getMessage());\n};\nIMAWrapper.prototype.onIMAAdClickThru = function(adEvent) {\n this.onAdClickThru('', adEvent.getAd().getAdId(), false);\n};\nIMAWrapper.prototype.onIMAAdVideoComplete = function() {\n this.onAdVideoComplete();\n};\nIMAWrapper.prototype.onIMAAdLoaded = function(adEvent) {\n this._currentAd = adEvent.getAd();\n this.onAdLoaded(this._currentAd);\n};\nIMAWrapper.prototype.onIMAAdError = function(adErrorEvent) {\n console.log('ima error', adErrorEvent);\n this.onAdError(adErrorEvent.getError()); //.getError().getErrorCode() + ' ' + adErrorEvent.getError().getMessage() + ' ' + adErrorEvent.getError().getInnerError())\n};\nIMAWrapper.prototype.onIMAAdLog = function(adEvent) {\n (adEvent = adEvent.getAdData()).hasOwnProperty('adError') ? this.onAdLog(adEvent.adError.getMessage()) : this.onAdLog('IMA Ad Log');\n};\n\n// Events\nIMAWrapper.prototype.onAdsManagerLoaded = function() {\n this._callEvent(this.EVENTS.AdsManagerLoaded);\n};\nIMAWrapper.prototype.onAdLoaded = function(currentAd) {\n if (this.EVENTS.AdLoaded in this._eventCallbacks) {\n this._eventCallbacks[this.EVENTS.AdLoaded](currentAd);\n }\n};\nIMAWrapper.prototype.onAdStarted = function() {\n this._callEvent(this.EVENTS.AdStarted);\n};\nIMAWrapper.prototype.onAdDurationChange = function() {\n this._callEvent(this.EVENTS.AdDurationChange);\n};\nIMAWrapper.prototype.onAdLinearChange = function() {\n this._callEvent(this.EVENTS.AdLinearChange);\n};\nIMAWrapper.prototype.onAdSkippableStateChange = function() {\n this._callEvent(this.EVENTS.AdSkippableStateChange);\n};\nIMAWrapper.prototype.onAdSizeChange = function() {\n this._callEvent(this.EVENTS.AdSizeChange);\n};\nIMAWrapper.prototype.onAdVolumeChange = function() {\n this._callEvent(this.EVENTS.AdVolumeChange);\n};\nIMAWrapper.prototype.onAdVideoStart = function() {\n this._callEvent(this.EVENTS.AdVideoStart);\n};\nIMAWrapper.prototype.onAdImpression = function() {\n this._callEvent(this.EVENTS.AdImpression);\n};\nIMAWrapper.prototype.onAdVideoFirstQuartile = function() {\n this._callEvent(this.EVENTS.AdVideoFirstQuartile);\n};\nIMAWrapper.prototype.onAdVideoMidpoint = function() {\n this._callEvent(this.EVENTS.AdVideoMidpoint);\n};\nIMAWrapper.prototype.onAdVideoThirdQuartile = function() {\n this._callEvent(this.EVENTS.AdVideoThirdQuartile);\n};\nIMAWrapper.prototype.onAdVideoComplete = function() {\n this._callEvent(this.EVENTS.AdVideoComplete);\n};\nIMAWrapper.prototype.onAdPaused = function() {\n this._callEvent(this.EVENTS.AdPaused);\n};\nIMAWrapper.prototype.onAdPlaying = function() {\n this._callEvent(this.EVENTS.AdPlaying);\n};\nIMAWrapper.prototype.onAdSkipped = function() {\n this._callEvent(this.EVENTS.AdSkipped);\n // abort the ad, unsubscribe and reset to a default state\n this._abort();\n};\nIMAWrapper.prototype.onAdStopped = function() {\n this._callEvent(this.EVENTS.AdStopped);\n // abort the ad, unsubscribe and reset to a default state\n this._abort();\n};\nIMAWrapper.prototype.onAdClickThru = function(url, id, playerHandles) {\n if (this.EVENTS.AdClickThru in this._eventCallbacks) {\n this._eventCallbacks[this.EVENTS.AdClickThru](url, id, playerHandles);\n }\n};\nIMAWrapper.prototype.onAdInteraction = function() {\n this._callEvent(this.EVENTS.AdInteraction);\n};\nIMAWrapper.prototype.onAdUserClose = function() {\n this._callEvent(this.EVENTS.AdUserClose);\n};\nIMAWrapper.prototype.onAllAdsCompleted = function() {\n this.abort();\n this._callEvent(this.EVENTS.AllAdsCompleted);\n};\nIMAWrapper.prototype.onAdError = function(message) {\n this.abort();\n if (this.EVENTS.AdError in this._eventCallbacks) {\n this._eventCallbacks[this.EVENTS.AdError](message);\n }\n};\nIMAWrapper.prototype.onAdLog = function(message) {\n if (this.EVENTS.AdLog in this._eventCallbacks) {\n this._eventCallbacks[this.EVENTS.AdLog](message);\n }\n};\nIMAWrapper.prototype._callEvent = function(eventName) {\n if(eventName in this._eventCallbacks) {\n this._eventCallbacks[eventName]();\n }\n};\nIMAWrapper.prototype.addEventListener = function(eventName, callback, context) {\n const givenCallback = callback.bind(context);\n this._eventCallbacks[eventName] = givenCallback;\n};\nIMAWrapper.prototype.removeEventListener = function(eventName) {\n this._eventCallbacks[eventName] = null;\n};\nIMAWrapper.prototype.getVersion = function() {\n return this._attributes.version;\n};\n\n/* harmony default export */ const __WEBPACK_DEFAULT_EXPORT__ = (IMAWrapper);\n\n\n//# sourceURL=webpack://adserve/./src/ima-wrapper.js?"); +eval("__webpack_require__.r(__webpack_exports__);\n/* harmony export */ __webpack_require__.d(__webpack_exports__, {\n/* harmony export */ \"default\": () => (__WEBPACK_DEFAULT_EXPORT__)\n/* harmony export */ });\n/* harmony import */ var _utils__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./utils */ \"./src/utils.js\");\n/*global google*/\n\n\nconst IMAWrapper = function(adContainer, videoElement, callback) {\n\n if(!(adContainer && (adContainer instanceof HTMLElement\n || adContainer.getRootNode))\n || !(videoElement && (videoElement instanceof HTMLElement\n || videoElement.getRootNode))) {\n throw new Error('ad container and/or video element not defined');\n }\n\n this._adContainer = adContainer;\n this._videoElement = videoElement;\n this._callback = callback;\n\n // Events\n this.EVENTS = {\n AdsManagerLoaded: 'AdsManagerLoaded', // After success ad request, when vast xml is parsed and ready\n AdStarted: 'AdStarted',\n AdStopped: 'AdStopped',\n AdSkipped: 'AdSkipped',\n AdLoaded: 'AdLoaded',\n AdLinearChange: 'AdLinearChange',\n AdSizeChange: 'AdSizeChange',\n AdExpandedChange: 'AdExpandedChange',\n AdSkippableStateChange: 'AdSkippableStateChange',\n AdDurationChange: 'AdDurationChange',\n AdRemainingTimeChange: 'AdRemainingTimeChange',\n AdVolumeChange: 'AdVolumeChange',\n AdImpression: 'AdImpression',\n AdClickThru: 'AdClickThru',\n AdInteraction: 'AdInteraction',\n AdVideoStart: 'AdVideoStart',\n AdVideoFirstQuartile: 'AdVideoFirstQuartile',\n AdVideoMidpoint: 'AdVideoMidpoint',\n AdVideoThirdQuartile: 'AdVideoThirdQuartile',\n AdVideoComplete: 'AdVideoComplete',\n AdUserAcceptInvitation: 'AdUserAcceptInvitation',\n AdUserMinimize: 'AdUserMinimize',\n AdUserClose: 'AdUserClose',\n AdPaused: 'AdPaused',\n AdPlaying: 'AdPlaying',\n AdError: 'AdError',\n AdLog: 'AdLog',\n AllAdsCompleted: 'AllAdsCompleted' // After all ads completed, vast, vpaid, vmap\n };\n // An object containing all registered events.\n // These events are all callbacks.\n this._eventCallbacks = {};\n\n // IMA global variables\n this._adsLoader = null;\n this._adDisplayContainer = null;\n this._adDisplayContainerInitialized = false;\n this._adsManager = null;\n this._currentAd = null;\n this._adPodInfo = null;\n\n // Attributes\n this._attributes = {\n version: '1.0.7'\n };\n\n // Options\n this._options = {\n autoplay: true,\n muted: true,\n secure: false, // default false, google.ima.ImaSdkSettings.VpaidMode.INSECURE\n vastLoadTimeout: 23000,\n loadVideoTimeout: 8000, // default value is 8000 ms = 8 sec, timeout to load video of the ad\n };\n\n this._hasLoaded = false;\n this._hasStarted = false;\n\n // IMA SDK ima3.js\n this.IMA_SDK_SRC = '//imasdk.googleapis.com/js/sdkloader/ima3.js';\n // Check that Client Side IMA SDK has been included\n // NOTE: (window['google'] && google.ima) check for any\n // IMA SDK, including SDK for Server Side ads.\n // The 3rd check insures we have the right SDK:\n // {google.ima.AdsLoader} is an object that's part of Client Side IMA SDK\n // but not Server Side SDK.\n if (!window['google'] || !google.ima || !google.ima.AdsLoader) {\n console.log('ima3.js is not included');\n (0,_utils__WEBPACK_IMPORTED_MODULE_0__.loadScript)(this.IMA_SDK_SRC, true, (isIMALoaded) => {\n if(isIMALoaded) {\n console.log('ima3.js is loaded, setup IMA');\n this.setupIMA();\n } else {\n console.log('ima3.js is not loaded');\n if(this._callback\n && typeof this._callback === 'function') {\n this._callback('ima3.js - failed to load');\n }\n }\n });\n } else {\n console.log('ima3.js is included, use it');\n this.setupIMA();\n }\n\n};\nIMAWrapper.prototype.setupIMA = function() {\n console.log('setup IMA');\n google.ima.settings.setDisableCustomPlaybackForIOS10Plus(true);\n\n this._adDisplayContainer = new google.ima.AdDisplayContainer(this._adContainer, this._videoElement);\n this._adsLoader = new google.ima.AdsLoader(this._adDisplayContainer);\n\n // Listen and respond to ads loaded and error events.\n this._adsLoader.addEventListener(google.ima.AdsManagerLoadedEvent.Type.ADS_MANAGER_LOADED, this.onIMAAdsManagerLoaded.bind(this), false);\n this._adsLoader.addEventListener(google.ima.AdErrorEvent.Type.AD_ERROR, this.onIMAAdsManagerAdError.bind(this), false);\n\n if(this._callback\n && typeof this._callback === 'function') {\n this._callback();\n }\n};\nIMAWrapper.prototype.doContentComplete = function() {\n this._adsLoader && this._adsLoader.contentComplete();\n};\nIMAWrapper.prototype.maybeInitializeAdDisplayContainer = function() {\n this._adDisplayContainerInitialized || (this._adDisplayContainer.initialize(), this._adDisplayContainerInitialized = true);\n};\nIMAWrapper.prototype.init = function(width, height, viewMode) {\n console.log('ima init');\n this.maybeInitializeAdDisplayContainer();\n this._adsManager && this._adsManager.init(width, height, viewMode);\n};\nIMAWrapper.prototype.start = function() {\n console.log('ima start');\n this._adsManager && this._adsManager.start();\n};\nIMAWrapper.prototype.getDuration = function() {\n return this._currentAd ? this._currentAd.getDuration() : -2\n};\nIMAWrapper.prototype.getRemainingTime = function() {\n return this._adsManager ? this._adsManager.getRemainingTime() : -2\n};\nIMAWrapper.prototype.resume = function() {\n this._adsManager && this._adsManager.resume();\n};\nIMAWrapper.prototype.pause = function() {\n this._adsManager && this._adsManager.pause();\n};\nIMAWrapper.prototype.stop = function() {\n this._adsManager && this._adsManager.stop();\n};\nIMAWrapper.prototype.skip = function() {\n if(this._adsManager) {\n if (this._adsManager.getAdSkippableState()) {\n this._adsManager.skip();\n }\n // Force skip\n //this.onIMAAdSkipped();\n this.onAdSkipped();\n }\n};\nIMAWrapper.prototype.resize = function(width, height, viewMode) {\n this._adsManager && (this._adsManager.resize(width, height, viewMode), this.onAdSizeChange());\n};\nIMAWrapper.prototype.getVolume = function() {\n return this._adsManager && this._adsManager.getVolume()\n};\nIMAWrapper.prototype.setVolume = function(volume) {\n this._adsManager && this._adsManager.setVolume(volume);\n};\nIMAWrapper.prototype.getSkippableState = function() {\n return this._adsManager && this._adsManager.getAdSkippableState();\n};\nIMAWrapper.prototype.collapse = function() {\n this._adsManager && this._adsManager.collapse();\n};\nIMAWrapper.prototype.expand = function() {\n this._adsManager && this._adsManager.expand();\n};\nIMAWrapper.prototype.requestAds = function(vastUrl, options) {\n\n console.log('ima requestAds', vastUrl, options);\n\n // Assign options\n Object.assign(this._options, options);\n\n // Create adsRequest\n const adsRequest = new google.ima.AdsRequest();\n // Check if vastUrl is URL\n let isURL = false;\n try {\n new URL(vastUrl);\n isURL = true;\n } catch (e) {}\n\n // Use VAST URL or VAST XML\n isURL ? adsRequest.adTagUrl = vastUrl : adsRequest.adsResponse = vastUrl;\n\n // Get/Set options\n this._options.pageUrl && (adsRequest.pageUrl = this._options.pageUrl);\n\n adsRequest.vastLoadTimeout = this._options.vastLoadTimeout;\n\n adsRequest.linearAdSlotWidth = this._videoElement.width;\n adsRequest.linearAdSlotHeight = this._videoElement.height;\n adsRequest.nonLinearAdSlotWidth = this._videoElement.width;\n adsRequest.nonLinearAdSlotHeight = this._videoElement.height;\n\n adsRequest.setAdWillAutoPlay(this._options.autoplay);\n console.log('ima ad will play muted', this._options.muted);\n if(this._options.muted) {\n adsRequest.setAdWillPlayMuted(this._options.muted);\n }\n\n this._adsLoader.getSettings().setVpaidMode(this._options.secure ?\n google.ima.ImaSdkSettings.VpaidMode.ENABLED :\n google.ima.ImaSdkSettings.VpaidMode.INSECURE\n );\n // Request ads\n this._adsLoader.requestAds(adsRequest);\n};\nIMAWrapper.prototype.abort = function() {\n console.log('ima destroy adsManager');\n // Destroy\n this._adsManager && (this._adsManager.destroy(), this._adsManager = null);\n this.doContentComplete();\n};\nIMAWrapper.prototype._abort = function() {\n this.abort();\n // Dispatch AllAdsCompleted\n this.onAllAdsCompleted();\n}\n\n// IMA Events\nIMAWrapper.prototype.onIMAAdsManagerLoaded = function(adsManagerLoadedEvent) {\n\n console.log('ima adsManager loaded');\n\n const adsRenderingSettings = new google.ima.AdsRenderingSettings;\n adsRenderingSettings.restoreCustomPlaybackStateOnAdBreakComplete = true;\n adsRenderingSettings.loadVideoTimeout = this._options.loadVideoTimeout;\n\n this._adsManager = adsManagerLoadedEvent.getAdsManager(this._videoElement, adsRenderingSettings);\n\n this._adsManager.addEventListener(google.ima.AdErrorEvent.Type.AD_ERROR, this.onIMAAdError.bind(this));\n this._adsManager.addEventListener(google.ima.AdEvent.Type.AD_BREAK_READY, function() {});\n this._adsManager.addEventListener(google.ima.AdEvent.Type.AD_METADATA, function() {});\n this._adsManager.addEventListener(google.ima.AdEvent.Type.ALL_ADS_COMPLETED, this.onAllAdsCompleted.bind(this));\n this._adsManager.addEventListener(google.ima.AdEvent.Type.CLICK, this.onIMAAdClickThru.bind(this));\n this._adsManager.addEventListener(google.ima.AdEvent.Type.COMPLETE, this.onIMAAdVideoComplete.bind(this));\n this._adsManager.addEventListener(google.ima.AdEvent.Type.CONTENT_PAUSE_REQUESTED, this.onAdStarted.bind(this));\n this._adsManager.addEventListener(google.ima.AdEvent.Type.CONTENT_RESUME_REQUESTED, this.onAdStopped.bind(this));\n this._adsManager.addEventListener(google.ima.AdEvent.Type.DURATION_CHANGE, this.onAdDurationChange.bind(this));\n this._adsManager.addEventListener(google.ima.AdEvent.Type.FIRST_QUARTILE, this.onAdVideoFirstQuartile.bind(this));\n this._adsManager.addEventListener(google.ima.AdEvent.Type.IMPRESSION, this.onAdImpression.bind(this));\n this._adsManager.addEventListener(google.ima.AdEvent.Type.INTERACTION, this.onAdInteraction.bind(this));\n this._adsManager.addEventListener(google.ima.AdEvent.Type.LINEAR_CHANGED, this.onAdLinearChange.bind(this));\n this._adsManager.addEventListener(google.ima.AdEvent.Type.LOADED, this.onIMAAdLoaded.bind(this));\n this._adsManager.addEventListener(google.ima.AdEvent.Type.LOG, this.onIMAAdLog.bind(this));\n this._adsManager.addEventListener(google.ima.AdEvent.Type.MIDPOINT, this.onAdVideoMidpoint.bind(this));\n this._adsManager.addEventListener(google.ima.AdEvent.Type.PAUSED, this.onAdPaused.bind(this));\n this._adsManager.addEventListener(google.ima.AdEvent.Type.RESUMED, this.onAdPlaying.bind(this));\n this._adsManager.addEventListener(google.ima.AdEvent.Type.SKIPPABLE_STATE_CHANGED, this.onAdSkippableStateChange.bind(this));\n this._adsManager.addEventListener(google.ima.AdEvent.Type.SKIPPED, this.onAdSkipped.bind(this));\n this._adsManager.addEventListener(google.ima.AdEvent.Type.STARTED, this.onAdVideoStart.bind(this));\n this._adsManager.addEventListener(google.ima.AdEvent.Type.THIRD_QUARTILE, this.onAdVideoThirdQuartile.bind(this));\n this._adsManager.addEventListener(google.ima.AdEvent.Type.USER_CLOSE, this.onAdUserClose.bind(this));\n this._adsManager.addEventListener(google.ima.AdEvent.Type.VOLUME_CHANGED, this.onAdVolumeChange.bind(this));\n this._adsManager.addEventListener(google.ima.AdEvent.Type.VOLUME_MUTED, this.onAdVolumeChange.bind(this));\n\n this.onAdsManagerLoaded();\n};\nIMAWrapper.prototype.onIMAAdsManagerAdError = function(adsManagerAdErrorEvent) {\n this.onAdError(adsManagerAdErrorEvent.getError()); //.getError().getErrorCode() + ' ' + adsManagerAdErrorEvent.getError().getMessage());\n};\nIMAWrapper.prototype.onIMAAdClickThru = function(adEvent) {\n this.onAdClickThru('', adEvent.getAd().getAdId(), false);\n};\nIMAWrapper.prototype.onIMAAdVideoComplete = function() {\n this.onAdVideoComplete();\n};\nIMAWrapper.prototype.onIMAAdLoaded = function(adEvent) {\n this._hasLoaded = true;\n this._currentAd = adEvent.getAd();\n this.onAdLoaded(this._currentAd);\n};\nIMAWrapper.prototype.onIMAAdError = function(adErrorEvent) {\n console.log('ima error', adErrorEvent);\n this.onAdError(adErrorEvent.getError()); //.getError().getErrorCode() + ' ' + adErrorEvent.getError().getMessage() + ' ' + adErrorEvent.getError().getInnerError())\n};\nIMAWrapper.prototype.onIMAAdLog = function(adEvent) {\n (adEvent = adEvent.getAdData()).hasOwnProperty('adError') ? this.onAdLog(adEvent.adError.getMessage()) : this.onAdLog('IMA Ad Log');\n};\n\n// Events\nIMAWrapper.prototype.onAdsManagerLoaded = function() {\n this._callEvent(this.EVENTS.AdsManagerLoaded);\n};\nIMAWrapper.prototype.onAdLoaded = function(currentAd) {\n if (this.EVENTS.AdLoaded in this._eventCallbacks) {\n this._eventCallbacks[this.EVENTS.AdLoaded](currentAd);\n }\n};\nIMAWrapper.prototype.onAdStarted = function() {\n this._hasStarted = true;\n this._callEvent(this.EVENTS.AdStarted);\n};\nIMAWrapper.prototype.onAdDurationChange = function() {\n this._callEvent(this.EVENTS.AdDurationChange);\n};\nIMAWrapper.prototype.onAdLinearChange = function() {\n this._callEvent(this.EVENTS.AdLinearChange);\n};\nIMAWrapper.prototype.onAdSkippableStateChange = function() {\n this._callEvent(this.EVENTS.AdSkippableStateChange);\n};\nIMAWrapper.prototype.onAdSizeChange = function() {\n this._callEvent(this.EVENTS.AdSizeChange);\n};\nIMAWrapper.prototype.onAdVolumeChange = function() {\n this._callEvent(this.EVENTS.AdVolumeChange);\n};\nIMAWrapper.prototype.onAdVideoStart = function() {\n this._callEvent(this.EVENTS.AdVideoStart);\n};\nIMAWrapper.prototype.onAdImpression = function() {\n this._callEvent(this.EVENTS.AdImpression);\n};\nIMAWrapper.prototype.onAdVideoFirstQuartile = function() {\n this._callEvent(this.EVENTS.AdVideoFirstQuartile);\n};\nIMAWrapper.prototype.onAdVideoMidpoint = function() {\n this._callEvent(this.EVENTS.AdVideoMidpoint);\n};\nIMAWrapper.prototype.onAdVideoThirdQuartile = function() {\n this._callEvent(this.EVENTS.AdVideoThirdQuartile);\n};\nIMAWrapper.prototype.onAdVideoComplete = function() {\n this._callEvent(this.EVENTS.AdVideoComplete);\n};\nIMAWrapper.prototype.onAdPaused = function() {\n this._callEvent(this.EVENTS.AdPaused);\n};\nIMAWrapper.prototype.onAdPlaying = function() {\n this._callEvent(this.EVENTS.AdPlaying);\n};\nIMAWrapper.prototype.onAdSkipped = function() {\n this._callEvent(this.EVENTS.AdSkipped);\n // abort the ad, unsubscribe and reset to a default state\n this._abort();\n};\nIMAWrapper.prototype.onAdStopped = function() {\n if(!this._hasStarted) {\n this.onAdError('ad stopped before ad started');\n } else {\n this._callEvent(this.EVENTS.AdStopped);\n // abort the ad, unsubscribe and reset to a default state\n this._abort();\n }\n};\nIMAWrapper.prototype.onAdClickThru = function(url, id, playerHandles) {\n if (this.EVENTS.AdClickThru in this._eventCallbacks) {\n this._eventCallbacks[this.EVENTS.AdClickThru](url, id, playerHandles);\n }\n};\nIMAWrapper.prototype.onAdInteraction = function() {\n this._callEvent(this.EVENTS.AdInteraction);\n};\nIMAWrapper.prototype.onAdUserClose = function() {\n this._callEvent(this.EVENTS.AdUserClose);\n};\nIMAWrapper.prototype.onAllAdsCompleted = function() {\n this.abort();\n this._callEvent(this.EVENTS.AllAdsCompleted);\n};\nIMAWrapper.prototype.onAdError = function(message) {\n this.abort();\n if (this.EVENTS.AdError in this._eventCallbacks) {\n this._eventCallbacks[this.EVENTS.AdError](message);\n }\n};\nIMAWrapper.prototype.onAdLog = function(message) {\n if (this.EVENTS.AdLog in this._eventCallbacks) {\n this._eventCallbacks[this.EVENTS.AdLog](message);\n }\n};\nIMAWrapper.prototype._callEvent = function(eventName) {\n if(eventName in this._eventCallbacks) {\n this._eventCallbacks[eventName]();\n }\n};\nIMAWrapper.prototype.addEventListener = function(eventName, callback, context) {\n const givenCallback = callback.bind(context);\n this._eventCallbacks[eventName] = givenCallback;\n};\nIMAWrapper.prototype.removeEventListener = function(eventName) {\n this._eventCallbacks[eventName] = null;\n};\nIMAWrapper.prototype.getVersion = function() {\n return this._attributes.version;\n};\n\n/* harmony default export */ const __WEBPACK_DEFAULT_EXPORT__ = (IMAWrapper);\n\n\n//# sourceURL=webpack://adserve/./src/ima-wrapper.js?"); /***/ }), diff --git a/src/ima-wrapper.js b/src/ima-wrapper.js index 9e60d48..b5f96e7 100644 --- a/src/ima-wrapper.js +++ b/src/ima-wrapper.js @@ -71,6 +71,9 @@ const IMAWrapper = function(adContainer, videoElement, callback) { loadVideoTimeout: 8000, // default value is 8000 ms = 8 sec, timeout to load video of the ad }; + this._hasLoaded = false; + this._hasStarted = false; + // IMA SDK ima3.js this.IMA_SDK_SRC = '//imasdk.googleapis.com/js/sdkloader/ima3.js'; // Check that Client Side IMA SDK has been included @@ -276,6 +279,7 @@ IMAWrapper.prototype.onIMAAdVideoComplete = function() { this.onAdVideoComplete(); }; IMAWrapper.prototype.onIMAAdLoaded = function(adEvent) { + this._hasLoaded = true; this._currentAd = adEvent.getAd(); this.onAdLoaded(this._currentAd); }; @@ -297,6 +301,7 @@ IMAWrapper.prototype.onAdLoaded = function(currentAd) { } }; IMAWrapper.prototype.onAdStarted = function() { + this._hasStarted = true; this._callEvent(this.EVENTS.AdStarted); }; IMAWrapper.prototype.onAdDurationChange = function() { @@ -344,9 +349,13 @@ IMAWrapper.prototype.onAdSkipped = function() { this._abort(); }; IMAWrapper.prototype.onAdStopped = function() { - this._callEvent(this.EVENTS.AdStopped); - // abort the ad, unsubscribe and reset to a default state - this._abort(); + if(!this._hasStarted) { + this.onAdError('ad stopped before ad started'); + } else { + this._callEvent(this.EVENTS.AdStopped); + // abort the ad, unsubscribe and reset to a default state + this._abort(); + } }; IMAWrapper.prototype.onAdClickThru = function(url, id, playerHandles) { if (this.EVENTS.AdClickThru in this._eventCallbacks) {