Last active
November 29, 2022 13:56
-
-
Save Crysp/109e91e76616b3202507b04a1add8480 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let oneScreen = document.body.offsetHeight - window.innerHeight; | |
let scrollY = window.scrollY; | |
let shouldUpdate = true; | |
document.addEventListener('scroll', () => { | |
if (shouldUpdate) { | |
scrollY = window.scrollY; | |
} | |
}); | |
window.addEventListener('resize', () => { | |
oneScreen = document.body.offsetHeight - window.innerHeight; | |
scrollY = window.scrollY; | |
}); | |
let documentHeight = 0; | |
async function waitForPageLoaded() { | |
documentHeight = document.body.offsetHeight; | |
return new Promise(resolve => { | |
const interval = setInterval(() => { | |
if (document.body.offsetHeight > documentHeight) { | |
clearInterval(interval); | |
resolve(); | |
} | |
}, 500); | |
}); | |
} | |
async function loadContent(times) { | |
if (times < 1) { | |
return; | |
} | |
const loadMore = document.getElementById('trending'); | |
loadMore.click(); | |
await waitForPageLoaded(); | |
await loadContent(times - 1); | |
} | |
document.addEventListener('click', async e => { | |
const isCloseClick = | |
e.target.innerText === 'Close' || | |
e.target.getAttribute('class') === 'gallery-close'; | |
const isLoadMoreClick = e.target.innerText.toLowerCase() === 'more images'; | |
shouldUpdate = e.target.nodeName === 'IMG' ? false : isLoadMoreClick; | |
if (isCloseClick) { | |
const clicksCount = scrollY / oneScreen; | |
await loadContent(clicksCount); | |
shouldUpdate = true; | |
window.scroll({ top: scrollY }); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment