跳转到内容

MediaWiki:Common.js

维基百科,自由的百科全书
注意:保存之后,你必须清除浏览器缓存才能看到做出的更改。Google ChromeFirefoxMicrosoft EdgeSafari:按住⇧ 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.load( 'ext.gadget.EditCommon' );
		}

		mw.hook( 've.activationComplete' ).add( () => {
			mw.loader.load( 'ext.gadget.EditCommon' );
		} );

		/* 避免在主條目的註腳中出現捲軸框 */
		if ( !mw.config.get( 'wgCanonicalNamespace' ) ) {
			$( 'div#mw-content-text ol.references' ).each( function() {
				var needobjs = [], $curobj = $( this );
				do {
					$curobj = $curobj.parent();
					if ( !$curobj ) { break; }
					if (
						$curobj.attr( 'id' ) === 'mw-content-text'
						|| $curobj.prop( 'tagName' ).toLowerCase()
							=== 'body'
					) { break; }
					if (
						$curobj.css( 'overflow' ).match(
							/(?: auto|scroll)/i,
						)
						|| $curobj.css( 'overflow-x' ).match(
							/(?:auto|scroll)/i,
						)
						|| $curobj.css( 'overflow-y' ).match(
							/(?:auto|scroll)/i,
						)
					) {
						/* null */
					} else { continue; }
					if (
						( '' + $curobj.attr( 'class' ) ).split( ' ' )
							.indexOf( 'noprint' ) >= 0
					) { return; }
					needobjs.push( $curobj.get( 0 ) );
				} while ( true );
				$( needobjs )
					.css( 'overflow', 'visible' )
					.css( 'overflow-x', 'visible' )
					.css( 'overflow-y', 'visible' )
					.css( 'border', '' )
					.css( 'height', '' )
					.css( 'max-height', '' );
			} );
		}

		/**
		 * metaBox
		 * Funcionament de la Plantilla:Metacaixa
		 * Implementat per: Usuari:Peleguer.
		 * Actualitzat per Joanjoc seguint les indicacions d'en Martorell
		 */
		function MetaCaixaInit() {
			// S'executa al carregar-se la pàgina, si hi ha metacaixes,
			// s'assignen els esdeveniments als botons
			// alert('MetaCaixaInit');
			var i = 0; // Inicialitzem comptador de caixes
			for ( i = 0; i <= 9; i++ ) {
				var vMc = document.getElementById( 'mc' + i );
				if ( !vMc ) { break; }
				// alert('MetaCaixaInit, trobada Metacaixa mc' + i);
				var j = 1, // Inicialitzem comptador de botons dins de la caixa
					vPsIni = 0; // Pestanya visible inicial
				for ( j = 1; j <= 9; j++ ) {
					var vBt = document.getElementById( 'mc' + i + 'bt' + j );
					if ( !vBt ) { break; }
					// alert('MetaCaixaInit, trobat botó mc' + i + 'bt' + j);
					vBt.onclick = MetaCaixaMostraPestanya; // A cada botó assignem l'esdeveniment onclick
					// alert(vBt.className);
					if ( vBt.className === 'mcBotoSel' ) { vPsIni = j; // Si tenim un botó seleccionat, en guardem l'index
					 }
				}
				// alert('mc=' + i + ', ps=' + j + ', psini=' + vPsIni);
				if ( vPsIni === 0 ) { // Si no tenim cap botó seleccionat, n'agafem un aleatòriament
					vPsIni = 1 + Math.floor( ( j - 1 ) * Math.random() );
					// alert('Activant Pestanya a l\'atzar; _mc' + i + 'bt' + vPsIni + '_');
					try {
						document.getElementById( 'mc' + i + 'ps' + vPsIni )
							.style.display = 'block';
						document.getElementById( 'mc' + i + 'ps' + vPsIni )
							.style.visibility = 'visible';
						document.getElementById( 'mc' + i + 'bt' + vPsIni )
							.className = 'mcBotoSel';
					} catch ( e ) {
						// TypeError: null is not an object (evaluating 'document.getElementById('mc'+i+'ps'+vPsIni).style')
					}
				}
			}
		}

		function MetaCaixaMostraPestanya() {
			// S'executa al clicar una pestanya,
			// aquella es fa visible i les altres s'oculten
			var vMcNom = this.id.substr( 0, 3 ), // A partir del nom del botó, deduïm el nom de la caixa
				vIndex = this.id.substr( 5, 1 ), // I l'index
				i = 1;
			for ( i = 1; i <= 9; i++ ) { // busquem totes les pestanyes d'aquella caixa
				// alert(vMcNom + 'ps' + i);
				var vPsElem = document.getElementById( vMcNom + 'ps' + i );
				if ( !vPsElem ) { break; }
				if ( vIndex == i ) { // Si és la pestanya bona la mostrem i canviem la classe de botó
					vPsElem.style.display = 'block';
					vPsElem.style.visibility = 'visible';
					document.getElementById( vMcNom + 'bt' + i ).className = 'mcBotoSel';
				} else { // Sinó, l'ocultem i canviem la classe de botó
					vPsElem.style.display = 'none';
					vPsElem.style.visibility = 'hidden';
					document.getElementById( vMcNom + 'bt' + i ).className = 'mcBoto';
				}
			}
			return false; // evitem la recàrrega de la pàgina
		}
		MetaCaixaInit();

		if (
			!+mw.user.options.get( 'discussiontools-newtopictool' )
			|| !+mw.user.options.get( 'discussiontools-betaenable' )
		) {
			/* 智能讨论页编辑(新建) */
			var catalk = $( '#ca-talk' );
			if (
				catalk.hasClass( 'new' )
				&& mw.config.get( 'wgNamespaceNumber' ) != 2
			) {
				var a = $( 'a:first', catalk );
				a.attr( 'href', a.attr( 'href' ) + '&section=new' );
			}
		}

		/**
		 * &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值',
						} ),
					},
				);
			}
		}

		/* 页面历史加&hilight=高亮 */
		var hilight = mw.util.getParamValue( 'hilight' );
		if ( mw.config.get( 'wgAction' ) === 'history' && hilight ) {
			$.each( hilight.split( ',' ), function( _, v ) {
				$( 'input[name=oldid][value=' + v + ']' ).parent().addClass(
					'not-patrolled',
				);
			} );
		}

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

		/* 修正摺疊後定位變化 */
		if ( location.hash ) { location.href = location.hash; }

		/* 引用錯誤標籤名字解碼 */
		$( '.anchordecodeme' ).each( function() {
			$( this ).text(
				decodeURIComponent(
					$( this ).text().replace( /\.([0-9A-F]{2})/g, '%$1' ),
				),
			);
		} );

		/* Check for any client-side simplified/traditional Chinese conversion */
		/* This routine must be placed here to make sure the field is inserted in time */
		$( '#antispam-container' ).append(
			$( '<input type="text" />' ).attr( {
				id: 'wpAntiConv',
				value: '\u6c49\u6f22',
			} ),
		);

		/**
		 * 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 );

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