Torn Stat Estimator - Working Version

Estimate Torn user battle stats directly on profile pages using public API data (fixed wrong fields error)

  1. // ==UserScript==
  2. // @name Torn Stat Estimator - Working Version
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.5
  5. // @description Estimate Torn user battle stats directly on profile pages using public API data (fixed wrong fields error)
  6. // @author You
  7. // @match https://www.torn.com/profiles.php*
  8. // @grant none
  9. // ==/UserScript==
  10.  
  11. (function() {
  12. 'use strict';
  13.  
  14. const API_KEY = 'REPLACETHISWITHYOURAPIKEY'; // 🔒 Replace with your own API key
  15.  
  16. // Wait for the profile page to load
  17. function waitForProfile() {
  18. return new Promise(resolve => {
  19. const interval = setInterval(() => {
  20. const nameElem = document.querySelector('span.bold');
  21. if (nameElem && nameElem.textContent.trim().length > 0) {
  22. clearInterval(interval);
  23. resolve();
  24. }
  25. }, 500);
  26. });
  27. }
  28.  
  29. // Get Torn ID from the URL
  30. function getPlayerID() {
  31. const urlParams = new URLSearchParams(window.location.search);
  32. return urlParams.get('XID');
  33. }
  34.  
  35. // ✅ Corrected API call: only using valid selection fields
  36. async function fetchUser(tornID) {
  37. const url = `https://api.torn.com/user/${tornID}?selections=basic&key=${API_KEY}`;
  38. const res = await fetch(url);
  39. const data = await res.json();
  40. if (data.error) throw new Error(`Torn API error: ${data.error.error}`);
  41. return data;
  42. }
  43.  
  44. // Estimate based on basic info
  45. function estimateStats(data) {
  46. const baseStat = 1_500_000;
  47. const level = data.level ?? 0;
  48. const age = data.age ?? 0;
  49.  
  50. const levelFactor = 1 + (level / 1000);
  51. const ageFactor = 1 + (age / 3650); // age is in days
  52.  
  53. const modifier = levelFactor * ageFactor;
  54. const estimateLow = baseStat * modifier * 0.85;
  55. const estimateHigh = baseStat * modifier * 1.15;
  56.  
  57. return {
  58. low: Math.round(estimateLow).toLocaleString(),
  59. high: Math.round(estimateHigh).toLocaleString()
  60. };
  61. }
  62.  
  63. function insertUI(estimates, name) {
  64. if (document.getElementById('tornStatEstimatorOverlay')) return;
  65.  
  66. const container = document.createElement('div');
  67. container.id = 'tornStatEstimatorOverlay';
  68. container.style.position = 'fixed';
  69. container.style.top = '100px';
  70. container.style.right = '20px';
  71. container.style.zIndex = 9999;
  72. container.style.background = '#222';
  73. container.style.color = '#fff';
  74. container.style.padding = '12px 18px';
  75. container.style.border = '2px solid #444';
  76. container.style.borderRadius = '8px';
  77. container.style.fontSize = '14px';
  78. container.style.boxShadow = '0 0 10px rgba(0,0,0,0.5)';
  79. container.style.fontFamily = 'Arial, sans-serif';
  80. container.style.minWidth = '180px';
  81. container.style.textAlign = 'center';
  82.  
  83. container.innerHTML = `<b>🧠 Est. Stats for ${name}</b><br>
  84. <span style="font-size:16px; font-weight:600;">${estimates.low}</span> &mdash;
  85. <span style="font-size:16px; font-weight:600;">${estimates.high}</span>`;
  86.  
  87. document.body.appendChild(container);
  88. }
  89.  
  90. async function main() {
  91. await waitForProfile();
  92. const tornID = getPlayerID();
  93. if (!tornID) return;
  94.  
  95. try {
  96. const userData = await fetchUser(tornID);
  97. const name = userData.name || "Unknown";
  98. const estimates = estimateStats(userData);
  99. insertUI(estimates, name);
  100. } catch (err) {
  101. console.error("Stat Estimator error:", err);
  102. }
  103. }
  104.  
  105. main();
  106. })();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址