MediaWiki:Gadget-blueUsers.js
Vai alla navigazione
Vai alla ricerca
Nota: dopo aver pubblicato, potrebbe essere necessario pulire la cache del proprio browser per vedere i cambiamenti.
- Firefox / Safari: tieni premuto il tasto delle maiuscole Shift e fai clic su Ricarica, oppure premi Ctrl-F5 o Ctrl-R (⌘-R su Mac)
- Google Chrome: premi Ctrl-Shift-R (⌘-Shift-R su un Mac)
- Internet Explorer / Edge: tieni premuto il tasto Ctrl e fai clic su Aggiorna, oppure premi Ctrl-F5
- Opera: premi Ctrl-F5.
mw.loader.using( [ 'mediawiki.api', 'mediawiki.util' ] ).then( function () {
const namespaceIds = mw.config.get('wgNamespaceIds');
const namespaces = Object.keys( namespaceIds ).filter( ns => namespaceIds[ns] === 2 );
const selector = namespaces.map( ns => mw.config.get('wgArticlePath').replace('$1', ns + ':').toLowerCase() );
// User cache to not check them multiple times.
var blueUsers = [];
var missingUsers = [];
if ( mw.config.get('wgUserName') ) {
blueUsers.push( mw.util.wikiUrlencode( mw.config.get('wgUserName') ) );
}
var api = new mw.Api( {parameters: {
action: 'query',
list: 'users',
formatversion: 2
} } );
// Run every time content is added
mw.hook( 'wikipage.content' ).add( makeUsersBlue );
// Catch links outside the content area
makeUsersBlue( $('a.new').not('#mw-content-text a.new').parent() );
function makeUsersBlue( $content ) {
var users = [];
var userlinks = $content.find('a.new').filter( function () {
var pathname = decodeURIComponent( this.pathname ).toLowerCase();
return selector.some( ns => pathname.startsWith( ns ) );
} ).each( function () {
var encodedUsername = this.pathname.split(':')[1];
if ( missingUsers.includes( encodedUsername ) ) return;
if ( blueUsers.includes( encodedUsername ) ) {
this.href = this.pathname;
this.classList.remove( 'new' );
this.classList.add( 'mw-newuserlink' );
return;
}
var username = decodeURIComponent( encodedUsername );
if ( users.includes( username ) || username.includes( '/' ) ) return;
users.push( username );
} );
if ( !users.length ) return;
var apiRequests = [];
while ( users.length ) {
apiRequests.push( api.get( {
ususers: users.splice(0, 50)
} ).done( function ( data ) {
data.query.users.forEach( function ( user ) {
if ( user.missing || user.invalid ) {
missingUsers.push( mw.util.wikiUrlencode( user.name ) );
return;
}
blueUsers.push( mw.util.wikiUrlencode( user.name ) );
} );
} ) );
}
Promise.all( apiRequests ).then( function () {
userlinks.each( function () {
if ( !blueUsers.includes( this.pathname.split(':')[1] ) ) return;
this.href = this.pathname;
this.classList.remove( 'new' );
this.classList.add( 'mw-newuserlink' );
} );
} );
}
} );