VOOZH about

URL: https://minecraft.wiki/w/MediaWiki:Gadget-betterRandom.js

⇱ MediaWiki:Gadget-betterRandom.js – Minecraft Wiki


MediaWiki:Gadget-betterRandom.js

From Minecraft Wiki
Jump to navigation Jump to search
In other languages

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 key and click the Reload toolbar button.
For details and instructions about other browsers, see Wikipedia:Bypass your cache.

$(()=>{
constmwconfig=mw.config.get([
'wgContentNamespaces',
'wgCanonicalSpecialPageName',
'wgTitle',
'wgFormattedNamespaces',
'wgNamespaceIds',
'wgArticlePath',
]);
constconfig={
blockDisambiguations:true,
blockedCategories:[
'Category:Soft redirects',
],
blockedTemplates:[
'Template:Infobox version',
],
allowedSubpages:[
'Commands',
'Tutorials',
'Dungeons:Tutorials',
],
queryLimit:5,
sidebarLinks:{
'n-randompage':mwconfig.wgContentNamespaces,
'n-in-Minecraft':[0],
'n-in-Dungeons':[10000],
'n-in-Earth':[10002],
'n-in-Story-Mode':[10004],
'n-in-Legends':[10006],
},
specialpage:{
heading:'Random page',
text:'You will be redirected to a random page shortly.'
},
};
if(mwconfig.wgCanonicalSpecialPageName==='Blankpage'){
let[,pagename,namespace]=mwconfig.wgTitle.split('/');
if(pagename==='random'){
document.getElementById('firstHeading').textContent=config.specialpage.heading;
document.getElementById('mw-content-text').textContent=config.specialpage.text;
letnamespaces=[];
if(namespace){
namespace=namespace.toLowerCase().replace(/_/g,' ').trim().replace(/ /g,'_');
if(namespace.includes(',')){
namespaces=namespace.split(/_*,_*/).map(ns=>mwconfig.wgNamespaceIds[ns]).filter(ns=>ns!=undefined);
}
elsenamespaces=[mwconfig.wgNamespaceIds[namespace]||0];
}
namespaces=[...newSet(namespaces.filter(ns=>ns>=0))];
if(!namespaces.length)namespaces=mwconfig.wgContentNamespaces;
letfallback=mw.util.getParamValue('fallback')||'Special:RandomRootpage/'+(namespace||'');
returnredirect(namespaces,fallback);
}
}
// Replace "Random page" buttons on Vector skin
for(letsidebarLinkIdinconfig.sidebarLinks){
letsidebarItem=document.getElementById(sidebarLinkId);
if(sidebarItem){
letsidebarLink=sidebarItem.getElementsByTagName('a')[0];
if(sidebarLink)generateListener(sidebarLink,config.sidebarLinks[sidebarLinkId]);
}
}
// Replace "Random page" button on Minerva skin
letmobileSidebarLink=document.querySelector('#p-navigation > li:nth-child(2) > a');
if(mobileSidebarLink)generateListener(mobileSidebarLink,mwconfig.wgContentNamespaces);
functiongenerateListener(sidebarLink,namespaces){
sidebarLink.addEventListener('click',function(e){
e.preventDefault();
redirect(namespaces,this.dataset.fallback||'Special:RandomRootpage');
});
letarticlePath=mwconfig.wgArticlePath.replace('$1','');
letfallback='';
if(sidebarLink.pathname.startsWith(articlePath)){
fallback=decodeURI(sidebarLink.pathname.slice(articlePath.length));
}
letpagename='Special:BlankPage/random';
if(namespaces!==mwconfig.wgContentNamespaces){
letnamespaceNames=namespaces.map(ns=>mwconfig.wgFormattedNamespaces[ns]).join(',')||'Main';
pagename+='/'+namespaceNames;
if(!fallback)fallback='Special:RandomRootpage/'+namespaceNames;
}
sidebarLink.href=mw.util.getUrl(pagename);
if(fallback){
sidebarLink.dataset.fallback=fallback;
sidebarLink.search='?fallback='+fallback.replace(/&/g,'%26');
}
}
functionredirect(namespaces,fallback){
constapi=newmw.Api();
api.get({
action:'query',
generator:'random',
grnnamespace:namespaces,
grnfilterredir:'nonredirects',
grnlimit:config.queryLimit,
prop:[
'info',
'pageprops',
'categories',
'templates',
],
inprop:'url',
ppprop:[
'noindex',
'disambiguation',
],
tltemplates:config.blockedTemplates,
tllimit:'max',
clcategories:config.blockedCategories,
cllimit:'max',
formatversion:2,
requestid:'gadget-betterRandom',
}).then(result=>{
letpages=result.query.pages;
// Remove strongly disallowed pages
pages=pages.filter(page=>{
if(page.pageprops&&'noindex'inpage.pageprops)returnfalse;
if(page.categories&&config.blockedCategories.length)returnfalse;
if(page.title.includes('/')){
letsplit=page.title.split('/');
if(split.length>2)returnfalse;
returnconfig.allowedSubpages.includes(split[0]);
}
returntrue;
});
// Use fallback if no valid pages remain
if(!pages.length){
location.href=mw.util.getUrl(fallback);
return;
}
// Shuffle pages because API returns them ordered by age
letcurrentIndex=pages.length;
while(currentIndex>0){
letrandomIndex=Math.floor(Math.random()*currentIndex);
currentIndex--;
[pages[currentIndex],pages[randomIndex]]=[pages[randomIndex],pages[currentIndex]];
}
// Find allowed page
letpage=pages.find(page=>{
if(page.pageprops&&'disambiguation'inpage.pageprops&&config.blockDisambiguations)returnfalse;
if(page.templates&&config.blockedTemplates.length)returnfalse;
returntrue;
})||pages[0];
location.href=page.fullurl;
}).catch(error=>{
console.log(error);
location.href=mw.util.getUrl(fallback);
});
}
});
Retrieved from "https://minecraft.wiki/w/MediaWiki:Gadget-betterRandom.js?oldid=3530417"

Navigation menu