if (document.querySelector('main').classList.contains('feed') && document.querySelectorAll('.postsList > .post').length > 0) { const observerCheck = (entry, observer) => { if (entry[0].isIntersecting) { console.log('intersect'); const container = document.querySelector('.postsList'); const lastPost = document.querySelector('.postsList > .post:last-child'); const uri = container.getAttribute('data-uri'); const cursor = container.getAttribute('data-cursor'); const params = new URLSearchParams(); params.append("feed", uri); params.append("cursor", cursor); observer.unobserve(lastPost); fetch(`/api/feedPosts?${params}`) .then((data) => { data.text().then((content) => { const data = JSON.parse(content); container.innerHTML += data.posts; container.setAttribute(data.cursor); const newobserver = new IntersectionObserver(observerCheck); newobserver.observe(document.querySelector('.postsList > .post:last-child')); }); }, (err) => { console.error(err); const newobserver = new IntersectionObserver(observerCheck); newobserver.observe(document.querySelector('.postsList > .post:last-child')); }); } } const observer = new IntersectionObserver(observerCheck); observer.observe(document.querySelector('.postsList > .post:last-child')); }