MediaWiki:Common.js: Difference between revisions

From DefacerID Encyclopedia
No edit summary
No edit summary
Line 1: Line 1:
var wikihome = 'http://localhost/wikidef';
const wikihome = 'http://localhost/wikidef';


$(document).ready(function() {
$(document).ready(function() {
Line 8: Line 8:


(function() {
(function() {
    // Utility function to shuffle an array
     function shuffleArray(array) {
     function shuffleArray(array) {
         for (let i = array.length - 1; i > 0; i--) {
         for (let i = array.length - 1; i > 0; i--) {
Line 17: Line 16:
     }
     }


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


            // Group articles by category
             Object.values(pages).forEach(page => {
             Object.values(pages).forEach(page => {
                 if (page.categories) {
                 if (page.categories) {
Line 47: Line 43:
                         categories[categoryName].push(page.title);
                         categories[categoryName].push(page.title);
                     });
                     });
                } else {
                    uncategorizedArticles.push(page.title);
                 }
                 }
             });
             });


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


Line 73: Line 71:
                 container.appendChild(div);
                 container.appendChild(div);
             });
             });
            if (uncategorizedArticles.length > 0) {
                const div = document.createElement('div');
                div.className = 'category-group';
                const moreEncyclopediaTitle = document.createElement('h2');
                moreEncyclopediaTitle.textContent = 'More Encyclopedia';
                div.appendChild(moreEncyclopediaTitle);
                uncategorizedArticles.forEach(articleTitle => {
                    const articleTitleElement = document.createElement('h3');
                    const link = document.createElement('a');
                    link.href = `${wikihome}/index.php/${articleTitle.replace(/ /g, "_")}`;
                    link.textContent = articleTitle;
                    articleTitleElement.appendChild(link);
                    div.appendChild(articleTitleElement);
                });
                container.appendChild(div);
            }
         })
         })
         .catch(error => {
         .catch(error => {

Revision as of 13:35, 7 August 2024

const wikihome = 'http://localhost/wikidef';

$(document).ready(function() {
    $('.cs-comment-form').show();
});



(function() {
    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(`${wikihome}/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 = '';

            const pageIds = articles.map(article => article.id).join('|');

            return fetch(`${wikihome}/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 = {};
            const uncategorizedArticles = [];

            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);
                    });
                } else {
                    uncategorizedArticles.push(page.title);
                }
            });

            const shuffledCategories = shuffleArray(Object.keys(categories));

            shuffledCategories.forEach(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 = `${wikihome}/index.php/${articleTitle.replace(/ /g, "_")}`;
                    link.textContent = articleTitle;
                    articleTitleElement.appendChild(link);
                    div.appendChild(articleTitleElement);
                });

                container.appendChild(div);
            });

            if (uncategorizedArticles.length > 0) {
                const div = document.createElement('div');
                div.className = 'category-group';

                const moreEncyclopediaTitle = document.createElement('h2');
                moreEncyclopediaTitle.textContent = 'More Encyclopedia';
                div.appendChild(moreEncyclopediaTitle);

                uncategorizedArticles.forEach(articleTitle => {
                    const articleTitleElement = document.createElement('h3');
                    const link = document.createElement('a');
                    link.href = `${wikihome}/index.php/${articleTitle.replace(/ /g, "_")}`;
                    link.textContent = articleTitle;
                    articleTitleElement.appendChild(link);
                    div.appendChild(articleTitleElement);
                });

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