User:Artoria2e5/MediaWiki:Gadget-Page descriptions.js

维基百科,自由的百科全书

注意:保存之后,你必须清除浏览器缓存才能看到做出的更改。Google ChromeFirefoxMicrosoft EdgeSafari:按住⇧ Shift键并单击工具栏的“刷新”按钮。参阅Help:绕过浏览器缓存以获取更多帮助。

/* forked from https://en.wikipedia.org/w/index.php?title=MediaWiki:Gadget-Page_descriptions.js&oldid=868096148
 * modifications:
 *  - Replace the set-description link with just the simple Wikidata link
 *  - Wikidata is not a bad thing; don't make it orange; Always show a link
 *  - Replace missing link (template) with WP:SHORTDESC because we dont use it here
 *  - Enable display for all NS. Only disable prompt for Flow.  
 *  - Display in #contentSub as with SDH
 *  - Localization with sitelib
 *
 * // [[User:Artoria2e5/MediaWiki:Gadget-Page_descriptions.js]]
 * importScript('User:Artoria2e5/MediaWiki:Gadget-Page_descriptions.js'); */
if ( mw.config.get( 'wgAction' ) === 'view' ) {
	var _pdns = mw.config.get( 'wgNamespaceNumber' );
	mw.loader.using( 'mediawiki.api' ).then( function () {
		// Adds a page description just below the "page title"
		// Behaves a lot like the mobile version
		var isEditor = ![10, 14, 710, 828, 2300, 2302, 2600].includes(_pdns);
		var wgQid = mw.config.get( 'wgWikibaseItemId' ),
			api = new mw.Api(),
			callPromise = api.get( {
				action: 'query',
				titles: mw.config.get( 'wgPageName' ),
				prop: 'description',
				formatversion: 2
			} );


		$.when( callPromise, $.ready ).then( function ( results ) {
			var isLocal = undefined;
			var pageDescription, $description,
				response = results[ 0 ];
			if (
				response.query &&
				response.query.pages &&
				response.query.pages[ 0 ].description
			) {
				pageDescription = response.query.pages[ 0 ].description;
				$description = $( '<span>' )
					.addClass( 'mw-page-description' )
					.text( pageDescription );
				isLocal = response.query.pages[ 0 ].descriptionsource === 'local';
			} else if ( isEditor ) {
				$description = $( '<span>' )
					.css( 'color', 'DarkRed' )
					.append(
						'缺少',
						$( '<a>' )
							.attr( 'href', '/wiki/WP:SHORTDESC' )
							.text(wgUVS('简短描述', '簡短描述'))
					);
			}
			if ( $description ) {
				if (isLocal) {
					$description.append('(本地)');
				}
				if ( !isLocal && wgQid && isEditor ) {
					$description.append(
						'(',
						$( '<a>' )
							.attr( 'href', 'https://www.wikidata.org/wiki/' + wgQid + '?uselang=' + mw.config.get( 'wgContentLanguage' ) )
							.text(wgUVS('维基数据', '維基數據')),
						')'
					);
				}
				$description
					.attr('id', 'gadget-pagedesc')
					.append( '。' );
				$( '#contentSub' ).prepend(
					$description
				);
			}
		} );
	} );
}