Level Progress Estimation

Displays an estimated fractional level based on Hall of Fame position.

As of 17.04.2026. See апошняя версія.

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

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

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

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

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

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name         Level Progress Estimation
// @namespace    http://tampermonkey.net/
// @version      10.26
// @description  Displays an estimated fractional level based on Hall of Fame position.
// @author       Pint-Shot-Riot
// @match        https://www.torn.com/*
// @grant        GM_setValue
// @grant        GM_getValue
// @grant        GM_xmlhttpRequest
// @connect      api.torn.com
// @license MIT
// ==/UserScript==

(function () {
  "use strict";

  async function getApiKey() {
    let key = localStorage.getItem("APIKey") || await GM_getValue("torn_api_key", "");
    if (!key || key.length < 10) {
      key = prompt("Please enter your Torn API Key:");
      if (key) await GM_setValue("torn_api_key", key.trim());
    }
    return key ? key.trim() : null;
  }

  async function fetchTorn(url) {
    return new Promise((resolve, reject) => {
      GM_xmlhttpRequest({
        method: "GET",
        url: url,
        onload: (res) => {
          try {
            const data = JSON.parse(res.responseText);
            if (data.error) reject(data.error.error);
            else resolve(data);
          } catch (e) { reject("JSON Error"); }
        },
        onerror: (err) => reject(err)
      });
    });
  }

  async function getAccurateLevel() {
    const key = await getApiKey();
    if (!key) return null;

    try {
      const userHof = await fetchTorn(`https://api.torn.com/v2/user/hof?key=${key}`);
      const level = userHof.hof.level.value;
      const myRank = userHof.hof.level.rank;

      if (level >= 100) return "100.00";

      const offset = Math.max(0, myRank - 250);
      const hofData = await fetchTorn(`https://api.torn.com/v2/torn/hof?limit=500&offset=${offset}&cat=level&key=${key}`);
      const players = hofData.hof || [];

      const currentLevelPositions = players.filter(p => p.level === level).map(p => p.position);
      const previousLevelPositions = players.filter(p => p.level === (level - 1)).map(p => p.position);

      if (currentLevelPositions.length === 0 || previousLevelPositions.length === 0) {
          return level.toFixed(2);
      }

      const topOfLevel = Math.min(...currentLevelPositions);
      const topOfPrevious = Math.min(...previousLevelPositions);

      const range = topOfPrevious - topOfLevel;
      const progress = topOfPrevious - myRank;

      const fraction = Math.max(0, Math.min(0.99, progress / range));
      return (level + fraction).toFixed(2);
    } catch (e) {
      console.error(e);
      return null;
    }
  }

  function injectIcon(val) {
    const existingPill = document.getElementById('acc-lvl-pill');
    if (existingPill) {
      document.getElementById('acc-lvl-val').textContent = val;
      return;
    }

    const tray = document.querySelector('#header-root [class*="right_"]') || 
                 document.querySelector('.header-navigation') ||
                 document.querySelector('[class*="header-buttons"]');

    if (!tray) return;

    const pill = document.createElement('div');
    pill.id = 'acc-lvl-pill';
    pill.style = "display: inline-flex; align-items: center; background: #333; border: 1px solid #444; border-radius: 10px; padding: 2px 8px; margin: 0 4px; height: 22px; vertical-align: middle; cursor: pointer; flex-shrink: 0; z-index: 999; order: -1;";
    pill.innerHTML = `
      <span style="color: #85b200; font-size: 10px; font-weight: bold; margin-right: 4px; font-family: sans-serif;">LV</span>
      <span id="acc-lvl-val" style="color: #fff; font-size: 11px; font-family: 'Courier New', monospace; font-weight: bold;">${val}</span>
    `;

    pill.onclick = () => window.location.href = "/halloffame.php#/type=level";
    tray.prepend(pill);
  }

  async function run() {
    let val = await getAccurateLevel();
    if (val) injectIcon(val);
    setInterval(async () => { 
      let v = await getAccurateLevel(); 
      if(v) { val = v; injectIcon(val); } 
    }, 600000);
    setInterval(() => { 
      if (!document.getElementById('acc-lvl-pill') && val) injectIcon(val); 
    }, 2000);
  }

  if (document.readyState === "complete") run();
  else window.addEventListener("load", run);
})();