User:Ahmad252/Restorer.js
Jump to navigation
Jump to search
Note: After saving, you have to bypass your browser's cache to see the changes. Internet Explorer: press Ctrl-F5, Mozilla: hold down Shift while clicking Reload (or press Ctrl-Shift-R), Opera/Konqueror: press F5, Safari: hold down Shift + Alt while clicking Reload, Chrome: hold down Shift while clicking Reload.
Documentation for this user script can be added at User:Ahmad252/Restorer. |
// Easily restore an older version of a page
// Documentation at [[User:BrandonXLF/Restorer]]
// Original version: [[:en:User:BrandonXLF/Restorer.js]]
$(function(){
function restore (user, revid) {
$.post( mw.config.get('wgScriptPath') + '/api.php', {
action: 'edit',
pageid: mw.config.get('wgArticleId'),
undo: mw.config.get('wgCurRevisionId'),
undoafter: revid,
summary: 'Restored revision ' + revid + ' by [[Special:Contributions/' + user + '|' + user + ']] ([[en:User:BrandonXLF/Restorer|Restorer]])',
token: mw.user.tokens.get('csrfToken'),
format: 'json'
})
.fail(function (a,b,c,d,e) {
console.log(a,b,c,d,e);
mw.notify('An error occured while restoring the revision.', {type:'error'});
})
.done(function (result) {
console.log(result);
if (result.error) {
mw.notify(result.error.info, {type:'error'});
} else {
mw.notify('Restored revision sucessfully.');
location.reload();
}
});
}
function addLink (item) {
var revid = item.getAttribute('data-mw-revid'), user, links, ele, parent;
if (revid != mw.config.get('wgCurRevisionId')) {
user = item.getElementsByClassName('history-user')[0].getElementsByTagName('a')[0].getAttribute('title').replace('User:','').replace('Special:Contribs/','').replace('Special:Contributions/','');
links = item.getElementsByClassName('mw-changeslist-links');
links = links[links.length - 1];
parent = document.createElement('span');
ele = document.createElement('a');
ele.addEventListener('click', function() {
restore(user, revid);
});
ele.innerHTML = 'restore';
parent.appendChild(ele);
links.appendChild(parent);
}
}
(function () {
if (location.search.includes('action=history')) {
var i, parents = document.querySelectorAll('li[data-mw-revid]');
console.log(parents);
for (i = 0; i < parents.length; i++) {
addLink(parents[i]);
}
}
})();
});