Jefreesujit / CricInfo commentary scrapper

// ==UserScript==
// @name         CricInfo commentary scrapper
// @namespace    https://openuserjs.org/users/jefreesujit
// @version      1.3
// @description  A simple script to scrap the match commentary from CricInfo
// @author       Jefreesujit
// @match        https://www.espncricinfo.com/series/**/**/ball-by-ball-commentary
// @copyright    2021, Jefreesujit (https://openuserjs.org/users/Jefreesujit)
// @grant        none
// @license      MIT

// ==/UserScript==

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();
};

const waitAndResolve = (ms) => {
  return new Promise(resolve => {
    setTimeout(() => {
      resolve('resolved');
    }, ms);
  });
}

const getRunsList = () => {
  const runList = [];
  const matchComments = document.querySelectorAll('.match-comment');
  const team = document.querySelector('.comment-container-head .dropdown-container button').innerText;
  matchComments.forEach((e) => {
    const over = e.querySelector('.match-comment-over').innerText;
    const run = e.querySelector('.match-comment-run').innerText;
    const shortText = e.querySelector('.match-comment-short-text').innerText.trim();
    const longTextEl = e.querySelector('.match-comment-long-text');
    const longText = longTextEl && longTextEl.innerText;
    const wicketTextEl = e.querySelector('.match-comment-wicket');
    const wicketText = wicketTextEl && wicketTextEl.innerText;

    if (e.querySelector('.comms')) {
      let players = [];
      const endOfOver = e.querySelector('.comment-over-end-caps').innerText;
      e.querySelectorAll('.comment-over-end-player').forEach(e => {
          let [player, stat] = e.innerText.split('\n');
          players.push({
              player,
              stat
          })
      });
      runList.push({
         endOfOver,
         batsman: players.slice(0, 2),
         bowler: players.slice(2),
      });
    }

    runList.push({
      over,
      run,
      shortText,
      longText,
      wicketText
    });
  });
  console.log(runList);
  return runList.reverse();
  // downloadTextFile(JSON.stringify(runList), `${team}.json`);
};

const getMatchCommentary = async (matchCommentary, team) => {
  await waitAndResolve(3000);
  const interval = setInterval(() => {
    document.body.scrollIntoView(false);
  });
  await waitAndResolve(5000);
  matchCommentary[team] = getRunsList();
  clearInterval(interval);
};

(async function () {
  'use strict';
  console.log('Begin Scrapping');
  const matchCommentary = {};
  await waitAndResolve(3000);
  const teamNames = [];
  const innings = [];
  document.querySelectorAll('.match-header .team .name').forEach(e => teamNames.push(e.innerText));
  const buttonEl = document.querySelector('.comment-container-head .dropdown-container button');
  buttonEl.click();
  const dropEl = document.querySelectorAll('.ci-dd__menu li');
  console.log('dropEl', dropEl);
  dropEl.forEach(e => innings.push(e.innerText));
  console.log('innings', innings);

  for (let i = 0; i < innings.length; i++) {
    const team = innings[i];
    document.querySelectorAll('.ci-dd__menu li')[i].click();
    await getMatchCommentary(matchCommentary, team);
    buttonEl.click();
  }
  const fileName = `${teamNames[0]} vs ${teamNames[1]}.json`;
  downloadTextFile(JSON.stringify(matchCommentary), fileName);
})();