Steam 语言切换器 NEW

固定语言切换按钮到页面右上角,自动记忆语言选择,响应式布局,亮蓝 Steam 风格按钮

// ==UserScript==
// @name         Steam 语言切换器 NEW
// @namespace    Squarelan
// @version      1.0.0
// @description  固定语言切换按钮到页面右上角,自动记忆语言选择,响应式布局,亮蓝 Steam 风格按钮
// @match        https://store.steampowered.com/*
// @grant        none
// @run-at       document-end
// @license MIT
// ==/UserScript==

(function() {
  const steam_lang_btn_map = [
    { label: "中", url: "schinese", iso: "zh-cn" },
    { label: "日", url: "japanese", iso: "ja" },
    { label: "英", url: "english", iso: "en" }
  ];

  const insert_styles = () => {
    const style = document.createElement("style");
    style.textContent = `
      .steam-lang-btn-group {
        position: fixed;
        top: 6px;
        right: 12px;
        display: flex;
        gap: 6px;
        z-index: 99999;
      }
      .steam-lang-btn {
        padding: 4px 10px;
        border-radius: 2px;
        background-color: #1A9FFF;
        color: #FFFFFF;
        font-size: 13px;
        font-family: "Motiva Sans", Sans-serif;
        cursor: pointer;
        transition: background-color 0.2s ease;
        box-shadow: 0 0 2px rgba(0,0,0,0.3);
      }
      .steam-lang-btn:hover:not(.disabled) {
        background-color: #167ACC;
      }
      .steam-lang-btn.disabled {
        background-color: #3b3f45;
        color: #7f8c99;
        cursor: not-allowed;
        opacity: 0.6;
      }
      @media (max-width: 600px) {
        .steam-lang-btn-group { display: none; }
      }
    `;
    document.head.appendChild(style);
  };

  const inject_lang_buttons = () => {
    if (document.body.querySelector('.steam-lang-btn-group')) return;

    const group = document.createElement('div');
    group.className = 'steam-lang-btn-group';

    steam_lang_btn_map.forEach(({ label, url, iso }) => {
      const currentLang = document.documentElement.lang;
      const isActive = currentLang.startsWith(iso) || new URL(window.location).searchParams.get("l") === url;

      const btn = document.createElement('div');
      btn.className = 'steam-lang-btn' + (isActive ? ' disabled' : '');
      btn.textContent = label;

      if (!isActive) {
        const newURL = new URL(window.location);
        newURL.searchParams.set("l", url);
        btn.onclick = () => {
          localStorage.setItem("steamLangPref", url);
          window.location = newURL.href;
        };
      }

      group.appendChild(btn);
    });

    document.body.appendChild(group);
  };

  const patch_links_with_lang = () => {
    const currentLang = document.documentElement.lang;
    const langEntry = steam_lang_btn_map.find(({ iso }) => currentLang.startsWith(iso));
    if (!langEntry) return;

    document.querySelectorAll('a[href]').forEach(node => {
      try {
        const url = new URL(node.href);
        if (["steamcommunity.com", "store.steampowered.com"].includes(url.host)) {
          url.searchParams.set("l", langEntry.url);
          node.href = url.href;
        }
      } catch (e) {}
    });
  };

  const hide_es_warning = () => {
    const warning = document.querySelector(".es_language_warning");
    if (warning) warning.style.display = "none";
  };

  const auto_redirect_if_needed = () => {
    const saved = localStorage.getItem("steamLangPref");
    const current = new URL(window.location).searchParams.get("l");
    if (saved && saved !== current) {
      const newURL = new URL(window.location);
      newURL.searchParams.set("l", saved);
      window.location.replace(newURL.href);
    }
  };

  const init = () => {
    auto_redirect_if_needed();
    insert_styles();
    inject_lang_buttons();
    patch_links_with_lang();
    hide_es_warning();

    const observer = new MutationObserver(() => {
      inject_lang_buttons();
      patch_links_with_lang();
      hide_es_warning();
    });
    observer.observe(document.body, { childList: true, subtree: true });
  };

  setTimeout(init, 1000);
})();