Jump to content

MediaWiki:Gadget-imagelinks.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by MSGJ (talk | contribs) at 09:53, 4 September 2013 (changes requested by User:Jackmcbarn). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
/*  _____________________________________________________________________________
 * |                                                                             |
 * |                    === 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
 * Files hosted at Commons have local image links redirected to Commons.
 * Imported from version 9 as of 2012-12-08
 *
 * @source mediawiki.org/wiki/Snippets/Direct_imagelinks_to_Commons
 * @author [[commons:User:Krinkle]]
 * @version 9
 */
if ( mw.config.get( 'wgNamespaceNumber', 0 ) >= 0 ) {
	$( function () {
		var
			uploadBaseRe = /^\/\/upload\.wikimedia\.org\/wikipedia\/commons/,

			localBasePath = new RegExp('^' + mw.util.wikiGetlink( mw.config.get( 'wgFormattedNamespaces' )['6'] + ':' ).replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&")),
			localBaseScript = new RegExp('^' + (mw.util.wikiScript() + '?title=' + mw.util.wikiUrlencode( mw.config.get( 'wgFormattedNamespaces' )['6'] + ':' )).replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&")),

			commonsBasePath =  '//commons.wikimedia.org/wiki/File:',
			commonsBaseScript =  '//commons.wikimedia.org/w/index.php?title=File:';
 
		$( 'a.image' ).attr( 'href', function ( i, currVal ) {
			if ( uploadBaseRe.test( $(this).find( 'img' ).attr( 'src' ) ) ) {
				return currVal
					.replace( localBasePath, commonsBasePath )
					.replace( localBaseScript, commonsBaseScript );
			}
		});
	});
}