Jump to content

User:Gary/enhanced random article.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Gary (talk | contribs) at 14:57, 26 May 2010 (creating). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)
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.
/*
	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;
}

enhancedRandomArticle = 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:enhancedRandomArticle();', '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.enhancedRandomArticleCallback = enhancedRandomArticleCallback;
	unsafeWindow.enhancedRandomArticleCallback2 = enhancedRandomArticleCallback2;
}