Jump to content

User talk:GregU/randomlink.js

Page contents not supported in other languages.
From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by GregU (talk | contribs) at 15:32, 8 September 2009 (Customization: See wikibits.js for documentation on addPortletLink().). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

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.

Installation

To enable this tool, just 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 (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)".

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(), for convenience. The following code adds three menu options in addition to "Random link":

addOnloadHook (function()
{
  addPortletLink('p-navigation', 'javascript:randomLink("Special:AllPages",4)',
                 'Random page', 'n-randompage2', 'Load a random article, the slow way');

  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 most articles in a WikiProject
  bblist = "Special:WhatLinksHere/Template:WikiProject_Baseball?namespace=1&hidelinks=1&limit=100";

  addPortletLink('p-navigation', 'javascript:randomlink_paged=1;randomLink(bblist)',
                 'Baseball article', 'n-randombb', 'Random article in WikiProject Baseball');
});

See wikibits.js for documention on addPortletLink().