User:Emojiwiki/module/QuickRevert.js

维基百科,自由的百科全书

注意:保存之后,你必须清除浏览器缓存才能看到做出的更改。Google ChromeFirefoxMicrosoft EdgeSafari:按住⇧ Shift键并单击工具栏的“刷新”按钮。参阅Help:绕过浏览器缓存以获取更多帮助。

let QRlog = (function (msg) {
	console.log("[QuickRevert] " + msg);
});

if ( mw.config.get("wgAction") != "history" ) {
	QRlog("Not loading beacuse this is not a history page.");
}
mw.util.addCSS( '.qr-btn::before { content: "[" } .qr-btn::after { content: "]" }' );
let api = new mw.Api();
let curTS;
api.get({
	"action": "query",
	"curtimestamp": true
}).done(function (data) {
	curTS = data.curtimestamp;
});

$(".mw-contributions-current, ul#pagehistory li:first-child").each(function () {
	let $JQ_T = $(this);
	$('<span>')
		.html('QR')
		.addClass('qr-btn')
		.attr('rev',$JQ_T.attr('data-mw-revid'))
		.attr('qr_done','0')
		.click(function () {
			if ($(this).attr('qr_done') == '1') {
				mw.notify('This edit is aready reverted.');
			}
			$(this).html('...');
			let reason = prompt("Give your revert a reason.");
			if ( reason == null ) {
				mw.notify('Action cancelled.');
				$(this).html('QR');
			} else if ( reason == "" ) {
				QRlog("Not given reason, using the default one");
				reason = "No reason given";
			}
			let REVID = $(this).attr('rev');
			api.post({
				"action": "edit",
				"undo": REVID,
				"starttimestamp": curTS,
				"errorformat": "html",
				"summary": "Revert " + REVID + ": " + reason + " (QuickRevert)"
			}).done(function (data) {
				QRlog(data);
				if (data.errors != null) {
					for (let x in data.errors) {
						QRlog("ERROR!\nCode:" + x.code + "\n" + x['*']);
					}
					$(this).html('QR');
				} else {
					QRlog("Revert complete.");
					$(this).attr('qr_done','1');
					$(this).html('<s>QR</s>');
				}
			});
			
		})
		.appendTo($JQ_T);
});