Jump to content

User:Nardog/AutoTestcases.js

From Wikipedia, the free encyclopedia
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.
['edit', 'submit'].includes(mw.config.get('wgAction')) &&
[10, 828].includes(mw.config.get('wgNamespaceNumber')) &&
!mw.config.get('wgTitle').endsWith('/doc') &&
$(async () => {
	let input = document.querySelector('#wpTemplateSandboxPage > input');
	if (!input || input.value) return;
	let isModule = mw.config.get('wgNamespaceNumber') === 828;
	let title = mw.config.get('wgTitle');
	let ns = mw.config.get('wgFormattedNamespaces');
	if (title.endsWith('/testcases')) {
		if (isModule) {
			input.value = `${ns[829]}:${title}`;
		}
		return;
	}
	let titles = [], last;
	title.replace(/\/sandbox$/, '').split('/').forEach(s => {
		last = last ? `${last}/${s}` : s;
		titles.push(`${ns[10]}:${last}/testcases`);
		if (isModule) {
			titles.push(`${ns[829]}:${last}/testcases`);
		}
	});
	await mw.loader.using('mediawiki.api');
	let { query: { pages } } = await new mw.Api().post({
		action: 'query',
		titles: titles,
		formatversion: 2
	}, {
		headers: { 'Promise-Non-Write-API-Action': 1 }
	});
	let cand;
	pages.forEach(page => {
		if (!page.missing && (
			!cand || page.ns > cand.ns || page.title.length > cand.title.length
		)) {
			cand = page;
		}
	});
	if (cand) {
		input.value = cand.title;
	}
});