User:Zuohaocheng/langlinks.js

注意:保存之后,你必须清除浏览器缓存才能看到做出的更改。Google ChromeFirefoxMicrosoft EdgeSafari:按住⇧ Shift键并单击工具栏的“刷新”按钮。参阅Help:绕过浏览器缓存以获取更多帮助。
/**
   Name: Langlinks
   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年8月12日 (五) 00:45 (UTC)
   用途: 检测具有中文版的条目,若有,则改变链接的外观。".locArticleExist"
   Usage: Check whether localized article exists, and change its appearance. ".locArticleExist"
*/
if (typeof(targetLang) === 'undefined') {
    var targetLang = 'en';
}
 
$(function() {
    var process = function(baseURI, continueSelector, continueKey) {
	var processResult = function(resultjq) {
	    //处理重定向
	    resultjq.find('redirects r').each(function() {
		var article = $(this).attr('to');
		var orgArticle = $(this).attr('from');
		$('a[title="' + orgArticle + '"]').attr('title', article);
	    });

	    //处理页面 
	    var langSel = "langlinks ll[lang='" + targetLang + "']";
	    resultjq.find('pages page').each(function() {
		var article = $(this).attr('title');
		var localLink = $(this).find(langSel);
		if (localLink.length !== 0) {
		    var locArticle = localLink.text();
		    var titleSel = "a[title='" + article.replace('\'', '\\\'') + "']";
		    $(titleSel).addClass("locArticleExist").attr('title', locArticle);
		}
	    });
	};
	
	var continueGetLanglink = function(resultjq, URI) {
	    var continueLanglink = resultjq.find('query-continue langlinks');
	    if (continueLanglink.length !== 0) {
		//继续获取跨语言链接 
		var continueStr = continueLanglink.attr('llcontinue');
		var postData = {llcontinue: continueStr};
		
		$.post(URI, postData, function(result) {
		    continueGetLanglink($(result), URI);
		});
	    }
	    processResult(resultjq);
	}
	
	var continueGet = function(result) {
	    var resultjq = $(result);
	    var continueLink = resultjq.find(continueSelector);
	    if (continueLink.length !== 0) {
		//继续获取内部链接 
		var continueLinkURI = baseURI + '&' + continueKey + '=' + continueLink.attr(continueKey);
		$.get(continueLinkURI, continueGet);
	    }
	    
	    continueGetLanglink(resultjq, this.url);
	};
	
	$.get(baseURI, continueGet);
    };

    var pageName = encodeURIComponent(mediaWiki.config.get('wgPageName'));

    var apiURIprefix = '/w/api.php';
    var linksURIprefix = apiURIprefix + '?action=query&rawcontinue=&format=xml&generator=links&gpllimit=500&prop=langlinks&lllimit=500&redirects=true'
    var linksURI = linksURIprefix + '&titles=' + pageName;
 
    process(linksURI, 'query-continue links', 'gplcontinue');

    if (mediaWiki.config.get('wgNamespaceNumber') === 14) {
	var cateMemberURI = apiURIprefix + '?format=xml&rawcontinue=&redirects=true&action=query&generator=categorymembers&gcmnamespace=0|14&gcmlimit=500&prop=langlinks&lllimit=500&gcmtitle=' + pageName;
	process(cateMemberURI, 'query-continue categorymembers', 'gcmcontinue');
    }
});