User:PortalandPortal2Rocks/translatelinks.js
Appearance
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
Documentation for this user script can be added at User:PortalandPortal2Rocks/translatelinks. |
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');
}
});