MediaWiki:Common.js: Difference between revisions

From Descendants of Darkness Wiki

No edit summary
No edit summary
Line 18: Line 18:
       if (!isNaN(scrollPosition)) {
       if (!isNaN(scrollPosition)) {
         var sidebar = document.getElementById(sidebarId);
         var sidebar = document.getElementById(sidebarId);
         if (sidebar) {
        var contentArea = document.querySelector('.mw-body'); // Adjust the selector as needed
         if (sidebar && contentArea) {
           sidebar.scrollTo({
           sidebar.scrollTo({
             top: scrollPosition,
             top: scrollPosition,
Line 40: Line 41:
   document.addEventListener('visibilitychange', function () {
   document.addEventListener('visibilitychange', function () {
     if (document.visibilityState === 'visible') {
     if (document.visibilityState === 'visible') {
       restoreSidebarScrollPosition();
       setTimeout(function () {
        restoreSidebarScrollPosition();
      }, 500); // Adjust the delay as needed (in milliseconds)
     }
     }
   });
   });
Line 47: Line 50:
   var sidebar = document.getElementById(sidebarId);
   var sidebar = document.getElementById(sidebarId);
   if (sidebar) {
   if (sidebar) {
     sidebar.addEventListener('scroll', saveSidebarScrollPosition);
     sidebar.addEventListener('scroll', function () {
      saveSidebarScrollPosition();
    });
   }
   }


Line 53: Line 58:
   var observer = new MutationObserver(function (mutations) {
   var observer = new MutationObserver(function (mutations) {
     if (document.readyState === 'complete') {
     if (document.readyState === 'complete') {
       restoreSidebarScrollPosition();
       setTimeout(function () {
        restoreSidebarScrollPosition();
      }, 500); // Adjust the delay as needed (in milliseconds)
       observer.disconnect();
       observer.disconnect();
     }
     }

Revision as of 11:30, 14 April 2024

(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 = parseFloat(sessionStorage.getItem(currentPage));
      if (!isNaN(scrollPosition)) {
        var sidebar = document.getElementById(sidebarId);
        var contentArea = document.querySelector('.mw-body'); // Adjust the selector as needed
        if (sidebar && contentArea) {
          sidebar.scrollTo({
            top: scrollPosition,
            behavior: 'auto'
          });
        }
      }
    } 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') {
      setTimeout(function () {
        restoreSidebarScrollPosition();
      }, 500); // Adjust the delay as needed (in milliseconds)
    }
  });

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

  // Create a MutationObserver to detect when the content has finished loading
  var observer = new MutationObserver(function (mutations) {
    if (document.readyState === 'complete') {
      setTimeout(function () {
        restoreSidebarScrollPosition();
      }, 500); // Adjust the delay as needed (in milliseconds)
      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);
  }
})();