“MediaWiki:Common.js”的版本间的差异

来自科学ADV中文wiki
跳到导航 跳到搜索
第175行: 第175行:
 
function addNotoSerifSCFontSupport($content) {
 
function addNotoSerifSCFontSupport($content) {
 
if (!document.querySelector('#noto_serif_sc')) {
 
if (!document.querySelector('#noto_serif_sc')) {
         let fontTag = document.createElement('link');
+
         var fontTag = document.createElement('link');
 
         fontTag.rel = 'stylesheet';
 
         fontTag.rel = 'stylesheet';
 
         fontTag.type = 'text/css';
 
         fontTag.type = 'text/css';

2021年5月14日 (五) 12:55的版本

// richtab

function richTabSetup( $content ) {
	$(".richtab > li > span > a").click(function(e) {
	    e.preventDefault();
		var richtab = $(this).closest(".richtab");
		var richtab_current = $(this).closest(".richtab > li");
		richtab.children().removeClass('active');
		richtab_current.addClass('active');
		var richtab_contents = richtab.next('.richtab-content');
		richtab_contents.children().removeClass('active');
		var richtab_index = richtab_current.prevAll().length;
	    var richtab_active_content = richtab_contents.children()[richtab_index];
	    $(richtab_active_content).addClass('active');
	});
}
mw.hook( 'wikipage.content' ).add( richTabSetup );

// 折叠支持

function makeCollapsibleMwCollapsible( $content ) {
	var $tables = $content
		.find( 'table.collapsible:not(.mw-collapsible)' )
		.addClass( 'mw-collapsible' );

	$.each( $tables, function ( index, table ) {
		// mw.log.warn( 'This page is using the deprecated class collapsible. Please replace it with mw-collapsible.');
		if ( $( table ).hasClass( 'collapsed' ) ) {
			$( table ).addClass( 'mw-collapsed' );
			// mw.log.warn( 'This page is using the deprecated class collapsed. Please replace it with mw-collapsed.');
		}
	} );
	if ( $tables.length > 0 ) {
		mw.loader.using( 'jquery.makeCollapsible' ).then( function () {
			$tables.makeCollapsible();
		} );
	}
}
mw.hook( 'wikipage.content' ).add( makeCollapsibleMwCollapsible );

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
			if ( $toggle.parent()[ 0 ].style.color ) {
				$toggle.find( 'a' ).css( 'color', 'inherit' );
			}
		}
	} );
}

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

// 外链音乐-网易云音乐

function outChainMusic163Setup( $content ) {
	var $players = $content
		.find( '.outchainmusic-163' );
	
	$.each( $players, function ( index, player ) {
		var id = player.dataset.id;
        var type = player.dataset.type;
        if (!/^\d+$/.test(id)) return self.css('color', 'red').text('Error in outchain music data: Invalid id.');
        if (!/^\d+$/.test(type)) return self.css('color', 'red').text('Error in outchain music data: Invalid type.');
		var source1 = '<iframe frameborder="no" border="0" marginwidth="0" marginheight="0" width=330 height=86 src="//music.163.com/outchain/player?auto=0&height=66&';
		var source2 = 'type=' + type + '&id=' + id;
		var source3 = '"></iframe>';
		var source = source1 + source2 + source3;
		$(player).empty().append(source);
	});
}

mw.hook( 'wikipage.content' ).add( outChainMusic163Setup );

// 页面锚路径切换
function hashSwitchSetup ($content) {
	var $hashSwitches = $content.find('#hash-switch-directory');
	if ($hashSwitches.length === 0)
		return;
	if ($hashSwitches.length > 1) {
		$.each( $hashSwitches, function ( index, $directory ) {
			$($directory).empty().append('multiple directory found');
		});
		return;
	}
	var $directory = ($hashSwitches[0].dataset.hash || '').split(';').map(function (s) { return s.split(','); });
	if (!$directory.length)
		return;
	function index_from_hash($directory, hash, $default) {
		var $hash = hash.substr(1);
		for (var i = 0; i < $directory.length; i++) {
			var $item = $directory[i];
			for (var j = 0; j < $item.length; j++) {
				if ($item[j] === $hash) {
					return i;
				}
			}
		}
		return $default;
	}
	function hash_from_index($directory, index) {
		return ($directory[index] || [])[0] || '';
	}
	function refresh_hash_switch_state($directory, choice) {
		var hash_active_class = 'hash-active';
		$.each($(document).find('.hash-switch'), function(index, item) {
			var $item = $(item);
			if ($item.hasClass(choice)) {
				$item.addClass(hash_active_class);
			} else {
				$item.removeClass(hash_active_class);
			}
		});
	}
	var curhash = window.location.hash || '';
	var hash_class_prefix = 'hash-value-';
	var initial_choice = index_from_hash($directory, curhash, 0);
	var initial_choice_hash_class = hash_class_prefix + hash_from_index($directory, initial_choice);
	refresh_hash_switch_state($directory, initial_choice_hash_class);
	window.addEventListener("hashchange", function() {
		var newhash = window.location.hash || '';
		var new_choice = index_from_hash($directory, newhash, -1);
		if (new_choice === -1)
			return;
		var new_choice_hash_class = hash_class_prefix + hash_from_index($directory, new_choice);
		refresh_hash_switch_state($directory, new_choice_hash_class);
	}, false);
	window.hashSwitchDirectory = $directory;
}

mw.hook( 'wikipage.content').add( hashSwitchSetup );

// 外链设置

function externalLinkSetup($content) {
	var $externalLinks = $content.find('.target-blank-fix');
	$.each( $externalLinks, function ( index, $externalLinkContainer ) {
		$.each( $($externalLinkContainer).find('a'), function ( index, $externalLink) {
			$externalLink.target = 'blank';
			$externalLink.rel = 'nofollow noref';
		});
	});	
}

mw.hook( 'wikipage.content').add( externalLinkSetup );

// 让表格横向滚动超出部分
function tableWrapper($content) {
	var $tables = $content.find('table');
	$tables.each(function( index, element ) {
		$(element).wrap('<div class="overflow-box"></div>');
	});
}

mw.hook( 'wikipage.content').add( tableWrapper );

// 思源宋体懒加载
function addNotoSerifSCFontSupport($content) {
	if (!document.querySelector('#noto_serif_sc')) {
        var fontTag = document.createElement('link');
        fontTag.rel = 'stylesheet';
        fontTag.type = 'text/css';
        fontTag.id = 'noto_serif_sc';
        fontTag.href = 'https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;700&display=swap';
        document.head.appendChild(fontTag);
	}
}

mw.hook( 'wikipage.content').add( addSerifSCFontSupport );