Jump to content

MediaWiki:Minerva.js

From Artemis Archive
Revision as of 13:09, 30 April 2025 by Bxuwd (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.
/* All JavaScript here will be loaded for users of the MinervaNeue skin */
// Wait for the document to be ready
$(document).ready(function () {
  console.log('[Minerva.js] Attempting to modify mobile menu...'); // Log start

  // Target the main menu UL element
  // Using a selector that hopefully works for MinervaNeue
  var $menuUl = $('#mw-mf-page-left .menu > ul').first();
  console.log('[Minerva.js] Target menu UL selector:', '#mw-mf-page-left .menu > ul');
  console.log('[Minerva.js] Found menu UL elements:', $menuUl.length); // Should be 1

  // Ensure the menu target exists
  if ($menuUl.length) {
    console.log('[Minerva.js] Menu UL found. Proceeding to append items...');

    // --- Function to generate URL safely (using mw.util or fallback) ---
    function getSafeUrl(pageName) {
      var url = '#error-generating-url'; // Default fallback URL
      try {
        if (typeof mw !== 'undefined' && typeof mw.util !== 'undefined' && typeof mw.util.getUrl === 'function') {
          // Use MediaWiki's preferred way if available
          url = mw.util.getUrl(pageName);
        } else {
          // Basic fallback if mw.util not ready (less ideal)
          console.warn('[Minerva.js] mw.util.getUrl not available, using basic fallback URL generation.');
          var articlePath = '/wiki/$1'; // Default path structure
          if (typeof mw !== 'undefined' && typeof mw.config !== 'undefined' && typeof mw.config.get === 'function') {
               articlePath = mw.config.get('wgArticlePath', '/wiki/$1'); // Get site's actual path
          }
          var prefix = articlePath.replace('$1', ''); // Get the prefix (e.g., '/index.php/' or '/wiki/')
          url = prefix + encodeURIComponent(pageName.replace(/ /g, '_'));
        }
      } catch (e) {
        console.error('[Minerva.js] Error within getSafeUrl for "' + pageName + '":', e);
      }
      // console.log('[Minerva.js] Generated URL for "' + pageName + '":', url); // Optional: uncomment for debugging URLs
      return url;
    }

    // --- Append items within a try...catch block for safety ---
    try {
      // Add Categories section header (as simple text in an LI)
      // Added class 'menu-section-header' for potential CSS styling
      $menuUl.append('<li class="menu-section-header">— CATEGORIES —</li>');

      // Add links, wrapping them in LI with a custom class for potential CSS styling
      $menuUl.append('<li class="custom-menu-item"><a href="' + getSafeUrl('Animal cruelty cases by location') + '">By Location/地点</a></li>');
      $menuUl.append('<li class="custom-menu-item"><a href="' + getSafeUrl('Animal cruelty cases by species') + '">By Species/物种</a></li>');
      $menuUl.append('<li class="custom-menu-item"><a href="' + getSafeUrl('Animal cruelty cases by year') + '">By Year/年份</a></li>');

      // Add Resources section header
      $menuUl.append('<li class="menu-section-header">— RESOURCES —</li>');

      // Add links
      $menuUl.append('<li class="custom-menu-item"><a href="' + getSafeUrl('useful links') + '">Useful links/实用链接</a></li>');
      $menuUl.append('<li class="custom-menu-item"><a href="' + getSafeUrl('documentaries') + '">Documentaries/纪录片</a></li>');
      $menuUl.append('<li class="custom-menu-item"><a href="' + getSafeUrl('psychological study') + '">Psychological study/心理研究</a></li>');

      console.log('[Minerva.js] Finished attempting to append items.');

    } catch (e) {
       // Catch errors specifically during the append process
       console.error('[Minerva.js] Error occurred during menu append process:', e);
    }

  } else {
       // Log a warning if the menu UL isn't found
       console.warn('[Minerva.js] Could not find the main mobile menu UL element (#mw-mf-page-left .menu > ul) to append items.');
  }
});

// Log message at the very end to ensure the whole script file was parsed without fatal syntax errors
console.log('[Minerva.js] Script End (parsed)');