Skip to content

Instantly share code, notes, and snippets.

@alabhyajindal
Created September 30, 2026 06:55
Show Gist options
  • Select an option

  • Save alabhyajindal/15a1fdc1167c924dc287b7126b6a775e to your computer and use it in GitHub Desktop.

Select an option

Save alabhyajindal/15a1fdc1167c924dc287b7126b6a775e to your computer and use it in GitHub Desktop.
// ==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