Benutzer:Hoo man/directLinksToCommons.js

aus Wikipedia, der freien Enzyklopädie
Zur Navigation springen Zur Suche springen

Hinweis: Leere nach dem Veröffentlichen den Browser-Cache, um die Änderungen sehen zu können.

  • Firefox/Safari: Umschalttaste drücken und gleichzeitig Aktualisieren anklicken oder entweder Strg+F5 oder Strg+R (⌘+R auf dem Mac) drücken
  • Google Chrome: Umschalttaste+Strg+R (⌘+Umschalttaste+R auf dem Mac) drücken
  • Internet Explorer/Edge: Strg+F5 drücken oder Strg drücken und gleichzeitig Aktualisieren anklicken
  • Opera: Strg+F5
/**
 * Direct imagelinks to Commons, modified
 * Depends on mediawiki.util, user
 *
 * @author: [[m:User:Hoo man]]
 * @source //de.wikipedia.org/wiki/Benutzer:Hoo_man/directLinksToCommons.js
 * @version 2
 */
(function ($, mw) {
	var localBasePath = mw.util.getUrl( mw.config.get( 'wgFormattedNamespaces' )['6'] + ':' ),
	localBaseScript = 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:';
	
	function directLinksToCommonsApi( origURI, done, fail ) {
		var filePage, fileTalk;
		if(origURI.indexOf('?') === -1) {
			filePage = origURI.substring( origURI.lastIndexOf( '/' ) + 1 );
		}else{
			filePage = mw.util.getParamValue( 'title',  origURI );
		}
		filePage = filePage.replace( /_/g, ' ' );
		fileTalk = filePage.replace( mw.config.get( 'wgFormattedNamespaces' )['6'], mw.config.get( 'wgFormattedNamespaces' )['7'] );
		$.ajax( {
			url : mw.config.get( 'wgServer' ) + mw.util.wikiScript( 'api' ),
			data : { action : 'query', format : 'json', titles : filePage + '|' + fileTalk, indexpageids : 1},
			cache: true,
			timeout : 3500 // timeout after 3.5 seconds
		} )
		.done( done )
		// an error occured
		.fail( fail );
	}
	// On mouseover, changes the href, if no local page exists
	// and sets 'directLinksToCommonsChecked' to make sure we don't check again
	function directLinksToCommonsOver() {
		if( !$(this).prop( 'directLinksToCommonsChecked' ) ) {
			var origURI = $(this).attr( 'href' );
			var $link = $(this);
			directLinksToCommonsApi( $(this).attr( 'href' ), function( data ) {
				var pageids = data.query.pageids;
				if(pageids[0] === '-1' && pageids[1] === '-2') {
					// both pages don't exist
					$link.attr( 'href', origURI.replace( localBasePath, commonsBasePath ).replace( localBaseScript, commonsBaseScript ) );
				}
				$link.prop( 'directLinksToCommonsChecked', true );
			}, function() {
				$link.prop( 'directLinksToCommonsChecked', true );
			} );
		}
	}
	// On click, looks whether we have local pages, if not yet done and acts accordingly
	function directLinksToCommonsClick() {
		if( !$(this).prop( 'directLinksToCommonsChecked' ) ) {
			var origURI = $(this).attr( 'href' );
			directLinksToCommonsApi( $(this).attr( 'href' ), function( data ) {
				var pageids = data.query.pageids;
				if(pageids[0] === '-1' && pageids[1] === '-2') {
					// both pages don't exist
					window.location = origURI.replace( localBasePath, commonsBasePath ).replace( localBaseScript, commonsBaseScript );
				}else{
					// at least one exists
					window.location = origURI;
				}
			}, function() {
				window.location = origURI;
			} );
			return false;
		}
	}
	// init
	if ( mw.config.get( 'wgNamespaceNumber', -1 ) >= 0 ) {
		$(document).ready( function () {
			var uploadBaseRe = new RegExp( '^' + $.escapeRE( '//upload.wikimedia.org/wikipedia/commons/' ) );
			$( 'a.image' ).attr( 'href', function() {
				if ( uploadBaseRe.test( $(this).find( 'img' ).attr( 'src' ) ) ) {
					$(this).on( 'click', directLinksToCommonsClick );
					if ( !mw.user.anonymous() ) {
						// Disable this for anons, to avoid blowing the API
						$(this).on( 'mouseover', directLinksToCommonsOver );
					}
				}
			} );
		} );
	}
}( jQuery, mediaWiki ) );