From ac295abe9cff23a617557c1b48fe0eaa9caaec5b Mon Sep 17 00:00:00 2001 From: Hongbo Wu Date: Wed, 5 Apr 2023 15:59:26 +0800 Subject: [PATCH] Stop scrolling if the url has changed to avoid infinite scrolling issue --- .../src/scripts/content/prepare-content.js | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/pkg/extension/src/scripts/content/prepare-content.js b/pkg/extension/src/scripts/content/prepare-content.js index 0820bbd03..59208ebed 100644 --- a/pkg/extension/src/scripts/content/prepare-content.js +++ b/pkg/extension/src/scripts/content/prepare-content.js @@ -187,15 +187,16 @@ if (pdfContent) { return pdfContent } + const url = window.location.href; try { - if (handleBackendUrl(window.location.href)) { + if (handleBackendUrl(url)) { return { type: 'url' } } } catch { console.log('error checking url') } - async function scrollPage () { + async function scrollPage (url) { const scrollingEl = (document.scrollingElement || document.body); const lastScrollPos = scrollingEl.scrollTop; const currentScrollHeight = scrollingEl.scrollHeight; @@ -209,15 +210,16 @@ /* * check below compares scrollTop against initial page height to handle * pages with infinite scroll else we shall be infinitely scrolling here. + * stop scrolling if the url has changed in the meantime. */ - while (scrollingEl.scrollTop <= (currentScrollHeight - 500)) { + while (scrollingEl.scrollTop <= (currentScrollHeight - 500) && window.location.href === url) { const prevScrollTop = scrollingEl.scrollTop; scrollingEl.scrollTop += 500; /* sleep upon scrolling position change for event loop to handle events from scroll */ await (new Promise((resolve) => { setTimeout(resolve, 10); })); if (scrollingEl.scrollTop === prevScrollTop) { - /* break out scroll loop if we are not able to scroll for any reason */ - // console.log('breaking out scroll loop', scrollingEl.scrollTop, currentScrollHeight); + /* break out scroll loop if we are not able to scroll for any reason */ + // console.log('breaking out scroll loop', scrollingEl.scrollTop, currentScrollHeight); break; } } @@ -225,7 +227,7 @@ /* sleep upon scrolling position change for event loop to handle events from scroll */ await (new Promise((resolve) => { setTimeout(resolve, 10); })); } - await scrollPage(); + await scrollPage(url); clearExistingBackdrops(); return { type: 'html', content: prepareContentPostScroll() };