Torn Bazaar - $1 Quick List Button

Adds a $1 button ONLY on #/add and #/manage (no effect on bazaar view page).

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Torn Bazaar - $1 Quick List Button
// @namespace    http://tampermonkey.net/
// @version      1.0.3
// @author       Muppet [3926388]
// @description  Adds a $1 button ONLY on #/add and #/manage (no effect on bazaar view page).
// @match        https://www.torn.com/bazaar.php*
// @run-at       document-idle
// @license      MIT
// @grant        none
// ==/UserScript==

(function () {
  "use strict";

  // Only work on these sub-pages
  const ACTIVE_HASHES = new Set(["#/add", "#/manage"]);

  // ---------- styles (only injected when active) ----------
  function ensureStyles() {
    if (document.getElementById("bf-one-dollar-style")) return;

    const style = document.createElement("style");
    style.id = "bf-one-dollar-style";
    style.textContent = `
      .bf-one-dollar-wrap{
        position:absolute;
        top:50%;
        right:42px; /* leaves room for the existing checkbox at right:8px */
        transform:translateY(-50%);
        z-index:5;
      }
      .bf-one-dollar-btn{
        font-size:12px;
        line-height:18px;
        height:20px;
        padding:0 6px;
        border-radius:6px;
        border:1px solid rgba(255,255,255,0.25);
        cursor:pointer;
        user-select:none;
        background: rgba(0,0,0,0.08);
      }
      body.dark-mode .bf-one-dollar-btn{
        background: rgba(255,255,255,0.08);
        border-color: rgba(255,255,255,0.15);
        color:#fff;
      }
    `;
    document.head.appendChild(style);
  }

  // ---------- helpers ----------
  function fire(el, type) {
    if (!el) return;
    el.dispatchEvent(new Event(type, { bubbles: true }));
  }

  function setInputValue(input, value) {
    if (!input) return;
    input.value = value;
    fire(input, "input");
    fire(input, "keyup");
    fire(input, "change");
  }

  // ---------- actions ----------
  function applyOneDollarAdd(row) {
    if (!row) return;

    const toggle = row.querySelector(".checkbox-wrapper input.item-toggle");
    if (toggle && !toggle.checked) toggle.checked = true;

    const qtyInput = row.querySelector(".amount input");
    const priceInput = row.querySelector(".price input");

    const choice = row.querySelector("div.amount.choice-container input");
    if (choice) {
      if (!choice.checked) choice.click();
    } else {
      const qtyText = (row.querySelector(".item-amount.qty")?.textContent || "").trim();
      if (qtyText) setInputValue(qtyInput, qtyText);
    }

    setInputValue(priceInput, "1");
  }

  function applyOneDollarManage(row) {
    if (!row) return;

    const toggle = row.querySelector(".checkbox-wrapper input.item-toggle");
    if (toggle && !toggle.checked) toggle.checked = true;

    let priceInput = row.querySelector(".price___DoKP7 .input-money-group.success input.input-money");
    if (!priceInput) {
      priceInput = row.querySelector("[class*=bottomMobileMenu___] [class*=priceMobile___] .input-money-group.success input.input-money");
    }

    setInputValue(priceInput, "1");
  }

  // ---------- UI injection ----------
  function injectButton(anchorEl, onClick) {
    if (!anchorEl) return;
    if (anchorEl.querySelector(".bf-one-dollar-wrap")) return;

    const computedPos = getComputedStyle(anchorEl).position;
    if (computedPos === "static") anchorEl.style.position = "relative";

    const wrap = document.createElement("div");
    wrap.className = "bf-one-dollar-wrap";

    const btn = document.createElement("button");
    btn.type = "button";
    btn.className = "bf-one-dollar-btn";
    btn.textContent = "$1";
    btn.addEventListener("click", (e) => {
      e.preventDefault();
      e.stopPropagation();
      onClick();
    });

    wrap.appendChild(btn);
    anchorEl.appendChild(wrap);
  }

  function addButtonsAddPage() {
    document.querySelectorAll(".items-cont .title-wrap").forEach((titleWrap) => {
      const row = titleWrap.closest("li.clearfix");
      if (!row) return;
      injectButton(titleWrap, () => applyOneDollarAdd(row));
    });
  }

  function addButtonsManagePage() {
    document.querySelectorAll(".item___jLJcf").forEach((row) => {
      const desc = row.querySelector(".desc___VJSNQ");
      if (!desc) return;
      injectButton(desc, () => applyOneDollarManage(row));
    });
  }

  function refresh() {
    if (!ACTIVE_HASHES.has(window.location.hash)) return;

    ensureStyles();

    if (window.location.hash === "#/add") addButtonsAddPage();
    if (window.location.hash === "#/manage") addButtonsManagePage();
  }

  // ---------- activation gating ----------
  let observer = null;
  let refreshTimer = null;

  function startIfNeeded() {
    // If not on add/manage, do nothing (no observers, no DOM touching)
    if (!ACTIVE_HASHES.has(window.location.hash)) return;

    // Already started
    if (observer) {
      refresh();
      return;
    }

    refresh();

    const target = document.querySelector("#bazaarRoot") || document.body;
    observer = new MutationObserver(() => {
      clearTimeout(refreshTimer);
      refreshTimer = setTimeout(refresh, 100);
    });
    observer.observe(target, { childList: true, subtree: true });
  }

  // Initial check and future navigation
  startIfNeeded();
  window.addEventListener("hashchange", startIfNeeded);
})();