User:DanCherek/UAABotRemover.js
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
/**
* Attribution: This script is a derivative of [[User:Σ/Testing facility/Archiver.js]]
* and [[User:DannyS712/SectionRemover.js]].
*/
$.when(mw.loader.using(['mediawiki.util', 'mediawiki.api']), $.ready).done(
function() {
var validReports = {},
fromTos = {},
wikiText = '',
revStamp, startRemoveButton, overlay;
if (mw.config.get('wgPageName') !==
'Wikipedia:Usernames_for_administrator_attention' &&
mw.config.get('wgPageName') !==
'Wikipedia:Usernames_for_administrator_attention/Bot') {
return;
}
$('head').append(
'<style>a.remylink { font-weight:bold } .remyhighlight { background-color:#b3ffcc }</style>'
);
startRemoveButton = mw.util.addPortletLink(
'p-cactions',
'#',
'Remove bot reports',
'ca-rmbot',
'Enter/exit the removal process',
null,
null
);
overlay = $(document.createElement('button'));
$(startRemoveButton).click(function(e) {
$('.remylink').click();
$('.remy').toggle();
$('#RemoveUAAbutton').toggle();
});
overlay.html("Remove the selected reports")
.attr("id", 'RemoveUAAbutton')
.css("position", 'sticky')
.css("bottom", 0)
.css("width", '100%')
.css("font-size", '200%');
$(document.body).append(overlay);
overlay.toggle();
overlay.click(function(e) {
var numOfReports, sections, archiveThis, cutOffset,
revisedPage;
function cut(s, start, end) {
return s.substr(0, start) + s.substring(end);
}
cutOffset = numOfReports = 0;
revisedPage = wikiText;
sections = $('a.remylink').map(function() {
return $(this).attr('data-name');
});
if (!(numOfReports = sections.length)) {
return alert('No reports selected, aborting');
}
sections.each(function(i, n) {
revisedPage = cut(
revisedPage,
fromTos[n][0] - cutOffset,
fromTos[n][1] - cutOffset
);
cutOffset += fromTos[n][1] - fromTos[n][0];
});
archiveThis = sections.map(function() {
return wikiText.substring(fromTos[this][0],
fromTos[this][1]);
}).toArray().join('');
if (1) {
new mw.Api().postWithToken(
'csrf', {
action: 'edit',
title: 'Wikipedia:Usernames_for_administrator_attention/Bot',
text: revisedPage,
summary: 'Removed bot report(s) using [[User:DanCherek/UAABotRemover|UAABotRemover]]',
basetimestamp: revStamp,
starttimestamp: revStamp
}
).done(function() {
new mw.Api().edit(
mw.config.get('wgPageName'),
function(rev) {
return rev.content;
}).then(function() {
window.location.reload();
});
}).fail(function() {
alert(
'failed to remove reports. aborting.'
);
window.location.reload();
});
}
});
new mw.Api().get({
action: 'query',
pageids: 11238105,
prop: ['revisions'],
rvprop: ['content', 'timestamp']
}).done(function(reader) {
var idx = [],
rv;
rv = reader.query.pages[11238105].revisions[0];
wikiText = rv['*'];
revStamp = rv.timestamp;
$('#mw-content-text')
.find("span[id$='-Bot-reported']")
.each(function(i, title) {
var username;
username = $(this).parent()[0].textContent;
idx[i] = wikiText.indexOf(username);
$(this).closest('.userlinks')[0]
.innerHTML += ' ' +
'<span class=remy style=display:none>' +
'<span class=mw-editsection-bracket>[</span>' +
'<a data-name=' + i + ' ' +
'onclick=$(this).closest(\'li\').toggleClass(\'remyhighlight\');$(this).toggleClass(\'remylink\');>' +
'remove report</a>' +
'<span class=mw-editsection-bracket>]</span>' +
'</span>';
});
for (let i = 0; i < idx.length; i++) {
fromTos[i] = [
idx[i] - 14,
idx[i + 1] !== undefined ? idx[i + 1] - 14 :
Infinity
];
}
});
});