跳转到内容

MediaWiki:Gadget-subjects.js

来自维基教科书,面向开放世界的开放书籍
注意:在保存后,更改可能不会立即生效。单击此处了解如何绕过浏览器的缓存。
  • Mozilla / Firefox / Safari:单击重新载入时按住Shift,或按Ctrl-Shift-R(在苹果 Mac 上按Cmd-Shift-R);
  • Internet Explorer:单击刷新时按住Ctrl,或按Ctrl-F5
  • Konqueror:只需单击重新载入按钮,或按F5
  • Opera用户可能需要在工具→首选项中完全清除其缓存。
$(document).ready( function() {
	var $links = $( '#booklinks' ).find( 'a' ), titles, categories = [
		'Freshly started books',
		'Partly developed books',
		'Half-finished books',
		'Books nearing completion',
		'Completed books',
		'Featured books',
		'Books with print version',
		'Books with PDF version'
	];

	function showSubjectMetadataCB(obj) {
		$.each( obj.query && obj.query.pages || [], function( id, page ) {
			var $link = [];

			if ( !page.categories ) {
				return;
			}

			$links.each( function() {
				var $this = $(this);
				if ( $this.attr( 'title' ) === page.title ) {
					$link = $this;
					return false;
				}
			});

			if ( !$link.length ) {
				return;
			}

			$.each( page.categories, function( key, cat) {
				var title = cat.title;

				switch ( title ) {
					case 'Category:Freshly started books':
						$link.before( $( '<span/>' ).addClass( 'book-level-0' ) );
						break;
					case 'Category:Partly developed books':
						$link.before( $( '<span/>' ).addClass( 'book-level-1' ) );
						break;
					case 'Category:Half-finished books':
						$link.before( $( '<span/>' ).addClass( 'book-level-2' ) );
						break;
					case 'Category:Books nearing completion':
						$link.before( $( '<span/>' ).addClass( 'book-level-3' ) );
						break;
					case 'Category:Completed books':
						$link.before( $( '<span/>' ).addClass( 'book-level-4' ) );
						break;
					case 'Category:Featured books':
						$link.before( $( '<span/>' ).addClass( 'book-level-5' ) );
						break;
					case 'Category:Books with print version':
						$link.after( $( '<span/>' ).addClass( 'book-print' ) );
						break;
					case 'Category:Books with PDF version':
						$link.after( $( '<span/>' ).addClass( 'book-pdf' ) );
						break;
				}
			});
		});
	}

	if ( !$links.length ) {
		return;
	}

	titles = $.map( $links, function( link ) { return $(link).attr( 'title' ); } ).join( '|' );
	categories = 'Category:' + categories.join( '|Category:' );

	$.getJSON( mw.util.wikiScript( 'api' ), {
		'format':		'json',
		'action':		'query',
		'prop':			'categories',
		'clshow':		'hidden',
		'cllimit':		500,
		'clcategories':		categories,
		'titles':		titles
	}, showSubjectMetadataCB);
});
华夏公益教科书