Jump to content

User:LNDDYL/cate-langlinks.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by LNDDYL (talk | contribs) at 13:19, 18 June 2014. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
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.
/**
   [[User:YFdyh000]]于2012.11.14修改,增加鼠标穿过时选中文本功能
   [[User:YFdyh000]]于2014.01.02修改,去除复制跨语言链接。
 
   The code from [[:zh:User:Zuohaocheng/cate-langlinks.js]]
 
   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 = wgTitle;
    var pageName = encodeURIComponent(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();
	targetLoc.after(copyZone);
 
    });
});
 
/**************************************************/
 
var SelectText = function(element) {
//var text = document.getElementById(element);
var text = element;
if ($.browser.msie) {
var range = document.body.createTextRange();
range.moveToElementText(text);
range.select();
} else if ($.browser.mozilla || $.browser.opera) {
var selection = window.getSelection();
var range = document.createRange();
range.selectNodeContents(text);
selection.removeAllRanges();
selection.addRange(range);
} else if ($.browser.safari) {
var selection = window.getSelection();
selection.setBaseAndExtent(text, 0, text, 1);
}
}
 
$('#catlinks div:last').live('mouseenter', function(){SelectText(this);});