User talk:GregU/randomlink.js
This tool adds a "Random link" option to the sidebar menu. When selected, you will travel to a random link on the current page (or specified page or pages). This is useful if you wish to view a random article that is related to the current article. It is also useful for unbiasedly selecting an article to work on from a specified category or list of articles.
The tool will consider only "normal" links in an article. It is also designed to do the right thing on special pages, considering only the "main" links. For example, on recent changes or user contributions (among many others), it will choose a title from the list, excluding user links and other links. But on history pages, it will follow only user page links, as they are the main thing there. In categories, it will choose only items... if there are any pages or files in the category. Else, it will use content links and subcategories (and parent categories, currently). However if hops > 1, it will always descend into subcategories if there are any.
The example configuration at the end shows the kinds of things you can do, adding menu options:
- Random page – loads a random article the slow way, by drilling down randomly through Special:AllPages
- Featured article – goes to a random featured article listed on WP:FA
- Video game article – goes to a random article in WikiProject Video games, via categories
- Baseball article – goes to a random article in WikiProject Baseball, via What links here
Installation
To enable this tool, add importScript("User:GregU/randomlink.js")
to your monobook.js page, then hit reload.
Customization
Several variables can be added to your monobook.js to customize the tool.
- randomlink_open = true;
- Causes the new page to be opened in a new window or tab, leaving the current window/tab unchanged.
- randomlink_start = "Wikipedia:Featured articles";
- randomlink_start = [ "Wikipedia:Featured articles", "Wikipedia:Featured pictures" ];
- Use this variable to start from a specified page rather than from the current page. If you list multiple start pages (either as an array or as a pipe-delimited string), the tool will select randomly among them. To weight a start page more strongly (because it has more links under it), list it multiple times.
- randomlink_hops = 2;
- Normally, only one level is followed, not counting the start page. But if your start page is a category that lists other categories, it will take several hops to reach an actual article. Hops are limited to 4.
- randomlink_paged = true;
- Some special pages are paged, with the first page listing only the first hundred or so links. To get around this limitation, set this variable to true, and the tool will attempt to select a random section of the full result set for its start page. For example, if your start page is a category that usually has over 200 articles in it or what links here with over 500 links, you should probably set this. Also use it on special pages like uncategorized pages.
- randomlink_exclude = /^List of|\(series\)/;
- If certain titles are being selected that you wish to exclude, you can use this variable to filter them out based on a regular expression. The above example excludes titles beginning with "List of" or containing "(series)".
- randomlink_maxfrom = 24000000;
- If you're the kind of person who likes to consume something to the last drop, then this is the option for you. This parameter sets the maximum page id allowed when doing a paged what links here. The default value is about 15% less than the maximum page id, to ensure that at least 100 articles are always found past it even for smallish WikiProjects. But you can tune it for larger cases to ensure that the most recent articles make it into the pool. As of this writing, the value above is tuned to reach the end of the baseball articles. It might return nothing for smaller WikiProjects, where 22000000 might be more appropriate.
Multiple menu options
To configure the tool for several different purposes, you can add your own menu options that call randomLink(). The randomlink_start and randomlink_hops settings can be specified as parameters to randomLink() in this case. The following code adds four menu options in addition to "Random link":
importScript("User:GregU/randomlink.js");
addOnloadHook( function()
{
addPortletLink('p-navigation', 'javascript:randomLink("Special:AllPages",4)',
'Random page', 'n-randompage2', 'Load a random article, the slow way');
// Ignore most of the top and bottom meta links on WP:FA
randomlink_exclude = /^Wikipedia:|^Portal:|^(Lists?|Outline|Library) of|^(Deaths in )?20\d\d$/;
addPortletLink('p-navigation', 'javascript:randomLink("Wikipedia:Featured articles")',
'Featured article', 'n-randomfa', 'Pick a random featured article');
vgcats = "Top|High|Mid|Low|Low|Low|Low|Low|Low|Low|NA|Unknown";
vgcats = vgcats.replace(/\w+/g, "Category:$&-importance video game articles");
addPortletLink('p-navigation', 'javascript:randomlink_paged=1;randomLink(vgcats)',
'Video game article', 'n-randomvg', 'Random article in WikiProject Video games');
// WhatLinksHere is probably a better way to find all articles in a WikiProject
bblist = "Special:WhatLinksHere/Template:WikiProject_Baseball?namespace=1&hidelinks=1&limit=250";
addPortletLink('p-navigation', 'javascript:randomlink_paged=1;randomlink_maxfrom=24000000;randomLink(bblist)',
'Baseball article', 'n-randombb', 'Random article in WikiProject Baseball');
});
See wikibits.js for documention on addPortletLink().