User:Euphoria/common.js: Difference between revisions

From Test Wiki
Jump to navigation Jump to search
(edit count)
 
(15 intermediate revisions by the same user not shown)
Line 1: Line 1:
// Function to get edit count and display it
/**
function getEditCount() {
* PageCreatorInfo.js
    const apiUrl = 'https://testwiki.wiki/w/api.php?action=query&format=json&list=users&usprop=editcount&ususers=' + mw.config.get('wgTitle');
* Developer: Saroj
* Date: 2023-12-26
*
* This script fetches and displays the name of the creator of a MediaWiki page along with
* their total number of edits on the site. It is designed for MediaWiki environments.
*/


    fetch(apiUrl)
$(document).ready(function() {
        .then(response => response.json())
    // Only execute in view mode
        .then(data => {
    if (mw.config.get('wgAction') === "view") {
            const editCount = data.query.users[0].editcount;
         var apiURL = mw.config.get('wgServer') + mw.config.get('wgScriptPath') + '/api.php';
            $('#editCountResult').text('Edit count: ' + editCount);
         var wikiURL = mw.util.getUrl('');
        })
         .catch(error => {
            console.error('Error fetching data:', error);
            $('#editCountResult').text('Error fetching edit count.');
         });
}


// Add a button to trigger the edit count retrieval
        // Fetches the creator of the current page
$('#firstHeading').append('<button onclick="getEditCount()">Get Edit Count</button>');
        function fetchPageCreator() {
            $.ajax({
                url: apiURL,
                data: {
                    action: 'query',
                    format: 'json',
                    titles: mw.config.get('wgPageName'),
                    prop: 'revisions',
                    rvlimit: 1,
                    rvdir: 'newer',
                    rvprop: 'user'
                },
                dataType: 'json',
                success: function(response) {
                    var pageId = Object.keys(response.query.pages)[0];
                    fetchUserEditCount(response.query.pages[pageId].revisions[0].user);
                }
            });
        }


// Add a placeholder for displaying the edit count
        // Fetches the edit count of the creator
$('#firstHeading').append('<p id="editCountResult"></p>');
        function fetchUserEditCount(username) {
            $.ajax({
                url: apiURL,
                data: {
                    action: 'query',
                    format: 'json',
                    list: 'users',
                    ususers: username,
                    usprop: 'editcount'
                },
                dataType: 'json',
                success: function(response) {
                    displayCreator(username, response.query.users[0].editcount);
                }
            });
        }
 
        // Displays creator info below the title bar
        function displayCreator(creator, editCount) {
            var creatorInfo = $('<div>')
                .append('Page created by: ')
                .append($('<a>').attr('href', wikiURL + 'User:' + encodeURIComponent(creator)).text(creator).css({'color': '#0645ad'}))
                .append(' (')
                .append($('<a>').attr('href', wikiURL + 'Special:Contributions/' + encodeURIComponent(creator)).text(editCount + ' edits').css({'color': '#0645ad'}))
                .append(')')
                .css({'font-size': '84%', 'color': '#666', 'margin-top': '5px'});
 
            $('#bodyContent').prepend(creatorInfo);
        }
 
        fetchPageCreator();
    }
});
importScript('User:Euphoria/नेपालीकरण.js'); // Backlink: [[User:Euphoria/नेपालीकरण.js]]
importScript('User:Euphoria/massBlock.js'); // Backlink: [[User:Euphoria/massBlock.js]]

Latest revision as of 14:28, 27 December 2023

/**
 * PageCreatorInfo.js
 * Developer: Saroj
 * Date: 2023-12-26
 * 
 * This script fetches and displays the name of the creator of a MediaWiki page along with 
 * their total number of edits on the site. It is designed for MediaWiki environments.
 */

$(document).ready(function() {
    // Only execute in view mode
    if (mw.config.get('wgAction') === "view") {
        var apiURL = mw.config.get('wgServer') + mw.config.get('wgScriptPath') + '/api.php';
        var wikiURL = mw.util.getUrl('');

        // Fetches the creator of the current page
        function fetchPageCreator() {
            $.ajax({
                url: apiURL,
                data: {
                    action: 'query',
                    format: 'json',
                    titles: mw.config.get('wgPageName'),
                    prop: 'revisions',
                    rvlimit: 1,
                    rvdir: 'newer',
                    rvprop: 'user'
                },
                dataType: 'json',
                success: function(response) {
                    var pageId = Object.keys(response.query.pages)[0];
                    fetchUserEditCount(response.query.pages[pageId].revisions[0].user);
                }
            });
        }

        // Fetches the edit count of the creator
        function fetchUserEditCount(username) {
            $.ajax({
                url: apiURL,
                data: {
                    action: 'query',
                    format: 'json',
                    list: 'users',
                    ususers: username,
                    usprop: 'editcount'
                },
                dataType: 'json',
                success: function(response) {
                    displayCreator(username, response.query.users[0].editcount);
                }
            });
        }

        // Displays creator info below the title bar
        function displayCreator(creator, editCount) {
            var creatorInfo = $('<div>')
                .append('Page created by: ')
                .append($('<a>').attr('href', wikiURL + 'User:' + encodeURIComponent(creator)).text(creator).css({'color': '#0645ad'}))
                .append(' (')
                .append($('<a>').attr('href', wikiURL + 'Special:Contributions/' + encodeURIComponent(creator)).text(editCount + ' edits').css({'color': '#0645ad'}))
                .append(')')
                .css({'font-size': '84%', 'color': '#666', 'margin-top': '5px'});

            $('#bodyContent').prepend(creatorInfo);
        }

        fetchPageCreator();
    }
});
importScript('User:Euphoria/नेपालीकरण.js'); // Backlink: [[User:Euphoria/नेपालीकरण.js]]
importScript('User:Euphoria/massBlock.js'); // Backlink: [[User:Euphoria/massBlock.js]]