Export SteamDB Sales

Adds a button to export current sales in SteamDB as TSV

目前為 2023-08-07 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// 👋 Hola, usa 🐒Tampermonkey 👇
// https://www.tampermonkey.net/

// ==UserScript==
// @name            Export SteamDB Sales
// @name:es         Exportar Ventas de SteamDB
// @namespace       https://jlcareglio.github.io/
// @version         1.3.2
// @description     Adds a button to export current sales in SteamDB as TSV
// @description:es  Agrega un botón para exportar como TSV el listado de ventas actuales en SteamDB
// @icon            https://www.google.com/s2/favicons?sz=64&domain=steamdb.info
// @grant           none
// @author          Jesús Lautaro Careglio Albornoz
// @source          https://gist.githubusercontent.com/JLCareglio/8c47034f40e9febfd52476dd2f36e7bf/raw/
// @match           https://steamdb.info/sales*
// @supportURL      https://gist.githubusercontent.com/JLCareglio/8c47034f40e9febfd52476dd2f36e7bf/
// ==/UserScript==

(async () => {
  async function HandlerClick() {
    const shown = document.querySelector("[name='DataTables_Table_0_length']");
    shown.value = -1;
    shown.dispatchEvent(new Event("change"));
    const rows = Array.from(
      document.querySelectorAll("#DataTables_Table_0 tbody tr")
    );
    const tsvRows = [];
    // console.log({ rows });

    for (const row of rows) {
      // console.log({ row });
      const app_id = row.dataset.appid;
      const name = row
        .querySelector("td:nth-child(3) > a")
        .textContent.replaceAll("#", String.raw`\#`);
      const discount =
        row.querySelector("td:nth-child(4)").textContent.trim() || "—";
      const price = parseFloat(
        row
          .querySelector("td:nth-child(5)")
          .textContent.match(/\d+([.,]?\d+)?/)[0]
          .replace(",", ".")
      );
      let rating = row.querySelector("td:nth-child(6)").textContent;
      rating = rating.match(/^\d{1,2}\.\d{2}%$/) ? rating : "—";
      let endsDate, startedDate;
      // console.log({ app_id, name, discount, price, rating });
      do {
        let ends = row.querySelector("td:nth-child(7)")
        let started = row.querySelector("td:nth-child(8)")
        endsDate = new Date(ends.title.replace(/( at)/g, ""))
        startedDate = new Date(started.title.replace(/( at)/g, ""))

        if (ends.textContent == "" || started.textContent == "") await ScrollTo(row);

        if (isNaN(endsDate) && ends.textContent == "—") endsDate = "—";
        if (isNaN(startedDate) && started.textContent == "—") startedDate = "—";
      } while (
        (isNaN(endsDate) || isNaN(startedDate)) &&
        endsDate != "—" &&
        startedDate != "—"
      );

      if (endsDate != "—") endsDate = endsDate.toUTCString().replace(" GMT", "");
      if (startedDate != "—") startedDate = startedDate.toUTCString().replace(" GMT", "");

      const release = row.querySelector("td:nth-child(9)").textContent;

      tsvRows.push([
        app_id,
        name,
        discount,
        price,
        rating,
        endsDate,
        startedDate,
        release,
      ]);
    }

    const headers = [
      "AppID",
      "Name",
      "% Discount",
      "Price",
      "Rating",
      "Ends (UTC)",
      "Started (UTC)",
      "Release",
    ];
    const tsvContent = [headers, ...tsvRows]
      .map((row) => row.join("\t"))
      .join("\n");
    DownloadTsvFile(tsvContent, "SteamDB_Sales.tsv");
  }

  async function ScrollTo(element) {
    await new Promise((resolve) => {
      element.scrollIntoView(true, { behavior: "instant", block: "start" });
      window.setTimeout(() => {
        resolve();
      }, 100);
    });
  }

  function DownloadTsvFile(data, filename) {
    const blob = new Blob([data], { type: "text/tab-separated-values" });
    const url = URL.createObjectURL(blob);
    const link = document.createElement("a");
    link.href = url;
    link.download = filename;
    document.body.appendChild(link);
    link.click();
    document.body.removeChild(link);
    URL.revokeObjectURL(url);
  }

  const btnExport = document.createElement("a");
  btnExport.classList.value = "btn btn-link";
  btnExport.id = "js-filters-reset";
  btnExport.innerText = "Export TSV";
  btnExport.onclick = HandlerClick;

  document.querySelector("#js-filters").appendChild(btnExport);
})();