@dailymotion/ad-sdk-web
Version:
JavaScript Ad SDK API by Dailymotion
144 lines (143 loc) • 5.25 kB
JavaScript
var s = Object.defineProperty;
var l = (i, e, t) => e in i ? s(i, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : i[e] = t;
var d = (i, e, t) => l(i, typeof e != "symbol" ? e + "" : e, t);
const c = ".ad-container{display:none;position:absolute;top:0;bottom:0;left:0;right:0}.ad-video{width:100%;height:100%;background-color:#000}";
var a = /* @__PURE__ */ ((i) => (i.AD_BREAK_START = "AD_BREAK_START", i.AD_BREAK_END = "AD_BREAK_END", i.AD_LOAD = "AD_LOAD", i.AD_START = "AD_START", i.AD_PLAY = "AD_PLAY", i.AD_PAUSE = "AD_PAUSE", i.AD_END = "AD_END", i.AD_ERROR = "AD_ERROR", i.CONTENT_PAUSE_REQUESTED = "CONTENT_PAUSE_REQUESTED", i.CONTENT_RESUME_REQUESTED = "CONTENT_RESUME_REQUESTED", i))(a || {});
const A = "https://asw.dmcdn.net/0.0.0/core.js", p = { useFakeAd: !1 };
class E {
constructor() {
d(this, "adVideoElement", null);
d(this, "adContainer", null);
d(this, "playerContainer");
d(this, "scriptLoaded", !1);
d(this, "appState", null);
/**
* Provides access to the Ad SDK event names.
* Use these constants to subscribe or unsubscribe from ad lifecycle events.
*/
d(this, "Events", a);
this.adVideoElement = null, this.adContainer = null, this.playerContainer = null, this.scriptLoaded = !1, this.injectCss(c);
}
/**
* Initializes the Ad SDK by loading the external script and preparing the DOM elements.
* @throws If no container is provided or if script fails to load.
* @param playerContainer
*/
async initialize(e) {
if (!this.playerContainer)
try {
if (!e) throw new Error("No container provided");
this.playerContainer = e, this.createAdContainer(), this.createAdVideoElement(), await this.loadScript(A, this.playerContainer), this.scriptLoaded = !0;
} catch (t) {
console.error(t), this.scriptLoaded = !1;
}
}
/**
* Loads the ad with the given content video tag.
* @param appState
* @param developmentOptions
* @throws If the Ad SDK is not initialized or ad elements are missing.
*/
async loadAdsSequence(e, t) {
if (!this.scriptLoaded)
throw new Error("Ad SDK not initialized");
const n = {
adContainer: this.adContainer,
adVideoTag: this.adVideoElement,
appState: e,
developmentOptions: { ...p, ...t }
};
await AdSdk.loadAdsSequence(n);
}
/**
* Starts playing the ad.
* @throws If the Ad SDK is not initialized.
*/
playAd() {
if (!this.scriptLoaded)
throw new Error("Ad SDK not initialized");
AdSdk.playAd();
}
/**
* Stops playing the ad.
* @throws If the Ad SDK is not initialized.
*/
pauseAd() {
if (!this.scriptLoaded)
throw new Error("Ad SDK not initialized");
AdSdk.pauseAd();
}
/**
* Skips the ad.
* @throws If the Ad SDK is not initialized.
*/
skipAd() {
if (!this.scriptLoaded)
throw new Error("Ad SDK not initialized");
AdSdk.skipAd();
}
/**
* Retrieves details about the currently loaded ad.
* @returns An object containing the ad position (e.g., pre-roll, mid-roll).
* @throws If the Ad SDK is not initialized.
*/
getAdDetails() {
if (!this.scriptLoaded)
throw new Error("Ad SDK not initialized");
return AdSdk.getAdDetails();
}
/**
* Subscribes to a specific Ad SDK event.
* @param event - The name of the event to listen for.
* @param callback - The event handler callback.
* @throws If the Ad SDK is not initialized.
*/
on(e, t) {
if (!this.scriptLoaded)
throw new Error("Ad SDK not initialized");
AdSdk.on(e, t);
}
/**
* Unsubscribes from a specific Ad SDK event.
* @param event - The name of the event to stop listening for.
* @param callback - The callback function to remove.
* @throws If the Ad SDK is not initialized.
*/
off(e, t) {
if (!this.scriptLoaded)
throw new Error("Ad SDK not initialized");
AdSdk.off(e, t);
}
/**
* Updates the current initialization state of the Ad SDK.
* Use these constants to subscribe or unsubscribe from ad lifecycle events.
*/
updateAppState(e) {
if (!this.scriptLoaded)
throw new Error("Ad SDK not initialized");
this.appState = e, AdSdk.updateAppState(e);
}
createAdVideoElement() {
var e;
this.adVideoElement = document.createElement("video"), this.adVideoElement.classList.add("ad-video"), this.adVideoElement.controls = !0, (e = this.adContainer) == null || e.appendChild(this.adVideoElement);
}
createAdContainer() {
var e;
this.adContainer = document.createElement("div"), this.adContainer.classList.add("ad-container"), (e = this.playerContainer) == null || e.appendChild(this.adContainer);
}
loadScript(e, t) {
return t ? new Promise((n, r) => {
const o = document.createElement("script");
o.src = e, o.onload = () => n(), o.onerror = () => r(new Error(`Failed to load script: ${e}`)), t.appendChild(o);
}) : Promise.reject(new Error("Container is empty"));
}
injectCss(e) {
if (!document.getElementById("ad-sdk-styles")) {
const t = document.createElement("style");
t.id = "ad-sdk-styles", t.textContent = e, document.head.appendChild(t);
}
}
}
export {
E as AdSdkWeb
};