-
Notifications
You must be signed in to change notification settings - Fork 3
/
Restorer.js
78 lines (63 loc) · 2.07 KB
/
Restorer.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/*** Restorer ***/
// Easily restore an older version of a page
// Documentation at [[en:w:User:BrandonXLF/Restorer]]
// By [[en:w:User:BrandonXLF]]
$(function() {
if (mw.config.get('wgAction') != 'history') return;
window.restorerSummary = window.restorerSummary ||
'Restored revision $ID by [[Special:Contributions/$USER|$USER]] ([[en:w:User:BrandonXLF/Restorer|Restorer]])';
function restore(revid) {
var api = new mw.Api();
return api.get({
action: 'query',
revids: revid,
prop: 'revisions',
rvprop: 'user',
format: 'json',
formatversion: '2'
}).then(function(res) {
var user = res.query.pages[0].revisions[0].user;
return api.postWithEditToken({
action: 'edit',
pageid: mw.config.get('wgArticleId'),
undo: mw.config.get('wgCurRevisionId'),
undoafter: revid,
summary: window.restorerSummary.replace(/\$ID/g, revid).replace(/\$USER/g, user)
});
}).then(
function() {
mw.notify('Restored revision successfully.');
location.reload();
},
function(_, data) {
mw.notify(api.getErrorMessage(data), {type: 'error'});
}
);
}
function addLink(item) {
var revid = item.getAttribute('data-mw-revid');
if (revid == mw.config.get('wgCurRevisionId')) return;
var links = item.querySelector('.comment + .mw-changeslist-links');
if (!links) return;
var parent = document.createElement('span'),
el = document.createElement('a');
el.addEventListener('click', function() {
el.className = 'restorer-loading';
restore(revid).always(function() {
el.className = '';
});
});
el.textContent = 'restore';
parent.appendChild(el);
links.appendChild(parent);
}
var parents = document.querySelectorAll('li[data-mw-revid]');
for (var i = 0; i < parents.length; i++) {
addLink(parents[i]);
}
mw.loader.addStyleTag(
'@keyframes restorer-loading {' +
'0%, 100% {content: " ⡁"} 16% {content: " ⡈"} 33% {content: " ⠔"} 50% {content: " ⠒"} 66% {content: " ⠢"} 83% {content: " ⢁"}}' +
'.restorer-loading::after {white-space: pre; content: ""; animation: restorer-loading 0.5s infinite}'
);
});