User:Zuohaocheng/cate-langlinks.js

注意:保存之后,你必须清除浏览器缓存才能看到做出的更改。Google ChromeFirefoxMicrosoft EdgeSafari:按住⇧ Shift键并单击工具栏的“刷新”按钮。参阅Help:绕过浏览器缓存以获取更多帮助。
/**
   [[User:YFdyh000]]于2012.11.14修改,增加鼠标穿过时选中文本功能
 
   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日, 星期一 (11个月25日前), 09:35 PM (UTC+8)
   用途: 显示当前条目的分类所对应的其他语言版本.
   Usage: Shows categories in other langs of current article.
*/
if (typeof(targetLang) === 'undefined') {
    var targetLang = 'en';
}
 
$(function() {
    var apiURIprefix = '/w/api.php';
 
    var targetLoc = (function() {
	var targetL = $('<div></div>', {
	    id : 'cate-langlinks'
	});
 
	var seperator = $('<div></div>').css({
	    height: '1px',
	    'margin-top' : '0.5em',
	    'margin-bottom' : '0.5em',
	    'background-color' : '#aaa'
	});
 
	$('div#catlinks').append(seperator).append(targetL);
	return targetL;
    })();
    var targetWiki = '//' + targetLang + '.wikipedia.org/wiki/';
 
    var pageTitle = mw.config.get('wgTitle');
    var pageName = encodeURIComponent(mw.config.get('wgPageName'));
    var categoriesURI = apiURIprefix + '?format=xml&action=query&generator=categories&gclshow=!hidden&gcllimit=50&prop=langlinks&lllimit=500&titles=' + pageName;
 
    $.get(categoriesURI, function(result) {
	var locCates = [];
	var selector = "langlinks ll[lang='" + targetLang + "']";
	$(result).find('pages page').each(function() {
	    var cateName = $(this).attr('title');
	    var localLink = $(this).find(selector);
	    if (localLink.length !== 0) {
		locCates.push(localLink.text());
	    }
	});
 
	var copyText = [];
	var copyZone = $('<div></div>');
	targetLoc.empty();
 
	targetLoc.append($('<a></a>', {
	    'href': '#',
	    'text': "Localized Categories",
	    title : 'Show wiki text'
	}).click(function(event) {
	    event.preventDefault();
	    copyZone.slideToggle();
	}));
	targetLoc.append('(' + locCates.length + '): ');
	var targetUl = $('<ul></ul>');
	targetLoc.append(targetUl);
 
	for (i = 0; i<locCates.length; ++i) {
	    var targetCateName = locCates[i];
 
	    var linkURI = targetWiki + targetCateName;
	    var cateName = targetCateName.replace(/^.*?:(.*)$/, '$1');
 
	    $('<li></li>').append($('<a></a>', {
		'href': linkURI,
		'title': targetCateName,
		'text': cateName
	    })).appendTo(targetUl);
 
	    copyText.push('[[' +  $('<p></p>').text(targetCateName).html() + ']]');
	}
	copyZone.html(copyText.join('<br />')).hide().mouseenter(function(){
		if ($.browser.msie) {
		    range = document.body.createTextRange();
		    range.moveToElementText(this);
		    range.select();
		} else if ($.browser.mozilla || $.browser.opera) {
		    selection = window.getSelection();
		    range = document.createRange();
		    range.selectNodeContents(this);
		    selection.removeAllRanges();
		    selection.addRange(range);
		} else if ($.browser.webkit) {
		    selection = window.getSelection();
		    selection.setBaseAndExtent(this, 0, this, this.innerText.length-1);
		}
	    });
    	targetLoc.after(copyZone);
    });
});