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
- Match patterns to NOT show.
*/
if (typeof(enhancedRandomArticle) == 'undefined')
enhancedRandomArticle = {};
if (typeof(unsafeWindow) != 'undefined')
{
var addPortletLink = unsafeWindow.addPortletLink;
var mw = unsafeWindow.mw;
}
enhancedRandomArticleBegin = function()
{
/*
DEFAULT SETTINGS
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;
$.get(mw.config.get('wgScriptPath') + '/api.php', { action: 'query', list: 'random', rnnamespace: 0, format: 'json', rnlimit: 10 }, eRCreateArticleList);
}
eRCreateArticleList = function(obj)
{
var articles = obj.query.random;
var articlesArray = [];
for (var i = 0; i < articles.length; i++)
articlesArray.push(articles[i].title);
$.get(mw.config.get('wgScriptPath') + '/api.php', { action: 'query', prop: 'revisions', rvdir: 'older', rvprop: 'content', titles: articlesArray.join('|'), format: 'json' }, eRCheckMatches);
}
eRCheckMatches = 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;
var matchedPattern = '';
var eRA = enhancedRandomArticle;
if (typeof(eRA.patterns) == 'string') eRA.patterns = [eRA.patterns];
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++)
{
// Found a pattern in an article's title
if (title.search(eRA.patterns[i]) != -1)
{
matched = true;
matchedPattern = eRA.patterns[i];
break;
}
}
// User wants disambig and stubs, has no patterns
if (!eRA.patterns.length && eRA.showDisambiguationPages && eRA.showStubs) matched = true;
// User doesn't want disambig, gets disambig
if (matched && !eRA.showDisambiguationPages && (content.match(/disambig}}/i) || content.match(/dis}}/i) || content.match(/disambiguation}}/i))) matched = false;
else if (!eRA.patterns.length && !eRA.showDisambiguationPages && content.indexOf('{{disambig}}') == -1) matched = true;
// User doesn't want stubs, gets stub
if (matched && !eRA.showStubs && content.indexOf('stub}}') != -1) matched = false;
else if (!eRA.patterns.length && !eRA.showStubs && content.indexOf('stub}}') == -1) matched = true;
if (matched)
{
window.location = '/wiki/' + title;
return;
}
matched = false;
matchedPattern = '';
}
alert("ENHANCED RANDOM ARTICLES\n\nAn article that meets all of your criteria could not be found. Please try again.\nAlso, perhaps consider using less restrictive criteria.\n\nKeep in mind that only 10 articles are retrieved each time this script is activated,\nso there's a good chance that most of them won't match your criteria if it's too strict.");
}
function addEnhancedRandomArticlePortletLink()
{
if ($('#p-navigation').length)
{
mw.util.addPortletLink('p-navigation', 'javascript:enhancedRandomArticleBegin();', 'Enhanced random', 't-enhanced-random-article', 'Show a random article, with enhanced settings', '', '#n-randompage');
if (!$('wpDiff').length && !$('.historysubmit').length)
{
var node = $('#t-enhanced-random-article');
var nodeLink = node.children().eq(0);
nodeLink.attr('accesskey', 'v');
nodeLink.attr('title', nodeLink.attr('title') + ' [ctrl-v]');
}
}
}
$(document).ready(function()
{
addEnhancedRandomArticlePortletLink();
});
if (typeof(unsafeWindow) != 'undefined')
{
unsafeWindow.enhancedRandomArticle = enhancedRandomArticle;
unsafeWindow.enhancedRandomArticleBegin = enhancedRandomArticleBegin;
unsafeWindow.eRCheckMatches = eRCheckMatches;
unsafeWindow.eRCreateArticleList = eRCreateArticleList;
}