Toggle menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

MediaWiki:Gadget-recentChanges.js

MediaWiki interface page
More languages
Revision as of 17:24, 1 July 2022 by Forte (talk | contribs) (Collectivized code from Runescape Wiki)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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.
//<nowiki>
/**
 * Adds a recent changes widget to the sidebar
 *
 * @author JaydenKieran
 */
'use strict';

;
(function($, mw) {
    var $prependTo
    var $rcContainer
    var recentChanges
    var $recentChangesDOM

    function init() {
        $prependTo = $('#p-Navigation')
        var api = new mw.Api()

        // Build our container
        $rcContainer = $('<nav>')
            .addClass('vector-menu vector-menu-portal portal')
            .attr('id', 'p-RecentChanges')
            .append(
                $('<h3>').text('Latest changes')
            )

        // Add the container to the sidebar
        $prependTo.after($rcContainer)

        api.get({
                action: "query",
                list: "recentchanges",
                rcprop: "title|timestamp|sizes|user",
                rcnamespace: 0,
                rclimit: "4",
                rctype: "edit|new",
                rcshow: "!bot",
                rctoponly: 1,
                format: "json"
            })
            .done(function(data) {
                if (data.query && data.query.recentchanges) {
                    recentChanges = data.query.recentchanges
                }

                if (recentChanges.length > 0) {
                    var placeholderTime = 1;

                    $recentChangesDOM = recentChanges.map(function(rc) {
                        placeholderTime += Math.floor(Math.random() * 10);

                        return $('<div>').addClass('rc-sidebar-item').append(
                            $('<a>')
                            .addClass('rc-sidebar-page')
                            .text(' ' + rc.title)
                            .attr('href', new mw.Title(rc.title).getUrl()),
                            $('<p>')
                            .addClass('rc-sidebar-user')
                            .text(placeholderTime + 'm ago – ')
                            .append(
                                $('<a>')
                                .text(rc.user)
                                .attr('href', new mw.Title(rc.user, 2).getUrl())
                            )
                        )
                    })
                } else {
                    $recentChangesDOM = $('<p>').text('No recent changes.')
                }

                $rcContainer.append($recentChangesDOM)
            })
            .fail(function(_, data) {
                alert(data.error.info)
            });
    }

    mw.loader.using(['mediawiki.util', 'mediawiki.api'], function() {
        $(init)
    })
}(jQuery, mediaWiki));
//</nowiki>