/**
* [[ВП:Гаджеты/Прямой переход на Викисклад]]
* @source: https://www.mediawiki.org/wiki/Snippets/Direct_imagelinks_to_Commons
*/
mw.hook( 'wikipage.content' ).add( function ( $content ) {
var fileSelector = 'a.image, a.mw-file-description, a.mw-tmh-play, a.mw-file-magnify';
// Ignore most files in the mobile version until [[phab:T65504]] gets resolved
if ( mw.config.get( 'skin' ) === 'minerva' ) {
fileSelector = '.noviewer ' + fileSelector.split( ', ' ).join( ', .noviewer ' );
}
if ( mw.config.get( 'wgNamespaceNumber', 0 ) < 0 ) {
return;
}
mw.loader.using( 'mediawiki.util' ).done( function () {
var uploadBaseRe = /^(https:)?\/\/upload\.wikimedia\.org\/wikipedia\/commons/,
localFileNSString = mw.config.get( 'wgFormattedNamespaces' )[ '6' ] + ':',
server = '(https:)?' + mw.util.escapeRegExp( mw.config.get( 'wgServer' ) ),
localBasePath = new RegExp( '^(?:' + server + ')?' + mw.util.escapeRegExp(
mw.util.getUrl( localFileNSString ) ),
),
localBaseScript = new RegExp( '^(?:' + server + ')?' + mw.util.escapeRegExp(
mw.util.wikiScript() + '?title=' + mw.util.wikiUrlencode( localFileNSString ),
) ),
commonsBasePath = '/wiki/File:',
commonsBaseScript = '/w/index.php?title=File:';
$content.find( fileSelector ).attr( 'href', function ( i, currVal ) {
// Link has no current value, so exit early to avoid error
// TypeError: Cannot read properties of undefined (reading 'replace')
if ( !currVal ) return;
var src = $( this ).find( 'img' ).attr( 'src' );
if ( !src ) return;
if ( uploadBaseRe.test( src ) ) {
var url = currVal
.replace( localBasePath, commonsBasePath )
.replace( localBaseScript, commonsBaseScript );
// Override default language on Commons for non-logged in users only (per [[Special:Diff/95375054]])
if ( !mw.config.get( 'wgUserId' ) ) {
url += ( /\?/.test( url ) ? '&' : '?' ) + 'uselang=az';
}
return url;
}
} );
} );
} );