Jump to content

User:Stevenliuyi/Wikidata.js

From Wikipedia, the free encyclopedia
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.
$(function ($) {
  var translate = {
    'en': {
      notfound: "Wikidata item not found.",
      intro: "Wikidata: ",
      nolabel: "(No label)",
      nodescription: "no description given",
      aliases: "Aliases: ",
      noaliases: "None"
    },
    'zh': {
      notfound: "维基数据中未找到该项目,点击创建新项",
      intro: "维基数据",
      nolabel: "[无标签]",
      nodescription: "无描述",
      aliases: "别名",
      noaliases: "无"
    }
  }
  var namespace = mw.config.get('wgCanonicalNamespace');
  if (namespace === '') {
    var page = mw.config.get('wgTitle');
  } else {
    var page = namespace + ':' + mw.config.get('wgTitle');
  }
 
  $.ajax({
    url: '//www.wikidata.org/w/api.php',
    data: {
      'format': 'json',
        'action': 'wbgetitems',
        'sites': mw.config.get('wgDBname'),
        'titles': page,
        'props': 'info|labels|descriptions|aliases',
        'languages': mw.config.get('wgPageContentLanguage')
    },
    dataType: 'jsonp',
    success: function (data) {
/*
      var d = $("<div>");
      $("#contentSub").append(d);
      if (data.success) {
        for (var i in data.items) {
          var lang = mw.config.get('wgPageContentLanguage'), m = translate[ lang ] || translate.en;
          if (i == -1) {
            d.append($("<a>").attr("href", "//www.wikidata.org/wiki/Special:CreateItem").text( m.notfound ))
          } else {
            var item = data.items[i],
              i = 0, aliases = [];
            try {
              for (; i < item.aliases[lang].length; i++) {
                aliases.push(item.aliases[lang][i].value);
              }
            } catch (e) { }
            d.append($("<a>").attr("href", "//www.wikidata.org/wiki/" + item.title).text(m.intro + ( item.labels ? item.labels[lang].value : m.nolabel ) + " (" + item.title + ")")).append($("<span>").text(", ")).append($("<i>").text(item.descriptions ? item.descriptions[lang].value : m.nodescription)).append($("<br>")).append($("<span>").text(m.aliases + (aliases.join(", ") || m.noaliases)))
          }
        }
      }
*/
      var d = $("<li>");
      $("#p-views ul").append(d);
      if (data.success) {
        for (var i in data.items) {
          var lang = mw.config.get('wgPageContentLanguage'), m = translate[ lang ] || translate.en;
          if (i == -1) {
            d.append($("<span>").append($("<a>").attr("href", "//www.wikidata.org/wiki/Special:CreateItem").attr("title", m.notfound).append($("<img>").attr("src", "//upload.wikimedia.org/wikipedia/commons/thumb/f/ff/Wikidata-logo.svg/30px-Wikidata-logo.svg.png"))));
          } else {
            var item = data.items[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/Special:CreateItem").attr("title", ( item.labels ? item.labels[lang].value : m.nolabel ) + " (" + item.title + "):" + ( item.descriptions ? item.descriptions[lang].value : m.nodescription )).append($("<img>").attr("src", "//upload.wikimedia.org/wikipedia/commons/thumb/f/ff/Wikidata-logo.svg/30px-Wikidata-logo.svg.png"))));
          }
        }
      }

/*
<div class="metadata topicon noprint nopopups" title="{{{title|}}}" id="{{{id|}}}" style="display: none;">[[File:{{{image|Wikipedia-info.png}}}|{{{size|24px}}}|{{{alt|}}}|link={{{link|Template:Topicon}}}]]</div>

      var d1 = $("<div>").attr("class", "metadata topicon noprint nopopups");
      $("#mw-content-text").prepend(d1);
      if (data.success) {
        for (var i in data.items) {
          var lang = mw.config.get('wgPageContentLanguage'), m = translate[ lang ] || translate.en;
          if (i == -1) {
            d1.append($("<a>").attr("href", "//www.wikidata.org/wiki/Special:CreateItem").text($("<img>").attr("src", "//upload.wikimedia.org/wikipedia/commons/thumb/f/ff/Wikidata-logo.svg/30px-Wikidata-logo.svg.png").attr("alt", m.notfound)));
          } 

 else {
            var item = data.items[i],
              i = 0, aliases = [];
            try {
              for (; i < item.aliases[lang].length; i++) {
                aliases.push(item.aliases[lang][i].value);
              }
            } catch (e) { }
            d.append($("<a>").attr("href", "//www.wikidata.org/wiki/" + item.title).text(m.intro + ( item.labels ? item.labels[lang].value : m.nolabel ) + " (" + item.title + ")")).append($("<span>").text(", ")).append($("<i>").text(item.descriptions ? item.descriptions[lang].value : m.nodescription)).append($("<br>")).append($("<span>").text(m.aliases + (aliases.join(", ") || m.noaliases)))
          }
        }
      }
*/

    }
  });
});