Jump to content

User:PortalandPortal2Rocks/translatelinks.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
var targetLang = 'zh';
mw.loader.load('//zh.wikipedia.org/w/index.php?title=User:Zuohaocheng/cate-langlinks.js&action=raw&ctype=text/javascript');
 
 
 
/**
 
(王小朋友修改过的)
   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)
   用途: 检测具有中文版的条目,若有,则改变链接为中文。
   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").text("[["+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&format=xml&rawcontinue=&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&redirects=true&action=query&rawcontinue=&generator=categorymembers&gcmnamespace=0|14&gcmlimit=500&prop=langlinks&lllimit=500&gcmtitle=' + pageName;
	process(cateMemberURI, 'query-continue categorymembers', 'gcmcontinue');
    }
});