User:Underlying lk/reasonator.js

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
	/*
	USAGE : For use on Wikidata, add the line
importScript("User:Underlying lk/reasonator.js");// [[User:Underlying lk/reasonator.js]]
	to your [[Special:Mypage/common.js]] page, or for Wikipedia
mw.loader.load("//www.wikidata.org/w/index.php?title=User:Underlying lk/test.js&action=raw&ctype=text/javascript");
	*/

$(function ($) {
	reasonatorPath = "http://tools.wmflabs.org/reasonator/?q=";
	langPref = "&lang=" + mw.config.get("wgUserLanguage");
	
	if ( mw.config.get('wgDBname') === 'wikidatawiki') { // different method for wikidata
	    if (mw.config.get("wgNamespaceNumber") === 0) {
	    	// Add portlet link for item
	    	mw.util.addPortletLink("p-tb", reasonatorPath + mw.config.get("wbEntityId") + langPref, "Reasonator", "t-reasonator", "Wikidata in pretty");
	    }
	} else { // and this one for wikipedias
	
  $.ajax({
    url: '//www.wikidata.org/w/api.php',
    data: {
      'format': 'json',
        'action': 'wbgetentities',
        'sites': mw.config.get('wgDBname'),
        'titles': mw.config.get('wgTitle'),
        'props': 'info',
        'maxage': mw.config.get("wgUserName") == null ? 900 : 30,
        'smaxage': mw.config.get("wgUserName") == null ? 900 : 30,
        'maxlag': 1
    },
    dataType: 'jsonp',
    success: function (data) {
      if (data.success) {
        for (var i in data.entities) {
          if (i != -1) { // -1 = no Wikidata item
            var item = data.entities[i],
              i = 0;
            mw.util.addPortletLink("p-tb", reasonatorPath + item.title + langPref, "Reasonator", "t-reasonator", "Wikidata in pretty");
          }
        }
      }
    }
  });
}
});