Jump to content

User:Splarka/randab.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.
/* Random Dab Finder, version [0.0.0]
Originally from: http://en.wikipedia.org/wiki/User:Splarka/randab.js
Based on http://en.wikibooks.org/w/index.php?title=MediaWiki:Common.js/RandomBook.js&oldid=1496500

Grabs 10 random pages from the API, checks if any are disambiguations. Repeats until it finds a match.

Notes:
* Don't grab more than 10 random pages. Logged in users can get more, but anon users cannot. 
** Since this uses callback, all users are limited to 10 anyway.
* Automatically re-queries until it finds a page in "Category:All disambiguation pages".
** As 5.4% of pages are in this category, most queries should take only 2 or 3 iterations.
* Setting window.location.href eats history in some browsers.
** To show a link instead use: var ranDabAsLink = true; (users can set this in their monobook individually too).
* Uses curid links, I usually trust these more than trying to generate /wiki links.
*/

var ranDabAsLink = false;
var rbrqid = 0;
var rburl = mw.config.get('wgServer') + mw.config.get('wgScriptPath') + '/api.php?action=query&indexpageids=1&generator=random&grnnamespace=0&grnlimit=10&prop=categories&cllimit=max&format=json&callback=showRanDabCB&requestid=rb';
function showRanDab() {
  injectSpinner(document.getElementById('n-randab').firstChild,'ranDabSpinner');
  mw.loader.load(rburl + rbrqid);
  rbrqid++;
}

function showRanDabCB(obj) {
  if(!obj['query'] || !obj['query']['pages'] || !obj['query']['pageids']) {
    document.getElementById('n-randab').appendChild(document.createTextNode(' error'));
    removeSpinner('ranDabSpinner');
    return false;
  }
  var id = obj['query']['pageids'];
  var found;
  for(var i=0;i<id.length;i++) {
    var cats = obj['query']['pages'][id[i]]['categories'];
    if(!cats) continue
    for(var j=0;j<cats.length;j++) {
      var ct = cats[j]['title'];
      if(ct == 'Category:All disambiguation pages') {
        found = obj['query']['pages'][id[i]];
        break;
      }
    }
    if(found) break
  }
  if(!found) {
    // didn't find any, try again
    mw.loader.load(rburl + rbrqid);
    rbrqid++;
  } else {
    removeSpinner('ranDabSpinner');
    var link = mw.config.get('wgServer') + mw.config.get('wgScript') + '?curid=' + found['pageid'];

    if(window.ranDabAsLink) {
      var a = document.createElement('a');
      a.setAttribute('href',link);
      a.style.display = 'block';
      a.appendChild(document.createTextNode(' \u2022\u00A0' + found['title']))
      document.getElementById('n-randab').appendChild(a);
   } else if(window.location.assign) {
      window.location.assign(link);
   } else { 
      window.location.href = link; 
    }
  }
  return true;
}

function showRanDabLink() {
  mw.util.addPortletLink('p-navigation','javascript:showRanDab();','Random dab','n-randab','Show me a random disambiguation page');
}
$(showRanDabLink);