Created
September 30, 2026 06:55
-
-
Save alabhyajindal/15a1fdc1167c924dc287b7126b6a775e to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| // ==UserScript== | |
| // @name Remember scroll position | |
| // @match https://deeplearningwithpython.io/chapters/* | |
| // @grant none | |
| // @run-at document-idle | |
| // ==/UserScript== | |
| (function () { | |
| const key = 'scroll:' + location.pathname; | |
| function restore() { | |
| const saved = +localStorage.getItem(key); | |
| if (!saved) return; | |
| // retry a few times in case images/fonts are still shifting the layout | |
| let tries = 0; | |
| const iv = setInterval(() => { | |
| window.scrollTo(0, saved); | |
| if (Math.abs(window.scrollY - saved) < 5 || ++tries > 20) clearInterval(iv); | |
| }, 250); | |
| } | |
| if (document.readyState === 'complete') restore(); | |
| else window.addEventListener('load', restore); | |
| let t; | |
| window.addEventListener('scroll', () => { | |
| clearTimeout(t); | |
| t = setTimeout(() => localStorage.setItem(key, window.scrollY), 300); | |
| }); | |
| })(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment