User:燃灯/foldRef.js

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

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

importStylesheet( 'User:燃灯/foldRef.css' );
$(function() {
	var currentToFoldElement;
	
	// for article space only 
	if (mw.config.get('wgNamespaceNumber') !== 0) {
		return;
	}
	
	var viewPortHeight = window.innerHeight;
	
	// target at the following classes / IDs
	var toFold = $('.reflist, .refbegin, #references-NoteFoot');
	
	for (i = 0; i < toFold.length; i++){
		currentToFoldElement = $(toFold[i]);
		
		// if greater than 90% of the viewport height, fold the references list
		if (currentToFoldElement.height() > viewPortHeight * 0.9) {
			foldRef(currentToFoldElement);
		}
	}
	
	// for articles using <references /> without {{reflist}}-like templates
	toFold = $('ol.references');
	for (i = 0; i < toFold.length; i++){
		currentToFoldElement = $(toFold[i]);
		
		if (currentToFoldElement.parent('div').parent('div').hasClass('rd-ref-folded') ||
			currentToFoldElement.parent('div').hasClass('rd-ref-folded') ||
			currentToFoldElement.parent('div').hasClass('reflist')) {
			continue;
		}
		
		// if greater than 90% of the viewport height, fold the references list
		if (currentToFoldElement.height() > viewPortHeight * 0.9) {
			foldRef(currentToFoldElement);
		}
	}
	
	// attach event listener to the expand buttons
	$('.rd-ref-expand-button').click( function(){
		$(this).prev('.rd-ref-folded').removeClass('rd-ref-folded');
		$(this).remove();
		});
});

// create a div, place the reflists inside
function foldRef(currentToFoldElement) {
	var newRefFolder = $('<div class="rd-ref-folded"></div>');
	newRefFolder.insertBefore(currentToFoldElement);
	currentToFoldElement.appendTo(newRefFolder);
	
	// add an expand button at the bottom
	newRefFolder.after('<div class="rd-ref-expand-button">展开</div>');
}