MediaWiki:Gadget-imagelinks.js

/** * Direct imagelinks to Miraheze Commons * * Required modules: mediawiki.util * * @version 2019-09-14 */ if ( mw.config.get( 'wgNamespaceNumber', 0 ) >= 0 ) { mw.loader.using( [ 'mediawiki.util' ] ).then( function {		mw.hook( 'wikipage.content' ).add( function ( $content ) { var uploadBaseRe = /^(https:)?\/\/static\.miraheze\.org\/commonswiki/, localFileNSString = mw.config.get( 'wgFormattedNamespaces' )['6'] + ':', localBasePath = new RegExp( '^' + mw.util.escapeRegExp( mw.util.getUrl( localFileNSString ) ) ), localBaseScript = new RegExp( '^' + mw.util.escapeRegExp( mw.util.wikiScript + '?title=' + mw.util.wikiUrlencode( localFileNSString ) ) ),				commonsBasePath = 'https://commons.miraheze.org/wiki/File:', commonsBaseScript = 'https://commons.miraheze.org/w/index.php?title=File:';

$content.find( 'a.image' ).attr( 'href', function ( i, currVal ) {				if ( uploadBaseRe.test( $( this ).find( 'img' ).attr( 'src' ) ) ) {					return currVal						.replace( localBasePath, commonsBasePath )						.replace( localBaseScript, commonsBaseScript );				}			} ); } );	} ); }