User:NguoiDungKhongDinhDanh/AjaxRollback.js

Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
// <nowiki>
//
// For attribution:
// * [[User:BenjaminWillJS/AjaxRollback.js]]
// * [[User:Abelmoschus Esculentus/AjaxRollback.js]]
// * [[User:DannyS712/AjaxRollback.js]]

$(function() {
	if (!$('.mw-rollback-link').length) {
		return;
	}
	
	mw.loader.addStyleTag(`
		@keyframes loader {
			0%, 100% {
				content: '••• ';
			}
			25% {
				content: ' •••';
			}
			50% {
				content: '• ••';
			}
			75% {
				content: '•• •';
			}
		}
		
		.loader::after {
			content: '';
			font-family: monospace, monospace;
			font-size: 0.6em;
			white-space: pre;
			animation: loader 0.5s infinite;
		}
	`);
	
	var api = new mw.Api();
	
	mw.hook('wikipage.content').add(function() {
		$('.mw-rollback-link > a').click(function(e) {
			e.preventDefault();
			
			var $span = $(this).parent();
			var href = new mw.Uri($(this).attr('href'));
			
			$span.html(
				$('<span>').addClass('loader')
			);
			
			api.postWithToken('rollback', {
				action: 'rollback',
				title: href.query.title,
				user: href.query.from,
				format: 'json'
			}).done(function() {
				$span.text('✓');
			}).fail(function() {
				$span.text('✗');
			}).always(function() {
				$span.find('.loader').remove();
			});
		});
	});
});

// </nowiki>