User:Gary/enhanced random article.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. |
![]() | This user script seems to have a documentation page at User:Gary/enhanced random article. |
/*
ENHANCED RANDOM ARTICLE
Documentation: [[Wikipedia:Enhanced Random Article]]
Description: Enhances the existing "Random article" feature.
Disambiguation pages, redirects, and pages with titles that match a certain pattern can be skipped over.
TODO Option to choose the smallest filesize for a page to show it.
*/
if (typeof(enhancedRandomArticle) == 'undefined')
enhancedRandomArticle = {};
if (typeof(unsafeWindow) != 'undefined')
{
var addPortletLink = unsafeWindow.addPortletLink;
var importScriptURI = unsafeWindow.importScriptURI;
var wgServer = unsafeWindow.wgServer;
var wgScriptPath = unsafeWindow.wgScriptPath;
}
enhancedRandomArticleBegin = function()
{
/*
enhancedRandomArticle = {};
enhancedRandomArticle.patterns = [];
enhancedRandomArticle.showDisambiguationPages = false;
enhancedRandomArticle.showStubs = false;
*/
if (typeof(enhancedRandomArticle) == 'undefined')
enhancedRandomArticle = {};
if (typeof(enhancedRandomArticle.patterns) == 'undefined')
enhancedRandomArticle.patterns = [];
if (typeof(enhancedRandomArticle.showDisambiguationPages) == 'undefined')
enhancedRandomArticle.showDisambiguationPages = false;
if (typeof(enhancedRandomArticle.showStubs) == 'undefined')
enhancedRandomArticle.showStubs = false;
eRA = enhancedRandomArticle;
importScriptURI(wgServer + wgScriptPath + '/api.php?action=query&list=random&rnnamespace=0&format=json&rnlimit=10&callback=enhancedRandomArticleCallback');
}
enhancedRandomArticleCallback = function(obj)
{
var articles = obj['query']['random'];
var articlesArray = [];
for (var i = 0; i < articles.length; i++)
{
articlesArray.push(articles[i]['title']);
}
importScriptURI(wgServer + wgScriptPath + '/api.php?action=query&prop=revisions&rvdir=older&rvprop=content&titles=' + articlesArray.join('|') + '&format=json&callback=enhancedRandomArticleCallback2');
}
enhancedRandomArticleCallback2 = function(obj)
{
var articles = [];
for (var page in obj['query']['pages'])
articles.push(obj['query']['pages'][page])
// match patterns
var content, pattern, title;
var matched = false;
for (var j = 0; j < articles.length; j++)
{
content = articles[j]['revisions'][0]['*'];
title = articles[j]['title'];
for (var i = 0; i < eRA.patterns.length; i++)
{
pattern = eRA.patterns[i];
if (title.search(pattern) != -1)
matched = true;
}
if (!matched && ((!eRA.showDisambiguationPages && content.indexOf('{{disambig}}') != -1) || (!eRA.showStubs && content.indexOf('stub}}') != -1)))
matched = true;
if (!matched)
{
window.location = '/wiki/' + title;
return;
}
else
matched = false;
}
alert("ENHANCED RANDOM ARTICLES\n\nAn article that meets all of your criteria could not be found. Please try again.\n\nAlso, perhaps consider using less restrictive criteria.");
location.reload(true);
}
function addEnhancedRandomArticlePortletLink()
{
if (document.getElementById('p-navigation'))
{
addPortletLink('p-navigation', 'javascript:enhancedRandomArticleBegin();', 'Enhanced random', 't-enhanced-random-article', 'Show a random article, with enhanced settings');
if (!document.getElementById('wpDiff') && !document.getElementsByClassName('historysubmit')[0])
{
var node = document.getElementById('t-enhanced-random-article');
node.firstChild.accessKey = 'v';
node.firstChild.title += ' [ctrl-v]';
}
}
}
if (typeof(unsafeWindow) != 'undefined')
addEnhancedRandomArticlePortletLink();
else
{
addOnloadHook(function()
{
addEnhancedRandomArticlePortletLink();
});
}
if (typeof(unsafeWindow) != 'undefined')
{
unsafeWindow.enhancedRandomArticle = enhancedRandomArticle;
unsafeWindow.enhancedRandomArticleBegin = enhancedRandomArticleBegin;
unsafeWindow.enhancedRandomArticleCallback = enhancedRandomArticleCallback;
unsafeWindow.enhancedRandomArticleCallback2 = enhancedRandomArticleCallback2;
}