Jump to content

MediaWiki:Common.js

From Gyaanipedia

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.
/* Any JavaScript here will be loaded for all users on every page load. */

// Custom Random Page with Minimum Word Count
$(document).ready(function() {
    // Override the default random page link behavior
    $('a[href*="Special:Random"]').on('click', function(e) {
        e.preventDefault(); // Prevent default link behavior
        
        // Function to count words in text
        function countWords(text) {
            return text.replace(/[\s\n]+/g, ' ').trim().split(' ').length;
        }
        
        // Function to fetch a random page and check word count
        function getRandomPageWithMinWords(minWords) {
            $.ajax({
                url: mw.util.wikiScript('api'),
                data: {
                    action: 'query',
                    format: 'json',
                    list: 'random',
                    rnnamespace: 0, // Main namespace only
                    rnlimit: 1
                },
                dataType: 'json',
                success: function(data) {
                    if (data.query && data.query.random && data.query.random.length > 0) {
                        var pageId = data.query.random[0].id;
                        // Fetch page content
                        $.ajax({
                            url: mw.util.wikiScript('api'),
                            data: {
                                action: 'query',
                                format: 'json',
                                prop: 'revisions',
                                pageids: pageId,
                                rvprop: 'content',
                                rvslots: 'main'
                            },
                            dataType: 'json',
                            success: function(contentData) {
                                var pageContent = contentData.query.pages[pageId].revisions[0].slots.main['*'];
                                var wordCount = countWords(pageContent);
                                
                                if (wordCount >= minWords) {
                                    // Redirect to the page if it meets the word count
                                    window.location = mw.util.getUrl(contentData.query.pages[pageId].title);
                                } else {
                                    // Retry if word count is too low
                                    getRandomPageWithMinWords(minWords);
                                }
                            },
                            error: function() {
                                // Retry on error
                                getRandomPageWithMinWords(minWords);
                            }
                        });
                    } else {
                        // Retry if no page is returned
                        getRandomPageWithMinWords(minWords);
                    }
                },
                error: function() {
                    // Retry on error
                    getRandomPageWithMinWords(minWords);
                }
            });
        }
        
        // Start the process with minimum 50 words
        getRandomPageWithMinWords(50);
    });
});