打开/关闭菜单
打开/关闭个人菜单
未登录
未登录用户的IP地址会在进行任意编辑后公开展示。

MediaWiki:Gadget-recentChanges.js

MediaWiki界面页面
更多语言
Forte讨论 | 贡献2022年8月11日 (四) 00:36的版本 (Testing)

注意:在发布之后,您可能需要清除浏览器缓存才能看到所作出的变更的影响。

  • Firefox或Safari:按住Shift的同时单击刷新,或按Ctrl-F5Ctrl-R(Mac为⌘-R
  • Google Chrome:Ctrl-Shift-R(Mac为⌘-Shift-R
  • Internet Explorer或Edge:按住Ctrl的同时单击刷新,或按Ctrl-F5
  • Opera: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('最近更改')
            )

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

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

                if (recentChanges.length > 0) {
                    var Time = 1;
                    $recentChangesDOM = recentChanges.map(function(rc) {
                        const timeMatch = rc.timestamp.match(/([0-9]+)-([0-9]+)-([0-9]+)T([0-9]+):([0-9]+):([0-9]+)Z/);
                    	var editYear = timeMatch[1];
                    	var editMonth = timeMatch[2];
                    	var editDay = timeMatch[3];
                    	var editHour = timeMatch[4];
                    	var editMinute = timeMatch[5];
                    	var editSecond = timeMatch[6];
                    	
                    	var editDate = new Date(editYear, (editMonth-1), editDay, editHour, editMinute, editSecond);
						var currentDate = new Date();
						currentDate = currentDate.getTime() + (currentDate.getTimezoneOffset() * 60000)
						var diffDate = currentDate - editDate;
						var diffDays = Math.floor(diffDate/(1000*60*60*24));
						var diffHours = Math.floor(diffDate/(1000*60*60));
						var diffMinutes = Math.floor(diffDate/(1000*60));
						var diffSeconds = Math.floor(diffDate/(1000));
						
						if (diffDays > 0) {
							Time = diffDays + '天以前 – ';
						} else if (diffHours > 0) {
							Time = diffHours + '小时前 – ';
						} else if (diffMinutes > 0) {
							Time = diffMinutes + '分钟前 – ';
						} else if (diffSeconds > 0) {
							Time = diffSeconds + '秒前 – ';
						} else {
							Time = 1 + Math.floor(Math.random() * 10) + '分钟前 – ';
						}

                        return $('<div>').addClass('rc-sidebar-item').append(
                            $('<a>')
                            .addClass('rc-sidebar-page')
                            .text("Title"),
                            $('<p>')
                            .addClass('rc-sidebar-user')
                            .text("X time ago")
                            .append(
                                $('<a>')
                                .text(rc.user)
                                .attr('href', new mw.Title(rc.user, 2).getUrl())
                            )
                        )
                    })
                } else {
                    $recentChangesDOM = $('<p>').text('最近没有变化。')
                }

                $rcContainer.append($recentChangesDOM)
                var $showMore
				$showMore = $('<div>')
                	.addClass('rc-sidebar-item rc-sidebar-more')
                	.append(
                		$('<a>')
                		.addClass('rc-sidebar-page')
                		.text('See more...')
                		.attr('href', '/wiki/Special:RecentChanges')
                )
                $rcContainer.append($showMore)
            })
            .fail(function(_, data) {
                alert(data.error.info)
            });
    }

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