From f048649e5863ff8ce83bd6ce4190f5db25bcc50f Mon Sep 17 00:00:00 2001 From: tophf Date: Thu, 14 Dec 2017 01:13:16 +0300 Subject: [PATCH] keep scroll position on install/delete --- popup/search-results.js | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/popup/search-results.js b/popup/search-results.js index 46fbbbda..320d86c4 100755 --- a/popup/search-results.js +++ b/popup/search-results.js @@ -534,7 +534,9 @@ window.addEventListener('showStyles:done', function _() { function onUninstallClicked(event) { event.stopPropagation(); const entry = this.closest('.search-result'); - deleteStyleSafe({id: entry._result.installedStyleId}); + saveScrollPosition(entry); + deleteStyleSafe({id: entry._result.installedStyleId}) + .then(restoreScrollPosition); } /** Installs the current userstyleSearchResult into Stylus. */ @@ -546,6 +548,7 @@ window.addEventListener('showStyles:done', function _() { const installButton = $('.search-result-install', entry); showSpinner(entry); + saveScrollPosition(entry); installButton.disabled = true; entry.style.setProperty('pointer-events', 'none', 'important'); @@ -570,6 +573,7 @@ window.addEventListener('showStyles:done', function _() { $.remove('.lds-spinner', entry); installButton.disabled = false; entry.style.pointerEvents = ''; + restoreScrollPosition(); }); function fetchStyleSettings(result) { @@ -581,6 +585,21 @@ window.addEventListener('showStyles:done', function _() { } } + function saveScrollPosition(entry) { + dom.scrollPosition = entry.getBoundingClientRect().top; + dom.scrollPositionElement = entry; + } + + function restoreScrollPosition() { + const t0 = performance.now(); + new MutationObserver((mutations, observer) => { + if (performance.now() - t0 < 1000) { + window.scrollBy(0, dom.scrollPositionElement.getBoundingClientRect().top - dom.scrollPosition); + } + observer.disconnect(); + }).observe(document.body, {childList: true, subtree: true, attributes: true}); + } + //endregion //region USO API wrapper