-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathQuickVandalBlock.js
More file actions
154 lines (142 loc) · 5.41 KB
/
QuickVandalBlock.js
File metadata and controls
154 lines (142 loc) · 5.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// Forked from https://en.wikipedia.org/wiki/User:Enterprisey/quick-vand-block.js, with thanks to the original creator, Enterprisey.
// This is a script to help admins process [[WP:AIV]] requests quickly.
// In diffs and filter logs, places ( indef ) links next to usernames, and ( 31h | proxy ) links next to IPs. Clicking one of these will prompt you "are you sure you want to block X?", then will block them for that duration, and leave vandalism-related talk page notifications and edit summaries. Note that clicking "proxy" will skip leaving a talk page notification.
/*
Changes:
* linted/refactored
* added ? to end of question in popup
* added proxy block
* fixed the indef button to work with temporary accounts
*/
// <nowiki>
$.when( mw.loader.using( [ 'mediawiki.api', 'mediawiki.util' ] ), $.ready ).then( () => {
const MONTHS = mw.config.get( 'wgMonthNames' ).slice( 1 ); // theirs starts with the empty string
const api = new mw.Api();
function addLinksAndListener( obj ) {
obj.find( 'span.mw-usertoollinks' ).each( function ( idx, element ) {
const isIp = this.previousElementSibling.className.includes( 'mw-anonuserlink' );
let $linkAndListener;
if ( isIp ) {
$linkAndListener = $( '<a>' )
.attr( 'href', '#' )
.text( '31h' )
.on( 'click', function () {
const username = $( this ).parent().get( 0 ).previousElementSibling.textContent;
const duration = '31 hours';
const logReason = '[[Wikipedia:Vandalism|Vandalism]]';
const templateName = 'uw-vblock';
const templateParams = {
anon: 'yes',
time: '31 hours',
sig: 'yes'
};
const isMainspaceSpecialOrMedia = mw.config.get( 'wgNamespaceNumber' ) < 1;
if ( !isMainspaceSpecialOrMedia ) {
templateParams.page = mw.config.get( 'wgPageName' );
}
block( username, duration, logReason, templateName, templateParams );
} );
$( element ).contents().last().before( ' | ', $linkAndListener );
$linkAndListener = $( '<a>' )
.attr( 'href', '#' )
.text( 'proxy' )
.on( 'click', function () {
const username = $( this ).parent().get( 0 ).previousElementSibling.textContent;
const duration = '1 year';
const logReason = '[[Wikipedia:Vandalism|Vandalism]]';
// no talk page message
const templateName = null;
const templateParams = {};
const isMainspaceSpecialOrMedia = mw.config.get( 'wgNamespaceNumber' ) < 1;
if ( !isMainspaceSpecialOrMedia ) {
templateParams.page = mw.config.get( 'wgPageName' );
}
block( username, duration, logReason, templateName, templateParams );
} );
$( element ).contents().last().before( ' | ', $linkAndListener );
} else {
$linkAndListener = $( '<a>' )
.attr( 'href', '#' )
.text( 'indef' )
.on( 'click', function () {
const username = $( this ).parent().siblings( '.mw-userlink' ).find( 'bdi' ).text();
const duration = 'never';
const logReason = '[[Wikipedia:Vandalism|Vandalism]]';
const templateName = 'uw-vblock';
const templateParams = {
indef: 'yes',
sig: 'yes'
};
const isMainspaceSpecialOrMedia = mw.config.get( 'wgNamespaceNumber' ) < 1;
if ( !isMainspaceSpecialOrMedia ) {
templateParams.page = mw.config.get( 'wgPageName' );
}
block( username, duration, logReason, templateName, templateParams );
} );
$( element ).contents().last().before( ' | ', $linkAndListener );
}
} );
}
function block( username, duration, logReason, templateName, templateParams ) {
if ( confirm( 'Block ' + username + '?' ) ) {
new mw.Api().postWithToken( 'csrf', {
action: 'block',
user: username,
expiry: duration,
reason: logReason,
nocreate: 'true',
autoblock: 'true',
watchuser: 'true',
allowusertalk: 'true'
} ).then( () => {
if ( templateName ) {
mw.notify( 'Blocked ' + username + '; sending notification...' );
deliverBlockTemplate( username, templateName, templateParams );
} else {
mw.notify( 'Blocked ' + username + '. No talk page notification sent.' );
}
} );
return false;
}
}
function deliverBlockTemplate( username, templateName, templateParams ) {
const now = new Date();
const sectionName = MONTHS[ now.getMonth() ] + ' ' + now.getFullYear();
api.get( {
prop: 'revisions',
rvprop: 'content',
rvlimit: '1',
rvslots: 'main',
titles: 'User talk:' + username,
formatversion: '2'
} ).then( ( data ) => {
let existingText;
if ( data.query.pages[ 0 ].missing ) {
existingText = '';
} else {
existingText = data.query.pages[ 0 ].revisions[ 0 ].slots.main.content;
}
const shouldAddSectionHeader = !( new RegExp( /==\s*/.source +
sectionName.replace( ' ', '\\s*' ) + /\s*==/.source ).test( existingText ) );
let textToAdd = '\n\n';
textToAdd += ( shouldAddSectionHeader ? '== ' + sectionName + ' ==\n\n' : '' );
textToAdd += '{{subst:' + templateName;
textToAdd += Object.entries( templateParams )
.map( ( [ key, value ] ) => `|${ key }=${ value }` )
.join( '' );
textToAdd += '}}';
return api.postWithToken( 'csrf', {
action: 'edit',
title: 'User talk:' + username,
appendtext: textToAdd,
summary: 'You have been blocked from editing for persistent vandalism.'
} );
} ).then( () => {
mw.notify( 'Notification sent.' );
} );
}
mw.hook( 'wikipage.content' ).add( ( obj ) => {
addLinksAndListener( obj );
} );
} );
// </nowiki>