User:Liangent/Scripts/TagCommenter.js

注意:保存之后,你必须清除浏览器缓存才能看到做出的更改。Google ChromeFirefoxMicrosoft EdgeSafari:按住⇧ Shift键并单击工具栏的“刷新”按钮。参阅Help:绕过浏览器缓存以获取更多帮助。
( function( $, mw ) {
	// mw.util.addCSS( '.comment-box:after { content: " - by " attr(data-comment-user); }' ); // DEBUG
	mw.loader.using( [ 'mediawiki.util', 'mediawiki.Title' ], function() { $( function() {
		if ( $( '#ca-addsection' ).length === 0 || mw.config.get( 'wgAction' ) !== 'view' ) {
			return;
		}
		var pageNameRe = new RegExp( $.escapeRE( mw.config.get( 'wgArticlePath' ) ).replace( $.escapeRE( '$1' ), '(.*)' ) );
		var guessUserNameFromFuzzyPageName = function( pageName ) {
			// https://bugzilla.wikimedia.org/show_bug.cgi?id=56466
			var title = mw.Title.newFromText( decodeURIComponent( pageName ) );
			if ( title === null ) {
				return null;
			}
			var mainText = title.getNameText() + title.getDotExtension();
			if ( ( title.getNamespaceId() & ~1 ) === 2 ) {
				return mainText;
			} else if ( title.getNamespaceId() === -1 ) {
				var parts = mainText.split( '/' );
				if ( parts.length > 1 ) {
					return parts[1];
				}
			}
			return null;
		};
		var guessUserNameFromHref = function( href ) {
			var value = null;
			if ( ( value = pageNameRe.exec(href) ) !== null ) {
				return guessUserNameFromFuzzyPageName( value[1] );
			} else if ( ( value = mw.util.getParamValue( 'title', href ) ) !== null ){
				return guessUserNameFromFuzzyPageName( value );
			}
			return null;
		};
		var execRoot = function() {
			var $root = $( this );
			$( 'dt, dd, li', $root ).not( '.hlist li' ).each( function() {
				var $this = $( this );
				$this.contents().filter( function() {
					return this.nodeType === 3; // Text node
				} ).wrap( '<span class="comment-text"></span>' );
				$this.children().not( 'dl, ul' ).wrapAll( '<div class="comment-box"></div>' );
			} );
			$( 'p, blockquote', $root ).not( $( 'div p, div blockquote', $root ) )
			.addClass( 'comment-box' ).contents().filter( function() {
				return this.nodeType === 3; // Text node
			} ).wrap( '<span class="comment-text"></span>' );
			var boxes = [];
			$( '.comment-box', $root ).add( '> h1, > h2, > h3, > h4, > h5, > h6', $root ).each( function() {
				if ( $( this ).is( 'h1, h2, h3, h4, h5, h6' ) ) {
					$( boxes ).attr( 'data-comment-user', '' );
					boxes = [];
					return true;
				}
				boxes.push( this );
				var isSigned = false;
				var userName = null;
				$( $( '.comment-text', this ).get().reverse() ).each( function() {
					var text = $( this ).text().trim();
					if ( text !== '' ) {
						if ( /\d\d\d\d年\d\d?月\d\d?日 \(.\) \d\d?:\d\d? \(UTC\)/.exec( text ) ) {
							isSigned = true;
						}
						return false;
					}
				} );
				if ( isSigned ) {
					var href = $( 'a', this ).last().attr( 'href' );
					if ( href !== undefined ) {
						userName = guessUserNameFromHref( href );
					}
					$( boxes ).attr( 'data-comment-user', userName !== null ? userName : '' );
					boxes = [];
				}
			} );
			$( boxes ).attr( 'data-comment-user', '' );
			// FIXME div is sometimes a quote-block but sometimes a basic container.
			// w/o semantic tags it's difficult to tell.
			$( '> div', $root ).each( execRoot );
		};
		execRoot.apply( $( '#mw-content-text' ) );
	} ); } );
} )( jQuery, mediaWiki );