Steam | Display game languages

Display images of selected languages on store search steam page.

目前为 2024-01-02 提交的版本。查看 最新版本

// ==UserScript==
// @name            Steam | Display game languages
// @name:uk         Steam | Відобразити мови гри
// @namespace       http://tampermonkey.net/
// @version         2.1.2024
// @description     Display images of selected languages on store search steam page.
// @description:uk  Виводить обрані мови (svg картинка) на сторінці пошуку ігр в steam.
// @author          Black_Yuzia
// @match           https://store.steampowered.com/search*
// @icon            https://store.steampowered.com/favicon.ico
// @grant           GM_addStyle
// @grant           GM_xmlhttpRequest
// @grant           GM.getValue
// @grant           GM.setValue
// @grant           GM.registerMenuCommand
// @grant           GM.notification
// @run-at          document-body
// @license         CC BY-NC-ND 4.0
// @license-url     https://creativecommons.org/licenses/by-nc-nd/4.0/
// @compatible      firefox
// @compatible      chrome
// @compatible      opera
// @compatible      safari
// @compatible      edge
// @require         https://code.jquery.com/jquery-3.7.1.slim.min.js
// @connect         localhost:1500
// @connect         tm.starserv.net
// ==/UserScript==

(() => {
  // src/inject.ts
  var baseURL = "https://tm.starserv.net";

  // src/index.ts
  (async function() {
    "use strict";
    GM_addStyle(`
        .mr-2 {
            margin-right: 2px
        }
    `);
    const baseURL2 = baseURL || "http://localhost:1500";
    const notification = await GM.getValue("notification", false);
    const languages = await GM.getValue("languages", ["ukrainian", "russian"]);
    console.log("lang", languages);
    if (!notification) {
      await GM.notification("Please choose languages in menu! Default lang: 'Ukrainian,\u0279ussian'", "Warning");
    }
    await GM.registerMenuCommand("Set Languages", async () => {
      const languages2 = prompt("Set language(s) [example: Ukrainian,English,\u0279ussian]");
      if (languages2) {
        await GM.setValue("notification", true);
        await GM.setValue("languages", languages2.split(",").map((v) => v.trim().toLowerCase()));
      }
    });
    const container = await waitForElement("#search_result_container");
    const observer = new MutationObserver(async (mutations) => {
      const elements = [];
      for (const mutate of mutations) {
        const e = mutate.target;
        if (e.className?.includes("search_result_row") && !e.className?.includes("tm_process")) {
          e.className += " tm_process";
          elements.push(e);
        }
      }
      if (elements.length > 0) {
        const ids = elements.map(parseGameID);
        GM_xmlhttpRequest({
          url: `${baseURL2}/steam/games/lang?id=${ids.join(",")}`,
          method: "GET",
          fetch: true,
          responseType: "json",
          onload(res) {
            if (res.status === 200) {
              const { response } = res;
              for (let i = 0; i < response.length; i++) {
                const items = response[i];
                if (items.length > 0) {
                  for (const lang of languages) {
                    if (items.includes(lang)) {
                      const element = elements[i];
                      const img = document.createElement("img");
                      img.height = 20;
                      img.width = 20;
                      img.className = "mr-2";
                      img.src = `https://fastdl.starserv.net/languages/${lang}@2x.svg`;
                      const platforms = $(element).find(".platform_img").first().parent("div");
                      platforms.append(img);
                    }
                  }
                }
              }
            }
          }
        });
      }
    });
    observer.observe(container, {
      childList: true,
      subtree: true
    });
  })();
  function waitForElement(selector) {
    let i = 0;
    return new Promise((resolve) => {
      if (document.querySelector(selector)) {
        return resolve(document.querySelector(selector));
      }
      const observer = new MutationObserver((mutations) => {
        if (document.querySelector(selector)) {
          observer.disconnect();
          resolve(document.querySelector(selector));
        }
      });
      observer.observe(document.body, {
        childList: true,
        subtree: true
      });
    });
  }
  function parseGameID(e) {
    const link = e.attributes.getNamedItem("href")?.value;
    const [id] = link.match(/\d+/) || [];
    return id;
  }
})();