Перейти к содержанию

MediaWiki:Gadget-directLinkToCommons.js: различия между версиями

Материал из Википедии — свободной энциклопедии
Содержимое удалено Содержимое добавлено
исправление под Parsoid, он в отличие от mw.util.getUrl выводит Файл: в URL ссылок на файлы без их кодирования, про ?title= не знаю, так что не трогаю
Строка 4: Строка 4:
*/
*/
mw.hook( 'wikipage.content' ).add( function ( $content ) {
mw.hook( 'wikipage.content' ).add( function ( $content ) {
var fileSelector = 'a.image, a.mw-file-description, a.mw-tmh-play, a.mw-file-magnify';
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
// Ignore most files in the mobile version until [[phab:T65504]] gets resolved
if ( mw.config.get( 'skin' ) === 'minerva' ) {
if ( mw.config.get( 'skin' ) === 'minerva' ) {
fileSelector = '.noviewer ' + fileSelector.split( ', ' ).join( ', .noviewer ' );
fileSelector = '.noviewer ' + fileSelector.split( ', ' ).join( ', .noviewer ' );
}
}


if ( mw.config.get( 'wgNamespaceNumber', 0 ) < 0 ) {
if ( mw.config.get( 'wgNamespaceNumber', 0 ) < 0 ) {
return;
return;
}
}


mw.loader.using( 'mediawiki.util' ).done( function () {
mw.loader.using( 'mediawiki.util' ).done( function () {
var uploadBaseRe = /^(https:)?\/\/upload\.wikimedia\.org\/wikipedia\/commons/,
var uploadBaseRe = /^(https:)?\/\/upload\.wikimedia\.org\/wikipedia\/commons/,
localFileNSString = mw.config.get( 'wgFormattedNamespaces' )[ '6' ] + ':',
localFileNSString = mw.config.get( 'wgFormattedNamespaces' )[ '6' ] + ':',
server = '(https:)?' + mw.util.escapeRegExp( mw.config.get( 'wgServer' ) ),
server = '(https:)?' + mw.util.escapeRegExp( mw.config.get( 'wgServer' ) ),
localBasePath = new RegExp( '^(?:' + server + ')?' + mw.util.escapeRegExp(
localBasePath = new RegExp(
'^(?:' + server + ')?'
mw.util.getUrl( localFileNSString ) ),
+ mw.util.escapeRegExp( mw.util.getUrl( '' ) + localFileNSString ),
),
),
localBaseScript = new RegExp( '^(?:' + server + ')?' + mw.util.escapeRegExp(
localBaseScript = new RegExp( '^(?:' + server + ')?' + mw.util.escapeRegExp(
mw.util.wikiScript() + '?title=' + mw.util.wikiUrlencode( localFileNSString ),
mw.util.wikiScript() + '?title=' + mw.util.wikiUrlencode( localFileNSString ),
) ),
) ),
commonsBasePath = 'https://commons.wikimedia.org/wiki/File:',
commonsBaseScript = 'https://commons.wikimedia.org/w/index.php?title=File:';
commonsBasePath = 'https://commons.wikimedia.org/wiki/File:',
commonsBaseScript = 'https://commons.wikimedia.org/w/index.php?title=File:';


$content.find( fileSelector ).attr( 'href', function ( i, currVal ) {
$content.find( fileSelector ).attr( 'href', function ( i, currVal ) {
// Link has no current value, so exit early to avoid error
// Link has no current value, so exit early to avoid error
if ( !currVal ) return;
// TypeError: Cannot read properties of undefined (reading 'replace')
if ( !currVal ) return;


var src = $( this ).find( 'img' ).attr( 'src' );
var src = $( this ).find( 'img' ).attr( 'src' );
if ( !src ) return;
if ( !src ) return;


if ( uploadBaseRe.test( src ) ) {
if ( uploadBaseRe.test( src ) ) {
var url = currVal
var url = currVal
.replace( localBasePath, commonsBasePath )
.replace( localBasePath, commonsBasePath )
.replace( localBaseScript, commonsBaseScript );
.replace( localBaseScript, commonsBaseScript );


// Override default language on Commons for non-logged in users only (per [[Special:Diff/95375054]])
// Override default language on Commons for non-logged in users only (per [[Special:Diff/95375054]])
if ( !mw.config.get( 'wgUserId' ) ) {
if ( currVal !== url && !mw.config.get( 'wgUserId' ) ) {
url += ( /\?/.test( url ) ? '&' : '?' ) + 'uselang=ru';
url += ( /\?/.test( url ) ? '&' : '?' ) + 'uselang=ru';
}
}


return url;
return url;
}
}
} );
} );
} );
} );
} );
} );

Версия от 11:06, 8 мая 2026

/**
 * [[ВП:Гаджеты/Прямой переход на Викисклад]]
 * @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 = 'https://commons.wikimedia.org/wiki/File:',
			commonsBaseScript = 'https://commons.wikimedia.org/w/index.php?title=File:';

		$content.find( fileSelector ).attr( 'href', function ( i, currVal ) {
			// Link has no current value, so exit early to avoid error
			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 ( currVal !== url && !mw.config.get( 'wgUserId' ) ) {
					url += ( /\?/.test( url ) ? '&' : '?' ) + 'uselang=ru';
				}

				return url;
			}
		} );
	} );
} );