-
Notifications
You must be signed in to change notification settings - Fork 3
/
AJAXUndo.js
55 lines (48 loc) · 1.41 KB
/
AJAXUndo.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/*** AJAX Undo ***/
// Adds a button to undo changes using AJAX to history pages
// Documentation at [[en:w:User:BrandonXLF/AJAXUndo]]
// By [[en:w:User:BrandonXLF]]
$(function() {
if (mw.config.get('wgAction') != 'history') return;
function undo(undoId, undoafter) {
return new mw.Api().postWithEditToken({
action: 'edit',
undo: undoId,
undoafter: undoafter,
title: mw.config.get('wgPageName'),
}).then(
function() {
mw.notify('Edit undone successfully! Reloading...');
location.reload();
},
function(_, data) {
mw.notify(new mw.Api().getErrorMessage(data), {type: 'error'});
}
);
}
$('.mw-history-undo').parent().after(
$('<span>').append(
$('<a>')
.text('ajax undo')
.click(function() {
var el = $(this),
undoLink = el
.closest('.mw-changeslist-links')
.find('.mw-history-undo a')
.attr('href');
el.addClass('ajax-undo-loading');
undo(
undoLink.match(/undo=([0-9]+)/)[1],
undoLink.match(/undoafter=([0-9]+)/)[1]
).always(function() {
el.removeClass('ajax-undo-loading');
});
})
)
);
mw.loader.addStyleTag(
'@keyframes ajax-undo-loading {' +
'0%, 100% {content: " ⡁"} 16% {content: " ⡈"} 33% {content: " ⠔"} 50% {content: " ⠒"} 66% {content: " ⠢"} 83% {content: " ⢁"}}' +
'.ajax-undo-loading::after {white-space: pre; content: ""; animation: ajax-undo-loading 0.5s infinite}'
);
});