User:Ricordisamoa/LowercaseDescription.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>
 *
 * LowercaseDescription.js
 * @author [[User:Ricordisamoa]]
 * Provides a link to automatically 'lcFirst' an item's description in the user's language
*/
$( document ).ready( function () {
	if ( !mw.config.exists( 'wbEntity' ) ) {
		return;
	}
	var entity = JSON.parse( mw.config.get( 'wbEntity' ) );
	if ( entity.descriptions === undefined ) {
		return;
	}
	var desc = entity.descriptions[mw.config.get( 'wgUserLanguage' )];
	if ( desc === undefined ) {
		return;
	}
	desc = desc.value;
	var lcFirstDesc = desc.charAt( 0 ).toLowerCase() + desc.substr( 1 );
	if ( lcFirstDesc === desc ) {
		return;
	}
	var messages = {
		'en': {
			'lcFirst': 'lcFirst',
			'success': 'Description successfully edited!'
		},
		'it': {
			'lcFirst': 'lcFirst',
			'success': 'Descrizione modificata con successo!'
		}
	};
	messages = messages[mw.config.get( 'wgUserLanguage' )] || messages.en;
	$( '.wb-description .wb-ui-toolbar a.wb-ui-toolbar-button' )// 'edit' description link
	.after(
		$( '<a>' )
		.text( messages.lcFirst )
		.attr( 'href', '#' )
		.click( function ( event ) {
			event.preventDefault();
			$( this ).hide().prev().hide();// hide link+bar
			new mw.Api().postWithToken( 'edit', {
				action: 'wbsetdescription',
				id: mw.config.get( 'wbEntityId' ),
				language: mw.config.get( 'wgUserLanguage' ),
				summary: '[[User:Ricordisamoa/LowercaseDescription|lowercase description]]',
				value: lcFirstDesc
			} )
			.done( function () {
				mw.notify( messages.success );
				$( '.wb-description .wb-value' ).text( lcFirstDesc );
			} )
			.fail( function ( error ) {
				mw.notify( error );
			} );
		} )
	)
	.after( '<span>|</span>' );
} );