Jump to content

MediaWiki:Common.js

From knowlepedia

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.
mw.loader.using('mediawiki.util').then(function () {
  function initAutoScroll() {
    const container = document.getElementById('scrollable-articles');
    if (!container) return;

    // Only initialize once
    if (container.dataset.autoScrollInitialized) {
      return;
    }
    container.dataset.autoScrollInitialized = 'true';

    // Duplicate content once by cloning children instead of innerHTML += innerHTML
    if (!container.dataset.duplicated) {
      const originalChildren = Array.from(container.children);
      originalChildren.forEach(child => {
        const clone = child.cloneNode(true);
        container.appendChild(clone);
      });
      container.dataset.originalWidth = container.scrollWidth / 2; // half, since duplicated
      container.dataset.duplicated = 'true';
    }

    let scrollSpeed = 0.5;
    let requestId;

    function autoScroll() {
      container.scrollLeft += scrollSpeed;

      const originalWidth = parseFloat(container.dataset.originalWidth);

      // Reset scroll when past original content width
      if (container.scrollLeft >= originalWidth) {
        container.scrollLeft -= originalWidth;
      }

      requestId = requestAnimationFrame(autoScroll);
      container._autoScrollRequestId = requestId;
    }

    // Cancel any existing scroll animation
    if (container._autoScrollRequestId) {
      cancelAnimationFrame(container._autoScrollRequestId);
    }

    // Pause/resume handlers
    function pauseScroll() {
      if (container._autoScrollRequestId) {
        cancelAnimationFrame(container._autoScrollRequestId);
        container._autoScrollRequestId = null;
      }
    }
    function resumeScroll() {
      if (!container._autoScrollRequestId) {
        autoScroll();
      }
    }

    // Remove old listeners if any to prevent stacking
    container.removeEventListener('mouseenter', pauseScroll);
    container.removeEventListener('mouseleave', resumeScroll);

    container.addEventListener('mouseenter', pauseScroll);
    container.addEventListener('mouseleave', resumeScroll);

    // Start auto scroll
    autoScroll();
  }

  // Run on initial page load
  if (document.readyState === 'complete' || document.readyState === 'interactive') {
    initAutoScroll();
  } else {
    document.addEventListener('DOMContentLoaded', initAutoScroll);
  }

  // Run after every content update (e.g., AJAX navigation)
  mw.hook('wikipage.content').add(initAutoScroll);
});


// Hide 'View source' tab using JS fallback
$(document).ready(function () {
    $('#ca-viewsource').hide();
});