Перейти к основному контенту
(function () {
if (window.__AT_TIMERS_FIXED__) return;
window.__AT_TIMERS_FIXED__ = true;
const MOBILE_MAX = 767;
const CYCLE_DAYS = 3;
const CYCLE_SECONDS = CYCLE_DAYS * 24 * 60 * 60;
const isMobile = () => window.innerWidth <= MOBILE_MAX;
// === CSS (КОЛЬОРИ НЕ МІНЯЮ) ===
const css = `
/* ===== catalogCard-main (жовтий/синій) ===== */
.discount-timer{
display:flex;
align-items:center;
gap:15%;
margin:6px 0 6px;
font-size:13px;
line-height:1.25;
font-weight:600;
color:#111;
}
.discount-timer__badge{
padding:4px 8px;
border-radius:999px;
background:#d32f2f;
color:#fff;
font-weight:800;
font-size:14px;
box-shadow:0 2px 6px rgba(0,0,0,.25);
white-space:nowrap;
}
.discount-timer__clock{
margin-left:28%;
display:flex;
align-items:center;
gap:4px;
padding:4px 8px;
border-radius:999px;
background:#ffeb3b !important;
color:#0b6cff !important;
font-family:monospace;
font-size:13px;
box-shadow:0 2px 6px rgba(0,0,0,.25);
white-space:nowrap;
}
.discount-timer__icon{ font-size:14px; }
.discount-timer__time{ font-weight:700; }
@media(max-width:767px){
.discount-timer{ font-size:12px; gap:10%; margin:4px 0; margin-left:0; }
.discount-timer__badge{ font-size:12px; padding:3px 7px; }
.discount-timer__clock{ padding:3px 7px; font-size:12px; }
}
/* ===== product page ===== */
.product-discount-box{
margin-top:10px;
margin-bottom:14px;
display:flex;
align-items:center;
gap:12px;
}
.discount-timer{
display:flex;
align-items:center;
gap:10px;
font-size:15px;
font-weight:600;
color:#111;
}
.discount-timer__badge{
background:#d32f2f;
color:#fff;
padding:4px 10px;
border-radius:999px;
font-size:15px;
font-weight:800;
}
.discount-timer__clock{
margin-left:28%;
background:#1f2937;
padding:4px 10px;
border-radius:999px;
color:#e5e7eb;
font-family:monospace;
font-size:15px;
}
/* ===== catalog-card ===== */
.discount-timer{
display:flex;
align-items:center;
margin:6px 0 6px;
font-size:13px;
line-height:1.25;
font-weight:600;
color:#111;
width:100%;
}
.discount-timer__badge{
padding:4px 8px;
border-radius:999px;
background:#d32f2f;
color:#fff;
font-weight:800;
font-size:14px;
box-shadow:0 2px 6px rgba(0,0,0,.25);
white-space:nowrap;
}
.discount-timer__clock{
margin-left:auto;
margin-right:25%;
display:flex;
align-items:center;
padding:4px 8px;
border-radius:999px;
background:#111827;
color:#e5e7eb;
font-family:monospace;
font-size:13px;
box-shadow:0 2px 6px rgba(0,0,0,.25);
white-space:nowrap;
}
.discount-timer__time{ font-weight:700; }
@media(max-width:767px){
.discount-timer{ font-size:12px; margin:4px 0; }
.discount-timer__badge{ font-size:12px; padding:3px 7px; }
.discount-timer__clock{ padding:3px 7px; font-size:12px; margin-right:25%; }
}
/* ===== product-card mobile ===== */
.product-discount-box{
margin-top:8px;
margin-bottom:10px;
display:flex;
align-items:center;
gap:12px;
}
.discount-timer__clock{
background:#1f2937;
padding:4px 10px;
border-radius:999px;
color:#e5e7eb;
font-family:monospace;
font-size:15px;
}
/* ===== brands button ===== */
@media(min-width:768px){
.news__all--brands { display:none; }
}
`;
function ensureStyles() {
if (document.getElementById("at-discount-style-fixed")) return;
const style = document.createElement("style");
style.id = "at-discount-style-fixed";
style.textContent = css;
document.head.appendChild(style);
}
function parsePrice(text) {
const n = parseInt(String(text || "").replace(/\D/g, ""), 10);
return Number.isFinite(n) ? n : NaN;
}
// === 3-денний цикл від сьогодні 00:00 ===
function getCycleStart() {
const now = new Date();
return new Date(now.getFullYear(), now.getMonth(), now.getDate(), 0, 0, 0, 0);
}
function getCycleEnd(start) {
return new Date(start.getTime() + CYCLE_SECONDS * 1000);
}
let cycleStart = getCycleStart();
let cycleEnd = getCycleEnd(cycleStart);
function secondsLeft() {
const now = new Date();
if (now >= cycleEnd) {
cycleStart = getCycleStart();
cycleEnd = getCycleEnd(cycleStart);
}
let sec = Math.floor((cycleEnd - now) / 1000);
if (sec < 0) sec = 0;
return sec;
}
function formatTime(sec) {
if (sec <= 0) return "00:00:00";
if (sec >= 86400) return Math.ceil(sec / 86400) + " дні";
const h = Math.floor(sec / 3600);
const m = Math.floor((sec % 3600) / 60);
const s = sec % 60;
return [h, m, s].map(v => String(v).padStart(2, "0")).join(":");
}
// === MOUNTS (вставляємо лише якщо ще не вставлено) ===
function mountCatalogCardMainTimers() {
const cards = document.querySelectorAll(".catalogCard-main");
if (!cards.length) return;
cards.forEach(card => {
if (card.querySelector(".discount-timer[data-kind='catalogCard-main']")) return;
const oldEl = card.querySelector(".catalogCard-oldPrice");
const newEl = card.querySelector(".catalogCard-price");
const info = card.querySelector(".catalogCard-info");
const titleEl = info ? info.querySelector(".catalogCard-title") : null;
if (!oldEl || !newEl || !titleEl) return;
const oldPrice = parsePrice(oldEl.textContent);
const newPrice = parsePrice(newEl.textContent);
if (!oldPrice || !newPrice || newPrice >= oldPrice) return;
const discountPercent = Math.round((1 - newPrice / oldPrice) * 100);
const timerWrapper = document.createElement("div");
timerWrapper.className = "discount-timer";
timerWrapper.dataset.kind = "catalogCard-main";
timerWrapper.innerHTML = `
-${discountPercent}%
⏱
`;
titleEl.insertAdjacentElement("afterend", timerWrapper);
});
}
function mountProductPageTimer() {
if (document.querySelector(".product-discount-box[data-kind='product-page']")) return;
const priceBox = document.querySelector(".product-price__box");
if (!priceBox) return;
const newPriceEl = priceBox.querySelector(".product-price__item--new");
const oldPriceEl = priceBox.querySelector(".product-price__old-price");
if (!newPriceEl || !oldPriceEl) return;
const newPrice = parsePrice(newPriceEl.textContent);
const oldPrice = parsePrice(oldPriceEl.textContent);
if (!newPrice || !oldPrice || newPrice >= oldPrice) return;
const discountPercent = Math.round(100 - (newPrice / oldPrice) * 100);
const headerTop = document.querySelector(".product-header__row--top") || document.querySelector("h1");
if (!headerTop) return;
const box = document.createElement("div");
box.className = "product-discount-box";
box.dataset.kind = "product-page";
box.innerHTML = `
-${discountPercent}%
`;
headerTop.insertAdjacentElement("afterend", box);
}
function mountCatalogCardTimers() {
const cards = document.querySelectorAll(".catalog-card");
if (!cards.length) return;
cards.forEach(card => {
if (card.querySelector(".discount-timer[data-kind='catalog-card']")) return;
const newEl = card.querySelector(".catalog-card__price--new");
const oldEl = card.querySelector(".catalog-card__old-price");
const titleEl = card.querySelector(".catalog-card__title");
if (!newEl || !oldEl || !titleEl) return;
const oldPrice = parsePrice(oldEl.textContent);
const newPrice = parsePrice(newEl.textContent);
if (!oldPrice || !newPrice || newPrice >= oldPrice) return;
const discountPercent = Math.round(100 - (newPrice / oldPrice) * 100);
const timerWrapper = document.createElement("div");
timerWrapper.className = "discount-timer";
timerWrapper.dataset.kind = "catalog-card";
timerWrapper.innerHTML = `
-${discountPercent}%
⏱
`;
titleEl.insertAdjacentElement("afterend", timerWrapper);
});
}
function mountProductCardTimer() {
if (!isMobile()) return;
if (document.querySelector(".product-discount-box[data-kind='product-card']")) return;
const priceBox = document.querySelector(".product-card__price-box");
if (!priceBox) return;
const newPriceEl = priceBox.querySelector(".product-card__price--new");
const oldPriceEl = priceBox.querySelector(".product-card__old-price");
if (!newPriceEl || !oldPriceEl) return;
const newPrice = parsePrice(newPriceEl.textContent);
const oldPrice = parsePrice(oldPriceEl.textContent);
if (!newPrice || !oldPrice || newPrice >= oldPrice) return;
const discountPercent = Math.round(100 - (newPrice / oldPrice) * 100);
const bodyEl = document.querySelector(".product-card__body");
if (!bodyEl) return;
const presenceEl = bodyEl.querySelector(".product-card__presence");
const box = document.createElement("div");
box.className = "product-discount-box";
box.dataset.kind = "product-card";
box.innerHTML = `
-${discountPercent}%
`;
if (presenceEl) presenceEl.insertAdjacentElement("afterend", box);
else bodyEl.insertAdjacentElement("afterbegin", box);
}
function mountBrandsButton() {
if (!isMobile()) return;
const brandsWrap = document.querySelector(".brands__wrap");
if (!brandsWrap) return;
if (document.querySelector(".news__all--brands")) return;
const block = document.createElement("div");
block.className = "news__all news__all--brands";
block.innerHTML = `Всі бренди`;
brandsWrap.insertAdjacentElement("afterend", block);
}
// === UPDATE ONLY (без важких операцій) ===
function updateAllTimes() {
const text = formatTime(secondsLeft());
document.querySelectorAll(".discount-timer__time").forEach(el => {
el.textContent = text;
});
}
// === RUN (важке) ===
function runAll() {
ensureStyles();
mountCatalogCardMainTimers();
mountProductPageTimer();
mountCatalogCardTimers();
mountProductCardTimer();
mountBrandsButton();
updateAllTimes();
}
// === BOOT ===
function boot() {
runAll();
// Один глобальний інтервал (не множимо!)
if (!window.__AT_TIMERS_INTERVAL__) {
window.__AT_TIMERS_INTERVAL__ = setInterval(updateAllTimes, 1000);
}
// MutationObserver з тротлінгом, щоб не “молотив” постійно
if (!window.__AT_TIMERS_OBSERVER__) {
let scheduled = false;
const mo = new MutationObserver(() => {
if (scheduled) return;
scheduled = true;
setTimeout(() => {
scheduled = false;
runAll();
}, 200);
});
mo.observe(document.documentElement, { childList: true, subtree: true });
window.__AT_TIMERS_OBSERVER__ = mo;
}
window.addEventListener("resize", () => runAll(), { passive: true });
}
if (document.readyState === "loading") {
document.addEventListener("DOMContentLoaded", boot);
} else {
boot();
}
})();
Команда AstreiaMilitary