MediaWiki:Gadget-betterRandom.js

来自Minecraft Wiki
跳转到导航 跳转到搜索
其他语言中

注意:在保存之后,您可能需要清除浏览器缓存才能看到所作出的更改的影响。
  • Firefox或Safari:按住Shift的同时单击刷新,或按Ctrl-F5或Ctrl-R(Mac为⌘-R)
  • Google Chrome:按Ctrl-Shift-R(Mac为⌘-Shift-R)
  • Internet Explorer:按住Ctrl的同时单击刷新,或按Ctrl-F5
  • Opera:前往菜单 → 设置(Mac为Opera → 选项),然后隐私和安全 → 清除浏览数据 → 缓存的图片和文件。
$(() => {
	const config = {
		blockDisambiguations: true,
		blockedCategories: [
			'Category:软重定向',
		],
		blockedTemplates: [
			'Template:Infobox version',
		],
		allowedSubpages: [
			'命令',
			'属性',
		],
		queryLimit: 5,
		sidebarLinks: {
			'n-randompage': mw.config.get('wgContentNamespaces'),
			'n-sidebar-navigation-randomminecraftpage': [0, 9998],
			'n-sidebar-navigation-randomdungeonspage': [9996, 10000],
			'n-sidebar-navigation-randomearthpage': [10002],
			// 'n-sidebar-navigation-randomstorymodespage': [10004],
			'n-sidebar-navigation-randomlegendspage': [9994, 10006],
		},
	};

	const generateListener = (namespaces) => function(e) {
		e.preventDefault();
		redirect(namespaces, this.href);
	};
	
	// Replace "Random page" buttons on Vector skin
	for (let sidebarLinkId in config.sidebarLinks) {
		let sidebarItem = document.getElementById(sidebarLinkId);
		if (sidebarItem) {
			let sidebarLink = sidebarItem.getElementsByTagName('a')[0];
			if (sidebarLink) sidebarLink.addEventListener('click', generateListener(config.sidebarLinks[sidebarLinkId]));
		}
	}
	
	// Replace "Random page" button on Minerva skin
	let mobileSidebarLink = document.querySelector('#p-navigation > li:nth-child(2) > a');
	if (mobileSidebarLink) mobileSidebarLink.addEventListener('click', generateListener(mw.config.get('wgContentNamespaces')));

	function redirect(namespaces, fallback) {
		const api = new mw.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,
		}).then(result => {
			let pages = result.query.pages;
			
			// Remove strongly disallowed pages
			pages = pages.filter(page => {
				if (page.pageprops && 'noindex' in page.pageprops) return false;
				if (page.categories && config.blockedCategories.length) return false;
				if (page.title.includes('/')) {
					let split = page.title.split('/');
					if (split.length > 2) return false;
					return config.allowedSubpages.includes(split[0]);
				}
				return true;
			});
			
			// Use fallback if no valid pages remain
			if (!pages.length) {
				location.href = fallback;
				return;
			}
			
			// Find allowed page
			let page = pages.find(page => {
				if (page.pageprops && 'disambiguation' in page.pageprops && config.blockDisambiguations) return false;
				if (page.templates && config.blockedTemplates.length) return false;
				return true;
			}) || pages[0];
			
			location.href = page.fullurl;
		});
	}
});