Jump to content

User:Habst/getWikiWinners.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Habst (talk | contribs) at 15:58, 30 August 2024 (// <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). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
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.
// <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>