From 9fcb8abd268971f28bcf06dc8193e8a4e2d150e0 Mon Sep 17 00:00:00 2001 From: Dimitris Dafnis <68849116+jim-daf@users.noreply.github.com> Date: Sat, 25 Apr 2026 21:11:52 +0200 Subject: [PATCH] feat(webview): saveScrollPosition / restoreScrollPosition Refs #626 --- .../ferg/awfulapp/webview/AwfulWebView.java | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/Awful.apk/src/main/java/com/ferg/awfulapp/webview/AwfulWebView.java b/Awful.apk/src/main/java/com/ferg/awfulapp/webview/AwfulWebView.java index eddc3b230..4061e9438 100644 --- a/Awful.apk/src/main/java/com/ferg/awfulapp/webview/AwfulWebView.java +++ b/Awful.apk/src/main/java/com/ferg/awfulapp/webview/AwfulWebView.java @@ -172,6 +172,38 @@ public void refreshPageContents() { } + /** + * Issue #626: capture the current scroll position into a + * window-scoped variable so a follow-up content swap can restore it. + * Pair with {@link #restoreScrollPosition()} called once the new + * body HTML has been rendered. + */ + public void saveScrollPosition() { + runJavascript("window.__awfulSavedScroll = window.scrollY;"); + } + + /** + * Restore a previously captured scroll position. Safe to call before + * the DOM has finished settling, the JS uses requestAnimationFrame + * to retry until the document height covers the saved offset. + */ + public void restoreScrollPosition() { + runJavascript( + "(function(){" + + "var target = window.__awfulSavedScroll;" + + "if (target == null) { return; }" + + "function tryScroll() {" + + "if (document.documentElement.scrollHeight >= target) {" + + "window.scrollTo({top: target});" + + "} else {" + + "window.requestAnimationFrame(tryScroll);" + + "}" + + "}" + + "tryScroll();" + + "})();"); + } + + /** * Set and display the current HTML for the container body. *