UNPKG

richpartners-telegram-sdk

Version:
271 lines (270 loc) 14.9 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.InterstitialVideoAds = void 0; const index_js_1 = require("../services/index.js"); const index_js_2 = require("../config/index.js"); const index_js_3 = require("../types/index.js"); const index_js_4 = require("./index.js"); class InterstitialVideoAds extends index_js_4.BaseAds { constructor() { super(...arguments); this.waitShowed = false; this.isPause = false; this.videoLink = null; this.config = { INTERSTITIAL_VIDEO_IMPRESSION_DELAY: 30, INTERSTITIAL_VIDEO_IMPRESSION_INTERVAL: 86400, INTERSTITIAL_VIDEO_LIMIT_IMPRESSION_PER_INTERVAL: 20, INTERSTITIAL_VIDEO_RESTART_LIMIT_IMPRESSIONS: false, INTERSTITIAL_VIDEO_CLOSABLE_WITHOUT_VIEW: false, INTERSTITIAL_VIDEO_SSP_ID: 14862, INTERSTITIAL_VIDEO_AD_DURATION: 10, }; this.togglePlayPause = () => { let videoBlock = document.getElementById('adVideo'); ; const videoPlaceholder = document.getElementById('videoPlaceholder'); if (videoBlock.paused) { videoBlock.play(); this.isPause = false; videoPlaceholder.style.display = 'none'; } else { videoBlock.pause(); this.isPause = true; videoPlaceholder.style.display = 'block'; this.openAdLink(this.videoLink); } }; } getSsp() { return this.config.INTERSTITIAL_VIDEO_SSP_ID; } getType() { return index_js_3.WidgetType.INTERSTITIAL_VIDEO; } getConfigTypeName() { return 'config-interstitial-video'; } handleTrigger(autoRedirect) { return new Promise((resolve, reject) => { this.initialize(); const updatedRequestData = { ...this.requestData, motivated: true, widget_id: this.widgetId, bid_floor: this.getDefaultBidFloor(), number_of_bids: 1, }; this.adRequestService.fetchAds(updatedRequestData, this.getSsp(), (data) => { if (data.length === 0) { reject(new Error('Ads not found')); } this.displayInterstitialVideoAds(data); document.addEventListener('click', function (event) { const target = event.target.closest('.telegram-interstitial-video-ad-overlay'); if (target) { resolve('success'); } }); }).catch(reject); }); } handle() { this.initialize(); const updatedRequestData = { ...this.requestData, motivated: false, widget_id: this.widgetId, bid_floor: this.getDefaultBidFloor(), number_of_bids: 1, }; document.addEventListener('click', (e) => { if (this.isNeededIgnoreClickByEvent(e)) { return; } if (this.isNeedFetchAds()) { this.waitShowed = true; this.adRequestService.fetchAds(updatedRequestData, this.getSsp(), (data) => { if (data.length > 0) { this.displayInterstitialVideoAds(data); this.incrementInterstitialBannerCap(data.length); } this.waitShowed = false; }); } }); } displayInterstitialVideoAds(data) { if (data.length > 0) { this.getOrCreateInterstitialVideoContainer(); } data.forEach((item) => { this.showInterstitialVideoAds(item); }); } getOrCreateInterstitialVideoContainer() { const existingInterstitialBanner = document.getElementById('telegram-interstitial-video-content'); existingInterstitialBanner?.remove(); const interstitialVideoBlock = document.createElement('div'); const closeBlock = document.createElement('div'); interstitialVideoBlock.id = 'telegram-interstitial-video-content'; interstitialVideoBlock.className = 'telegram-interstitial-video-container'; closeBlock.id = this.getIdCloseButton(); closeBlock.className = 'telegram-interstitial-video-close-btn'; closeBlock.addEventListener('click', () => interstitialVideoBlock.remove()); closeBlock.style.display = 'none'; interstitialVideoBlock.appendChild(closeBlock); document.body.appendChild(interstitialVideoBlock); return interstitialVideoBlock; } showInterstitialVideoAds(adInterstitialVideo) { const container = document.getElementById('telegram-interstitial-video-content'); if (!container) { throw new Error('Container telegram-interstitial-video-content not found.'); } const interstitialVideo = document.createElement('div'); let interstitialVideoId = 'interstitial-video-' + Math.random().toString(36).substr(2, 9); let countdown = this.config.INTERSTITIAL_VIDEO_AD_DURATION; this.videoLink = adInterstitialVideo.link; interstitialVideo.innerHTML = ` <div class="telegram-interstitial-video-ad-overlay" id="${interstitialVideoId}"> <div id="videoTimer" class="telegram-interstitial-video-timer">10</div> <div class="telegram-interstitial-video-ad-container"> <img src="${adInterstitialVideo.icon}" class="telegram-interstitial-video-icon" alt="telegram-interstitial-video-avatar"> <div class="telegram-interstitial-video-ad-content-link"> <div class="telegram-interstitial-video-ad-block"> <div class="telegram-interstitial-video-block"> <video id="adVideo" class="telegram-interstitial-video-ad-image" playsinline muted autoplay> <source src="${adInterstitialVideo.video}" type="video/mp4"> </video> <div id="videoPlaceholder" class="video-placeholder"> <button id="playButton">▶</button> </div> <button id="muteToggle" class="video-mute-button">🔇</button> </div> <div class="telegram-interstitial-video-ad-header"> <p class="telegram-interstitial-video-ad-title">${adInterstitialVideo.title}</p> </div> <div class="telegram-interstitial-video-ad-message"> <p>${adInterstitialVideo.message}</p> </div> <button id="closeAd" disabled> Close in ${countdown}</button> <div class="telegram-interstitial-video-ad-tail"></div> </div> <div class="telegram-interstitial-video-ad-button">${adInterstitialVideo.button}</div> </div> </div> </div> `; container.appendChild(interstitialVideo); let videoBlock = document.getElementById('adVideo'); const muteToggle = document.getElementById('muteToggle'); const closeAdButton = document.getElementById('closeAd'); const videoPlaceholder = document.getElementById('videoPlaceholder'); const timerElement = document.getElementById('videoTimer'); const closeButton = document.querySelector('.telegram-interstitial-video-close-btn'); const videoAdButton = document.querySelector('.telegram-interstitial-video-ad-button'); const timer = setInterval(() => { if (this.isPause) { return; } countdown--; timerElement.textContent = countdown.toString(); closeAdButton.textContent = `Close in ${countdown}`; if (countdown <= 0) { clearInterval(timer); closeAdButton.style.display = 'none'; closeButton.style.display = 'flex'; } }, 1000); if (videoBlock) { muteToggle.addEventListener('click', () => { videoBlock.muted = !videoBlock.muted; muteToggle.textContent = videoBlock.muted ? '🔇' : '🔊'; }); closeAdButton.addEventListener('click', () => { container.remove(); }); videoAdButton.addEventListener('click', () => { this.openAdLink(this.videoLink); if (countdown <= 0) { container.remove(); } }); videoBlock.play().then(r => { videoPlaceholder.style.display = 'none'; }).catch(() => console.log("Autoplay disabled")); videoBlock.addEventListener("click", this.togglePlayPause); videoPlaceholder.addEventListener("click", this.togglePlayPause); } } isCapped() { if (this.waitShowed) { return true; } let notificationLimit = Math.floor(index_js_1.LS.get(index_js_2.LOCAL_STORAGE_KEYS.PUSH_STYLE_CAP)) ?? 0; return (this.config.INTERSTITIAL_VIDEO_LIMIT_IMPRESSION_PER_INTERVAL <= notificationLimit) || index_js_1.LS.get(index_js_2.LOCAL_STORAGE_KEYS.PUSH_STYLE_EXPIRES_CAP) === 1; } isNeedFetchAds() { return this.widgetManager?.getDebug() === true || !this.isCapped(); } incrementInterstitialBannerCap(showedItem = 0) { const capOptions = { capKey: index_js_2.LOCAL_STORAGE_KEYS.INTERSTITIAL_VIDEO_CAP, expiresCapKey: index_js_2.LOCAL_STORAGE_KEYS.INTERSTITIAL_VIDEO_EXPIRES_CAP, capIntervalKey: index_js_2.LOCAL_STORAGE_KEYS.INTERSTITIAL_VIDEO_CAP_PER_INTERVAL, delay: this.config.INTERSTITIAL_VIDEO_IMPRESSION_DELAY, interval: this.config.INTERSTITIAL_VIDEO_LIMIT_IMPRESSION_PER_INTERVAL, showedItem: showedItem, }; this.incrementCap(capOptions); } isConfigInstalledInLocalStorage() { return (index_js_1.LS.get(index_js_2.LOCAL_STORAGE_KEYS.INTERSTITIAL_VIDEO_IMPRESSION_DELAY) && index_js_1.LS.get(index_js_2.LOCAL_STORAGE_KEYS.INTERSTITIAL_VIDEO_IMPRESSION_INTERVAL) && index_js_1.LS.get(index_js_2.LOCAL_STORAGE_KEYS.INTERSTITIAL_VIDEO_LIMIT_IMPRESSION_PER_INTERVAL) && index_js_1.LS.get(index_js_2.LOCAL_STORAGE_KEYS.INTERSTITIAL_VIDEO_AD_DURATION) && index_js_1.LS.get(index_js_2.LOCAL_STORAGE_KEYS.INTERSTITIAL_VIDEO_SSP_ID) && index_js_1.LS.get(index_js_2.LOCAL_STORAGE_KEYS.INTERSTITIAL_VIDEO_RESTART_LIMIT_IMPRESSIONS) !== undefined && index_js_1.LS.get(index_js_2.LOCAL_STORAGE_KEYS.INTERSTITIAL_VIDEO_CLOSABLE_WITHOUT_VIEW) !== undefined); } loadConfigByLocalStorage() { this.config.INTERSTITIAL_VIDEO_IMPRESSION_DELAY = Number(index_js_1.LS.get(index_js_2.LOCAL_STORAGE_KEYS.INTERSTITIAL_VIDEO_IMPRESSION_DELAY)); this.config.INTERSTITIAL_VIDEO_IMPRESSION_INTERVAL = Number(index_js_1.LS.get(index_js_2.LOCAL_STORAGE_KEYS.INTERSTITIAL_VIDEO_IMPRESSION_INTERVAL)); this.config.INTERSTITIAL_VIDEO_LIMIT_IMPRESSION_PER_INTERVAL = Number(index_js_1.LS.get(index_js_2.LOCAL_STORAGE_KEYS.INTERSTITIAL_VIDEO_LIMIT_IMPRESSION_PER_INTERVAL)); this.config.INTERSTITIAL_VIDEO_RESTART_LIMIT_IMPRESSIONS = index_js_1.LS.get(index_js_2.LOCAL_STORAGE_KEYS.INTERSTITIAL_VIDEO_RESTART_LIMIT_IMPRESSIONS) === true; this.config.INTERSTITIAL_VIDEO_CLOSABLE_WITHOUT_VIEW = index_js_1.LS.get(index_js_2.LOCAL_STORAGE_KEYS.INTERSTITIAL_VIDEO_CLOSABLE_WITHOUT_VIEW) === true; this.config.INTERSTITIAL_VIDEO_SSP_ID = Number(index_js_1.LS.get(index_js_2.LOCAL_STORAGE_KEYS.INTERSTITIAL_VIDEO_SSP_ID)); this.config.INTERSTITIAL_VIDEO_AD_DURATION = Number(index_js_1.LS.get(index_js_2.LOCAL_STORAGE_KEYS.INTERSTITIAL_VIDEO_AD_DURATION)); } updateConfigParams(telegramConfig) { let customConfig = telegramConfig.widget[this.getType()]; if (customConfig) { if (customConfig.ssp_id) { this.config.INTERSTITIAL_VIDEO_SSP_ID = customConfig.ssp_id ?? this.config.INTERSTITIAL_VIDEO_SSP_ID; } if (this.getConfigTypeName() in customConfig) { let item = customConfig[this.getConfigTypeName()]; this.config.INTERSTITIAL_VIDEO_IMPRESSION_DELAY = Number(item.impression_delay); this.config.INTERSTITIAL_VIDEO_IMPRESSION_INTERVAL = Number(item.impression_interval); this.config.INTERSTITIAL_VIDEO_LIMIT_IMPRESSION_PER_INTERVAL = Number(item.limit_impression_per_interval); this.config.INTERSTITIAL_VIDEO_RESTART_LIMIT_IMPRESSIONS = item.restart_limit_impressions ?? this.config.INTERSTITIAL_VIDEO_RESTART_LIMIT_IMPRESSIONS; this.config.INTERSTITIAL_VIDEO_CLOSABLE_WITHOUT_VIEW = item.closable_without_view_available ?? this.config.INTERSTITIAL_VIDEO_CLOSABLE_WITHOUT_VIEW; this.config.INTERSTITIAL_VIDEO_AD_DURATION = Number(item.ad_duration) ?? this.config.INTERSTITIAL_VIDEO_AD_DURATION; } } this.setLocalStorageConfig(); } setLocalStorageConfig() { index_js_1.LS.set(index_js_2.LOCAL_STORAGE_KEYS.INTERSTITIAL_VIDEO_IMPRESSION_INTERVAL, this.config.INTERSTITIAL_VIDEO_IMPRESSION_INTERVAL); index_js_1.LS.set(index_js_2.LOCAL_STORAGE_KEYS.INTERSTITIAL_VIDEO_IMPRESSION_DELAY, this.config.INTERSTITIAL_VIDEO_IMPRESSION_DELAY); index_js_1.LS.set(index_js_2.LOCAL_STORAGE_KEYS.INTERSTITIAL_VIDEO_LIMIT_IMPRESSION_PER_INTERVAL, this.config.INTERSTITIAL_VIDEO_LIMIT_IMPRESSION_PER_INTERVAL); index_js_1.LS.set(index_js_2.LOCAL_STORAGE_KEYS.INTERSTITIAL_VIDEO_RESTART_LIMIT_IMPRESSIONS, this.config.INTERSTITIAL_VIDEO_RESTART_LIMIT_IMPRESSIONS); index_js_1.LS.set(index_js_2.LOCAL_STORAGE_KEYS.INTERSTITIAL_VIDEO_SSP_ID, this.config.INTERSTITIAL_VIDEO_SSP_ID); index_js_1.LS.set(index_js_2.LOCAL_STORAGE_KEYS.INTERSTITIAL_VIDEO_CLOSABLE_WITHOUT_VIEW, this.config.INTERSTITIAL_VIDEO_CLOSABLE_WITHOUT_VIEW); index_js_1.LS.set(index_js_2.LOCAL_STORAGE_KEYS.INTERSTITIAL_VIDEO_AD_DURATION, this.config.INTERSTITIAL_VIDEO_AD_DURATION); } } exports.InterstitialVideoAds = InterstitialVideoAds; //# sourceMappingURL=InterstitialVideoAds.js.map