<script>
(function () {
const params = new URLSearchParams(location.search);
const src = params.get('utm_source') || '';
const med = params.get('utm_medium') || '';
const cmp = params.get('utm_campaign') || '';
if (!src && !med && !cmp) return;
const clean = (v, max) =>
v.toString()
.trim()
.replace(/\s+/g,'-')
.replace(/[^A-Za-z0-9_-]/g,'')
.slice(0, max);
const payload = (
`s_${clean(src,12)}-m_${clean(med,12)}-c_${clean(cmp,24)}`
).slice(0,64).replace(/-$/,'');
const buildHref = (href) => {
try {
const url = new URL(href);
if (!/^(t\.me|telegram\.me)$/.test(url.hostname)) return null;
const parts = url.pathname.split('/').filter(Boolean);
if (!parts[0]) return null;
return `https://t.me/${parts[0]}?start=${payload}`;
} catch {
return null;
}
};
const updateLinks = () => {
document.querySelectorAll('a[href*="t.me/"], a[href*="telegram.me/"]').forEach(a => {
const newHref = buildHref(a.href);
if (newHref && a.href !== newHref) {
a.href = newHref;
}
});
};
updateLinks();
const observer = new MutationObserver(updateLinks);
observer.observe(document.body, {
childList: true,
subtree: true,
attributes: true
});
})();
</script>