MediaWiki:Common.js: Difference between revisions

From DefacerID Encyclopedia
No edit summary
No edit summary
Line 5: Line 5:


(function() {
(function() {
    // Fetch random articles
     fetch('http://localhost/wikidef/api.php?action=query&list=random&rnnamespace=0&rnlimit=10&format=json')
     fetch('http://localhost/wikidef/api.php?action=query&list=random&rnnamespace=0&rnlimit=10&format=json')
         .then(response => response.json())
         .then(response => response.json())
Line 12: Line 13:
             container.innerHTML = ''; // Clear the loading text
             container.innerHTML = ''; // Clear the loading text
              
              
            // Collect page IDs for category fetch
             const pageIds = articles.map(article => article.id).join('|');
             const pageIds = articles.map(article => article.id).join('|');
             return fetch(`http://localhost/wikidef/api.php?action=query&prop=extracts&pageids=${pageIds}&exintro&format=json`);
           
            // 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(response => response.json())
         .then(data => {
         .then(data => {
             const container = document.getElementById('random-articles');
             const container = document.getElementById('random-articles');
             Object.values(data.query.pages).forEach(page => {
            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 => {
                 const div = document.createElement('div');
                 const div = document.createElement('div');
                 div.className = 'random-article';
                 div.className = 'category-group';
               
 
                 const title = document.createElement('h2');
                 const categoryTitle = document.createElement('h2');
                 const link = document.createElement('a');
                 categoryTitle.textContent = category;
                link.href = `http://localhost/wikidef/index.php/${page.title}`;
                div.appendChild(categoryTitle);
                link.textContent = page.title;
 
                title.appendChild(link);
                categories[category].forEach(articleTitle => {
               
                    const articleTitleElement = document.createElement('h3');
                const description = document.createElement('p');
                    const link = document.createElement('a');
                description.innerHTML = page.extract; // Extracted summary of the article
                    link.href = `http://localhost/wikidef/index.php/${articleTitle}`;
               
                    link.textContent = articleTitle;
                div.appendChild(title);
                    articleTitleElement.appendChild(link);
                 div.appendChild(description);
                    div.appendChild(articleTitleElement);
               
                 });
 
                 container.appendChild(div);
                 container.appendChild(div);
             });
             });
        })
        .catch(error => {
            console.error('Error fetching data:', error);
         });
         });
})();
})();

Revision as of 10:11, 5 August 2024

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


(function() {
    // 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 => {
                const div = document.createElement('div');
                div.className = 'category-group';

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

                categories[category].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);
        });
})();