örnek çalışan site gösterebilirim özelden ama trendyol da güzel bir örnek.
Trendyol da görmüşsünüzdür
- son 24 saatte X kişi sepete Ekledi
- Son 3 günde XX Kişi Favoriledi
- Son 1 Günde X kişi satın aldı.
bu 3 mesajı 2 sn aralıklarda değişecek şekilce manuel vermek istiyorum, site de ürün detay sayfasına ekleyeceğim. 3 mesaj içinde sabit değişkenler belirleyip x-y arasında sayı üret gibi olabilir.
şu anda buna banzer bir yapıyı ben kullanıyorum ama o sadece XXX Kişi ziyaret etti mesajı veriyor sadece bunu 3 farklı mesaj verecek şekilde yapmak istiyorum.
<script>
(() => {
"use strict";
const CONFIG = {
targetSelector: "#\30 > div > div > div.grid.grid-cols-1.sm\:grid-cols-1.md\:grid-cols-2.lg\:grid-cols-3.xl\:grid-cols-3 > div.product-detail-page-detail-box.relative.col-span-2.sm\:col-span-2.md\:col-span-2.px-4.lg\:col-span-1.xl\:col-span-1 > div.flex.flex-1.product-detail-page-buy-box.mt-4.mb-2.flex-col > div",
widgetId: "bp-viewer-box",
styleId: "bp-viewer-styles",
startMin: 120,
startMax: 480,
clampMin: 45,
clampMax: 980,
stepDownMax: 7,
stepUpMax: 11,
intervalMin: 4000,
intervalMax: 5200
};
/* RNG ve util */
const hashStr = (s) => { let h = 2166136261>>>0; for (let i=0;i<s.length;i++){ h^=s.charCodeAt(i); h=Math.imul(h,16777619);} return h>>>0; };
const makeRNG = (seed) => { let x=(seed||123456789)>>>0; return ()=>{ x^=x<<13; x>>>=0; x^=x>>17; x>>>=0; x^=x<<5; x>>>=0; return (x>>>0)/0xFFFFFFFF; }; };
const between = (rng, min, max) => Math.floor(rng()*(max-min+1))+min;
const productKey = (() => {
const og=document.querySelector('meta[property="og:title"]');
const title=og?.getAttribute("content")||document.title| |"";
return location.origin+location.pathname+"|"+title.trim() ;
})();
const seed=hashStr(productKey);
const rng=makeRNG(seed);
const storeKey="bp_viewers_"+seed;
const initialValue = (() => {
const fromSession=sessionStorage.getItem(storeKey);
if(fromSession) return parseInt(fromSession,10)||CONFIG.startMin;
const v=between(rng,CONFIG.startMin,CONFIG.startMax);
sessionStorage.setItem(storeKey,String(v));
return v;
})();
let _value=initialValue;
const setValue =
_value=Math.max(CONFIG.clampMin,Math.min(CONFIG.cl ampMax,n));
sessionStorage.setItem(storeKey,String(_value));
const span=document.querySelector(`#${CONFIG.widgetId} .bp-viewer-count`);
if(span) span.textContent=String(_value);
};
/* Stil */
const ensureStyles = () => {
if(document.getElementById(CONFIG.styleId)) return;
const style=document.createElement("style");
style.id=CONFIG.styleId;
style.textContent=`
#${CONFIG.widgetId} { width:100% !important; box-sizing:border-box; margin-top:16px; }
.bp-viewer-box {
display:flex; align-items:center; gap:8px;
padding:10px 14px; background:#f8f8f8; border-radius:12px;
font-family:system-ui, -apple-system, Segoe UI, Roboto, Arial, sans-serif;
font-size:16px; font-weight:600; width:100%;
}
.bp-viewer-icon { color:#ff5252; min-width:22px; }
.bp-viewer-count { color:#ff5252; font-weight:700; margin:0 4px; }
@media(max-width:480px){ .bp-viewer-box{ font-size:14px; padding:8px 12px; } }
`;
document.head.appendChild(style);
};
/* Widget oluştur */
const buildWidget = (initial) => {
const box=document.createElement("div");
box.className="bp-viewer-box";
box.id=CONFIG.widgetId;
box.innerHTML=`
<svg class="bp-viewer-icon" xmlns="http://www.w3.org/2000/svg" height="18" viewBox="0 0 24 24" width="18">
<path fill="currentColor" d="M12 6c-5 0-9 3.6-10 6 1 2.4 5 6 10 6s9-3.6 10-6c-1-2.4-5-6-10-6zm0 10c-2.2 0-4-1.8-4-4s1.8-4
4-4 4 1.8 4 4-1.8 4-4 4z"></path>
<circle fill="currentColor" cx="12" cy="12" r="2"></circle>
</svg>
<span><span class="bp-viewer-count">${initial}</span> kişi ürünü görüntülüyor!</span>
`;
return box;
};
const placeWidget = (target) => {
let widget=document.getElementById(CONFIG.widgetId);
if(!widget) widget=buildWidget(_value);
if(target.nextElementSibling!==widget){
target.insertAdjacentElement("afterend",widget);
}
};
/* Güncelleme döngüsü */
const scheduleNext = () => {
const nextIn=between(rng,CONFIG.intervalMin,CONFIG.inter valMax);
setTimeout(() => {
const dirUp=rng()>0.44;
const step=dirUp?between(rng,1,CONFIG.stepUpMax):-between(rng,1,CONFIG.stepDownMax);
setValue(_value+step);
scheduleNext();
},nextIn);
};
/* Başlat */
const start = () => {
ensureStyles();
const tryMount = () => {
const target=document.querySelector(CONFIG.targetSelecto r);
if(target){
placeWidget(target);
if(!window.__bpViewerStarted){
window.__bpViewerStarted=true;
scheduleNext();
}
observe(target);
return true;
}
return false;
};
if(!tryMount()){
const poll=setInterval(()=>{ if(tryMount()) clearInterval(poll); },300);
const mo=new MutationObserver(()=>{ tryMount(); });
mo.observe(document.documentElement||document.body ,{childList:true,subtree:true});
}
};
const observe = (target) => {
const mo=new MutationObserver(()=>{
const t=document.querySelector(CONFIG.targetSelector);
if(t) placeWidget(t);
});
mo.observe(document.documentElement||document.body ,{childList:true,subtree:true});
};
if(document.readyState==="complete"||document.read yState==="interactive") start();
else {
document.addEventListener("DOMContentLoaded",start ,{once:true});
window.addEventListener("load",start,{once:true});
}
})();
</script>