MediaWiki:Gadget-WikidataLink.js

From Seeds of the Word, the encyclopedia of the influence of the Gospel on culture

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
/**
 * Beneath the title of each article, category or template, 
 * create a link to the connected Wikidata element if there is one, 
 * otherwise to the page to create one.
 *
 * Known Bug:
 * * [[phab:T185437]]
 * 
 * Author: --[[Mediawiki:Utente:Valerio Bozzolan]] 19:06, 31 lug 2018 (CEST)
 */
( function ( $, config ) {
	// human labels
	var L10n = {
		existing:   'Wikidata Item',
		unexisting: 'Create Wikidata item',
	};

	// allowed namespaces
	var nsWhitelist = [
		 0, // <main>
		10, // Template
		14, // Category
	];

	var ns    = config.get( 'wgNamespaceNumber' );
	var item  = config.get( 'wgWikibaseItemId'  );
	var title = config.get( 'wgPageName'        ).replace( /_/g, ' ' );

	if( nsWhitelist.indexOf( ns ) === -1 ) {
		return;
	}

	var text = item
		? L10n.existing
		: L10n.unexisting;

	var url  = item
		? '//en.seminaverbi.bibleget.io/wiki/Special:EntityPage/' + item
		: '//en.seminaverbi.bibleget.io/wiki/Special:NewItem?label=' + encodeURIComponent( title );

	var $url = $( '<a>' )
		.text( text )
		.prop( 'href', url );

	var $container = $( '<div>' ).append( $url );

	if( item ) {
		$container
			.append( ' ' )
			.append( $( '<code>' ).text( item ) );
	}

	$( '#contentSub' ).append( $container );
} )( $, mw.config );