MediaWiki:Common.js: Difference between revisions

From Descendants of Darkness Wiki

No edit summary
No edit summary
Line 2: Line 2:
     var sidebarId = 'mw-navigation';
     var sidebarId = 'mw-navigation';


     // Save the sidebar scroll position before the page unloads
     // Save the sidebar scroll position to URL parameter
     window.addEventListener('beforeunload', function () {
     function saveSidebarScrollPosition() {
         localStorage.setItem('sidebarScrollPosition', document.getElementById(sidebarId).scrollTop);
         var scrollPosition = document.getElementById(sidebarId).scrollTop;
     });
        var newUrl = updateUrlParameter(window.location.href, 'sidebarScroll', scrollPosition);
        window.history.replaceState(null, '', newUrl);
     }


     // Restore the sidebar scroll position after the new page loads
     // Restore the sidebar scroll position from URL parameter
     window.addEventListener('DOMContentLoaded', function () {
     function restoreSidebarScrollPosition() {
         var scrollPosition = localStorage.getItem('sidebarScrollPosition');
         var scrollPosition = getUrlParameter('sidebarScroll');
         if (scrollPosition !== null) {
         if (scrollPosition !== null) {
             document.getElementById(sidebarId).scrollTop = parseInt(scrollPosition);
             document.getElementById(sidebarId).scrollTop = parseInt(scrollPosition);
         }
         }
     });
     }
 
    // Update URL parameter
    function updateUrlParameter(uri, key, value) {
        var re = new RegExp("([?&])" + key + "=.*?(&|$)", "i");
        var separator = uri.indexOf('?') !== -1 ? "&" : "?";
        if (uri.match(re)) {
            return uri.replace(re, '$1' + key + "=" + value + '$2');
        } else {
            return uri + separator + key + "=" + value;
        }
    }
 
    // Get URL parameter
    function getUrlParameter(name) {
        name = name.replace(/[\[]/, '\\[').replace(/[\]]/, '\\]');
        var regex = new RegExp('[\\?&]' + name + '=([^&#]*)');
        var results = regex.exec(location.search);
        return results === null ? null : decodeURIComponent(results[1].replace(/\+/g, ' '));
    }
 
    // Save the sidebar scroll position before navigating away
    window.addEventListener('beforeunload', saveSidebarScrollPosition);
 
    // Restore the sidebar scroll position on page load
    window.addEventListener('DOMContentLoaded', restoreSidebarScrollPosition);


     // Save the sidebar scroll position when clicking on sidebar links
     // Save the sidebar scroll position when clicking on sidebar links
     document.querySelectorAll('#' + sidebarId + ' a').forEach(function (link) {
     document.querySelectorAll('#' + sidebarId + ' a').forEach(function (link) {
         link.addEventListener('click', function (event) {
         link.addEventListener('click', function (event) {
             var scrollPosition = document.getElementById(sidebarId).scrollTop;
             saveSidebarScrollPosition();
            localStorage.setItem('sidebarScrollPosition', scrollPosition);
            return true;
         });
         });
    });
    // Restore the main content scroll position after the new page loads
    window.addEventListener('DOMContentLoaded', function () {
        var hash = window.location.hash;
        if (hash) {
            var target = document.querySelector(hash);
            if (target) {
                target.scrollIntoView({
                    behavior: 'auto'
                });
            }
        }
     });
     });
})();
})();

Revision as of 19:49, 6 April 2024

(function () {
    var sidebarId = 'mw-navigation';

    // Save the sidebar scroll position to URL parameter
    function saveSidebarScrollPosition() {
        var scrollPosition = document.getElementById(sidebarId).scrollTop;
        var newUrl = updateUrlParameter(window.location.href, 'sidebarScroll', scrollPosition);
        window.history.replaceState(null, '', newUrl);
    }

    // Restore the sidebar scroll position from URL parameter
    function restoreSidebarScrollPosition() {
        var scrollPosition = getUrlParameter('sidebarScroll');
        if (scrollPosition !== null) {
            document.getElementById(sidebarId).scrollTop = parseInt(scrollPosition);
        }
    }

    // Update URL parameter
    function updateUrlParameter(uri, key, value) {
        var re = new RegExp("([?&])" + key + "=.*?(&|$)", "i");
        var separator = uri.indexOf('?') !== -1 ? "&" : "?";
        if (uri.match(re)) {
            return uri.replace(re, '$1' + key + "=" + value + '$2');
        } else {
            return uri + separator + key + "=" + value;
        }
    }

    // Get URL parameter
    function getUrlParameter(name) {
        name = name.replace(/[\[]/, '\\[').replace(/[\]]/, '\\]');
        var regex = new RegExp('[\\?&]' + name + '=([^&#]*)');
        var results = regex.exec(location.search);
        return results === null ? null : decodeURIComponent(results[1].replace(/\+/g, ' '));
    }

    // Save the sidebar scroll position before navigating away
    window.addEventListener('beforeunload', saveSidebarScrollPosition);

    // Restore the sidebar scroll position on page load
    window.addEventListener('DOMContentLoaded', restoreSidebarScrollPosition);

    // Save the sidebar scroll position when clicking on sidebar links
    document.querySelectorAll('#' + sidebarId + ' a').forEach(function (link) {
        link.addEventListener('click', function (event) {
            saveSidebarScrollPosition();
        });
    });
})();