MediaWiki:Common.js: Difference between revisions

From DefacerID Encyclopedia
No edit summary
No edit summary
 
(6 intermediate revisions by the same user not shown)
Line 1: Line 1:
const wikihome = 'http://localhost/wikidef';
const wikihome = 'https://wiki.defacer.id';


$(document).ready(function() {
$(document).ready(function() {
Line 5: Line 5:
});
});


$(document).ready(function () {
    if (mw.config.get('wgUserGroups').indexOf('sysop') === -1) {
        $('#p-tb').hide();
    }
});




Line 16: Line 21:
     }
     }


     fetch(`${wikihome}/api.php?action=query&list=random&rnnamespace=0&rnlimit=10&format=json`)
     fetch(`${wikihome}/api.php?action=query&list=allcategories&aclimit=max&format=json`)
         .then(response => response.json())
         .then(response => response.json())
         .then(data => {
         .then(data => {
             const articles = data.query.random;
             const allCategories = data.query.allcategories
            const container = document.getElementById('random-articles');
                .map(cat => cat['*'])
            container.innerHTML = '';
                .filter(category => category !== 'Pages with ignored display titles');


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


            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 container = document.getElementById('random-articles');
             const pages = data.query.pages;
             container.innerHTML = '';
            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 if (page.title !== 'Main Page') { // Exclude "Main Page"
                    uncategorizedArticles.push(page.title);
                }
            });
 
            const shuffledCategories = shuffleArray(Object.keys(categories));


             shuffledCategories.forEach(category => {
             shuffledCategories.forEach(category => {
                const shuffledArticles = shuffleArray(categories[category]);
                 const div = document.createElement('div');
                 const div = document.createElement('div');
                 div.className = 'category-group';
                 div.className = 'category-group';


                 const categoryTitle = document.createElement('h2');
                 const categoryTitle = document.createElement('h2');
                 categoryTitle.textContent = category;
                 const link = document.createElement('a');
                link.href = `${wikihome}/index.php/Category:${encodeURIComponent(category)}`;
                link.textContent = category;
                categoryTitle.appendChild(link);
                 div.appendChild(categoryTitle);
                 div.appendChild(categoryTitle);


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


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


                 const moreEncyclopediaTitle = document.createElement('h2');
                 fetch(`${wikihome}/api.php?action=query&list=categorymembers&cmtitle=Category:${encodeURIComponent(category)}&cmtype=page&cmlimit=10&format=json`)
                moreEncyclopediaTitle.textContent = 'More Encyclopedia';
                    .then(response => response.json())
                div.appendChild(moreEncyclopediaTitle);
                    .then(data => {
                        const articles = data.query.categorymembers.map(member => member.title);
                        const shuffledArticles = shuffleArray(articles);


                uncategorizedArticles.forEach(articleTitle => {
                        div.removeChild(loadingMessage);
                    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);
                        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);
                        });
                    })
                    .catch(error => {
                        console.error('Error fetching category members:', error);
                        loadingMessage.textContent = 'Error loading articles';
                    });
             });
         })
         })
         .catch(error => {
         .catch(error => {
             console.error('Error fetching data:', error);
             console.error('Error fetching categories:', error);
         });
         });
})();
})();

Latest revision as of 09:58, 15 August 2024

const wikihome = 'https://wiki.defacer.id';

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

$(document).ready(function () {
    if (mw.config.get('wgUserGroups').indexOf('sysop') === -1) {
        $('#p-tb').hide();
    }
});


(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=allcategories&aclimit=max&format=json`)
        .then(response => response.json())
        .then(data => {
            const allCategories = data.query.allcategories
                .map(cat => cat['*'])
                .filter(category => category !== 'Pages with ignored display titles');

            const shuffledCategories = shuffleArray(allCategories);

            const container = document.getElementById('random-articles');
            container.innerHTML = '';

            shuffledCategories.forEach(category => {
                const div = document.createElement('div');
                div.className = 'category-group';

                const categoryTitle = document.createElement('h2');
                const link = document.createElement('a');
                link.href = `${wikihome}/index.php/Category:${encodeURIComponent(category)}`;
                link.textContent = category;
                categoryTitle.appendChild(link);
                div.appendChild(categoryTitle);

                const loadingMessage = document.createElement('h3');
                loadingMessage.textContent = 'loading';
                div.appendChild(loadingMessage);

                container.appendChild(div);

                fetch(`${wikihome}/api.php?action=query&list=categorymembers&cmtitle=Category:${encodeURIComponent(category)}&cmtype=page&cmlimit=10&format=json`)
                    .then(response => response.json())
                    .then(data => {
                        const articles = data.query.categorymembers.map(member => member.title);
                        const shuffledArticles = shuffleArray(articles);

                        div.removeChild(loadingMessage);

                        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);
                        });
                    })
                    .catch(error => {
                        console.error('Error fetching category members:', error);
                        loadingMessage.textContent = 'Error loading articles';
                    });
            });
        })
        .catch(error => {
            console.error('Error fetching categories:', error);
        });
})();