Export SteamDB Sales

Adds a button to export current sales in SteamDB as TSV

当前为 2023-10-14 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 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);
})();