video-ad-sdk
Version:
VAST/VPAID SDK that allows video ads to be played on top of any player
104 lines (103 loc) • 3.62 kB
JavaScript
var _a;
import { loadScript } from './helpers/loadScript';
import { createAdVideoElement } from './helpers/createAdVideoElement';
import { createAdContainer } from './helpers/createAdContainer';
import { createIframe } from './helpers/createIframe';
import { createSlot } from './helpers/createSlot';
import { getContentDocument } from './helpers/getContentDocument';
import { unique } from './helpers/unique';
const nextId = unique('videoAdContainer');
const hidden = Symbol('hidden');
/**
* This class provides everything necessary to contain and create a video ad within a given placeholder Element.
*/
export class VideoAdContainer {
/**
* Creates a VideoAdContainer.
*
* @param placeholder DIV that will contain the ad.
* @param videoElement optional videoElement that will be used to play the ad.
*/
constructor(placeholder, videoElement) {
this[_a] = {
destroyed: false,
id: nextId(),
iframe: undefined,
readyPromise: undefined
};
if (!(placeholder instanceof Element)) {
throw new TypeError('placeholder is not an Element');
}
this.element = createAdContainer();
this.isOriginalVideoElement = Boolean(videoElement);
if (videoElement) {
this.videoElement = videoElement;
}
else {
this.videoElement = createAdVideoElement();
this.element.appendChild(this.videoElement);
}
placeholder.appendChild(this.element);
}
/**
* Adds the passed script to the ad container.
*
* @param src script source uri.
* @param options Options map.
*/
async addScript(source, options = {}) {
var _b;
if (this.isDestroyed()) {
throw new Error('VideoAdContainer has been destroyed');
}
let { iframe } = this[hidden];
if (!iframe) {
iframe = await createIframe(this.element, this[hidden].id);
this[hidden].iframe = iframe;
this.executionContext = iframe.contentWindow;
}
const placeholder = (_b = getContentDocument(iframe)) === null || _b === void 0 ? void 0 : _b.body;
return loadScript(source, Object.assign({ placeholder }, options));
}
/**
* Adds the slot to the ad container.
*
* @param width - Slot width.
* @param height - Slot height.
*/
addSlot(width, height) {
if (this.isDestroyed()) {
throw new Error('VideoAdContainer has been destroyed');
}
if (!this.slotElement) {
this.slotElement = createSlot(this.element, width, height);
}
}
/**
* Destroys the VideoAdContainer.
*/
destroy() {
// NOTE: calling destroy immediately terminates the iframe and cancels
// tracking requests from vpaid script, so destroying should
// immediately hides and remove the iframe from dom after timeout
const removePromise = new Promise((resolve) => {
setTimeout(() => {
var _b;
(_b = this.element.parentNode) === null || _b === void 0 ? void 0 : _b.removeChild(this.element);
resolve();
}, 1000);
});
this.element.style.zIndex = '-9999';
this[hidden].destroyed = true;
return removePromise;
}
/**
* Checks if the container is destroyed.
*
* @returns true if the container is destroyed and false otherwise.
*/
isDestroyed() {
return this[hidden].destroyed;
}
}
_a = hidden;