User:Habst/getWikiWinners.js
Appearance
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. |
![]() | Documentation for this user script can be added at User:Habst/getWikiWinners. |
// <nowiki>
window.meets ??= {};
window.redirs ??= {};
{
const getRedir = async pg => {
redirs[pg] ??= new DOMParser().parseFromString(await (await fetch(`/wiki/${pg}`)).text(), 'text/html').querySelector('title').innerText.slice(0, -12);
return redirs[pg];
}
const bullet = (yr) => `* ${yr}: ${winners[yr]?.map(w => `[[${w}]]`).join(' & ') ?? ''}`;
const range = (start, end) => [...Array(end - start)].map((_, i) => i + start);
const GEN = 'Women';
const START = 1927;
const END = new Date().getFullYear();
const matchEvts = ['200 metres'];
const winners = {};
for (const y of range(START, END)) {
meets[y] ??= await (await fetch(`/wiki/${y}_USA_Indoor_Track_and_Field_Championships`)).text();
const doc = new DOMParser().parseFromString(meets[y], 'text/html');
const trs = [GEN, `${GEN}\\'s_track`].flatMap(id => [...doc.querySelector(`#${id}`)?.parentElement.nextSibling.nextSibling.querySelectorAll('tr') ?? []]);
const evtRows = [];
for (const tr of trs) {
const tds = tr.querySelectorAll('td');
const rowEvt = tds[0]?.querySelector('a')?.href.split('/').at(-1).split('#').at(-1);
if (!rowEvt) continue;
const evt = await getRedir(rowEvt);
if (matchEvts.includes(evt)) evtRows.push(tds);
}
for (const tds of evtRows) {
const winnerA = tds[1].querySelector('a');
const winner = winnerA?.href.split('/').at(-1);
if (!winner) { console.log('no winner', y); continue; }
const rWinner = winnerA.classList.contains('new') ? new URLSearchParams(winner.split('?').at(-1)).get('title').replaceAll('_', ' ') : await getRedir(winner);
winners[y] ??= [];
winners[y].push(rWinner);
}
}
const abbrEvt = matchEvts[0].replace('metres', 'm');
console.log(`
{{Navbox
| name = USA Indoor Track and Field Championships winners in women's ${abbrEvt}
| title = [[USA Indoor Track and Field Championships]] winners in women's [[${matchEvts[0]}]]
| state = {{{state<includeonly>|collapsed</includeonly>}}}
| listclass = hlist
| nowrapitems = yes
| titlestyle = background: #F0DC82
| groupstyle = text-align: center; background: #F0DC82
| group1 = 1927–1979<br />{{small|[[Amateur Athletic Union]]}}
| list1 =
${range(1927, 1979).map(bullet).join('\n')}
| group2 = 1980–1992<br />{{small|[[The Athletics Congress]]}}
| list2 =
${range(1980, 1992).map(bullet).join('\n')}
| group3 = 1993–present<br />{{small|[[USA Track & Field]]}}
| list3 =
${range(1993, END).map(bullet).join('\n')}
|group4 = Notes
|list4 =
{{allow wrap|* Distances have varied as follows: 440 yards (1959–1986), 400 meters (1987–date) alternating with 300 meters in odd numbered years starting 2015}}
}}<noinclude>
{{collapsible option}}
[[Category:United States indoor track and field champions navigational boxes|${abbrEvt}, women]]
</noinclude>`);
}
// </nowiki>