Scroll Position Saver

Save scroll position in session cookies (clears on browser exit)

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(Tôi đã có Trình quản lý tập lệnh người dùng, hãy cài đặt nó!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name         Scroll Position Saver
// @namespace    http://greasyfork.org/
// @version      1.1
// @description  Save scroll position in session cookies (clears on browser exit)
// @author       Bui Quoc Dung
// @match        *://*/*
// @grant        none
// @run-at       document-start
// ==/UserScript==

(function() {
    'use strict';

    const storageKey = 'scrollPosition_' + window.location.href;

    const savePosition = function() {
        try {
            const scrollData = {
                horizontalCoordinate: window.scrollX,
                verticalCoordinate: window.scrollY
            };
            sessionStorage.setItem(storageKey, JSON.stringify(scrollData));
        } catch (error) {}
    };

    const restorePosition = function() {
        try {
            const savedScrollPosition = sessionStorage.getItem(storageKey);
            if (savedScrollPosition) {
                const parsedPositionData = JSON.parse(savedScrollPosition);
                const horizontalCoordinate = parsedPositionData.horizontalCoordinate;
                const verticalCoordinate = parsedPositionData.verticalCoordinate;

                window.scrollTo(horizontalCoordinate, verticalCoordinate);

                window.addEventListener('load', function() {
                    window.scrollTo(horizontalCoordinate, verticalCoordinate);
                }, {
                    once: true
                });
            }
        } catch (error) {}
    };

    restorePosition();

    let scrollTimeoutTimer;

    window.addEventListener('scroll', function() {
        clearTimeout(scrollTimeoutTimer);
        scrollTimeoutTimer = setTimeout(savePosition, 3000);
    }, {
        passive: true
    });

    window.addEventListener('beforeunload', savePosition);
})();