跳转到内容

User:Cwek/Wikidata.js

维基百科,自由的百科全书
注意:保存之后,你必须清除浏览器缓存才能看到做出的更改。Google ChromeFirefoxMicrosoft EdgeSafari:按住⇧ Shift键并单击工具栏的“刷新”按钮。参阅Help:绕过浏览器缓存以获取更多帮助。
//改写自[[User:Stevenliuyi/scripts/Wikidata.js]]和[[:d:User:Stevenliuyi/scripts/Wikidata.js]]

$(function ($) {
  var translate = 
  {
    'zh-hans': 
	{
      notfound: "维基数据中相应项目尚未建立,点击创建新项",
      nolabel: "[无标签]",
      nodescription: "[无描述]"
    },
    'zh-hant': 
	{
      notfound: "維基數據中相應項目尚未建立,點擊創建新項",
      nolabel: "[無標籤]",
      nodescription: "[無描述]"
    }
  }

  var variant = 
  {
    'zh-hans': 'zh-hans',
    'zh-cn': 'zh-hans',
    'zh-sg': 'zh-hans',
    'zh-hant': 'zh-hant',
    'zh-hk': 'zh-hant',
    'zh-tw': 'zh-hant'
  }

  var namespace = mw.config.get('wgNamespaceNumber');
 if (namespace!=2&&(namespace%2==0&&namespace>=0&&mw.config.get('wgAction')=='view'))
 {  
    var page = (mw.config.get('wgCanonicalNamespace')===''?'':mw.config.get('wgFormattedNamespaces')[namespace]+":")+mw.config.get('wgTitle');
 }
 /*else
 {
    return null;
 }*/

  $.ajax
  (
	{
    url: '//www.wikidata.org/w/api.php',
    data: 
	{
      'format': 'json',
        'action': 'wbgetentities',
        'sites': mw.config.get('wgDBname'),
        'titles': page,
        'props': 'info|labels|descriptions|aliases',
        'languages': mw.config.get('wgPageContentLanguage')
    },

    dataType: 'jsonp',
    success: function (data) 
		{
			var d = $("<li>");
			$("#p-views ul").append(d);
			if (data.success) 
			{
				for (var i in data.entities) 
				{
					var lang = mw.config.get('wgPageContentLanguage');
					var m = translate[ variant[mw.config.get('wgUserVariant')] ] || translate['zh-hans'];
					if (i == -1) 
					{
						d.append($("<span>").append($("<a>").attr("href", "//www.wikidata.org/wiki/Special:CreateItem?uselang="+wgPageContentLanguage+"&label="+page+"&site="+mw.config.get('wgDBname')+"&page=" +page).attr("title", m.notfound).append($("<img>").attr("src", "//upload.wikimedia.org/wikipedia/commons/e/e8/Wikidata-favicon.png").attr("style", "margin-bottom: 5px; opacity:0.3; filter:alpha(opacity=30);"))));
					} 
					else 
					{
						var item = data.entities[i],
						i = 0, aliases = [];
						try 
						{
							for (; i < item.aliases[lang].length; i++) 
							{
								aliases.push(item.aliases[lang][i].value);
							}
						} catch (e) { }
						d.append($("<span>").append($("<a>").attr("href", "//www.wikidata.org/wiki/" + item.title).attr("title", ( item.labels && item.labels[lang] ? item.labels[lang].value : m.nolabel ) + " (" + item.title + "):" + ( item.descriptions[lang] ? item.descriptions[lang].value : m.nodescription )).append($("<img>").attr("src", "//upload.wikimedia.org/wikipedia/commons/e/e8/Wikidata-favicon.png").attr("style", "margin-bottom: 5px;"))));
					}
				}
			} 
		}
	}
  );
});