Torn API Test (PapaNads)

Tests GM_xmlhttpRequest access to api.torn.com and prints the raw result.For troubleshooting your api.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Torn API Test (PapaNads)
// @namespace    https://tampermonkey.net/
// @version      0.1
// @description  Tests GM_xmlhttpRequest access to api.torn.com and prints the raw result.For troubleshooting your api.
// @author       papanad[3928917]
// @match        https://www.torn.com/*
// @grant        GM_xmlhttpRequest
// @grant        GM_addStyle
// @connect      api.torn.com
// @run-at       document-idle
// @license      MAT
// ==/UserScript==

(() => {
  "use strict";

  GM_addStyle(`
    #pn-api-test {
      position: fixed; right: 12px; bottom: 12px; z-index: 999999;
      background: rgba(0,0,0,.75); color: #39ff14; border: 1px solid rgba(57,255,20,.35);
      padding: 10px; border-radius: 10px; font-family: monospace; width: 360px;
      box-shadow: 0 0 18px rgba(57,255,20,.12);
    }
    #pn-api-test button {
      background: rgba(120,0,0,.85); color:#39ff14; border:1px solid rgba(255,51,51,.6);
      border-radius: 8px; padding: 6px 10px; cursor:pointer; font-weight:700;
    }
    #pn-api-test pre { white-space: pre-wrap; margin: 8px 0 0 0; max-height: 200px; overflow:auto; }
    #pn-api-key { width: 100%; box-sizing: border-box; margin-top: 8px; padding: 8px; border-radius: 8px;
      border: 1px solid rgba(57,255,20,.35); background: rgba(0,0,0,.35); color:#39ff14; }
  `);

  const box = document.createElement("div");
  box.id = "pn-api-test";
  box.innerHTML = `
    <div style="font-weight:800; margin-bottom:6px;">Torn API TEST</div>
    <button id="pn-test-btn">TEST API KEY</button>
    <input id="pn-api-key" type="password" placeholder="Paste your API key here (test only)" />
    <pre id="pn-out">Waiting...</pre>
  `;
  document.body.appendChild(box);

  const out = box.querySelector("#pn-out");
  const keyInput = box.querySelector("#pn-api-key");

  box.querySelector("#pn-test-btn").addEventListener("click", () => {
    const key = keyInput.value.trim();
    if (!key) {
      out.textContent = "Paste your API key first.";
      return;
    }

    const url = `https://api.torn.com/user/?selections=battlestats,workstats&key=${encodeURIComponent(key)}`;
    out.textContent = `Requesting...\n${url}`;

    GM_xmlhttpRequest({
      method: "GET",
      url,
      timeout: 15000,
      onload: (resp) => {
        const head = `HTTP status: ${resp.status}\nFinal URL: ${url}\n\n`;
        out.textContent = head + (resp.responseText || "(empty response)");
      },
      onerror: () => {
        out.textContent =
          "onerror fired.\n\nThis almost always means:\n- @connect is missing/blocked\n- another extension blocked api.torn.com\n- the userscript manager is not Tampermonkey / permissions not granted";
      },
      ontimeout: () => {
        out.textContent =
          "Timed out.\n\nThis can be:\n- Torn API temporarily slow\n- api.torn.com blocked by network/adblock";
      }
    });
  });
})();