User:Ricordisamoa/InterwikiList.js

From Wikidata
Jump to navigation Jump to search

Note: After saving, 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: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Go to Menu → Settings (Opera → Preferences on a Mac) and then to Privacy & security → Clear browsing data → Cached images and files.
/* <nowiki>
 *
 * InterwikiList.js
 * @author [[User:Ricordisamoa]]
 * Generates a list of 'old' interwiki links from an item
*/
$( document ).ready( function () {
	if ( mw.config.get( 'wgAction' ) !== 'view' || !mw.config.exists( 'wbEntity' ) ) {
		return;// an item, in view mode
	}
	var messages = {
		'en': {
			'p-label': 'Generate interwiki list',
			'p-title': 'Generate a list of "old" interwiki links, e.g to be used on Commons',
			'dialog-title': 'List of interwikis'
		},
		'it': {
			'p-label': 'Genera lista di interwiki',
			'p-title': 'Genera una lista di "vecchi" collegamenti interwiki, da usare per esempio su Commons',
			'dialog-title': 'Lista di interwiki'
		}
	};
	messages = messages[mw.config.get( 'wgUserLanguage' )] || messages.en;
	$( mw.util.addPortletLink( 'p-cactions', '#', messages['p-label'], 'item-archive', messages['p-title'] ) )
	.click( function ( event ) {
		event.preventDefault();
		var sitelinks = JSON.parse( mw.config.get( 'wbEntity' ) ).sitelinks;
		if ( sitelinks !== undefined ) {
			var interwikis = $.map( sitelinks, function ( e ) {
				return e.site;
			} ).sort()
			.map( function ( dbName ) {
				return '[[' + dbName.replace( /wiki$/, '' ).replace( /_/g, '-' ) +
					':' + sitelinks[dbName].title + ']]';
			} ).join( '\n' );
			var buttons = {};
			buttons[mw.message( 'close' )] = function () {
				$( this ).dialog( 'close' );
			};
			$( '<div>' )
			.append(
				$( '<textarea>' )
				.val( interwikis )
			)
			.dialog( {
				title: messages['dialog-title'],
				buttons: buttons,
				modal: true
			} );
		}
	} );
} );