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.
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.
// Travel to a random page linked in the article or listed on a special page.// See talk page for documentation. Greg Ubben//randomlink_start = [ "Wikipedia:Featured articles", "Wikipedia:Featured pictures" ];//randomlink_hops = 2;//randomlink_exclude = /^List of/;//randomlink_paged = true; // cats > 200 entries or multi-paged lists//randomlink_open = true; // if you want to open in new windows//randomlink_debug = true; // set this, to debugrandomlink_maxfrom=22000000;// 10-20% less than max page idrandomlink_maxoffset=950;// en.wikipedia is limited to 1000functionscrapeLinks(descend){vartopnode=document.getElementById('bodyContent')||document;varnode=document.getElementById('mw-subcategories');varatags=[];varlinks=[];varnspat=/^(Talk|User|Wik\w+|File|MediaWiki|Template|Help|Category|Portal|Book)( talk)?:/i;varspec=getElementsByClassName(topnode,'div','mw-spcontent');if(spec.length==1)topnode=spec[0];// skip help links at top of specialsif(node&&descend)topnode=node;// pick a sub-categoryelsefor(varidin{'mw-pages':0,'mw-category-media':0}){node=document.getElementById(id);if(node){varnodelist=node.getElementsByTagName('a');for(vari=0;i<nodelist.length;i++)atags.push(nodelist[i]);}}if(atags.length==0)atags=topnode.getElementsByTagName('a');nextlink:for(vari=0;i<atags.length;i++){varlink=atags[i];varhref=link.href;vartitle=link.title;if(!href)// needed?continue;if(link.className.search(/\b(external|internal|extiw|image)\b/)>=0)continue;if(href.search(/\/Special:|\?(?!.*redirect=|.*from=.*to=)/)>=0)continue;if(mw.config.get('wgIsArticle')&&mw.config.get('wgNamespaceNumber')!=14&&title.search(/^(Category|File):|^$|^#/)>=0)continue;if(mw.config.get('wgIsArticle')&&mw.config.get('wgNamespaceNumber')==0&&title.search(nspat)>=0)continue;if((mw.config.get('wgAction')=="history")!=hasClass(link,"mw-userlink"))continue;if(link.hostname!=___location.hostname)// commons.wikimedia.org on imagescontinue;if(link.parentNode.id=="coordinates")continue;// coords too common, or help linkif(typeofrandomlink_exclude!="undefined"&&title.search(randomlink_exclude)>=0)continue;// Exclude message boxes and the Metadata section on Image pages.// And also mw-usertool links and comments in page listings.// And also top links on Recent changes and watchlists.//for(varn=link.parentNode;n!=topnode;n=n.parentNode){if(n.id.search(/^mw-watchlist-options|^recentchangestext/)>=0)continuenextlink;if(n.className.search(/\b(usertool|summary|metadata|.mbox|comment|warn)/)>=0)continuenextlink;}links.push(link);}while(links.length&&links[0].parentNode.id.search(/contentSub|jump-to|target/i)>=0)links.shift();// breadcrumb linksif(links.length&&links[0].title=="Wikipedia:FAQ/Categories")links.shift();// "learn more" link in catsreturnlinks;}functionrandomLink(links,hops){varcontinuing=(typeoflinks=="object"&&links==null);if(typeoflinks=="undefined"&&typeofrandomlink_start!="undefined")links=randomlink_start;if(typeofhops=="undefined"&&typeofrandomlink_hops!="undefined")hops=randomlink_hops;if(typeofhops=="undefined")hops=1;if(hops>4)// sanity check; 4 needed for Special:AllPageshops=4;if(typeoflinks=="string"&&links.search(/^Special:(WhatLinksHere|RecentChangesLinked)$/i)>=0){links+="/"+mw.config.get('wgPageName')+"?limit=250";if(mw.config.get('wgNamespaceNumber')==0)links+="&namespace=0";// stay in article space if in it now}// Random contrib: Toggles between user, page, user, page, ...//if(typeoflinks=="string"&&links=="Special:Contributions")if(mw.config.get('wgPageName').search(/^User:[^\/]+$/)>=0)links+="/"+mw.config.get('wgPageName').slice(5)+"?namespace=0";elselinks=mw.config.get('wgPageName')+"?action=history";if(typeoflinks=="string")links=links.split("|");if(typeoflinks=="object"&&links!=null){for(vari=0;i<links.length;i++)links[i]=mw.config.get('wgArticlePath').replace("$1",links[i]).replace(/ /g,"_");hops++;}else{links=scrapeLinks(hops>1);}if(typeofrandomlink_debug!="undefined"&&randomlink_debug){varmsg=links.slice(0);if(msg.length>36)msg.splice(20,msg.length-35,"...");alert(links.length+" links:\n "+msg.join("\n "));}if(links.length==0)returnalert("I am unable to comply.");varnewpage=links[Math.floor(links.length*Math.random())].toString();if(typeofrandomlink_paged!="undefined"&&randomlink_paged)newpage=pagedUrl(newpage);if(mw.config.get('wgCanonicalNamespace')=="Category"&&newpage.indexOf("/Category:")==-1)hops=1;if(--hops>0){newpage+=(newpage.indexOf("?")>=0)?"&":"?";newpage+="random_hops="+hops;}// WikiProjects organize by talk pages, but let's end on the subject page.//if((mw.config.get('wgCanonicalNamespace')=="Category"||mw.config.get('wgCanonicalSpecialPageName')=="Whatlinkshere")&&continuing&&hops<=0)newpage=newpage.replace("/Talk:","/").replace("_talk:",":");if(typeofrandomlink_open!="undefined"&&randomlink_open&&!continuing)window.open(newpage);elsewindow.___location=newpage;}functionpagedUrl(url){varparam="";varvalue;varalphabet="!abcdefghijklmnoprstuvwy~";if(url.indexOf("/Category:")>=0){value=alphabet.charAt(alphabet.length*Math.random()).toUpperCase()+alphabet.charAt(alphabet.length*Math.random());param=(value<"M"?"from":"until");}if(url.indexOf("Special:WhatLinksHere")>=0){param="from";value=Math.floor(randomlink_maxfrom*Math.random());// Clustering will hurt randomness, but better than nothing}if(url.search(/Special:\w+s\b/)>=0){param="offset";value=Math.floor(randomlink_maxoffset*Math.random());}if(param){url+=(url.indexOf("?")>=0)?"&":"?";url+=param+"="+encodeURIComponent(value);}returnurl;}addOnloadHook(function(){varhops=document.URL.match(/[?&]random_hops=(\d+)/);if(hops)randomLink(null,hops[1]);varwhere='this page';if(typeofrandomlink_start!='undefined')where=randomlink_start.toString().slice(0,500);mw.util.addPortletLink('p-navigation','javascript:randomLink()','Random link','n-randomlink','Follow a randomly chosen link on '+where,'@');});