User:Nardog/AutoTestcases.js

['edit', 'submit'].includes(mw.config.get('wgAction')) && [10, 828].includes(mw.config.get('wgNamespaceNumber')) && !mw.config.get('wgTitle').endsWith('/doc') && $(function autoTestcases {	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'); });	mw.loader.using('mediawiki.api').then( => (		new mw.Api.post({ action: 'query', titles: titles, formatversion: 2 }, {			headers: { 'Promise-Non-Write-API-Action': 1 } })	)).then(response => { let obj; (((response || {}).query || {}).pages || []).forEach(page => {			if (page.missing || obj && (page.ns < obj.ns || page.title.length < obj.title.length) ) {				return;			}			obj = page;		}); if (obj) input.value = obj.title; }); });