Jump to content

User:Habst/dl.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.
{
  const cityNameTo = {
    "xiamen": {
      "countryCodeAlpha2": "cn",
      "timeZone": "Asia/Shanghai"
    },
    "shanghai": {
      "countryCodeAlpha2": "cn",
      "timeZone": "Asia/Shanghai"
    },
    "doha": {
      "countryCodeAlpha2": "qa",
      "timeZone": "Asia/Qatar"
    },
    "rabat": {
      "countryCodeAlpha2": "ma",
      "timeZone": "Africa/Casablanca"
    },
    "eugene": {
      "countryCodeAlpha2": "us",
      "timeZone": "America/Los_Angeles"
    },
    "oslo": {
      "countryCodeAlpha2": "no",
      "timeZone": "Europe/Oslo"
    },
    "stockholm": {
      "countryCodeAlpha2": "se",
      "timeZone": "Europe/Stockholm"
    },
    "paris": {
      "countryCodeAlpha2": "fr",
      "timeZone": "Europe/Paris"
    },
    "monaco": {
      "countryCodeAlpha2": "mc",
      "timeZone": "Europe/Monaco"
    },
    "london": {
      "countryCodeAlpha2": "gb",
      "timeZone": "Europe/London"
    },
    "lausanne": {
      "countryCodeAlpha2": "ch",
      "timeZone": "Europe/Zurich"
    },
    "silesia": {
      "countryCodeAlpha2": "pl",
      "timeZone": "Europe/Warsaw"
    },
    "rome": {
      "countryCodeAlpha2": "it",
      "timeZone": "Europe/Rome"
    },
    "zurich": {
      "countryCodeAlpha2": "ch",
      "timeZone": "Europe/Zurich"
    },
    "brussels": {
      "countryCodeAlpha2": "be",
      "timeZone": "Europe/Brussels"
    }
  };
  function inTz(dateString, targetTimeZone) {
    const localDate = new Date(dateString);
    if (isNaN(localDate)) throw new Error("Invalid date string");
    const options = {
      timeZone: targetTimeZone,
      year: 'numeric',
      month: '2-digit',
      day: '2-digit',
      hour: '2-digit',
      minute: '2-digit',
      second: '2-digit',
      hour12: false,
    };
    const targetDateString = localDate.toLocaleString("en-US", options);
    const parsedTargetDate = new Date(targetDateString);
    const diffMs = localDate.getTime() - parsedTargetDate.getTime();
    return new Date(localDate.getTime() + diffMs);
  }
  const months = {
    Jan: 1, Feb: 2, Mar: 3, Apr: 4, May: 5, Jun: 6,
    Jul: 7, Aug: 8, Sep: 9, Oct: 10, Nov: 11, Dec: 12,
  };
  const city = 'xiamen';
  const url = '';
  const tz = cityNameTo[city].timeZone;
  Object.fromEntries([...document.querySelectorAll('[data-row=time]')].map(eDiv => {
    const evt = eDiv.querySelectorAll('div')[2].innerText;
    if (['U23', 'National'].some(k => evt.includes(k))) return [];
    const pv = prop => window.getComputedStyle(eDiv).getPropertyValue(prop).slice(1, -1);
    const time = inTz(`${pv('--date-venue')} 2025 ${pv('--time-venue')}`, tz);
    const entrants = [...eDiv.nextElementSibling.querySelectorAll('.grid')]
      .filter(x => !x.classList.contains('font-medium'))
      .map(entDiv => {
        const entCols = [...entDiv.querySelectorAll('div')];
        const [nat, athName, sb, pb] = entCols.map(d => d.innerText.trim());
        const id = entDiv.querySelector('a').href.split('/').at(-1);
        const words = athName.split(' ');
        const firstMixedCase = words.findIndex(w => w.toUpperCase() !== w);
        const lastName = words.slice(0, firstMixedCase).join(' ');
        const firstName = words.slice(firstMixedCase).join(' ');
        return {
          pb, sb, nat, id,
          firstName, lastName,
        }
      });
    
    return [evt, {
      date: time,
      url,
      entrants,
    }];
  }).filter(x => x.length));
}