<script>
(function() {
const KEY = 'u11_blog_scroll_state';
function extractUid(url) {
const match = url && url.match(/\/blog\/tpost\/([^\/?#]+)/i);
return match ? match[1].split('-')[0] : null;
}
function saveState(state) {
try { sessionStorage.setItem(KEY, JSON.stringify(state)); } catch(e) {}
}
function readState() {
try { return JSON.parse(sessionStorage.getItem(KEY)); } catch(e){ return null; }
}
function clearState() {
try { sessionStorage.removeItem(KEY); } catch(e){}
}
function findCardByUid(uid) {
if (!uid) return null;
const selector = `a[href*="/blog/tpost/${uid}"]`;
const link = document.querySelector(selector);
return link ? link.closest('.t-feed__grid-col, .t-col') : null;
}
function loadNextPage() {
const moreButton = document.querySelector('.js-feed-btn-show-more, .t-feed__showmore-btn');
if (moreButton) { moreButton.click(); return true; }
return false;
}
document.addEventListener('click', function(e) {
const link = e.target.closest('a[href*="/blog/tpost/"]');
if (link && !link.hash && link.closest('.js-feed')) {
const uid = extractUid(link.href);
if (!uid) return;
const scrollPosition = window.pageYOffset;
const card = link.closest('.t-feed__grid-col, .t-col');
let page = 1;
if (card) {
const feedContainer = card.closest('.js-feed-container');
if (feedContainer) {
const visibleCards = feedContainer.querySelectorAll('.t-feed__grid-col, .t-col');
const cardIndex = Array.from(visibleCards).indexOf(card);
page = Math.floor(cardIndex / 6) + 1;
}
}
saveState({ uid, scroll: scrollPosition, page, timestamp: Date.now(), fromBlog: true });
}
});
function restoreScrollPosition() {
const state = readState();
if (!state || !state.uid || !state.fromBlog) return;
if (Date.now() - state.timestamp > 600000) { clearState(); return; }
let attempts = 0;
const maxAttempts = 30;
let pageLoaded = state.page <= 1;
const interval = setInterval(() => {
attempts++;
if (!pageLoaded) pageLoaded = loadNextPage();
const card = findCardByUid(state.uid);
if (card) {
clearInterval(interval);
setTimeout(() => {
const offset = card.getBoundingClientRect().top + window.pageYOffset - 100;
window.scrollTo({ top: offset, behavior: 'smooth' });
clearState();
}, 300);
} else if (attempts >= maxAttempts) {
clearInterval(interval);
if (state.scroll) window.scrollTo({ top: state.scroll, behavior: 'smooth' });
clearState();
}
}, 500);
}
if (document.location.pathname === '/blog') {
setTimeout(restoreScrollPosition, 1000);
}
})();
</script><script>
document.addEventListener('DOMContentLoaded', function() {
const BLOG_URL = '/blog';
const KEY = 'u11_blog_scroll_state';
const backButtons = document.querySelectorAll('a[href="#back"]');
const state = JSON.parse(sessionStorage.getItem(KEY) || '{}');
const cameFromBlog = state.fromBlog;
const cleanUrl = window.location.href.split('#')[0];
function replaceHistory() {
if (window.history.replaceState) {
window.history.replaceState(null, null, cleanUrl);
}
}
replaceHistory();
document.querySelectorAll('a[href^="#"]').forEach(anchor => {
anchor.addEventListener('click', function(e) {
e.preventDefault();
const targetId = this.getAttribute('href').substring(1);
const targetEl = document.getElementById(targetId);
if (targetEl) targetEl.scrollIntoView({ behavior: 'smooth' });
if (window.history.replaceState) {
window.history.replaceState(null, null, cleanUrl + '#' + targetId);
}
});
});
backButtons.forEach(button => {
button.addEventListener('click', function(e) {
e.preventDefault();
if (cameFromBlog) {
// Если история содержит блог
if (document.referrer.includes(BLOG_URL) || window.history.length > 1) {
window.history.back();
} else {
window.location.href = BLOG_URL;
}
} else {
window.location.href = BLOG_URL;
}
});
});
});
</script>