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 these menu options:
- Random link – default behavior, follows a random link on current page
- Random back-link – goes backwards, to a random page that links here
- 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
[edit]To enable this tool, add importScript("User:GregU/randomlink.js")
to your personal JavaScript page, then hit reload.
Customization
[edit]Several variables can be added to your vector.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
[edit]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 five menu options in addition to "Random link":
importScript("User:GregU/randomlink.js");
addOnloadHook( function()
{
// Other special cases are "Special:RecentChangesLinked" and "Special:Contributions"
addPortletLink('p-navigation', 'javascript:randomLink("Special:WhatLinksHere")',
'Random back-link', 'n-randomback', 'Random page that links here');
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().
Bookmarks
[edit]To access this tool from browser bookmarks, see this discussion.
See also
[edit]- hotkeys.js – enter symbols and common phrases using customizable keyboard macros
- familytree.js – tool to easily create or update {{familytree}} diagrams
- randomarticle.php on the toolserver
- Wikipedia:Random
Interface-protected edit request on 25 August 2022
[edit]This edit request has been answered. Set the |answered= or |ans= parameter to no to reactivate your request. |
When trying to install and use the script, I found that it was broken. Specifically, manually calling randomLink()
lead to an error regarding the fact that getElementsByClassName
is not defined in a script. A quick search and testing in my common.js (version as of writing) revealed that the problem could be fixed by replacing line 21, which currently reads var spec = getElementsByClassName(topnode, 'div', 'mw-spcontent');
, with var spec = document.getElementsByClassName(topnode, 'div', 'mw-spcontent');
. I personally would appreciate being able to load the script rather than copy and paste the fixed version into my common.js and I am sure others who learn of this script would feel the same. Thank you for any help and take care. —The Editor's Apprentice (talk) 19:12, 25 August 2022 (UTC)
- @The Editor's Apprentice the maintainer of this script hasn't been active in over a decade, you probably shouldn't rely on it here anymore. So what can be better - is close to what you didn't want to do. Fork this this User:The Editor's Apprentice/randomlink.js and import that. Then you can maintain it. We could even deprecate this one with an alert that it is deprecated and invite people to use maintained version instead if they want to. — xaosflux Talk 18:47, 26 August 2022 (UTC)
- Deactivated ER, at least pending some response to this. — xaosflux Talk 00:25, 28 August 2022 (UTC)