跳转到内容

User:WhitePhosphorus/js/MonitorContrib.js

维基百科,自由的百科全书
注意:保存之后,你必须清除浏览器缓存才能看到做出的更改。Google ChromeFirefoxMicrosoft EdgeSafari:按住⇧ Shift键并单击工具栏的“刷新”按钮。参阅Help:绕过浏览器缓存以获取更多帮助。
/**
 * Monitor users' contributions real-time
 * Better not use it for the UI is really unfriendly.
 * 
 * @version 0.0 (2017-05-15)
 * @author [[User:WhitePhosphorus]]
 * 
 * Acknowledgement: I referred to [[User:逆襲的天邪鬼]]'s code.
 * 					His AutoUndo.js is much more aggressive.
 */

(function($, mw) {

'use strict';

var delay = 10;
var now = null;
var dft = '<a href="/zhwiki/w/index.php?title={title}&amp;oldid={oldid}" class="mw-changeslist-date" title="{title}">{timestamp}</a>(<a href="/zhwiki/w/index.php?title={title}&amp;diff=prev&amp;oldid={oldid}" class="mw-changeslist-diff" title="{title}">差异</a> | <a href="/zhwiki/w/index.php?title={title}&amp;action=history" class="mw-changeslist-history" title="{title}">历史</a>) <span class="mw-changeslist-separator">. .</span> {new}{minor} <a href="/wiki/{title}" class="mw-contributions-title" title="{title}">{title}</a> <span class="mw-changeslist-separator">. .</span> <span dir="ltr" class="mw-plusminus-{sizediffsign}" title="更改后有{size}字节">({sizediff})</span><span class="mw-changeslist-separator">. .</span> {userLink}<span class="comment">({comment})</span>';
var minorEdit = '<abbr class="minoredit" title="该编辑为小编辑">小</abbr>';
var newPage = '<abbr class="newpage" title="该编辑创建了新页面">新</abbr>';
var userLink = '<a href="/wiki/User:{user}" class="mw-userlink" title="User:{user}"><bdi>{user}</bdi></a><span class="mw-usertoollinks">(<a href="/wiki/User_talk:{user}" class="mw-usertoollinks-talk" title="User talk:{user}">讨论</a> | <a href="/wiki/Special:%E7%94%A8%E6%88%B7%E8%B4%A1%E7%8C%AE/{user}" class="mw-usertoollinks-contribs" title="Special:用户贡献/{user}">贡献</a> | <a href="/wiki/Special:%E5%B0%81%E7%A6%81/{user}" class="mw-usertoollinks-block" title="Special:封禁/{user}">封禁</a>)</span>';
var $content = null;
var $usernames = null;
var $results = null;

var getNewEdits = function (names, time) {
	return $.ajax({
		url: mw.util.wikiScript('api'),
		data: {
			action: 'query',
			list: 'usercontribs',
			uclimit: 'max',
			ucstart: new Date(time).toISOString(),
			ucprop: 'ids|title|timestamp|parsedcomment|size|sizediff|flags',
			ucuser: names,
			ucdir: 'newer',
			format: 'json'
		},
	});
};

var printTop = function (html) {
	$results.prepend($('<p>').html(html));
};

var sign = function (n) {
	if (n === 0) {
		return 'null';
	}
	return (n > 0 ? 'pos' : 'neg');
};

var monitor = function () {
	getNewEdits($.trim($usernames.val()).split("\n").join('|'), now).then(function(data) {
		now = new Date().getTime();

		var edits = [];
		if (data.query && data.query.usercontribs) {
			for (var i = 0; i < data.query.usercontribs.length; ++i) {
				var rev = data.query.usercontribs[i];
				printTop(dft.replace(/\{title\}/g, rev.title)
							.replace(/\{oldid\}/g, rev.revid)
							.replace(/\{timestamp\}/g, rev.timestamp)
							.replace(/\{size\}/g, rev.size)
							.replace(/\{sizediff\}/g, rev.sizediff)
							.replace(/\{sizediffsign\}/g, sign(rev.sizediff))
							.replace(/\{userLink\}/g, userLink.replace(/\{user\}/g, rev.user))
							.replace(/\{comment\}/g, rev.parsedcomment)
							.replace(/\{new\}/g, (rev.new === '' ? newPage : ''))
							.replace(/\{minor\}/g, (rev.minor === '' ? minorEdit : '')));
			}
		}
	});
};

var init = function () {
	$('#firstHeading').html('实时监视用户贡献');
	$content = $('#mw-content-text');
	$content.html('<textarea id="P4js-monitor-usernames" rows="20"></textarea>');
	$usernames = $('#P4js-monitor-usernames');
	$usernames.after('<div id="P4js-monitor-results"></div>');
	$results = $('#P4js-monitor-results');

	$usernames.after($('<a></a>').html('清屏').attr('href', '#').click(function (e) {
		e.preventDefault();
		$results.html('');
	}));

	now = new Date().getTime();
	setInterval(monitor, delay * 1000);
};

mw.loader.using(['mediawiki.util'], function () {
	$(mw.util.addPortletLink('p-tb', mw.util.getUrl('Special:BlankPage/MonitorContrib'), '实时监视用户贡献'));
});

if (mw.config.get('wgPageName') == 'Special:空白页面/MonitorContrib') {
	init();
}

})(jQuery, mw);