User:Enterprisey/revert-and-block.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:Enterprisey/revert-and-block. |
$( function () {
var ADVERT = " ([[User:Enterprisey/revert-and-block|RNB]])";
if( mw.config.get( "wgAction" ) === "history" ) {
mw.loader.using( [ "mediawiki.util", "mediawiki.api" ] ).then( function () {
var MONTHS = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
var api = new mw.Api();
function deliverBlockTemplate( username, isAnon ) {
var now = new Date();
var sectionName = MONTHS[now.getMonth()] + " " + now.getFullYear();
api.get( {
prop: "revisions",
rvprop: "content",
rvlimit: "1",
rvslots: "main",
titles: "User talk:" + username,
formatversion: "2"
} ).then( function ( data ) {
var existingText;
if( data.query.pages[0].missing ) {
existingText = "";
} else {
existingText = data.query.pages[0].revisions[0].slots.main.content;
}
var shouldAddSectionHeader = !( new RegExp( /==\s*/.source +
sectionName.replace( " ", "\\s*" ) + /\s*==/.source ).test( existingText ) );
var textToAdd = "\n\n" +
( shouldAddSectionHeader
? "== " + sectionName + " ==\n\n"
: ""
) +
"<div class="user-block" style="min-height: 40px">[[File:Stop x nuvola with clock.svg|40px|left|alt=Stop icon with clock]]<div style="margin-left:45px">You have been '''[[WP:Blocking policy|blocked]]''' from editing for a period of '''31 hours''' to prevent further [[Wikipedia:Vandalism|vandalism]], as you did at [[:" + mw.config.get( "wgPageName" ) + "]]. Once the block has expired, you are welcome to [[WP:Five pillars|make useful contributions]]. </div><div style="margin-left:45px">If you think there are good reasons for being unblocked, please read the [[WP:Guide to appealing blocks|guide to appealing blocks]], then add the following text below the block notice on your talk page: <!-- Copy the text as it appears on your page, not as it appears in this edit area. Do not include the "tlx|" code. -->{{tlx|unblock|2=reason=''Your reason here ~~~~''}}. </div></div><!-- Template:uw-vblock -->";
return api.postWithToken( "csrf", {
action: "edit",
title: "User talk:" + username,
appendtext: textToAdd,
summary: "You have been blocked from editing for persistent vandalism." + ADVERT
} );
} ).then( function () {
mw.notify( "Notification sent." );
} );
}
$( "span.mw-changeslist-links:not(.mw-history-histlinks):not(.mw-usertoollinks)" ).append( " | ",
$( "<a>" )
.attr( "href", "#" )
.text( "RNB" )
.click( function () {
var api = new mw.Api();
var parentLine = $( this ).parents( "li" );
var username = parentLine.find( ".history-user a" ).get( 0 ).textContent;
var parentLineEl = parentLine.get(0);
// A crucial confirmation...
if( confirm( "Revert and block " + username + "?" ) ) {
// Revert edit
api.postWithToken( "csrf", {
action: "edit",
title: mw.config.get( "wgPageName" ),
undo: parentLineEl.dataset.mwRevid,
undoafter: parentLineEl.nextElementSibling && parentLineEl.nextElementSibling.dataset.mwRevid
} ).then( function () { mw.notify( "Undid revision." ); } );
// Add talk page template
var isAnon = parentLine.find( ".history-user a" ).hasClass( "mw-anonuserlink" );
deliverBlockTemplate( username, isAnon );
// Place indef/31 hr block (for logged-in users & IPs respectively)
api.postWithToken( "csrf", {
action: "block",
user: username,
expiry: isAnon ? "31 hours" : "never",
reason: "[[Wikipedia:Vandalism|Vandalism]]",
nocreate: "true",
autoblock: "false",//true",
watchuser: "true",
} ).then( function () { mw.notify( "Blocked." ); } );
}
} ) );
} );
}
} );