MediaWiki:Common.js

From DefacerID Encyclopedia
Revision as of 10:44, 5 August 2024 by DefacerID (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)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
$(document).ready(function() {
    $('.cs-comment-form').show();
});



(function() {
    // Utility function to shuffle an array
    function shuffleArray(array) {
        for (let i = array.length - 1; i > 0; i--) {
            const j = Math.floor(Math.random() * (i + 1));
            [array[i], array[j]] = [array[j], array[i]];
        }
        return array;
    }

    // Fetch random articles
    fetch('http://localhost/wikidef/api.php?action=query&list=random&rnnamespace=0&rnlimit=10&format=json')
        .then(response => response.json())
        .then(data => {
            const articles = data.query.random;
            const container = document.getElementById('random-articles');
            container.innerHTML = ''; // Clear the loading text
            
            // Collect page IDs for category fetch
            const pageIds = articles.map(article => article.id).join('|');
            
            // Fetch categories for the articles
            return fetch(`http://localhost/wikidef/api.php?action=query&prop=categories&pageids=${pageIds}&format=json`);
        })
        .then(response => response.json())
        .then(data => {
            const container = document.getElementById('random-articles');
            const pages = data.query.pages;
            const categories = {};

            // Group articles by category
            Object.values(pages).forEach(page => {
                if (page.categories) {
                    page.categories.forEach(cat => {
                        const categoryName = cat.title.replace('Category:', '');
                        if (!categories[categoryName]) {
                            categories[categoryName] = [];
                        }
                        categories[categoryName].push(page.title);
                    });
                }
            });

            // Create HTML structure for each category
            Object.keys(categories).forEach(category => {
                // Shuffle the articles in each category
                const shuffledArticles = shuffleArray(categories[category]);

                const div = document.createElement('div');
                div.className = 'category-group';

                const categoryTitle = document.createElement('h2');
                categoryTitle.textContent = category;
                div.appendChild(categoryTitle);

                shuffledArticles.forEach(articleTitle => {
                    const articleTitleElement = document.createElement('h3');
                    const link = document.createElement('a');
                    link.href = `http://localhost/wikidef/index.php/${articleTitle}`;
                    link.textContent = articleTitle;
                    articleTitleElement.appendChild(link);
                    div.appendChild(articleTitleElement);
                });

                container.appendChild(div);
            });
        })
        .catch(error => {
            console.error('Error fetching data:', error);
        });
})();