User:Euphoria/common.js: Difference between revisions

From Test Wiki
Jump to navigation Jump to search
Content deleted Content added
No edit summary
No edit summary
Line 1: Line 1:
// This script assumes you are on a Wikimedia page and have jQuery available
$(document).ready(function() {
$(document).ready(function() {
// Only execute in view mode
var pageTitle = mw.config.get('wgPageName'); // Get the current page title
var apiURL = mw.config.get('wgServer') + mw.config.get('wgScriptPath') + '/api.php';
if (mw.config.get('wgAction') === "view") {
var wikiURL = mw.config.get('wgServer') + mw.config.get('wgScriptPath');
var apiURL = mw.config.get('wgServer') + mw.config.get('wgScriptPath') + '/api.php';
var wikiURL = mw.config.get('wgServer') + mw.config.get('wgScriptPath');


// Function to fetch page creator
// Fetches the creator of the current page
function fetchPageCreator() {
function fetchPageCreator() {
$.ajax({
$.ajax({
url: apiURL,
url: apiURL,
data: {
data: {
action: 'query',
action: 'query',
format: 'json',
format: 'json',
titles: pageTitle,
titles: mw.config.get('wgPageName'),
prop: 'revisions',
prop: 'revisions',
rvlimit: 1,
rvlimit: 1,
rvdir: 'newer',
rvdir: 'newer',
rvprop: 'user'
rvprop: 'user'
},
},
dataType: 'json',
dataType: 'json',
success: function(response) {
success: function(response) {
if (response.query && response.query.pages) {
var pageId = Object.keys(response.query.pages)[0];
var pages = response.query.pages;
fetchUserEditCount(response.query.pages[pageId].revisions[0].user);
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
// Fetches the edit count of the creator
function fetchUserEditCount(username) {
function fetchUserEditCount(username) {
$.ajax({
$.ajax({
url: apiURL,
url: apiURL,
data: {
data: {
action: 'query',
action: 'query',
format: 'json',
format: 'json',
list: 'users',
list: 'users',
ususers: username,
ususers: username,
usprop: 'editcount'
usprop: 'editcount'
},
},
dataType: 'json',
dataType: 'json',
success: function(response) {
success: function(response) {
if (response.query && response.query.users && response.query.users[0]) {
displayCreator(username, response.query.users[0].editcount);
var editCount = response.query.users[0].editcount;
displayCreator(username, editCount);
}
}
}
});
});
}
}


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


var creatorLink = $('<a>')
$('#bodyContent').prepend(creatorInfo);
.attr('href', userPageURL)
}
.text(creator)
.css({'color': '#0645ad'});


var contribsLink = $('<a>')
fetchPageCreator();
.attr('href', userContribsURL)
.text(editCount + ' edits')
.css({'color': '#0645ad'});

var creatorInfo = $('<div>')
.append('Page created by: ')
.append(creatorLink)
.append(' (')
.append(contribsLink)
.append(')')
.css({
'font-size': 'small',
'color': '#666',
'margin-top': '5px'
});

$('#bodyContent').prepend(creatorInfo); // Try '#mw-content-text' if '#bodyContent' doesn't work
}
}

fetchPageCreator();
});
});

Revision as of 09:57, 26 December 2023

$(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.config.get('wgServer') + mw.config.get('wgScriptPath');

        // 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 + '/wiki/User:' + encodeURIComponent(creator)).text(creator).css({'color': '#0645ad'}))
                .append(' (')
                .append($('<a>').attr('href', wikiURL + '/wiki/Special:Contributions/' + encodeURIComponent(creator)).text(editCount + ' edits').css({'color': '#0645ad'}))
                .append(')')
                .css({'font-size': 'x-small', 'color': '#666', 'margin-top': '5px'});

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

        fetchPageCreator();
    }
});