User:Euphoria/common.js

From Test Wiki
Revision as of 09:01, 26 December 2023 by Euphoria (talk | contribs) (PageCreatorInfo)

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)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
// 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;
                        displayCreator(creator);
                    }
                }
            }
        });
    }

    // Function to display the creator
    function displayCreator(creator) {
        var creatorInfo = $('<div>')
            .text('Page created by: ' + creator)
            .css({
                'font-weight': 'bold',
                'margin-bottom': '10px'
            });

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

    fetchPageCreator();
});