User:SD0001/hide-reverted-edits.js
Appearance
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
![]() | This user script seems to have a documentation page at User:SD0001/hide-reverted-edits. |
/**
* Script to provide a button on page histories
* which when clicked shows a sanitized version of the history
* with reverted edits (and their reversions) removed
*
*/
$.ready.then(function() {
if (mw.config.get('wgAction') !== 'history') return;
// bool: are the reverts currently hidden or not?
var hidingReverts;
if (/&hidereverted=y/.test(window.location.href)) {
hideRevertedEdits();
fixLinks(true);
hidingReverts = true;
} else {
hidingReverts = false;
}
$('<button>')
.addClass('reverted-edits-showhide')
.text(hidingReverts ? 'Show reverted edits' : 'Hide reverted edits')
.on('click', function(e) {
e.preventDefault();
if (!hidingReverts) {
hideRevertedEdits();
$('.reverted-edits-showhide').text('Show reverted edits');
} else {
$('#pagehistory li').show();
$('.reverted-edits-showhide').text('Hide reverted edits');
}
hidingReverts = !hidingReverts;
fixLinks(hidingReverts);
}).insertAfter('.mw-history-compareselectedversions');
$('.mw-history-compareselectedversions').css('display', 'inline');
});
function hideRevertedEdits() {
$('#pagehistory li').each(function(idx) {
if (this.style.display === 'none') {
return true; // continue if already hidden. This handles reverts of reverts.
}
var editcomment = $(this).find('.comment').text();
var rgx;
// Plain mediawiki undo with untampered edit summary
if (rgx = /^Undid revision (\d+) by/.exec(editcomment)) {
var reverted_rev_id = rgx[1];
var $reverted_rev = $('[data-mw-revid=' + reverted_rev_id +']');
// just to confirm that the edit is a revert, find the byte count changes for the
// two edits: if they add up to 0, then this is a revert (in all likelihood)
var diffbytes1 = parseInt($(this).find('[class^=mw-plusminus]').text());
var diffbytes2 = parseInt($reverted_rev.find('[class^=mw-plusminus]').text());
if (diffbytes1 + diffbytes2 === 0) {
$(this).hide();
$reverted_rev.hide();
if (window.hre_debug) console.log(idx, $(this).find('.mw-changeslist-date').text(), 'undo');
}
// 'Restore this version' reverts using Twinkle or popups or the pending changes tool
// TW: Reverted to revision 3234343 by ...
// popups: Revert to revision 34234234 by ...
// PC tool: Revereted 3 pending edits by Foo and Bar to revision 3243432 by ...
} else if (rgx = /^Revert(?:ed)? (?:\d+ pending edits? by .*?)?to revision (\d+)/.exec(editcomment)) {
var last_good_revision = rgx[1];
$(this).hide();
var rev = $(this).next();
if (parseInt(last_good_revision) > parseInt(rev.attr('data-mw-revid')) ||
parseInt(last_good_revision) < 100) { // sanity checks
return true; // revision id given has to be wrong
}
while ( rev.attr('data-mw-revid') !== last_good_revision ) {
rev.hide();
rev = rev.next();
if (rev.length === 0) break; // end of page history in current view
}
if (window.hre_debug) console.log(idx, $(this).find('.mw-changeslist-date').text(), 'TW restore');
} else {
var reverted_user;
// Twinkle rollbacks
if (rgx = /^Reverted (?:good faith|\d+) edits? by (.*?) \(talk\)/.exec(editcomment)) {
reverted_user = rgx[1];
if (window.hre_debug) console.log(idx, $(this).find('.mw-changeslist-date').text(), 'Twinkle rollback');
// Old Twinke vandalism rollback
} else if (rgx = /^Reverted \d+ edits? by (.*?) identified as vandalism/.exec(editcomment)) {
reverted_user = rgx[1];
if (window.hre_debug) console.log(idx, $(this).find('.mw-changeslist-date').text(), 'Twinkle (old) rollback');
// STiki AGF/normal/vandalism revert
} else if (rgx = /^Reverted \d+ (?:good faith )?edits? by (.*?) (?:identified as test\/vandalism )?using STiki/.exec(editcomment)) {
reverted_user = rgx[1];
if (window.hre_debug) console.log(idx, $(this).find('.mw-changeslist-date').text(), 'STiki rollback');
// normal mediawiki rollback and Huggle rollback
} else if (rgx = /^Reverted edits by (.*?) \(talk\)/.exec(editcomment)) {
reverted_user = rgx[1];
if (window.hre_debug) console.log(idx, $(this).find('.mw-changeslist-date').text(), 'mw/huggle rollback');
// ClueBot
} else if ( ['ClueBot NG', 'ClueBot'].includes($(this).find('.mw-userlink bdi').text()) ) {
reverted_user = /^Reverting possible vandalism by (.*?) to version by/.exec(editcomment)[1];
if (window.hre_debug) console.log(idx, $(this).find('.mw-changeslist-date').text(), 'cluebot rollback');
// XLinkBot
} else if ( $(this).find('.mw-userlink bdi').text() === 'XLinkBot' ) {
reverted_user = /^BOT--Reverting link addition\(s\) by (.*?) to/.exec(editcomment)[1];
if (window.hre_debug) console.log(idx, $(this).find('.mw-changeslist-date').text(), 'xlinkbot rollback');
}
if (reverted_user) {
// page history shows compressed IPv6 address (with multiple 0's replaced by ::)
// though rollback edit summaries use the uncompressed form (though with leading 0's removed)
if (mw.util.isIPv6Address(reverted_user)) {
reverted_user = reverted_user.replace(/\b(?:0+:){2,}/, ':').toLowerCase();
}
$(this).hide();
var rev = $(this).next();
while ( rev.find('.mw-userlink bdi').text() === reverted_user ) {
rev.hide();
rev = rev.next();
if (rev.length === 0) break; // end of page history (in current view)
}
}
}
});
}
function fixLinks(hidingReverts) {
if (hidingReverts && !/&hidereverted=y/.test($('.mw-firstlink').attr('href'))) {
$('.mw-firstlink, .mw-lastlink, .mw-prevlink, .mw-nextlink, .mw-numlink').each(function() {
this.href += '&hidereverted=y';
});
} else {
$('.mw-firstlink, .mw-lastlink, .mw-prevlink, .mw-nextlink, .mw-numlink').each(function() {
this.href.replace(/&hidereverted=y/, '');
});
}
}