User:Zuohaocheng/navbox-link.js

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

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

/**
   Author: ZUO Haocheng [[User:zuohaocheng]]
   Email: Please feel free to email me via http://en.wikipedia.org/wiki/Special:EmailUser/Zuohaocheng
   电邮: 请通过 http://zh.wikipedia.org/wiki/Special:EmailUser/Zuohaocheng 给我发送电邮
   Date: 2011年11月21日 (一) 09:27 (UTC)
   用途: 对使用了{{Navbox}}的模版有效. 可以在分类之下显示本模版中链接到的条目, 有哪些没有包含本模版.
   Usage: Effects on for Templates which uses {{navbox}}. Shows a list of articles which the template links to but not included by the articles below categories.
*/

$(document).ready(function() {
    var apiURIprefix = '/w/api.php';
    var pageName = encodeURIComponent(mediaWiki.config.get('wgPageName'));

    var navboxLinksGet = function() {
	//获取本模版链接到的条目中是否包含了本模板
	var linksURI = apiURIprefix + '?action=query&format=xml&redirects=true&generator=links&gplnamespace=0&gpllimit=500&prop=templates&tllimit=500&tlnamespace=10&tltemplates=' + pageName + '&titles=' + pageName;
	var fGetLink = function(result) {
	    var targetLoc;
	    var targetUl;
	    // 添加目标位置
	    var targetLocFunc = function() {
		var nla = $('<div></div>', {
		    id : 'not-listed-articles',
		});
		var nla_refresh = $('<a></a>', {
		    href : '#',
		    title : '强制刷新', 
		    id : "nla-refresh",
		    text : "未添加本模版的条目"
		});
		nla.append(nla_refresh).append(': ');
		var seperator = $('<div></div>').css({
		    height: '1px',
		    'margin-top' : '0.5em',
		    'margin-bottom' : '0.5em',
		    'background-color' : '#aaa'
		});

		$('#catlinks').append(seperator).append(nla);

		nla_refresh.click(function(event) {
		    event.preventDefault();

		    var d = new Date();
		    var requestid = d.getTime();

		    var rLinksURI = linksURI + '&requestid=' + requestid;
		    $.get(rLinksURI, function(result) {
			seperator.remove();
			nla.remove();
			fGetLink(result);
		    });
		});
		return nla;
	    };

	    var redirects = {};
	    $(result).find('redirects r').each(function() {
		var item = $(this);
		redirects[item.attr('to')] = item.attr('from');
	    });

	    $(result).find("pages page").each(function() {
		//判断已包含 && 红链
		if ($(this).find("templates").length === 0 && typeof($(this).attr("missing")) === 'undefined') {
		    var pageTitle = $(this).attr('title');
		    if (typeof(targetLoc) === 'undefined') {
			targetLoc = targetLocFunc();
			targetUl = $('<ul></ul>');
			targetLoc.append(targetUl);
		    }

		    var link = $('<a></a>', {
			'title': pageTitle,
			'text': pageTitle,
			'href': '/wiki/' + encodeURIComponent(pageTitle)
		    });
		    $('<li></li>').append(link).appendTo(targetUl);

		    var linksToChange = $('a[title="' + pageTitle + '"], a[title="' + redirects[pageTitle] + '"]');

		    link.mouseenter(function() {
			linksToChange.css('background-color', 'yellow');
		    });

		    link.mouseleave(function() {
			linksToChange.css('background-color', '');
		    });
		}
	    });
	};
	$.get(linksURI, fGetLink);
    };

    // 判断是否属于Template && 是否包含navbox
    if (mediaWiki.config.get('wgNamespaceNumber') === 10 && $("table.navbox").length !== 0) {
    	navboxLinksGet();
    }
});