User:SD0001/hide-reverted-edits.js: Difference between revisions

Content deleted Content added
.addClass('mw-ui-button') to align with recent change to styling of "Compare selected revisions" button
structure; fixup undo bytecount matching that appears to have broken at some point
 
(7 intermediate revisions by the same user not shown)
Line 5:
*
*/
var hre = {
init: function() {
 
if (mw.config.get('wgAction') !== 'history') return;
$.ready.then(function() {
 
// bool: are the reverts currently hidden or not?
if (mw.config.get('wgAction') !== 'history') return;
var hidingReverts;
 
// bool: are the reverts currently hidden or not?
if (/&hidereverted=y/.test(window.___location.href)) {
var hidingReverts;
hre.hideRevertedEdits();
 
hre.fixLinks(true);
if (/&hidereverted=y/.test(window.___location.href)) {
hidingReverts = true;
hideRevertedEdits();
} else {
fixLinks(true);
hidingReverts = truefalse;
} else {
hidingReverts = false;
}
 
$('<button>')
.addClass('reverted-edits-showhide')
.addClass('mw-ui-button')
.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.
}
$('<button>')
.addClass('reverted-edits-showhide')
.addClass('mw-ui-button')
.text(hidingReverts ? 'Show reverted edits' : 'Hide reverted edits')
.css('margin-left', '3px')
.on('click', function(e) {
e.preventDefault();
if (!hidingReverts) {
hre.hideRevertedEdits();
$('.reverted-edits-showhide').text('Show reverted edits');
} else {
$('#pagehistory li').show();
$('.reverted-edits-showhide').text('Hide reverted edits');
}
hidingReverts = !hidingReverts;
hre.fixLinks(hidingReverts);
}).insertAfter('.mw-history-compareselectedversions input');
},
hideRevertedEdits: function() {
 
$('#pagehistory li').each(function(idx) {
var $this = $(this);
 
if (this.style.display === 'none') {
var editcomment = $this.find('.comment').text();
return true; // continue if already hidden. This handles reverts of reverts.
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 isn't a partial revert, find the byte count changes for the
// two edits: if they add up to 0, then this is a full 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');
}
 
var $this = $(this);
// 'Restore this version' reverts using Twinkle or popups or pending changes reverts
// TW: Reverted to revision 3234343 by ...
var editcomment = $this.find('.comment').text();
// popups: Revert to revision 34234234 by ...
var rgx, reverted_user, last_good_revision;
// 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)) {
// Plain MediaWiki undo with untampered edit summary
var last_good_revision = rgx[1];
if (rgx = /^Undid revision (\d+) by/.exec(editcomment)) {
$this.hide();
var $revreverted_rev_id = $this.next()rgx[1];
if var (parseInt(last_good_revision)$reverted_rev >= parseInt($rev.attr('[data-mw-revid=')) ||+ reverted_rev_id +']');
parseInt(last_good_revision) < 100) { // sanity checks
// just to confirm that the edit isn't a partial revert, find the byte count changes for the
return true; // revision id given has to be wrong
// two edits: if they add up to 0, then this is a full revert (in all likelihood)
}
var diffbytes1 = parseInt($this.find('[class^=mw-plusminus]').text().replace(/−/, '-'));
while ($rev.attr('data-mw-revid') !== last_good_revision) {
var diffbytes2 = parseInt($reverted_rev.find('[class^=mw-plusminus]').text().replace(/−/, '-'));
$rev.hide();
$rev = $rev.next();
if (diffbytes1 + diffbytes2 === 0) {
if ($rev.length === 0) break; // end of page history in current view
$this.hide();
}
$reverted_rev.hide();
if (window.hre_debug) console.log(idx, $this.find('.mw-changeslist-date').text(), 'restore');
if (window.hre_debug) console.log(idx, $this.find('.mw-changeslist-date').text(), 'undo');
 
} else {
 
// 'Restore this version' reverts using Twinkle (old) or popups or pending changes reverts
var reverted_user;
// TW(old): 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)) {
last_good_revision = rgx[1];
if (window.hre_debug) console.log(idx, $this.find('.mw-changeslist-date').text(), 'restore');
// 'Restore this version' reverts using Twinkle (new)
} else if (rgx = /^Restored revision (\d+) by/.exec(editcomment)) {
last_good_revision = rgx[1];
if (window.hre_debug) console.log(idx, $this.find('.mw-changeslist-date').text(), 'tw(new) restore');
// Reverts tagged as "Rollback"
} else if ($this.find('.mw-tag-marker-mw-rollback').length) {
reverted_user = $this.next().find('.mw-userlink bdi').text();
}
 
// Twinkle rollbacks
else 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 vandalism rollbacks, and all reverts using MediaWiki rollback, Huggle, Cluebot have the "Rollback" tag added
// and hence would have been handled above. The regex checks here are to account for old reverts done before the
// "Rollback" tag was introduced
 
// 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, and redwarn rollback
}// elseMW/Huggle: if (rgx = /^Reverted edits by (.*?)User \(talk\)/.exec(editcomment)) {
// RedWarn: Reverting edit(s) by User (talk)
} else if (rgx = /^Revert(?:ed|ing) edit\(?s\)? by (.*?) \(talk\)/.exec(editcomment)) {
reverted_user = rgx[1];
if (window.hre_debug) console.log(idx, $this.find('.mw-changeslist-date').text(), 'mw/huggle/redwarn 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)
Line 150 ⟶ 142:
}
}
 
if (last_good_revision) {
}
 
$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
/**
}
* Add or remove "&hidereverted=y" from the targets of links:
while ($rev.attr('data-mw-revid') !== last_good_revision) {
* (newest | oldest) (newer n | older n) (20 | 50 | 100 | 250 | 500)
$rev.hide();
*/
$rev = $rev.next();
function fixLinks(hidingReverts) {
if ($rev.length === 0) break; // end of page history in current view
if (hidingReverts) {
}
if (!/&hidereverted=y/.test($('.mw-numlink').attr('href'))) {
}
});
},
/**
* Add or remove "&hidereverted=y" from the targets of links:
* (newest | oldest) (newer n | older n) (20 | 50 | 100 | 250 | 500)
*/
fixLinks: function(hidingReverts) {
if (hidingReverts) {
if (!/&hidereverted=y/.test($('.mw-numlink').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 += 'this.href.replace(/&hidereverted=y/, '');
});
}
} else {
$('.mw-firstlink, .mw-lastlink, .mw-prevlink, .mw-nextlink, .mw-numlink').each(function() {
this.href = this.href.replace(/&hidereverted=y/, '');
});
}
}
$(hre.init);