richpartners-telegram-sdk
Version:
richPartners ads telegram
269 lines (267 loc) • 15.6 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.InterstitialBannerAds = 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 InterstitialBannerAds extends index_js_4.BaseAds {
constructor() {
super(...arguments);
this.waitShowed = false;
this.config = {
INTERSTITIAL_BANNER_IMPRESSION_DELAY: 15,
INTERSTITIAL_BANNER_IMPRESSION_INTERVAL: 86400,
INTERSTITIAL_BANNER_LIMIT_IMPRESSION_PER_INTERVAL: 20,
INTERSTITIAL_BANNER_RESTART_LIMIT_IMPRESSIONS: false,
INTERSTITIAL_BANNER_CLOSABLE_WITHOUT_VIEW: true,
INTERSTITIAL_BANNER_SSP_ID: 14657,
INTERSTITIAL_BANNER_AD_DURATION: 10,
INTERSTITIAL_BANNER_WIDTH: 1,
INTERSTITIAL_BANNER_HEIGHT: 1,
};
}
getSsp() {
return this.config.INTERSTITIAL_BANNER_SSP_ID;
}
getType() {
return index_js_3.WidgetType.INTERSTITIAL_BANNER;
}
handleTrigger(autoRedirect) {
return new Promise((resolve, reject) => {
this.initialize();
const updatedRequestData = {
...this.requestData,
motivated: true,
widget_id: this.widgetId,
bid_floor: this.getDefaultBidFloor(),
width: this.config.INTERSTITIAL_BANNER_WIDTH,
height: this.config.INTERSTITIAL_BANNER_HEIGHT,
number_of_bids: 1,
};
this.adRequestService.fetchAds(updatedRequestData, this.getSsp(), (data) => {
if (data.length === 0) {
reject(new Error('Ads not found'));
}
this.displayInterstitialBannerAds(data);
this.startTimer(this.config.INTERSTITIAL_BANNER_AD_DURATION);
const interstitialBanner = document.querySelector('.telegram-interstitial-banner-ad-overlay');
if (autoRedirect && interstitialBanner) {
const image = interstitialBanner.querySelector('.telegram-interstitial-banner-ad-image');
const adLink = interstitialBanner.querySelector('.telegram-interstitial-banner-ad-content-link');
if (image && adLink && adLink.href) {
image.onload = () => {
const container = document.getElementById('telegram-interstitial-banner-content');
container.remove();
try {
this.openAdLink(adLink.href);
resolve('success');
}
catch (e) {
reject(new Error("New tab blocked"));
}
};
}
else {
reject(new Error("Missing ad image or link"));
}
}
document.addEventListener('click', function (event) {
const target = event.target.closest('.telegram-interstitial-banner-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(),
width: this.config.INTERSTITIAL_BANNER_WIDTH,
height: this.config.INTERSTITIAL_BANNER_HEIGHT,
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.displayInterstitialBannerAds(data);
this.incrementInterstitialBannerCap(data.length);
}
this.waitShowed = false;
});
}
});
}
isCapped() {
if (this.waitShowed) {
return true;
}
let notificationLimit = Math.floor(index_js_1.LS.get(index_js_2.LOCAL_STORAGE_KEYS.INTERSTITIAL_BANNER_CAP)) ?? 0;
return (this.config.INTERSTITIAL_BANNER_LIMIT_IMPRESSION_PER_INTERVAL <= notificationLimit) || index_js_1.LS.get(index_js_2.LOCAL_STORAGE_KEYS.INTERSTITIAL_BANNER_EXPIRES_CAP) === 1;
}
incrementInterstitialBannerCap(showedItem = 0) {
const capOptions = {
capKey: index_js_2.LOCAL_STORAGE_KEYS.INTERSTITIAL_BANNER_CAP,
expiresCapKey: index_js_2.LOCAL_STORAGE_KEYS.INTERSTITIAL_BANNER_EXPIRES_CAP,
capIntervalKey: index_js_2.LOCAL_STORAGE_KEYS.INTERSTITIAL_BANNER_CAP_PER_INTERVAL,
delay: this.config.INTERSTITIAL_BANNER_IMPRESSION_DELAY,
interval: this.config.INTERSTITIAL_BANNER_LIMIT_IMPRESSION_PER_INTERVAL,
showedItem: showedItem,
};
this.incrementCap(capOptions);
}
isNeedFetchAds() {
return this.widgetManager?.getDebug() === true || !this.isCapped();
}
displayInterstitialBannerAds(data) {
if (data.length > 0) {
this.getOrCreateInterstitialBannerContainer();
}
data.forEach((item) => {
this.showInterstitialBannerAds(item);
});
}
getOrCreateInterstitialBannerContainer() {
const existingInterstitialBanner = document.getElementById('telegram-interstitial-banner-content');
existingInterstitialBanner?.remove();
const interstitialBannerBlock = document.createElement('div');
const closeBlock = document.createElement('div');
interstitialBannerBlock.id = 'telegram-interstitial-banner-content';
interstitialBannerBlock.className = 'telegram-interstitial-banner-container';
if (this.config.INTERSTITIAL_BANNER_CLOSABLE_WITHOUT_VIEW) {
closeBlock.id = this.getIdCloseButton();
closeBlock.className = 'telegram-interstitial-banner-close-btn';
closeBlock.addEventListener('click', () => interstitialBannerBlock.remove());
interstitialBannerBlock.appendChild(closeBlock);
}
document.body.appendChild(interstitialBannerBlock);
return interstitialBannerBlock;
}
startTimer(totalSeconds) {
let closeButton = document.querySelector(".telegram-interstitial-banner-close-btn");
let timerElement = document.querySelector(".telegram-interstitial-banner-timer");
timerElement.style.display = "block";
closeButton.style.display = "none";
function updateTimerDisplay(secondsLeft) {
let minutes = Math.floor(secondsLeft / 60);
let seconds = secondsLeft % 60;
timerElement.textContent = `${minutes}:${seconds.toString().padStart(2, '0')}`;
}
let timeLeft = totalSeconds;
updateTimerDisplay(timeLeft);
let countdown = setInterval(function () {
timeLeft--;
updateTimerDisplay(timeLeft);
if (timeLeft <= 0) {
clearInterval(countdown);
timerElement.style.display = "none";
closeButton.style.display = "flex";
}
}, 1000);
}
showInterstitialBannerAds(adInterstitialBanner) {
const container = document.getElementById('telegram-interstitial-banner-content');
if (!container) {
throw new Error('Container telegram-notification-content not found.');
}
const interstitialBanner = document.createElement('div');
let interstitialBannerId = 'interstitial-banner-' + Math.random().toString(36).substr(2, 9);
interstitialBanner.innerHTML = `
<div class="telegram-interstitial-banner-ad-overlay" id="${interstitialBannerId}">
<div class="telegram-interstitial-banner-timer"></div>
<div class="telegram-interstitial-banner-ad-container">
<img src="${adInterstitialBanner.icon}" class="telegram-interstitial-banner-icon" alt="telegram-interstitial-banner-avatar">
<a class="telegram-interstitial-banner-ad-content-link" href="${adInterstitialBanner.link}" target="_blank">
<div class="telegram-interstitial-banner-ad-block">
<img src="${adInterstitialBanner.banner}" class="telegram-interstitial-banner-ad-image" alt="telegram-interstitial-banner-banner">
<div class="telegram-interstitial-banner-ad-header">
<p class="telegram-interstitial-banner-ad-title">${adInterstitialBanner.title}</p>
</div>
<div class="telegram-interstitial-banner-ad-message">
<p>${adInterstitialBanner.message}</p>
</div>
<div class="telegram-interstitial-banner-ad-brand">
<p>${adInterstitialBanner.brand}</p>
</div>
<div class="telegram-interstitial-banner-ad-tail"></div>
</div>
<div class="telegram-interstitial-banner-ad-button">${adInterstitialBanner.button}</div>
</a>
</div>
</div>
`;
container.appendChild(interstitialBanner);
const adContent = document.querySelector('.telegram-interstitial-banner-ad-content-link');
if (!adContent) {
throw new Error('element telegram-interstitial-banner-ad-content-link not found.');
}
adContent.addEventListener('click', (event) => {
container.remove();
});
}
isConfigInstalledInLocalStorage() {
return (index_js_1.LS.get(index_js_2.LOCAL_STORAGE_KEYS.INTERSTITIAL_BANNER_IMPRESSION_DELAY)
&& index_js_1.LS.get(index_js_2.LOCAL_STORAGE_KEYS.INTERSTITIAL_BANNER_IMPRESSION_INTERVAL)
&& index_js_1.LS.get(index_js_2.LOCAL_STORAGE_KEYS.INTERSTITIAL_BANNER_LIMIT_IMPRESSION_PER_INTERVAL)
&& index_js_1.LS.get(index_js_2.LOCAL_STORAGE_KEYS.INTERSTITIAL_BANNER_WIDTH)
&& index_js_1.LS.get(index_js_2.LOCAL_STORAGE_KEYS.INTERSTITIAL_BANNER_HEIGHT)
&& index_js_1.LS.get(index_js_2.LOCAL_STORAGE_KEYS.INTERSTITIAL_BANNER_AD_DURATION)
&& index_js_1.LS.get(index_js_2.LOCAL_STORAGE_KEYS.INTERSTITIAL_BANNER_SSP_ID)
&& index_js_1.LS.get(index_js_2.LOCAL_STORAGE_KEYS.INTERSTITIAL_BANNER_RESTART_LIMIT_IMPRESSIONS) !== undefined
&& index_js_1.LS.get(index_js_2.LOCAL_STORAGE_KEYS.INTERSTITIAL_BANNER_CLOSABLE_WITHOUT_VIEW) !== undefined);
}
loadConfigByLocalStorage() {
this.config.INTERSTITIAL_BANNER_IMPRESSION_DELAY = Number(index_js_1.LS.get(index_js_2.LOCAL_STORAGE_KEYS.INTERSTITIAL_BANNER_IMPRESSION_DELAY));
this.config.INTERSTITIAL_BANNER_IMPRESSION_INTERVAL = Number(index_js_1.LS.get(index_js_2.LOCAL_STORAGE_KEYS.INTERSTITIAL_BANNER_IMPRESSION_INTERVAL));
this.config.INTERSTITIAL_BANNER_LIMIT_IMPRESSION_PER_INTERVAL = Number(index_js_1.LS.get(index_js_2.LOCAL_STORAGE_KEYS.INTERSTITIAL_BANNER_LIMIT_IMPRESSION_PER_INTERVAL));
this.config.INTERSTITIAL_BANNER_RESTART_LIMIT_IMPRESSIONS = index_js_1.LS.get(index_js_2.LOCAL_STORAGE_KEYS.INTERSTITIAL_BANNER_RESTART_LIMIT_IMPRESSIONS) === 'true';
this.config.INTERSTITIAL_BANNER_CLOSABLE_WITHOUT_VIEW = index_js_1.LS.get(index_js_2.LOCAL_STORAGE_KEYS.INTERSTITIAL_BANNER_CLOSABLE_WITHOUT_VIEW);
this.config.INTERSTITIAL_BANNER_SSP_ID = Number(index_js_1.LS.get(index_js_2.LOCAL_STORAGE_KEYS.INTERSTITIAL_BANNER_SSP_ID));
this.config.INTERSTITIAL_BANNER_AD_DURATION = Number(index_js_1.LS.get(index_js_2.LOCAL_STORAGE_KEYS.INTERSTITIAL_BANNER_AD_DURATION));
this.config.INTERSTITIAL_BANNER_WIDTH = Number(index_js_1.LS.get(index_js_2.LOCAL_STORAGE_KEYS.INTERSTITIAL_BANNER_WIDTH));
this.config.INTERSTITIAL_BANNER_HEIGHT = Number(index_js_1.LS.get(index_js_2.LOCAL_STORAGE_KEYS.INTERSTITIAL_BANNER_HEIGHT));
}
updateConfigParams(telegramConfig) {
let customConfig = telegramConfig.widget[this.getType()];
if (customConfig) {
if (customConfig.ssp_id) {
this.config.INTERSTITIAL_BANNER_SSP_ID = customConfig.ssp_id ?? this.config.INTERSTITIAL_BANNER_SSP_ID;
}
if ('config-interstitial-banner' in customConfig) {
let item = customConfig['config-interstitial-banner'];
this.config.INTERSTITIAL_BANNER_IMPRESSION_DELAY = Number(item.impression_delay);
this.config.INTERSTITIAL_BANNER_IMPRESSION_INTERVAL = Number(item.impression_interval);
this.config.INTERSTITIAL_BANNER_LIMIT_IMPRESSION_PER_INTERVAL = Number(item.limit_impression_per_interval);
this.config.INTERSTITIAL_BANNER_RESTART_LIMIT_IMPRESSIONS = item.restart_limit_impressions;
this.config.INTERSTITIAL_BANNER_CLOSABLE_WITHOUT_VIEW = item.closable_without_view_available ?? this.config.INTERSTITIAL_BANNER_CLOSABLE_WITHOUT_VIEW;
this.config.INTERSTITIAL_BANNER_AD_DURATION = Number(item.ad_duration);
this.config.INTERSTITIAL_BANNER_WIDTH = Number(item.banner_width);
this.config.INTERSTITIAL_BANNER_HEIGHT = Number(item.banner_height);
}
}
this.setLocalStorageConfig();
}
setLocalStorageConfig() {
index_js_1.LS.set(index_js_2.LOCAL_STORAGE_KEYS.INTERSTITIAL_BANNER_IMPRESSION_INTERVAL, this.config.INTERSTITIAL_BANNER_IMPRESSION_INTERVAL);
index_js_1.LS.set(index_js_2.LOCAL_STORAGE_KEYS.INTERSTITIAL_BANNER_IMPRESSION_DELAY, this.config.INTERSTITIAL_BANNER_IMPRESSION_DELAY);
index_js_1.LS.set(index_js_2.LOCAL_STORAGE_KEYS.INTERSTITIAL_BANNER_LIMIT_IMPRESSION_PER_INTERVAL, this.config.INTERSTITIAL_BANNER_LIMIT_IMPRESSION_PER_INTERVAL);
index_js_1.LS.set(index_js_2.LOCAL_STORAGE_KEYS.INTERSTITIAL_BANNER_RESTART_LIMIT_IMPRESSIONS, this.config.INTERSTITIAL_BANNER_RESTART_LIMIT_IMPRESSIONS);
index_js_1.LS.set(index_js_2.LOCAL_STORAGE_KEYS.INTERSTITIAL_BANNER_SSP_ID, this.config.INTERSTITIAL_BANNER_SSP_ID);
index_js_1.LS.set(index_js_2.LOCAL_STORAGE_KEYS.INTERSTITIAL_BANNER_CLOSABLE_WITHOUT_VIEW, this.config.INTERSTITIAL_BANNER_CLOSABLE_WITHOUT_VIEW);
index_js_1.LS.set(index_js_2.LOCAL_STORAGE_KEYS.INTERSTITIAL_BANNER_AD_DURATION, this.config.INTERSTITIAL_BANNER_AD_DURATION);
index_js_1.LS.set(index_js_2.LOCAL_STORAGE_KEYS.INTERSTITIAL_BANNER_WIDTH, this.config.INTERSTITIAL_BANNER_WIDTH);
index_js_1.LS.set(index_js_2.LOCAL_STORAGE_KEYS.INTERSTITIAL_BANNER_HEIGHT, this.config.INTERSTITIAL_BANNER_HEIGHT);
}
}
exports.InterstitialBannerAds = InterstitialBannerAds;
//# sourceMappingURL=InterstitialBannerAds.js.map