User:Splarka/randab.js
Appearance
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
Documentation for this user script can be added at User:Splarka/randab. |
/* 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);