Jefreesujit / Cricinfo Player Scrapper

// ==UserScript==
// @name         Cricinfo Player Scrapper
// @namespace    http://openuserjs.org/jefreesujit
// @version      1.1.0
// @description  Get the playing 11 and match result for a given match link
// @author       Jefreesujit
// @match        https://www.espncricinfo.com/series/**/**/full-scorecard
// @icon         https://www.google.com/s2/favicons?domain=espncricinfo.com
// @grant        none
// @license      MIT

// ==/UserScript==

/*jshint esversion: 8 */


const downloadTextFile = (text, name) => {
  const a = document.createElement('a');
  const type = name.split(".").pop();
  a.href = URL.createObjectURL(new Blob([text], {
    type: `text/${type === "txt" ? "plain" : type}`
  }));
  a.download = name;
  a.click();
};

(function () {
  'use strict';
  const data = {};
  data.urlDetails = location.pathname.split('/');
  data.matchDetails = document.querySelector('.ds-text-tight-m.ds-font-regular.ds-text-ui-typo-mid').innerText.split(',');
  data.teamDetails = document.querySelector('.ds-text-compact-xxs.ds-p-2.ds-px-4 .ds-flex.ds-flex-col.ds-mt-3').innerText.split('\n').filter(char => /[A-Z]/.test(char));
  data.winnerDetails = document.querySelector('.ds-text-compact-xxs.ds-p-2.ds-px-4 .ds-truncate.ds-text-typo-title').innerText;

  document.querySelectorAll('.ds-bg-fill-content-prime.ds-rounded-lg').forEach(team => {
    let players = [];
    const teamName = team.querySelector('.ds-text-tight-s.ds-font-bold.ds-uppercase').innerText;
    team.querySelectorAll('table.ci-scorecard-table tbody tr.ds-text-tight-s').forEach(item => {
        const batsman = item.querySelector('.ds-inline-flex.ds-items-center.ds-leading-none');
        if (batsman) {
            players.push({
              batsman: batsman.innerText.trim(),
              dismissal: batsman.parentElement.nextElementSibling.innerText.trim()
            });
        }
    });
    const dnb = team.querySelector('tr.\\!ds-border-b-0');
     if (dnb) {
        dnb.querySelectorAll('.ds-inline-flex.ds-items-center.ds-leading-none').forEach(plyr => players.push({
          batsman: plyr.innerText.trim(),
          dismissal: "Did not bat"
        }));
     }
    data[teamName] = players;
    console.log('players', players);
  });
  downloadTextFile(JSON.stringify(data), `${data.urlDetails[3]}-players.json`);
})();