Skip to content

Instantly share code, notes, and snippets.

@vaporwavie
Last active January 14, 2023 03:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vaporwavie/353971ae37bb58425ed369e23cf48aec to your computer and use it in GitHub Desktop.
Save vaporwavie/353971ae37bb58425ed369e23cf48aec to your computer and use it in GitHub Desktop.
csgostats.gg scraping that lists your matches based on specific filters.
function getRowText(t,e){return t.querySelector(e).innerText.trim()}let csgo=document.querySelectorAll("#match-list-outer > table > tbody > tr"),csgoMatches=[...csgo].map(t=>({date:getRowText(t,"td:nth-of-type(1)"),map:getRowText(t,"td:nth-of-type(3)"),score:getRowText(t,"td:nth-of-type(4)"),kills:+getRowText(t,"td.col-stats:nth-of-type(7)"),deaths:+getRowText(t,"td.col-stats:nth-of-type(8)"),diff:+getRowText(t,"td.col-stats:nth-of-type(10)"),rating:+getRowText(t,"td:nth-last-child(2)")}));function filterMatches(t="date"){const e=Object.keys(csgoMatches[0]);if(!e.some(e=>e===t))throw Error(`Invalid filter. Try using: ${e.join(", ")}. You could also just run it without a filter.`);return csgoMatches.sort((e,o)=>o[t]-e[t])}
@vaporwavie
Copy link
Author

vaporwavie commented May 10, 2021

Installing

Usage

When injected, just run the function by typing filterMatches(). See the docs below for further info on the allowed filters.

Docs

fn filterMatches(filterBy?: string)

Accepts the following filter keys: date, map, score, kills, deaths, diff and rating and will default to date when no argument is passed.

@vaporwavie
Copy link
Author

vaporwavie commented May 10, 2021

Unminified version:

function getRowText(element, selector) {
  return element.querySelector(selector).innerText.trim();
}

let csgo = document.querySelectorAll("#match-list-outer > table > tbody > tr");

let csgoMatches = [...csgo].map((row) => ({
  date: getRowText(row, "td:nth-of-type(1)"),
  map: getRowText(row, "td:nth-of-type(3)"),
  score: getRowText(row, "td:nth-of-type(4)"),
  kills: +getRowText(row, "td.col-stats:nth-of-type(7)"),
  deaths: +getRowText(row, "td.col-stats:nth-of-type(8)"),
  diff: +getRowText(row, "td.col-stats:nth-of-type(10)"),
  rating: +getRowText(row, "td:nth-last-child(2)"),
}));

function filterMatches(filterBy = "date") {
  const acceptedFilters = Object.keys(csgoMatches[0]);

  if (!acceptedFilters.some((filter) => filter === filterBy))
    throw Error(
      `Invalid filter. Try using: ${acceptedFilters.join(
        ", "
      )}. You could also just run it without a filter.`
    );

  return csgoMatches.sort((a, b) => b[filterBy] - a[filterBy]);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment