MediaWiki:Common.js: Difference between revisions

From Descendants of Darkness Wiki

No edit summary
No edit summary
Line 61: Line 61:
   // Create a MutationObserver to detect when the content has finished loading
   // Create a MutationObserver to detect when the content has finished loading
   var observer = new MutationObserver(function (mutations) {
   var observer = new MutationObserver(function (mutations) {
    console.log('Content mutations observed');
     restoreSidebarScrollPosition();
     restoreSidebarScrollPosition();
     observer.disconnect();
     observer.disconnect();
Line 72: Line 73:


   // Start observing the content element
   // Start observing the content element
   var contentElement = document.querySelector('.mw-body');
   var contentElement = document.body;
   if (contentElement) {
   if (contentElement) {
     observer.observe(contentElement, observerConfig);
     observer.observe(contentElement, observerConfig);
    console.log('Observing content element');
   } else {
   } else {
     console.warn('Content element not found');
     console.warn('Content element not found');

Revision as of 00:50, 14 April 2024

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

  function saveSidebarScrollPosition() {
    try {
      var scrollPosition = document.getElementById(sidebarId).scrollTop;
      var currentPage = window.location.href;
      console.log('Saving scroll position:', scrollPosition, 'for page:', currentPage);
      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);
      console.log('Restoring scroll position:', scrollPosition, 'for page:', currentPage);
      if (scrollPosition !== null) {
        var sidebar = document.getElementById(sidebarId);
        if (sidebar) {
          sidebar.scrollTop = parseInt(scrollPosition);
          console.log('Scroll position restored');
        } else {
          console.warn('Sidebar element not found');
        }
      }
    } 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') {
      console.log('Page visibility changed to hidden');
      saveSidebarScrollPosition();
    }
  });

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

  // Save scroll position when the sidebar is scrolled
  var sidebar = document.getElementById(sidebarId);
  if (sidebar) {
    sidebar.addEventListener('scroll', function () {
      console.log('Sidebar scrolled');
      saveSidebarScrollPosition();
    });
  } else {
    console.warn('Sidebar element not found');
  }

  // Create a MutationObserver to detect when the content has finished loading
  var observer = new MutationObserver(function (mutations) {
    console.log('Content mutations observed');
    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);
    console.log('Observing content element');
  } else {
    console.warn('Content element not found');
  }

  // Log relevant information for debugging
  console.log('Browser:', navigator.userAgent);
  console.log('JavaScript enabled:', typeof window !== 'undefined');
  console.log('sessionStorage supported:', typeof sessionStorage !== 'undefined');
})();