MediaWiki:Common.js

From Descendants of Darkness Wiki

Revision as of 11:21, 14 April 2024 by Ninja (talk | contribs)

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5.
(function () {
  var sidebarId = 'mw-navigation';

  function saveSidebarScrollPosition() {
    try {
      var scrollPosition = document.getElementById(sidebarId).scrollTop;
      var currentPage = window.location.href;
      sessionStorage.setItem(currentPage, scrollPosition);
    } catch (error) {
      console.error('Error saving scroll position:', error);
    }
  }

  function restoreSidebarScrollPosition() {
    try {
      var currentPage = window.location.href;
      var scrollPosition = sessionStorage.getItem(currentPage);
      if (scrollPosition !== null) {
        var sidebar = document.getElementById(sidebarId);
        if (sidebar) {
          sidebar.scrollTop = parseInt(scrollPosition);
        }
      }
    } catch (error) {
      console.error('Error restoring scroll position:', error);
    }
  }

  // Save scroll position when the page is hidden (user navigates away)
  document.addEventListener('visibilitychange', function () {
    if (document.visibilityState === 'hidden') {
      saveSidebarScrollPosition();
    }
  });

  // Restore scroll position when the page is visible (user comes back)
  document.addEventListener('visibilitychange', function () {
    if (document.visibilityState === 'visible') {
      restoreSidebarScrollPosition();
    }
  });

  // Save scroll position when the sidebar is scrolled
  var sidebar = document.getElementById(sidebarId);
  if (sidebar) {
    sidebar.addEventListener('scroll', saveSidebarScrollPosition);
  }

  // Create a MutationObserver to detect when the content has finished loading
  var observer = new MutationObserver(function (mutations) {
    if (document.readyState === 'complete') {
      restoreSidebarScrollPosition();
      observer.disconnect();
    }
  });

  // Configure the observer to watch for changes in the content
  var observerConfig = {
    childList: true,
    subtree: true
  };

  // Start observing the content element
  var contentElement = document.body;
  if (contentElement) {
    observer.observe(contentElement, observerConfig);
  }
})();