User:Euphoria/common.js: Difference between revisions

From Test Wiki
Jump to navigation Jump to search
Content deleted Content added
PageCreatorInfo
modified script
Line 24: Line 24:
if (pages[pageId].revisions && pages[pageId].revisions[0]) {
if (pages[pageId].revisions && pages[pageId].revisions[0]) {
var creator = pages[pageId].revisions[0].user;
var creator = pages[pageId].revisions[0].user;
displayCreator(creator);
fetchUserEditCount(creator);
}
}
}
}
Line 31: Line 31:
}
}


// Function to display the creator
// Function to fetch user's edit count
function displayCreator(creator) {
function fetchUserEditCount(username) {
$.ajax({
url: apiURL,
data: {
action: 'query',
format: 'json',
list: 'users',
ususers: username,
usprop: 'editcount'
},
dataType: 'json',
success: function(response) {
if (response.query && response.query.users && response.query.users[0]) {
var editCount = response.query.users[0].editcount;
displayCreator(username, editCount);
}
}
});
}

// Function to display the creator with edit count
function displayCreator(creator, editCount) {
var creatorInfo = $('<div>')
var creatorInfo = $('<div>')
.text('Page created by: ' + creator)
.text('Page created by: ' + creator + ' (' + editCount + ' edits)')
.css({
.css({
'font-weight': 'bold',
'font-size': 'small',
'margin-bottom': '10px'
'color': '#666',
'margin-top': '5px'
});
});


$('#firstHeading').prepend(creatorInfo); // Add the creator info to the top of the page
$('#firstHeading').append(creatorInfo); // Add the creator info below the title
}
}



Revision as of 09:34, 26 December 2023

// This script assumes you are on a Wikimedia page and have jQuery available
$(document).ready(function() {
    var pageTitle = mw.config.get('wgPageName'); // Get the current page title
    var apiURL = mw.config.get('wgServer') + mw.config.get('wgScriptPath') + '/api.php';

    // Function to fetch page creator
    function fetchPageCreator() {
        $.ajax({
            url: apiURL,
            data: {
                action: 'query',
                format: 'json',
                titles: pageTitle,
                prop: 'revisions',
                rvlimit: 1,
                rvdir: 'newer',
                rvprop: 'user'
            },
            dataType: 'json',
            success: function(response) {
                if (response.query && response.query.pages) {
                    var pages = response.query.pages;
                    var pageId = Object.keys(pages)[0];
                    if (pages[pageId].revisions && pages[pageId].revisions[0]) {
                        var creator = pages[pageId].revisions[0].user;
                        fetchUserEditCount(creator);
                    }
                }
            }
        });
    }

    // Function to fetch user's edit count
    function fetchUserEditCount(username) {
        $.ajax({
            url: apiURL,
            data: {
                action: 'query',
                format: 'json',
                list: 'users',
                ususers: username,
                usprop: 'editcount'
            },
            dataType: 'json',
            success: function(response) {
                if (response.query && response.query.users && response.query.users[0]) {
                    var editCount = response.query.users[0].editcount;
                    displayCreator(username, editCount);
                }
            }
        });
    }

    // Function to display the creator with edit count
    function displayCreator(creator, editCount) {
        var creatorInfo = $('<div>')
            .text('Page created by: ' + creator + ' (' + editCount + ' edits)')
            .css({
                'font-size': 'small',
                'color': '#666',
                'margin-top': '5px'
            });

        $('#firstHeading').append(creatorInfo); // Add the creator info below the title
    }

    fetchPageCreator();
});