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:15, 18 June 2014 (Created page with '/** User:YFdyh000于2012.11.14修改,增加鼠标穿过时选中文本功能 Author: ZUO Haocheng User:zuohaocheng Email: Please feel free t...'). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)
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修改,增加鼠标穿过时选中文本功能
 
   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 langlinksURI = apiURIprefix + '?format=xml&action=query&prop=langlinks&lllimit=500&titles=' + pageName;
	$.get(langlinksURI, function(result) {
	    var selector = "langlinks ll[lang!='" + targetLang + "']";
	    var langlinks = {};
	    $(result).find(selector).each(function() {
		langlinks[$(this).attr('lang')] = $(this).text();
	    });
	    var localLangVar = $("html").attr("lang");
	    var localMainLang = wgContentLanguage;
	    var namespace = wgCanonicalNamespace;
	    var fullName = '';
	    if (namespace.length !== 0) {
		fullName = namespace + ':';
	    }
	    fullName += pageTitle;
	    langlinks[localMainLang] = fullName;
 
	    /* 
	     * Return an array that holds the names of the enumerable own properties of o. 
	     */
	    var keys = function(o) { 
		if (typeof o !== "object") throw TypeError();
		var result = []; 
		for(var prop in o) {
		    if (o.hasOwnProperty(prop)) result.push(prop);
		}
		return result;
	    }
 
	    var sortedLangs = keys(langlinks).sort();
	    copyText.push('');
	    for (i in sortedLangs) {
		var lang = sortedLangs[i];
		copyText.push('[[' + lang + ':' + langlinks[lang] + ']]');
	    }
 
	    copyZone.html(copyText.join('<br />')).mouseenter(function(){
		if ($.browser.msie) {
		    var range = document.body.createTextRange();
		    range.moveToElementText(this);
		    range.select();
		} else if ($.browser.mozilla || $.browser.opera) {
		    var selection = window.getSelection();
		    var range = document.createRange();
		    range.selectNodeContents(this);
		    selection.removeAllRanges();
		    selection.addRange(range);
		} else if ($.browser.webkit) {
		    var selection = window.getSelection();
		    selection.setBaseAndExtent(this, 0, this, this.innerText.length-1);
		}
	    });
	});
    });
});