window.onload = () => {
let exchangeRateFetched = false;
function getExchangeRate() {
fetch('https://api.nbrb.by/exrates/rates/USD?parammode=2')
.then(response => response.json())
.then(data => {
const officialRate = data.Cur_OfficialRate;
if (!exchangeRateFetched) {
createNewPriceBlock(officialRate);
exchangeRateFetched = true;
}
})
.catch(error => console.error('Ошибка при получении курса обмена:', error));
}
function getExchangeRatePopup() {
fetch('https://api.nbrb.by/exrates/rates/USD?parammode=2')
.then(response => response.json())
.then(data => {
const officialRate = data.Cur_OfficialRate;
createNewPricePopup(officialRate);
})
.catch(error => console.error('Ошибка при получении курса обмена:', error));
}
function createNewPriceBlock(rate) {
const priceBlocks = document.querySelectorAll('.t-store__card__price');
priceBlocks.forEach(block => {
const blockValue = block.querySelector('.js-store-prod-price-val').textContent;
let newBlock = block.querySelector('.js-new-price');
if (!newBlock) {
console.log('newBlock:', newBlock);
newBlock = document.createElement('div');
newBlock.className = 'js-new-price js-product-price js-store-prod-price-val t-store__card__price-value';
newBlock.style.marginLeft = '20px';
newBlock.classList.add('js-new-price');
block.appendChild(newBlock);
}
const newPrice = (Number(blockValue.replace(/\s/g, '').replace(',', '.')) * rate).toFixed(2);
newBlock.innerHTML = `≈ ${newPrice} BYN`;
});
}
function createNewPricePopup(rate) {
const priceBlocks = document.querySelectorAll('.t-store__prod-popup__price');
priceBlocks.forEach(priceBlock => {
const isPopupHidden = !document.querySelector('.t-popup.t-popup_show');
if (isPopupHidden) {
let newBlock = priceBlock.querySelector('.js-new-popup-price');
if (newBlock) {
newBlock.remove();
}
} else {
const productPrice = priceBlock.querySelector('.js-product-price');
if (productPrice && productPrice.textContent.trim() !== '') {
const blockValue = productPrice.textContent;
let newBlock = priceBlock.querySelector('.js-new-popup-price');
if (!newBlock) {
newBlock = document.createElement('div');
newBlock.className = 'js-product-price js-new-popup-price js-store-prod-price-val t-store__prod-popup__price-value';
newBlock.style.marginLeft = '20px';
newBlock.classList.add('js-new-popup-price');
priceBlock.appendChild(newBlock);
}
const newPrice = (Number(blockValue.replace(/\s/g, '').replace(',', '.')) * rate).toFixed(2);
newBlock.innerHTML = `≈ ${newPrice} BYN`;
}
}
});
}
function setupPopupObserver(popupTarget) {
let isPopupOpen = false;
const popupObserver = new MutationObserver(function (mutations) {
mutations.forEach(function (mutation) {
if (!document.querySelector('.t-popup.t-popup_show')) {
const newBlock = document.querySelector('.js-new-popup-price');
if (newBlock) {
newBlock.remove();
}
isPopupOpen = false;
} else {
if (!isPopupOpen) {
getExchangeRatePopup();
isPopupOpen = true;
}
}
});
});
const popupConfig = { attributes: true, attributeFilter: ['class'] };
popupObserver.observe(popupTarget, popupConfig);
}
// Пример использования для каждого всплывающего окна
const popupTargets = document.querySelectorAll('.t-popup'); // Замените на соответствующий селектор для ваших всплывающих окон
popupTargets.forEach(popupTarget => {
setupPopupObserver(popupTarget);
});
setTimeout(() => {
getExchangeRate();
getExchangeRatePopup();
}, 3001);
window.addEventListener('click', event => {
if (
event.target.closest('.t-checkbox') ||
event.target.closest('.js-store-load-more-btn') ||
event.target.closest('.t-store__filter__chosen-val') ||
event.target.closest('.t-store__filter__reset') ||
event.target.closest('.t-store__filter__custom-sel .t-store__filter__title')
) {
setTimeout(function () {
getExchangeRate();
}, 1000);
}
});
};