UNPKG

richpartners-telegram-sdk

Version:
121 lines 5.01 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.EmbeddedBannerAds = 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 EmbeddedBannerAds extends index_js_4.BaseAds { constructor() { super(...arguments); this.IMPRESSION_INTERVAL = 60; this.config = { EMBEDDED_BANNER_DATA: [], EMBEDDED_BANNER_SSP_ID: 14263, }; } getType() { return index_js_3.WidgetType.EMBEDDED_BANNER; } getSsp() { return this.config.EMBEDDED_BANNER_SSP_ID; } handleTrigger(autoRedirect) { return new Promise((resolve) => { resolve('success'); }); } handle() { this.initialize(); this.config.EMBEDDED_BANNER_DATA.forEach((item) => { item.impression_interval = this.IMPRESSION_INTERVAL; let elements = document.querySelectorAll(item.selector); if (elements.length === 0) return; const updatedRequestData = { ...this.requestData, motivated: false, width: item.banner_width, height: item.banner_height, widget_id: this.widgetId, bid_floor: this.getDefaultBidFloor(), number_of_bids: 1, }; const showAds = () => { this.adRequestService.fetchAds(updatedRequestData, this.getSsp(), (data) => { if (data.length > 0) { this.showAd(data[0], item); } }); }; const hasLimits = (item.limit_impression_per_interval && item.impression_interval && item.impression_delay); if (hasLimits) { this.runAdCycle(showAds, item); } else { showAds(); } }); } runAdCycle(callback, item) { let count = 1; const startTime = Date.now(); callback(); const intervalId = setInterval(() => { const elapsed = Date.now() - startTime; const withinLimit = count < item.limit_impression_per_interval; const withinTime = elapsed <= item.impression_interval * 1000; if (!withinLimit || !withinTime) { clearInterval(intervalId); return; } count++; callback(); }, item.impression_delay * 1000); } showAd(ad, item) { const embeddedBanner = document.querySelector(item.selector); if (!embeddedBanner) { console.warn(`[showAds] Element with selector "${item.selector}" not found.`); return; } let embeddedBannerId = 'banner-' + Math.random().toString(36).substr(2, 9); embeddedBanner.innerHTML = ` <a href="${ad.link}" target="_blank" id="${embeddedBannerId}" class="banner-block-tg"> <img src="${ad.image}" alt="${item.tooltip ?? 'banner'}"> </a> `; if (item.tooltip?.trim()) { embeddedBanner.setAttribute('title', item.tooltip.trim()); } } isConfigInstalledInLocalStorage() { return index_js_1.LS.get(index_js_2.LOCAL_STORAGE_KEYS.EMBEDDED_BANNER_DATA); } loadConfigByLocalStorage() { this.config.EMBEDDED_BANNER_DATA = JSON.parse(index_js_1.LS.get(index_js_2.LOCAL_STORAGE_KEYS.EMBEDDED_BANNER_DATA)); this.config.EMBEDDED_BANNER_SSP_ID = Number(index_js_1.LS.get(index_js_2.LOCAL_STORAGE_KEYS.EMBEDDED_BANNER_SSP_ID)) || this.config.EMBEDDED_BANNER_SSP_ID; } updateConfigParams(telegramConfig) { let customConfig = telegramConfig.widget[this.getType()]; if (customConfig && customConfig.ssp_id) { this.config.EMBEDDED_BANNER_SSP_ID = customConfig.ssp_id ?? this.config.EMBEDDED_BANNER_SSP_ID; } index_js_1.LS.set(index_js_2.LOCAL_STORAGE_KEYS.EMBEDDED_BANNER_DATA, JSON.stringify([])); if (customConfig) { if (customConfig['config-banner']) { index_js_1.LS.set(index_js_2.LOCAL_STORAGE_KEYS.EMBEDDED_BANNER_DATA, JSON.stringify(customConfig['config-banner'])); } else if (customConfig['config-embedded-banner']) { index_js_1.LS.set(index_js_2.LOCAL_STORAGE_KEYS.EMBEDDED_BANNER_DATA, JSON.stringify(customConfig['config-embedded-banner'])); } if (index_js_1.LS.get(index_js_2.LOCAL_STORAGE_KEYS.EMBEDDED_BANNER_DATA)) { this.config.EMBEDDED_BANNER_DATA = JSON.parse(index_js_1.LS.get(index_js_2.LOCAL_STORAGE_KEYS.EMBEDDED_BANNER_DATA)); } } } } exports.EmbeddedBannerAds = EmbeddedBannerAds; //# sourceMappingURL=EmbeddedBannerAds.js.map