Jump to content

🚨 Get your Google Knowledge Panel
â—‰ Displays your name, photo, and profession in Google Search
Click here to get started now

MediaWiki:Common.js: Difference between revisions

From Knowlepedia
Created page with "→Any JavaScript here will be loaded for all users on every page load.: mw.loader.using('mediawiki.util').then(function () { function initAutoScroll() { const container = document.getElementById('scrollable-articles'); if (!container) return; if (container.dataset.autoScrollInitialized) return; container.dataset.autoScrollInitialized = 'true'; if (!container.dataset.duplicated) { const originalChildren = Array.from(container.children);..."
 
No edit summary
Line 84: Line 84:
       }
       }
     }
     }
  }
});
$(document).ready(function() {
  var desc = $('meta[name="knowlepedia-desc"]').attr('content');
  if (desc) {
    $('head').append('<meta name="description" content="' + desc + '">');
   }
   }
});
});

Revision as of 11:50, 21 June 2025

/* Any JavaScript here will be loaded for all users on every page load. */
mw.loader.using('mediawiki.util').then(function () {
  function initAutoScroll() {
    const container = document.getElementById('scrollable-articles');
    if (!container) return;

    if (container.dataset.autoScrollInitialized) return;
    container.dataset.autoScrollInitialized = 'true';

    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;
      container.dataset.duplicated = 'true';
    }

    let scrollSpeed = window.innerWidth < 500 ? 0.3 : 0.5;
    let requestId;

    function autoScroll() {
      container.scrollLeft += scrollSpeed;
      const originalWidth = parseFloat(container.dataset.originalWidth);

      if (container.scrollLeft >= originalWidth) {
        container.scrollLeft -= originalWidth;
      }

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

    if (container._autoScrollRequestId) {
      cancelAnimationFrame(container._autoScrollRequestId);
    }

    function pauseScroll() {
      if (container._autoScrollRequestId) {
        cancelAnimationFrame(container._autoScrollRequestId);
        container._autoScrollRequestId = null;
      }
    }

    function resumeScroll() {
      if (!container._autoScrollRequestId) {
        autoScroll();
      }
    }

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

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

    autoScroll();
  }

  if (document.readyState === 'complete' || document.readyState === 'interactive') {
    initAutoScroll();
  } else {
    document.addEventListener('DOMContentLoaded', initAutoScroll);
  }

  mw.hook('wikipage.content').add(initAutoScroll);
});

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

// Auto-tag NeedsReview for user-level edits only
$(document).ready(function() {
  if (mw.config.get("wgAction") === "edit" || mw.config.get("wgAction") === "submit") {
    var userGroups = mw.config.get('wgUserGroups');

    if (userGroups.includes('user') && !userGroups.includes('approver') && !userGroups.includes('sysop')) {
      var $wpTextbox1 = $('#wpTextbox1');
      if ($wpTextbox1.length && !$wpTextbox1.val().includes("{{NeedsReview}}")) {
        $wpTextbox1.val("{{NeedsReview}}\n" + $wpTextbox1.val());
      }
    }
  }
});

$(document).ready(function() {
  var desc = $('meta[name="knowlepedia-desc"]').attr('content');
  if (desc) {
    $('head').append('<meta name="description" content="' + desc + '">');
  }
});