跳转到内容

MediaWiki:Common.js

维基百科,自由的百科全书
注意:保存之后,你必须清除浏览器缓存才能看到做出的更改。Google Chrome、Firefox、Microsoft Edge及Safari:按住⇧ Shift键并单击工具栏的“刷新”按钮。参阅Help:绕过浏览器缓存以获取更多帮助。
/**
 * 所有用戶在加載任何頁面時,這裡的JavaScript都會加載
 * 注意!移动版会加载[[MediaWiki:Mobile.js]]而非此页面。如果需要在桌面版和移动版同时执行脚本,请考虑使用默认启用的小工具
 */

Promise.all( [
	$.ready,
	mw.loader.using( [ 'ext.gadget.HanAssist', 'mediawiki.util' ] ), // 在此添加额外的依赖
] ).then(
	function( [ _, require ] ) {
		// 注意在调用本回调函数的时候DOM已经加载完成,因此包装应使用IIFE而非 $( () => { ... } ) 回调
		const { conv } = require( 'ext.gadget.HanAssist' );

		mw.log.deprecate( window, 'JSConfig', {} );

		/**
		 * Map addPortletLink to mw.util
		 * @deprecated: Use mw.util.addPortletLink instead.
		 */
		mw.log.deprecate(
			window,
			'addPortletLink',
			mw.util.addPortletLink,
			'Use mw.util.addPortletLink instead',
		);

		// Load scripts specific to editing
		// This gadget is loaded by default on ?action=(edit|submit), other circumstances need to be handled manually
		if ( mw.config.get( 'wgCanonicalSpecialPageName' ) === 'Search' ) {
			mw.loader.using( [ 'ext.gadget.EditCommon-styles', 'ext.gadget.EditCommon' ] );
		}

		mw.hook( 've.activationComplete' ).add( () => {
			mw.loader.using( [ 'ext.gadget.EditCommon-styles', 'ext.gadget.EditCommon' ] );
		} );

		/**
		 * &withCSS= and &withJS= URL parameters
		 * Allow to try custom scripts from MediaWiki space
		 * without editing personal .css or .js files
		 * @source www.mediawiki.org/wiki/Snippets/Load_JS_and_CSS_by_URL
		 * @rev 6
		 */
		var extraCSS = mw.util.getParamValue( 'withCSS' ),
			extraJS = mw.util.getParamValue( 'withJS' );
		if ( extraCSS ) {
			if ( extraCSS.match( /^MediaWiki:[^&<>=%#]*\.css$/ ) ) {
				mw.loader.load(
					mw.util.getUrl( extraCSS, { action: 'raw', ctype: 'text/css' } ),
					'text/css',
				);
			} else {
				mw.notify(
					conv( {
						hans: '只允许从MediaWiki命名空间加载。',
						hant: '只允許從MediaWiki命名空間載入。',
					} ),
					{
						title: conv( {
							hans: '无效的withCSS值',
							hant: '無效的withCSS值',
						} ),
					},
				);
			}
		}
		if ( extraJS ) {
			if ( extraJS.match( /^MediaWiki:[^&<>=%#]*\.js$/ ) ) {
				mw.loader.load(
					mw.util.getUrl( extraJS, { action: 'raw', ctype: 'text/javascript' } ),
				);
			} else {
				mw.notify(
					conv( {
						hans: '只允许从MediaWiki命名空间加载。',
						hant: '只允許從MediaWiki命名空間載入。',
					} ),
					{
						title: conv( {
							hans: '无效的withJS值',
							hant: '無效的withJS值',
						} ),
					},
				);
			}
		}

		/* Main page hacks */
		if (
			mw.config.get( 'wgIsMainPage' )
			&& mw.config.get( 'wgAction' ) === 'view'
		) {
			/* Remove red links */
			$( '#mw-content-text a.new' ).contents().unwrap();
		}

		/**
		 * Add support to mw-collapsible for autocollapse, innercollapse and outercollapse
		 *
		 * Maintainers: TheDJ
		 */
		function mwCollapsibleSetup( $collapsibleContent ) {
			var $element,
				$toggle,
				autoCollapseThreshold = 2;
			$.each( $collapsibleContent, function( index, element ) {
				$element = $( element );
				if ( $element.hasClass( 'collapsible' ) ) {
					$element.find( 'tr:first > th:first' ).prepend(
						$element.find(
							'tr:first > * > .mw-collapsible-toggle',
						),
					);
				}
				if (
					$collapsibleContent.length >= autoCollapseThreshold
					&& $element.hasClass( 'autocollapse' )
				) {
					$element.data( 'mw-collapsible' ).collapse();
				} else if ( $element.hasClass( 'innercollapse' ) ) {
					if ( $element.parents( '.outercollapse' ).length > 0 ) {
						$element.data( 'mw-collapsible' ).collapse();
					}
				}
				// because of colored backgrounds, style the link in the text color
				// to ensure accessible contrast
				$toggle = $element.find( '.mw-collapsible-toggle' );
				if ( $toggle.length ) {
					// Make the toggle inherit text color (Updated for T333357 2023-04-29)
					if ( $toggle.parent()[0].style.color ) {
						$toggle.css( 'color', 'inherit' );
						$toggle.find( '.mw-collapsible-text' ).css(
							'color',
							'inherit',
						);
					}
				}
			} );
		}

		mw.hook( 'wikipage.collapsibleContent' ).add( mwCollapsibleSetup );

		// 请在本行前添加内容
	},
);
// 请勿在本行后添加内容