sales-widget / widget.js
haxwitcher's picture
Update widget.js
82c8c7b verified
Raw History Blame Contribute Delete
3.95 kB
;(function(){
// 1) Pronađi svoj <script> tag
const me = document.currentScript
|| Array.from(document.getElementsByTagName('script'))
.find(s => s.src && s.src.match(/widget\.js(\?.*)?$/));
if (!me) return console.error('[SalesWidget] ❌ Ne mogu pronaći widget.js tag');
// 2) Inline CSS
const css = `
#sales-notification-container {
position: fixed;
bottom: 20px;
left: 20px;
display: flex;
flex-direction: column-reverse;
gap: 12px;
z-index: 9999;
pointer-events: none;
}
.notification {
display: flex;
align-items: center;
background: #fff;
color: #000;
border: 1px solid #eee;
border-radius: 8px;
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
padding: 12px;
max-width: 320px;
font-family: 'Segoe UI', Tahoma, sans-serif;
font-size: 14px;
opacity: 0;
transform: translateY(20px);
transition: opacity 0.4s ease, transform 0.4s ease;
pointer-events: auto;
}
.notification.show {
opacity: 1;
transform: translateY(0);
}
.notification img.avatar {
width: 48px;
height: 48px;
border-radius: 50%;
margin-right: 12px;
object-fit: cover;
}
.notification .text {
line-height: 1.4;
}
.notification .text b {
font-weight: 600;
}
`;
const styleEl = document.createElement('style');
styleEl.textContent = css;
document.head.appendChild(styleEl);
// 3) Config URL
const BASE = me.src.replace(/\/widget\.js.*$/, '');
const CONFIG_URL = `${BASE}/config.json`;
// 4) Timing (ms)
const FIRST_DELAY = 4000; // 4s prije prve notifikacije
const DISPLAY_DURATION = 4000; // 4s prikaz
const HIDE_DURATION = 500; // 0.5s zatvaranje
const INTERVAL_BETWEEN = 3000; // 3s između
// 5) Helper
function el(tag, cls='') {
const e = document.createElement(tag);
if (cls) e.className = cls;
return e;
}
// 6) Odredi rod prema imenu
function getVerb(name) {
// ako ime završava na "a" -> žensko
return name.trim().toLowerCase().endsWith('a')
? 'naručila je'
: 'naručio je';
}
// 7) Kreiraj notifikaciju
function createNote(item) {
const verb = getVerb(item.name);
const w = el('div', 'notification');
w.innerHTML = `
<img class="avatar" src="https://i.postimg.cc/1tsc9MrJ/512x512.png" alt="${item.name}">
<div class="text">
<b>${item.name}</b> (${item.channel}: ${item.phone}) iz ${item.country}<br>
${verb} pretplatu za <b>${item.product}</b><br>
<small>${item.timeAgo}</small>
</div>`;
return w;
}
// 8) Container
const container = el('div');
container.id = 'sales-notification-container';
document.body.appendChild(container);
// 9) Prikaz jedne
function showOne(item) {
const note = createNote(item);
container.appendChild(note);
requestAnimationFrame(() => note.classList.add('show'));
setTimeout(() => {
note.classList.remove('show');
setTimeout(() => note.remove(), HIDE_DURATION);
}, DISPLAY_DURATION);
}
// 10) Učitavanje i sekvencijalni loop
async function start() {
let data;
try {
const res = await fetch(CONFIG_URL + '?_=' + Date.now(), { cache: 'no-cache' });
if (!res.ok) throw new Error(`HTTP ${res.status}`);
data = await res.json();
} catch (err) {
return console.error('[SalesWidget] greška pri učitavanju config.json:', err);
}
if (!Array.isArray(data) || data.length === 0) return;
let idx = 0;
setTimeout(function next() {
showOne(data[idx % data.length]);
idx++;
setTimeout(next, DISPLAY_DURATION + HIDE_DURATION + INTERVAL_BETWEEN);
}, FIRST_DELAY);
}
// 11) Start
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', start);
} else {
start();
}
})();