@dailymotion/ad-sdk-web
Version:
JavaScript Ad SDK API by Dailymotion
127 lines (126 loc) • 4.37 kB
JavaScript
var s = Object.defineProperty;
var l = (t, e, i) => e in t ? s(t, e, { enumerable: !0, configurable: !0, writable: !0, value: i }) : t[e] = i;
var d = (t, e, i) => l(t, typeof e != "symbol" ? e + "" : e, i);
var o = /* @__PURE__ */ ((t) => (t.AD_BREAK_START = "AD_BREAK_START", t.AD_BREAK_END = "AD_BREAK_END", t.AD_LOAD = "AD_LOAD", t.AD_START = "AD_START", t.AD_PLAY = "AD_PLAY", t.AD_PAUSE = "AD_PAUSE", t.AD_END = "AD_END", t.AD_ERROR = "AD_ERROR", t.CONTENT_PAUSE_REQUESTED = "CONTENT_PAUSE_REQUESTED", t.CONTENT_RESUME_REQUESTED = "CONTENT_RESUME_REQUESTED", t))(o || {});
const A = "https://asw.dmcdn.net/1.0.0/core.js", E = {
useFakeAd: !1,
vmapUrl: ""
};
class p {
constructor() {
d(this, "adVideoElement", null);
d(this, "adContainer", null);
d(this, "playerContainer");
d(this, "scriptLoaded", !1);
/**
* Provides access to the Ad SDK event names.
* Use these constants to subscribe or unsubscribe from ad lifecycle events.
*/
d(this, "Events", o);
this.adVideoElement = null, this.adContainer = null, this.playerContainer = null, this.scriptLoaded = !1;
}
/**
* 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 (i) {
console.error(i), 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, i) {
if (!this.scriptLoaded)
throw new Error("Ad SDK not initialized");
const a = {
adContainer: this.adContainer,
adVideoTag: this.adVideoElement,
appState: e,
developmentOptions: { ...E, ...i }
};
await AdSdk.loadAdsSequence(a);
}
/**
* 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();
}
on(e, i) {
if (!this.scriptLoaded)
throw new Error("Ad SDK not initialized");
AdSdk.on(e, i);
}
off(e, i) {
if (!this.scriptLoaded)
throw new Error("Ad SDK not initialized");
AdSdk.off(e, i);
}
/**
* 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");
AdSdk.updateAppState(e);
}
createAdVideoElement() {
var e;
this.adVideoElement = document.createElement("video"), this.adVideoElement.classList.add("ad-video"), (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, i) {
return i ? new Promise((a, n) => {
const r = document.createElement("script");
r.src = e, r.onload = () => a(), r.onerror = () => n(new Error(`Failed to load script: ${e}`)), i.appendChild(r);
}) : Promise.reject(new Error("Container is empty"));
}
}
export {
p as AdSdkWeb
};