How to always get to the image on commons instead of the local wiki page

mediawikiwikimedia-commonswikipedia

If you go to this article and click on the German flag, you get to

https://de.wikipedia.org/wiki/Datei:Flag_of_Germany.svg

and a link is presented that bascially says that the original page is in the commons

enter image description here

When you click on it, you get to

https://commons.wikimedia.org/wiki/File:Flag_of_Germany.svg

Is it possible to automatically get to the commons page if the image is on commons (which is not always the case)?

Best Answer

If you are looking for an answer specifically for de.wikipedia.org then go to the Gadgets section of the preferences and activate the Gadget with the description "Überspringen der lokalen Dateibeschreibungsseite, um sofort nach Commons zu kommen." Note that this currently only works if you have Media Viewer disabled.

To make this work on any wiki, or with Media Viewer enabled copy the following into your Common.js

/**
 * Direct imagelinks to Commons
 *
 * Required modules: jquery.mwExtension, mediawiki.util
 *
 * @source www.mediawiki.org/wiki/Snippets/Direct_imagelinks_to_Commons
 * @author Krinkle
 * @version 2014-09-04
 */
if ( mw.config.get( 'wgNamespaceNumber', 0 ) >= 0 ) {
    mw.hook( 'wikipage.content' ).add( function ( $content ) {
        var
            uploadBaseRe = /^\/\/upload\.wikimedia\.org\/wikipedia\/commons/,

            localBasePath = new RegExp( '^' + $.escapeRE( mw.util.getUrl( mw.config.get( 'wgFormattedNamespaces' )['6'] + ':' ) ) ),
            localBaseScript = new RegExp( '^' + $.escapeRE( mw.util.wikiScript() + '?title=' + mw.util.wikiUrlencode( mw.config.get( 'wgFormattedNamespaces' )['6'] + ':' ) ) ),

            commonsBasePath = '//commons.wikimedia.org/wiki/File:',
            commonsBaseScript = '//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 ) +
                    ( /\?/.test( currVal ) ? '&' : '?' ) +
                    'uselang=' + mw.config.get( 'wgUserLanguage' );
            }
        } );
    } );
}