[Solo] Started using the global pagination script
This commit is contained in:
parent
b469219ca3
commit
dd19e4a0d7
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
|
@ -4,71 +4,6 @@ function initParallax() {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function pagination(isInfinite = true, done) {
|
|
||||||
let loading = false;
|
|
||||||
const feedElement = document.querySelector('.gh-feed');
|
|
||||||
const target = feedElement.nextElementSibling || feedElement.parentElement.nextElementSibling;
|
|
||||||
|
|
||||||
const loadNextPage = async function () {
|
|
||||||
const nextElement = document.querySelector('link[rel=next]');
|
|
||||||
|
|
||||||
if (!nextElement) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
const res = await fetch(nextElement.href);
|
|
||||||
const html = await res.text();
|
|
||||||
const parser = new DOMParser();
|
|
||||||
const doc = parser.parseFromString(html, 'text/html');
|
|
||||||
|
|
||||||
const posts = doc.querySelectorAll('.gh-feed > *');
|
|
||||||
|
|
||||||
posts.forEach(function (post) {
|
|
||||||
feedElement.appendChild(document.importNode(post, true));
|
|
||||||
});
|
|
||||||
|
|
||||||
if (done) {
|
|
||||||
done();
|
|
||||||
}
|
|
||||||
|
|
||||||
const resNextElement = doc.querySelector('link[rel=next]');
|
|
||||||
if (resNextElement && resNextElement.href) {
|
|
||||||
nextElement.href = resNextElement.href;
|
|
||||||
} else {
|
|
||||||
nextElement.remove();
|
|
||||||
}
|
|
||||||
} catch (e) {
|
|
||||||
nextElement.remove();
|
|
||||||
throw e;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const callback = async function (entries) {
|
|
||||||
if (loading) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
loading = true;
|
|
||||||
|
|
||||||
if (entries[0].isIntersecting) {
|
|
||||||
// keep loading next page until target is out of the viewport or we've loaded the last page
|
|
||||||
while (target.getBoundingClientRect().top <= window.innerHeight && document.querySelector('link[rel=next]')) {
|
|
||||||
await loadNextPage();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
loading = false;
|
|
||||||
|
|
||||||
if (!document.querySelector('link[rel=next]')) {
|
|
||||||
observer.disconnect();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const observer = new IntersectionObserver(callback);
|
|
||||||
observer.observe(target);
|
|
||||||
}
|
|
||||||
|
|
||||||
(function () {
|
(function () {
|
||||||
if (!document.body.classList.contains('has-background-about')) return;
|
if (!document.body.classList.contains('has-background-about')) return;
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue