forked from dailymotion/vast-client-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vast-client.min.js
1 lines (1 loc) · 36.5 KB
/
vast-client.min.js
1
var VAST=function(e){"use strict";var t=function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")},i=function(){function e(e,t){for(var i=0;i<t.length;i++){var r=t[i];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}return function(t,i,r){return i&&e(t.prototype,i),r&&e(t,r),t}}(),r=function(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Super expression must either be null or a function, not "+typeof t);e.prototype=Object.create(t&&t.prototype,{constructor:{value:e,enumerable:!1,writable:!0,configurable:!0}}),t&&(Object.setPrototypeOf?Object.setPrototypeOf(e,t):e.__proto__=t)},n=function(e,t){if(!e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return!t||"object"!=typeof t&&"function"!=typeof t?e:t},a=function e(){t(this,e),this.id=null,this.sequence=null,this.system=null,this.title=null,this.description=null,this.advertiser=null,this.pricing=null,this.survey=null,this.errorURLTemplates=[],this.impressionURLTemplates=[],this.creatives=[],this.extensions=[]},s=function e(){t(this,e),this.attributes={},this.children=[]},o=function e(){t(this,e),this.name=null,this.value=null,this.attributes={}},l=function e(){t(this,e),this.id=null,this.width=0,this.height=0,this.type=null,this.staticResource=null,this.htmlResource=null,this.iframeResource=null,this.altText=null,this.companionClickThroughURLTemplate=null,this.companionClickTrackingURLTemplates=[],this.trackingEvents={}},c=function e(){var i=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};t(this,e),this.id=i.id||null,this.adId=i.adId||null,this.sequence=i.sequence||null,this.apiFramework=i.apiFramework||null,this.trackingEvents={}},u=function(e){function i(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};t(this,i);var r=n(this,(i.__proto__||Object.getPrototypeOf(i)).call(this,e));return r.type="companion",r.variations=[],r}return r(i,c),i}();function h(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},i=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{},r=[];for(var n in t.ASSETURI&&(t.ASSETURI=p(t.ASSETURI)),t.CONTENTPLAYHEAD&&(t.CONTENTPLAYHEAD=p(t.CONTENTPLAYHEAD)),!t.ERRORCODE||i.isCustomCode||/^[0-9]{3}$/.test(t.ERRORCODE)||(t.ERRORCODE=900),t.CACHEBUSTING=d(Math.round(1e8*Math.random()).toString()),t.TIMESTAMP=p((new Date).toISOString()),t.RANDOM=t.random=t.CACHEBUSTING,e){var a=e[n];if("string"==typeof a){for(var s in t){var o=t[s],l="["+s+"]",c="%%"+s+"%%";a=(a=a.replace(l,o)).replace(c,o)}r.push(a)}}return r}function p(e){return encodeURIComponent(e).replace(/[!'()*]/g,function(e){return"%"+e.charCodeAt(0).toString(16)})}function d(e){return e.length<8?f(0,8-e.length,!1).map(function(){return"0"}).join("")+e:e}function f(e,t,i){for(var r=[],n=e<t,a=i?n?t+1:t-1:t,s=e;n?s<a:s>a;n?s++:s--)r.push(s);return r}var v={track:function(e,t,i){h(e,t,i).forEach(function(e){"undefined"!=typeof window&&null!==window&&((new Image).src=e)})},resolveURLTemplates:h,encodeURIComponentRFC3986:p,leftpad:d,range:f,isNumeric:function(e){return!isNaN(parseFloat(e))&&isFinite(e)},flatten:function e(t){return t.reduce(function(t,i){return t.concat(Array.isArray(i)?e(i):i)},[])}};var m={childByName:function(e,t){var i=e.childNodes;for(var r in i){var n=i[r];if(n.nodeName===t)return n}},childrenByName:function(e,t){var i=[],r=e.childNodes;for(var n in r){var a=r[n];a.nodeName===t&&i.push(a)}return i},resolveVastAdTagURI:function(e,t){return t?0===e.indexOf("//")?""+location.protocol+e:-1===e.indexOf("://")?t.slice(0,t.lastIndexOf("/"))+"/"+e:e:e},parseBoolean:function(e){return-1!==["true","TRUE","1"].indexOf(e)},parseNodeText:function(e){return e&&(e.textContent||e.text||"").trim()},copyNodeAttribute:function(e,t,i){var r=t.getAttribute(e);r&&i.setAttribute(e,r)},parseDuration:function(e){if(null===e||void 0===e)return-1;if(v.isNumeric(e))return parseInt(e);var t=e.split(":");if(3!==t.length)return-1;var i=t[2].split("."),r=parseInt(i[0]);2===i.length&&(r+=parseFloat("0."+i[1]));var n=parseInt(60*t[1]),a=parseInt(60*t[0]*60);return isNaN(a)||isNaN(n)||isNaN(r)||n>3600||r>60?-1:a+n+r},splitVAST:function(e){var t=[],i=null;return e.forEach(function(r,n){if(r.sequence&&(r.sequence=parseInt(r.sequence,10)),r.sequence>1){var a=e[n-1];if(a&&a.sequence===r.sequence-1)return void(i&&i.push(r));delete r.sequence}i=[r],t.push(i)}),t},mergeWrapperAdData:function(e,t){e.errorURLTemplates=t.errorURLTemplates.concat(e.errorURLTemplates),e.impressionURLTemplates=t.impressionURLTemplates.concat(e.impressionURLTemplates),e.extensions=t.extensions.concat(e.extensions),e.creatives.forEach(function(e){if(t.trackingEvents&&t.trackingEvents[e.type])for(var i in t.trackingEvents[e.type]){var r=t.trackingEvents[e.type][i];Array.isArray(e.trackingEvents[i])||(e.trackingEvents[i]=[]),e.trackingEvents[i]=e.trackingEvents[i].concat(r)}}),t.videoClickTrackingURLTemplates&&t.videoClickTrackingURLTemplates.length&&e.creatives.forEach(function(e){"linear"===e.type&&(e.videoClickTrackingURLTemplates=e.videoClickTrackingURLTemplates.concat(t.videoClickTrackingURLTemplates))}),t.videoCustomClickURLTemplates&&t.videoCustomClickURLTemplates.length&&e.creatives.forEach(function(e){"linear"===e.type&&(e.videoCustomClickURLTemplates=e.videoCustomClickURLTemplates.concat(t.videoCustomClickURLTemplates))}),t.videoClickThroughURLTemplate&&e.creatives.forEach(function(e){"linear"!==e.type||null!==e.videoClickThroughURLTemplate&&void 0!==e.videoClickThroughURLTemplate||(e.videoClickThroughURLTemplate=t.videoClickThroughURLTemplate)})}};function g(e,t){var i=new u(t);return m.childrenByName(e,"Companion").forEach(function(e){var t=new l;t.id=e.getAttribute("id")||null,t.width=e.getAttribute("width"),t.height=e.getAttribute("height"),t.companionClickTrackingURLTemplates=[],m.childrenByName(e,"HTMLResource").forEach(function(e){t.type=e.getAttribute("creativeType")||"text/html",t.htmlResource=m.parseNodeText(e)}),m.childrenByName(e,"IFrameResource").forEach(function(e){t.type=e.getAttribute("creativeType")||0,t.iframeResource=m.parseNodeText(e)}),m.childrenByName(e,"StaticResource").forEach(function(i){t.type=i.getAttribute("creativeType")||0,m.childrenByName(e,"AltText").forEach(function(e){t.altText=m.parseNodeText(e)}),t.staticResource=m.parseNodeText(i)}),m.childrenByName(e,"TrackingEvents").forEach(function(e){m.childrenByName(e,"Tracking").forEach(function(e){var i=e.getAttribute("event"),r=m.parseNodeText(e);i&&r&&(Array.isArray(t.trackingEvents[i])||(t.trackingEvents[i]=[]),t.trackingEvents[i].push(r))})}),m.childrenByName(e,"CompanionClickTracking").forEach(function(e){t.companionClickTrackingURLTemplates.push(m.parseNodeText(e))}),t.companionClickThroughURLTemplate=m.parseNodeText(m.childByName(e,"CompanionClickThrough")),t.companionClickTrackingURLTemplate=m.parseNodeText(m.childByName(e,"CompanionClickTracking")),i.variations.push(t)}),i}var T=function(e){function i(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};t(this,i);var r=n(this,(i.__proto__||Object.getPrototypeOf(i)).call(this,e));return r.type="linear",r.duration=0,r.skipDelay=null,r.mediaFiles=[],r.videoClickThroughURLTemplate=null,r.videoClickTrackingURLTemplates=[],r.videoCustomClickURLTemplates=[],r.adParameters=null,r.icons=[],r}return r(i,c),i}(),k=function e(){t(this,e),this.program=null,this.height=0,this.width=0,this.xPosition=0,this.yPosition=0,this.apiFramework=null,this.offset=null,this.duration=0,this.type=null,this.staticResource=null,this.htmlResource=null,this.iframeResource=null,this.iconClickThroughURLTemplate=null,this.iconClickTrackingURLTemplates=[],this.iconViewTrackingURLTemplate=null},y=function e(){t(this,e),this.id=null,this.fileURL=null,this.deliveryType="progressive",this.mimeType=null,this.codec=null,this.bitrate=0,this.minBitrate=0,this.maxBitrate=0,this.width=0,this.height=0,this.apiFramework=null,this.scalable=null,this.maintainAspectRatio=null};function R(e,t){var i=void 0,r=new T(t);r.duration=m.parseDuration(m.parseNodeText(m.childByName(e,"Duration")));var n=e.getAttribute("skipoffset");if(void 0===n||null===n)r.skipDelay=null;else if("%"===n.charAt(n.length-1)&&-1!==r.duration){var a=parseInt(n,10);r.skipDelay=r.duration*(a/100)}else r.skipDelay=m.parseDuration(n);var s=m.childByName(e,"VideoClicks");s&&(r.videoClickThroughURLTemplate=m.parseNodeText(m.childByName(s,"ClickThrough")),m.childrenByName(s,"ClickTracking").forEach(function(e){r.videoClickTrackingURLTemplates.push(m.parseNodeText(e))}),m.childrenByName(s,"CustomClick").forEach(function(e){r.videoCustomClickURLTemplates.push(m.parseNodeText(e))}));var o=m.childByName(e,"AdParameters");o&&(r.adParameters=m.parseNodeText(o)),m.childrenByName(e,"TrackingEvents").forEach(function(e){m.childrenByName(e,"Tracking").forEach(function(e){var t=e.getAttribute("event"),n=m.parseNodeText(e);if(t&&n){if("progress"===t){if(!(i=e.getAttribute("offset")))return;t="%"===i.charAt(i.length-1)?"progress-"+i:"progress-"+Math.round(m.parseDuration(i))}Array.isArray(r.trackingEvents[t])||(r.trackingEvents[t]=[]),r.trackingEvents[t].push(n)}})}),m.childrenByName(e,"MediaFiles").forEach(function(e){m.childrenByName(e,"MediaFile").forEach(function(e){var t=new y;t.id=e.getAttribute("id"),t.fileURL=m.parseNodeText(e),t.deliveryType=e.getAttribute("delivery"),t.codec=e.getAttribute("codec"),t.mimeType=e.getAttribute("type"),t.apiFramework=e.getAttribute("apiFramework"),t.bitrate=parseInt(e.getAttribute("bitrate")||0),t.minBitrate=parseInt(e.getAttribute("minBitrate")||0),t.maxBitrate=parseInt(e.getAttribute("maxBitrate")||0),t.width=parseInt(e.getAttribute("width")||0),t.height=parseInt(e.getAttribute("height")||0);var i=e.getAttribute("scalable");i&&"string"==typeof i&&("true"===(i=i.toLowerCase())?t.scalable=!0:"false"===i&&(t.scalable=!1));var n=e.getAttribute("maintainAspectRatio");n&&"string"==typeof n&&("true"===(n=n.toLowerCase())?t.maintainAspectRatio=!0:"false"===n&&(t.maintainAspectRatio=!1)),r.mediaFiles.push(t)})});var l=m.childByName(e,"Icons");return l&&m.childrenByName(l,"Icon").forEach(function(e){var t=new k;t.program=e.getAttribute("program"),t.height=parseInt(e.getAttribute("height")||0),t.width=parseInt(e.getAttribute("width")||0),t.xPosition=function(e){if(-1!==["left","right"].indexOf(e))return e;return parseInt(e||0)}(e.getAttribute("xPosition")),t.yPosition=function(e){if(-1!==["top","bottom"].indexOf(e))return e;return parseInt(e||0)}(e.getAttribute("yPosition")),t.apiFramework=e.getAttribute("apiFramework"),t.offset=m.parseDuration(e.getAttribute("offset")),t.duration=m.parseDuration(e.getAttribute("duration")),m.childrenByName(e,"HTMLResource").forEach(function(e){t.type=e.getAttribute("creativeType")||"text/html",t.htmlResource=m.parseNodeText(e)}),m.childrenByName(e,"IFrameResource").forEach(function(e){t.type=e.getAttribute("creativeType")||0,t.iframeResource=m.parseNodeText(e)}),m.childrenByName(e,"StaticResource").forEach(function(e){t.type=e.getAttribute("creativeType")||0,t.staticResource=m.parseNodeText(e)});var i=m.childByName(e,"IconClicks");i&&(t.iconClickThroughURLTemplate=m.parseNodeText(m.childByName(i,"IconClickThrough")),m.childrenByName(i,"IconClickTracking").forEach(function(e){t.iconClickTrackingURLTemplates.push(m.parseNodeText(e))})),t.iconViewTrackingURLTemplate=m.parseNodeText(m.childByName(e,"IconViewTracking")),r.icons.push(t)}),r}var L=function(e){function i(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};t(this,i);var r=n(this,(i.__proto__||Object.getPrototypeOf(i)).call(this,e));return r.type="nonlinear",r.variations=[],r}return r(i,c),i}(),A=function e(){t(this,e),this.id=null,this.width=0,this.height=0,this.expandedWidth=0,this.expandedHeight=0,this.scalable=!0,this.maintainAspectRatio=!0,this.minSuggestedDuration=0,this.apiFramework="static",this.type=null,this.staticResource=null,this.htmlResource=null,this.iframeResource=null,this.nonlinearClickThroughURLTemplate=null,this.nonlinearClickTrackingURLTemplates=[],this.adParameters=null};function E(e,t){var i=new L(t);return m.childrenByName(e,"TrackingEvents").forEach(function(e){var t=void 0,r=void 0;m.childrenByName(e,"Tracking").forEach(function(e){t=e.getAttribute("event"),r=m.parseNodeText(e),t&&r&&(Array.isArray(i.trackingEvents[t])||(i.trackingEvents[t]=[]),i.trackingEvents[t].push(r))})}),m.childrenByName(e,"NonLinear").forEach(function(e){var t=new A;t.id=e.getAttribute("id")||null,t.width=e.getAttribute("width"),t.height=e.getAttribute("height"),t.expandedWidth=e.getAttribute("expandedWidth"),t.expandedHeight=e.getAttribute("expandedHeight"),t.scalable=m.parseBoolean(e.getAttribute("scalable")),t.maintainAspectRatio=m.parseBoolean(e.getAttribute("maintainAspectRatio")),t.minSuggestedDuration=m.parseDuration(e.getAttribute("minSuggestedDuration")),t.apiFramework=e.getAttribute("apiFramework"),m.childrenByName(e,"HTMLResource").forEach(function(e){t.type=e.getAttribute("creativeType")||"text/html",t.htmlResource=m.parseNodeText(e)}),m.childrenByName(e,"IFrameResource").forEach(function(e){t.type=e.getAttribute("creativeType")||0,t.iframeResource=m.parseNodeText(e)}),m.childrenByName(e,"StaticResource").forEach(function(e){t.type=e.getAttribute("creativeType")||0,t.staticResource=m.parseNodeText(e)});var r=m.childByName(e,"AdParameters");r&&(t.adParameters=m.parseNodeText(r)),t.nonlinearClickThroughURLTemplate=m.parseNodeText(m.childByName(e,"NonLinearClickThrough")),m.childrenByName(e,"NonLinearClickTracking").forEach(function(e){t.nonlinearClickTrackingURLTemplates.push(m.parseNodeText(e))}),i.variations.push(t)}),i}function U(e){var t=e.childNodes;for(var i in t){var r=t[i];if(-1!==["Wrapper","InLine"].indexOf(r.nodeName)){if(m.copyNodeAttribute("id",e,r),m.copyNodeAttribute("sequence",e,r),"Wrapper"===r.nodeName)return C(r);if("InLine"===r.nodeName)return w(r)}}}function w(e){var t=e.childNodes,i=new a;for(var r in i.id=e.getAttribute("id")||null,i.sequence=e.getAttribute("sequence")||null,t){var n=t[r];switch(n.nodeName){case"Error":i.errorURLTemplates.push(m.parseNodeText(n));break;case"Impression":i.impressionURLTemplates.push(m.parseNodeText(n));break;case"Creatives":m.childrenByName(n,"Creative").forEach(function(e){var t={id:e.getAttribute("id")||null,adId:N(e),sequence:e.getAttribute("sequence")||null,apiFramework:e.getAttribute("apiFramework")||null};for(var r in e.childNodes){var n=e.childNodes[r],a=void 0;switch(n.nodeName){case"Linear":(a=R(n,t))&&i.creatives.push(a);break;case"NonLinearAds":(a=E(n,t))&&i.creatives.push(a);break;case"CompanionAds":(a=g(n,t))&&i.creatives.push(a)}}});break;case"Extensions":b(i.extensions,m.childrenByName(n,"Extension"));break;case"AdSystem":i.system={value:m.parseNodeText(n),version:n.getAttribute("version")||null};break;case"AdTitle":i.title=m.parseNodeText(n);break;case"Description":i.description=m.parseNodeText(n);break;case"Advertiser":i.advertiser=m.parseNodeText(n);break;case"Pricing":i.pricing={value:m.parseNodeText(n),model:n.getAttribute("model")||null,currency:n.getAttribute("currency")||null};break;case"Survey":i.survey=m.parseNodeText(n)}}return i}function C(e){var t=w(e),i=m.childByName(e,"VASTAdTagURI");if(i?t.nextWrapperURL=m.parseNodeText(i):(i=m.childByName(e,"VASTAdTagURL"))&&(t.nextWrapperURL=m.parseNodeText(m.childByName(i,"URL"))),t.creatives.forEach(function(e){if(-1!==["linear","nonlinear"].indexOf(e.type)){if(e.trackingEvents){t.trackingEvents||(t.trackingEvents={}),t.trackingEvents[e.type]||(t.trackingEvents[e.type]={});var i=function(i){var r=e.trackingEvents[i];Array.isArray(t.trackingEvents[e.type][i])||(t.trackingEvents[e.type][i]=[]),r.forEach(function(r){t.trackingEvents[e.type][i].push(r)})};for(var r in e.trackingEvents)i(r)}e.videoClickTrackingURLTemplates&&(Array.isArray(t.videoClickTrackingURLTemplates)||(t.videoClickTrackingURLTemplates=[]),e.videoClickTrackingURLTemplates.forEach(function(e){t.videoClickTrackingURLTemplates.push(e)})),e.videoClickThroughURLTemplate&&(t.videoClickThroughURLTemplate=e.videoClickThroughURLTemplate),e.videoCustomClickURLTemplates&&(Array.isArray(t.videoCustomClickURLTemplates)||(t.videoCustomClickURLTemplates=[]),e.videoCustomClickURLTemplates.forEach(function(e){t.videoCustomClickURLTemplates.push(e)}))}}),t.nextWrapperURL)return t}function b(e,t){t.forEach(function(t){var i=new s,r=t.attributes,n=t.childNodes;if(t.attributes)for(var a in r){var l=r[a];l.nodeName&&l.nodeValue&&(i.attributes[l.nodeName]=l.nodeValue)}for(var c in n){var u=n[c],h=m.parseNodeText(u);if("#comment"!==u.nodeName&&""!==h){var p=new o;if(p.name=u.nodeName,p.value=h,u.attributes){var d=u.attributes;for(var f in d){var v=d[f];p.attributes[v.nodeName]=v.nodeValue}}i.children.push(p)}}e.push(i)})}function N(e){return e.getAttribute("AdID")||e.getAttribute("adID")||e.getAttribute("adId")||null}function x(){}function S(){S.init.call(this)}function I(e){return void 0===e._maxListeners?S.defaultMaxListeners:e._maxListeners}function D(e,t,i,r){var n,a,s,o;if("function"!=typeof i)throw new TypeError('"listener" argument must be a function');if((a=e._events)?(a.newListener&&(e.emit("newListener",t,i.listener?i.listener:i),a=e._events),s=a[t]):(a=e._events=new x,e._eventsCount=0),s){if("function"==typeof s?s=a[t]=r?[i,s]:[s,i]:r?s.unshift(i):s.push(i),!s.warned&&(n=I(e))&&n>0&&s.length>n){s.warned=!0;var l=new Error("Possible EventEmitter memory leak detected. "+s.length+" "+t+" listeners added. Use emitter.setMaxListeners() to increase limit");l.name="MaxListenersExceededWarning",l.emitter=e,l.type=t,l.count=s.length,o=l,"function"==typeof console.warn?console.warn(o):console.log(o)}}else s=a[t]=i,++e._eventsCount;return e}function O(e,t,i){var r=!1;function n(){e.removeListener(t,n),r||(r=!0,i.apply(e,arguments))}return n.listener=i,n}function _(e){var t=this._events;if(t){var i=t[e];if("function"==typeof i)return 1;if(i)return i.length}return 0}function B(e,t){for(var i=new Array(t);t--;)i[t]=e[t];return i}function P(){var e=void 0;return window.XDomainRequest&&(e=new XDomainRequest),e}x.prototype=Object.create(null),S.EventEmitter=S,S.usingDomains=!1,S.prototype.domain=void 0,S.prototype._events=void 0,S.prototype._maxListeners=void 0,S.defaultMaxListeners=10,S.init=function(){this.domain=null,S.usingDomains&&(!(void 0).active||this instanceof(void 0).Domain||(this.domain=(void 0).active)),this._events&&this._events!==Object.getPrototypeOf(this)._events||(this._events=new x,this._eventsCount=0),this._maxListeners=this._maxListeners||void 0},S.prototype.setMaxListeners=function(e){if("number"!=typeof e||e<0||isNaN(e))throw new TypeError('"n" argument must be a positive number');return this._maxListeners=e,this},S.prototype.getMaxListeners=function(){return I(this)},S.prototype.emit=function(e){var t,i,r,n,a,s,o,l="error"===e;if(s=this._events)l=l&&null==s.error;else if(!l)return!1;if(o=this.domain,l){if(t=arguments[1],!o){if(t instanceof Error)throw t;var c=new Error('Uncaught, unspecified "error" event. ('+t+")");throw c.context=t,c}return t||(t=new Error('Uncaught, unspecified "error" event')),t.domainEmitter=this,t.domain=o,t.domainThrown=!1,o.emit("error",t),!1}if(!(i=s[e]))return!1;var u="function"==typeof i;switch(r=arguments.length){case 1:!function(e,t,i){if(t)e.call(i);else for(var r=e.length,n=B(e,r),a=0;a<r;++a)n[a].call(i)}(i,u,this);break;case 2:!function(e,t,i,r){if(t)e.call(i,r);else for(var n=e.length,a=B(e,n),s=0;s<n;++s)a[s].call(i,r)}(i,u,this,arguments[1]);break;case 3:!function(e,t,i,r,n){if(t)e.call(i,r,n);else for(var a=e.length,s=B(e,a),o=0;o<a;++o)s[o].call(i,r,n)}(i,u,this,arguments[1],arguments[2]);break;case 4:!function(e,t,i,r,n,a){if(t)e.call(i,r,n,a);else for(var s=e.length,o=B(e,s),l=0;l<s;++l)o[l].call(i,r,n,a)}(i,u,this,arguments[1],arguments[2],arguments[3]);break;default:for(n=new Array(r-1),a=1;a<r;a++)n[a-1]=arguments[a];!function(e,t,i,r){if(t)e.apply(i,r);else for(var n=e.length,a=B(e,n),s=0;s<n;++s)a[s].apply(i,r)}(i,u,this,n)}return!0},S.prototype.addListener=function(e,t){return D(this,e,t,!1)},S.prototype.on=S.prototype.addListener,S.prototype.prependListener=function(e,t){return D(this,e,t,!0)},S.prototype.once=function(e,t){if("function"!=typeof t)throw new TypeError('"listener" argument must be a function');return this.on(e,O(this,e,t)),this},S.prototype.prependOnceListener=function(e,t){if("function"!=typeof t)throw new TypeError('"listener" argument must be a function');return this.prependListener(e,O(this,e,t)),this},S.prototype.removeListener=function(e,t){var i,r,n,a,s;if("function"!=typeof t)throw new TypeError('"listener" argument must be a function');if(!(r=this._events))return this;if(!(i=r[e]))return this;if(i===t||i.listener&&i.listener===t)0==--this._eventsCount?this._events=new x:(delete r[e],r.removeListener&&this.emit("removeListener",e,i.listener||t));else if("function"!=typeof i){for(n=-1,a=i.length;a-- >0;)if(i[a]===t||i[a].listener&&i[a].listener===t){s=i[a].listener,n=a;break}if(n<0)return this;if(1===i.length){if(i[0]=void 0,0==--this._eventsCount)return this._events=new x,this;delete r[e]}else!function(e,t){for(var i=t,r=i+1,n=e.length;r<n;i+=1,r+=1)e[i]=e[r];e.pop()}(i,n);r.removeListener&&this.emit("removeListener",e,s||t)}return this},S.prototype.removeAllListeners=function(e){var t,i;if(!(i=this._events))return this;if(!i.removeListener)return 0===arguments.length?(this._events=new x,this._eventsCount=0):i[e]&&(0==--this._eventsCount?this._events=new x:delete i[e]),this;if(0===arguments.length){for(var r,n=Object.keys(i),a=0;a<n.length;++a)"removeListener"!==(r=n[a])&&this.removeAllListeners(r);return this.removeAllListeners("removeListener"),this._events=new x,this._eventsCount=0,this}if("function"==typeof(t=i[e]))this.removeListener(e,t);else if(t)do{this.removeListener(e,t[t.length-1])}while(t[0]);return this},S.prototype.listeners=function(e){var t,i=this._events;return i&&(t=i[e])?"function"==typeof t?[t.listener||t]:function(e){for(var t=new Array(e.length),i=0;i<t.length;++i)t[i]=e[i].listener||e[i];return t}(t):[]},S.listenerCount=function(e,t){return"function"==typeof e.listenerCount?e.listenerCount(t):_.call(e,t)},S.prototype.listenerCount=_,S.prototype.eventNames=function(){return this._eventsCount>0?Reflect.ownKeys(this._events):[]};var V={get:function(e,t,i){var r="function"==typeof window.ActiveXObject?new window.ActiveXObject("Microsoft.XMLDOM"):void 0;if(!r)return i(new Error("FlashURLHandler: Microsoft.XMLDOM format not supported"));r.async=!1;var n=P();n.open("GET",e),n.timeout=t.timeout||0,n.withCredentials=t.withCredentials||!1,n.send(),n.onprogress=function(){},n.onload=function(){r.loadXML(n.responseText),i(null,r)}},supported:function(){return!!P()}};var F={get:function(e,t,i){i(new Error("Please bundle the library for node to use the node urlHandler"))}};function M(){try{var e=new window.XMLHttpRequest;return"withCredentials"in e?e:null}catch(e){return null}}var H={get:function(e,t,i){if("https:"===window.location.protocol&&0===e.indexOf("http://"))return i(new Error("XHRURLHandler: Cannot go from HTTPS to HTTP."));try{var r=M();r.open("GET",e),r.timeout=t.timeout||0,r.withCredentials=t.withCredentials||!1,r.overrideMimeType&&r.overrideMimeType("text/xml"),r.onreadystatechange=function(){4===r.readyState&&(200===r.status?i(null,r.responseXML):i(new Error("XHRURLHandler: "+r.statusText)))},r.send()}catch(e){i(new Error("XHRURLHandler: Unexpected error"))}},supported:function(){return!!M()}};var W={get:function(e,t,i){return i||("function"==typeof t&&(i=t),t={}),"undefined"==typeof window||null===window?F.get(e,t,i):H.supported()?H.get(e,t,i):V.supported()?V.get(e,t,i):i(new Error("Current context is not supported by any of the default URLHandlers. Please provide a custom URLHandler"))}},q={ERRORCODE:900,extensions:[]},j=function(e){function a(){t(this,a);var e=n(this,(a.__proto__||Object.getPrototypeOf(a)).call(this));return e.remainingAds=[],e.parentURLs=[],e.errorURLTemplates=[],e.rootErrorURLTemplates=[],e.maxWrapperDepth=null,e.URLTemplateFilters=[],e.fetchingOptions={},e}return r(a,S),i(a,[{key:"addURLTemplateFilter",value:function(e){"function"==typeof e&&this.URLTemplateFilters.push(e)}},{key:"removeURLTemplateFilter",value:function(){this.URLTemplateFilters.pop()}},{key:"countURLTemplateFilters",value:function(){return this.URLTemplateFilters.length}},{key:"clearURLTemplateFilters",value:function(){this.URLTemplateFilters=[]}},{key:"trackVastError",value:function(e,t){for(var i=arguments.length,r=Array(i>2?i-2:0),n=2;n<i;n++)r[n-2]=arguments[n];this.emit("VAST-error",Object.assign.apply(Object,[{},q,t].concat(r))),v.track(e,t)}},{key:"getErrorURLTemplates",value:function(){return this.rootErrorURLTemplates.concat(this.errorURLTemplates)}},{key:"fetchVAST",value:function(e,t,i){var r=this;return new Promise(function(n,a){r.URLTemplateFilters.forEach(function(t){e=t(e)}),r.parentURLs.push(e),r.emit("VAST-resolving",{url:e,wrapperDepth:t,originalUrl:i}),r.urlHandler.get(e,r.fetchingOptions,function(t,i){r.emit("VAST-resolved",{url:e,error:t}),t?a(t):n(i)})})}},{key:"initParsingStatus",value:function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};this.rootURL="",this.remainingAds=[],this.parentURLs=[],this.errorURLTemplates=[],this.rootErrorURLTemplates=[],this.maxWrapperDepth=e.wrapperLimit||10,this.fetchingOptions={timeout:e.timeout,withCredentials:e.withCredentials},this.urlHandler=e.urlHandler||e.urlhandler||W,this.vastVersion=null}},{key:"getRemainingAds",value:function(e){var t=this;if(0===this.remainingAds.length)return Promise.reject(new Error("No more ads are available for the given VAST"));var i=e?v.flatten(this.remainingAds):this.remainingAds.shift();return this.errorURLTemplates=[],this.parentURLs=[],this.resolveAds(i,{wrapperDepth:0,originalUrl:this.rootURL}).then(function(e){return t.buildVASTResponse(e)})}},{key:"getAndParseVAST",value:function(e){var t=this,i=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};return this.initParsingStatus(i),this.rootURL=e,this.fetchVAST(e).then(function(r){return i.originalUrl=e,i.isRootVAST=!0,t.parse(r,i).then(function(e){return t.buildVASTResponse(e)})})}},{key:"parseVAST",value:function(e){var t=this,i=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};return this.initParsingStatus(i),i.isRootVAST=!0,this.parse(e,i).then(function(e){return t.buildVASTResponse(e)})}},{key:"buildVASTResponse",value:function(e){var i=new function e(){t(this,e),this.ads=[],this.errorURLTemplates=[],this.version=null};return i.ads=e,i.errorURLTemplates=this.getErrorURLTemplates(),i.version=this.vastVersion,this.completeWrapperResolving(i),i}},{key:"parseVastXml",value:function(e,t){var i=t.isRootVAST,r=void 0!==i&&i;if(!e||!e.documentElement||"VAST"!==e.documentElement.nodeName)throw new Error("Invalid VAST XMLDocument");var n=[],a=e.documentElement.childNodes;if(r){var s=e.documentElement.getAttribute("version");s&&(this.vastVersion=s)}for(var o in a){var l=a[o];if("Error"===l.nodeName){var c=m.parseNodeText(l);r?this.rootErrorURLTemplates.push(c):this.errorURLTemplates.push(c)}if("Ad"===l.nodeName){var u=U(l);u?n.push(u):this.trackVastError(this.getErrorURLTemplates(),{ERRORCODE:101})}}return n}},{key:"parse",value:function(e,t){var i=t.resolveAll,r=void 0===i||i,n=t.wrapperSequence,a=void 0===n?null:n,s=t.originalUrl,o=void 0===s?null:s,l=t.wrapperDepth,c=void 0===l?0:l,u=t.isRootVAST,h=void 0!==u&&u,p=[];try{p=this.parseVastXml(e,{isRootVAST:h})}catch(e){return Promise.reject(e)}var d=p.length,f=p[d-1];return 1===d&&void 0!==a&&null!==a&&f&&!f.sequence&&(f.sequence=a),!1===r&&(this.remainingAds=m.splitVAST(p),p=this.remainingAds.shift()),this.resolveAds(p,{wrapperDepth:c,originalUrl:o})}},{key:"resolveAds",value:function(){var e=this,t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:[],i=arguments[1],r=i.wrapperDepth,n=i.originalUrl,a=[];return t.forEach(function(t){var i=e.resolveWrappers(t,r,n);a.push(i)}),Promise.all(a).then(function(t){var i=v.flatten(t);if(!i&&e.remainingAds.length>0){var a=e.remainingAds.shift();return e.resolveAds(a,{wrapperDepth:r,originalUrl:n})}return i})}},{key:"resolveWrappers",value:function(e,t,i){var r=this;return new Promise(function(n){if(t++,!e.nextWrapperURL)return delete e.nextWrapperURL,n(e);if(t>=r.maxWrapperDepth||-1!==r.parentURLs.indexOf(e.nextWrapperURL))return e.errorCode=302,delete e.nextWrapperURL,n(e);e.nextWrapperURL=m.resolveVastAdTagURI(e.nextWrapperURL,i);var a=e.sequence;i=e.nextWrapperURL,r.fetchVAST(e.nextWrapperURL,t,i).then(function(s){return r.parse(s,{originalUrl:i,wrapperSequence:a,wrapperDepth:t}).then(function(t){if(delete e.nextWrapperURL,0===t.length)return e.creatives=[],n(e);t.forEach(function(t){t&&m.mergeWrapperAdData(t,e)}),n(t)})}).catch(function(t){e.errorCode=301,e.errorMessage=t.message,n(e)})})}},{key:"completeWrapperResolving",value:function(e){if(0===e.ads.length)this.trackVastError(e.errorURLTemplates,{ERRORCODE:303});else for(var t=e.ads.length-1;t>=0;t--){var i=e.ads[t];(i.errorCode||0===i.creatives.length)&&(this.trackVastError(i.errorURLTemplates.concat(e.errorURLTemplates),{ERRORCODE:i.errorCode||303},{ERRORMESSAGE:i.errorMessage||""},{extensions:i.extensions},{system:i.system}),e.ads.splice(t,1))}}}]),a}(),X=null,Q={data:{},length:0,getItem:function(e){return this.data[e]},setItem:function(e,t){this.data[e]=t,this.length=Object.keys(this.data).length},removeItem:function(e){delete this.data[e],this.length=Object.keys(this.data).length},clear:function(){this.data={},this.length=0}},G=function(){function e(){t(this,e),this.storage=this.initStorage()}return i(e,[{key:"initStorage",value:function(){if(X)return X;try{X="undefined"!=typeof window&&null!==window?window.localStorage||window.sessionStorage:null}catch(e){X=null}return X&&!this.isStorageDisabled(X)||(X=Q).clear(),X}},{key:"isStorageDisabled",value:function(e){var t="__VASTStorage__";try{if(e.setItem(t,t),e.getItem(t)!==t)return e.removeItem(t),!0}catch(e){return!0}return e.removeItem(t),!1}},{key:"getItem",value:function(e){return this.storage.getItem(e)}},{key:"setItem",value:function(e,t){return this.storage.setItem(e,t)}},{key:"removeItem",value:function(e){return this.storage.removeItem(e)}},{key:"clear",value:function(){return this.storage.clear()}}]),e}(),Y=function(){function e(i,r,n){t(this,e),this.cappingFreeLunch=i||0,this.cappingMinimumTimeInterval=r||0,this.defaultOptions={withCredentials:!1,timeout:0},this.vastParser=new j,this.storage=n||new G,void 0===this.lastSuccessfulAd&&(this.lastSuccessfulAd=0),void 0===this.totalCalls&&(this.totalCalls=0),void 0===this.totalCallsTimeout&&(this.totalCallsTimeout=0)}return i(e,[{key:"getParser",value:function(){return this.vastParser}},{key:"hasRemainingAds",value:function(){return this.vastParser.remainingAds.length>0}},{key:"getNextAds",value:function(e){return this.vastParser.getRemainingAds(e)}},{key:"get",value:function(e){var t=this,i=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},r=Date.now();return(i=Object.assign({},this.defaultOptions,i)).hasOwnProperty("resolveAll")||(i.resolveAll=!1),this.totalCallsTimeout<r?(this.totalCalls=1,this.totalCallsTimeout=r+36e5):this.totalCalls++,new Promise(function(n,a){if(t.cappingFreeLunch>=t.totalCalls)return a(new Error("VAST call canceled – FreeLunch capping not reached yet "+t.totalCalls+"/"+t.cappingFreeLunch));var s=r-t.lastSuccessfulAd;if(s<0)t.lastSuccessfulAd=0;else if(s<t.cappingMinimumTimeInterval)return a(new Error("VAST call canceled – ("+t.cappingMinimumTimeInterval+")ms minimum interval reached"));t.vastParser.getAndParseVAST(e,i).then(function(e){return n(e)}).catch(function(e){return a(e)})})}},{key:"lastSuccessfulAd",get:function(){return this.storage.getItem("vast-client-last-successful-ad")},set:function(e){this.storage.setItem("vast-client-last-successful-ad",e)}},{key:"totalCalls",get:function(){return this.storage.getItem("vast-client-total-calls")},set:function(e){this.storage.setItem("vast-client-total-calls",e)}},{key:"totalCallsTimeout",get:function(){return this.storage.getItem("vast-client-total-calls-timeout")},set:function(e){this.storage.setItem("vast-client-total-calls-timeout",e)}}]),e}(),K=function(e){function a(e,i,r){var s=arguments.length>3&&void 0!==arguments[3]?arguments[3]:null;t(this,a);var o=n(this,(a.__proto__||Object.getPrototypeOf(a)).call(this));for(var l in o.ad=i,o.creative=r,o.variation=s,o.muted=!1,o.impressed=!1,o.skippable=!1,o.trackingEvents={},o._alreadyTriggeredQuartiles={},o.emitAlwaysEvents=["creativeView","start","firstQuartile","midpoint","thirdQuartile","complete","resume","pause","rewind","skip","closeLinear","close"],o.creative.trackingEvents){var c=o.creative.trackingEvents[l];o.trackingEvents[l]=c.slice(0)}return o.creative instanceof T?o._initLinearTracking():o._initVariationTracking(),e&&o.on("start",function(){e.lastSuccessfulAd=Date.now()}),o}return r(a,S),i(a,[{key:"_initLinearTracking",value:function(){this.linear=!0,this.skipDelay=this.creative.skipDelay,this.setDuration(this.creative.duration),this.clickThroughURLTemplate=this.creative.videoClickThroughURLTemplate,this.clickTrackingURLTemplates=this.creative.videoClickTrackingURLTemplates}},{key:"_initVariationTracking",value:function(){if(this.linear=!1,this.skipDelay=-1,this.variation){for(var e in this.variation.trackingEvents){var t=this.variation.trackingEvents[e];this.trackingEvents[e]?this.trackingEvents[e]=this.trackingEvents[e].concat(t.slice(0)):this.trackingEvents[e]=t.slice(0)}this.variation instanceof A?(this.clickThroughURLTemplate=this.variation.nonlinearClickThroughURLTemplate,this.clickTrackingURLTemplates=this.variation.nonlinearClickTrackingURLTemplates,this.setDuration(this.variation.minSuggestedDuration)):this.variation instanceof l&&(this.clickThroughURLTemplate=this.variation.companionClickThroughURLTemplate,this.clickTrackingURLTemplates=this.variation.companionClickTrackingURLTemplates)}}},{key:"setDuration",value:function(e){this.assetDuration=e,this.quartiles={firstQuartile:Math.round(25*this.assetDuration)/100,midpoint:Math.round(50*this.assetDuration)/100,thirdQuartile:Math.round(75*this.assetDuration)/100}}},{key:"setProgress",value:function(e){var t=this,i=this.skipDelay||-1;if(-1===i||this.skippable||(i>e?this.emit("skip-countdown",i-e):(this.skippable=!0,this.emit("skip-countdown",0))),this.assetDuration>0){var r=[];if(e>0){var n=Math.round(e/this.assetDuration*100);for(var a in r.push("start"),r.push("progress-"+n+"%"),r.push("progress-"+Math.round(e)),this.quartiles)this.isQuartileReached(a,this.quartiles[a],e)&&(r.push(a),this._alreadyTriggeredQuartiles[a]=!0)}r.forEach(function(e){t.track(e,!0)}),e<this.progress&&this.track("rewind")}this.progress=e}},{key:"isQuartileReached",value:function(e,t,i){var r=!1;return t<=i&&!this._alreadyTriggeredQuartiles[e]&&(r=!0),r}},{key:"setMuted",value:function(e){this.muted!==e&&this.track(e?"mute":"unmute"),this.muted=e}},{key:"setPaused",value:function(e){this.paused!==e&&this.track(e?"pause":"resume"),this.paused=e}},{key:"setFullscreen",value:function(e){this.fullscreen!==e&&this.track(e?"fullscreen":"exitFullscreen"),this.fullscreen=e}},{key:"setExpand",value:function(e){this.expanded!==e&&this.track(e?"expand":"collapse"),this.expanded=e}},{key:"setSkipDelay",value:function(e){"number"==typeof e&&(this.skipDelay=e)}},{key:"trackImpression",value:function(){this.impressed||(this.impressed=!0,this.trackURLs(this.ad.impressionURLTemplates),this.track("creativeView"))}},{key:"errorWithCode",value:function(e){var t=arguments.length>1&&void 0!==arguments[1]&&arguments[1];this.trackURLs(this.ad.errorURLTemplates,{ERRORCODE:e},{isCustomCode:t})}},{key:"complete",value:function(){this.track("complete")}},{key:"close",value:function(){this.track(this.linear?"closeLinear":"close")}},{key:"skip",value:function(){this.track("skip")}},{key:"click",value:function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:null;this.clickTrackingURLTemplates&&this.clickTrackingURLTemplates.length&&this.trackURLs(this.clickTrackingURLTemplates);var t=this.clickThroughURLTemplate||e;if(t){var i=this.linear?{CONTENTPLAYHEAD:this.progressFormatted()}:{},r=v.resolveURLTemplates([t],i)[0];this.emit("clickthrough",r)}}},{key:"track",value:function(e){var t=arguments.length>1&&void 0!==arguments[1]&&arguments[1];"closeLinear"===e&&!this.trackingEvents[e]&&this.trackingEvents.close&&(e="close");var i=this.trackingEvents[e],r=this.emitAlwaysEvents.indexOf(e)>-1;i?(this.emit(e,""),this.trackURLs(i)):r&&this.emit(e,""),t&&(delete this.trackingEvents[e],r&&this.emitAlwaysEvents.splice(this.emitAlwaysEvents.indexOf(e),1))}},{key:"trackURLs",value:function(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},i=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{};this.linear&&(this.creative&&this.creative.mediaFiles&&this.creative.mediaFiles[0]&&this.creative.mediaFiles[0].fileURL&&(t.ASSETURI=this.creative.mediaFiles[0].fileURL),t.CONTENTPLAYHEAD=this.progressFormatted()),v.track(e,t,i)}},{key:"progressFormatted",value:function(){var e=parseInt(this.progress),t=e/3600;t.length<2&&(t="0"+t);var i=e/60%60;i.length<2&&(i="0"+i);var r=e%60;return r.length<2&&(r="0"+i),t+":"+i+":"+r+"."+parseInt(100*(this.progress-e))}}]),a}();return e.VASTClient=Y,e.VASTParser=j,e.VASTTracker=K,e}({});