跳转到内容

User:Stevenliuyi/scripts/Wikidata.js

维基百科,自由的百科全书
注意:保存之后,你必须清除浏览器缓存才能看到做出的更改。Google ChromeFirefoxMicrosoft EdgeSafari:按住⇧ Shift键并单击工具栏的“刷新”按钮。参阅Help:绕过浏览器缓存以获取更多帮助。
$(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('wgCanonicalNamespace');
  if (namespace === '') {
    var page = mw.config.get('wgTitle');
  }
 
  $.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:NewItem?setlang=zh&label=" + mw.config.get('wgTitle') + "&site=zhwiki&page=" + mw.config.get('wgTitle')).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 && 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;"))));
          }
        }
      } 
    }
  });
});