Torn API Test (PapaNads)

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

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey, Greasemonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

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

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

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

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला 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";
      }
    });
  });
})();