ข้ามไปเนื้อหา

ผลต่างระหว่างรุ่นของ "มีเดียวิกิ:Gadget-imagelinks.js"

จาก วิกิซอร์ซ
เนื้อหาที่ลบ เนื้อหาที่เพิ่ม
He7d3r (คุย | ส่วนร่วม)
Migration: wikiGetlink → getUrl
อัพเดทเป็นรุ่นปัจจุบันตามใน mediawiki
บรรทัดที่ 1: บรรทัดที่ 1:
/**
/* _____________________________________________________________________________
* | |
* | === WARNING: GLOBAL GADGET FILE === |
* | Changes to this page affect many users. |
* | Please discuss changes on the talk page or on [[WT:Gadget]] before editing. |
* |_____________________________________________________________________________|
*
* Direct imagelinks to Commons
* Direct imagelinks to Commons
* Files hosted at Commons have local image links redirected to Commons.
* Imported from version 9 as of 2012-12-08
*
*
* Required modules: mediawiki.util
* @source mediawiki.org/wiki/Snippets/Direct_imagelinks_to_Commons
*
* @author [[commons:User:Krinkle]]
* @source https://www.mediawiki.org/wiki/Snippets/Direct_imagelinks_to_Commons
* @version 9
* @author Krinkle
* @version 2019-09-14
*/
*/
if ( mw.config.get( 'wgNamespaceNumber', 0 ) >= 0 ) {
if ( mw.config.get( 'wgNamespaceNumber', 0 ) >= 0 ) {
$( function () {
mw.loader.using( [ 'mediawiki.util' ] ).then( function () {
mw.hook( 'wikipage.content' ).add( function ( $content ) {
var
uploadBaseRe = /^\/\/upload\.wikimedia\.org\/wikipedia\/commons/,
var uploadBaseRe = /^(https:)?\/\/upload\.wikimedia\.org\/wikipedia\/commons/,
localFileNSString = mw.config.get( 'wgFormattedNamespaces' )['6'] + ':',
localBasePath = mw.util.getUrl( mw.config.get( 'wgFormattedNamespaces' )['6'] + ':' ),
localBasePath = new RegExp( '^' + mw.util.escapeRegExp( mw.util.getUrl( localFileNSString ) ) ),
localBaseScript = mw.util.wikiScript() + '?title=' + mw.util.wikiUrlencode( mw.config.get( 'wgFormattedNamespaces' )['6'] + ':' ),
localBaseScript = new RegExp( '^' + mw.util.escapeRegExp(
mw.util.wikiScript() + '?title=' + mw.util.wikiUrlencode( localFileNSString )
) ),
commonsBasePath = '//commons.wikimedia.org/wiki/File:',
commonsBaseScript = '//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:';

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

รุ่นแก้ไขเมื่อ 18:44, 28 มีนาคม 2565

/**
 * Direct imagelinks to Commons
 *
 * Required modules: mediawiki.util
 *
 * @source https://www.mediawiki.org/wiki/Snippets/Direct_imagelinks_to_Commons
 * @author Krinkle
 * @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:)?\/\/upload\.wikimedia\.org\/wikipedia\/commons/,
				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.wikimedia.org/wiki/File:',
				commonsBaseScript = 'https://commons.wikimedia.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 );
				}
			} );
		} );
	} );
}