Torn Trade Notification

Notifies when a user adds items to a trade

  1. // ==UserScript==
  2. // @name Torn Trade Notification
  3. // @namespace http://tampermonkey.net/
  4. // @version 3.00
  5. // @description Notifies when a user adds items to a trade
  6. // @author Weav3r [1853324]
  7. // @match https://www.torn.com/*
  8. // @grant GM_setValue
  9. // @grant GM_getValue
  10. // @grant GM_notification
  11. // ==/UserScript==
  12.  
  13. (async function() {
  14. const apiKey = 'YOUR_API_KEY_HERE'; // Replace YOUR_API_KEY_HERE with your FULL ACCESS API key
  15. const checkInterval = 60000; // Time to run in milliseconds (default: 60000)
  16.  
  17. async function fetchUserDetails(userId) {
  18. const cachedUser = GM_getValue(`user_${userId}`);
  19. if (cachedUser) return cachedUser;
  20.  
  21. const url = `https://api.torn.com/user/${userId}?selections=basic&key=${apiKey}`;
  22. const response = await fetch(url).catch(console.error);
  23. if (!response) return;
  24.  
  25. const data = await response.json().catch(console.error);
  26. if (!data || !data.name) return;
  27.  
  28. GM_setValue(`user_${userId}`, data.name);
  29. return data.name;
  30. }
  31.  
  32. async function checkTrades() {
  33. const now = Math.floor(Date.now() / 1000);
  34. const url = `https://api.torn.com/user/?selections=log&log=4482,4413&key=${apiKey}`;
  35. const response = await fetch(url).catch(console.error);
  36. if (!response) return;
  37.  
  38. const data = await response.json().catch(console.error);
  39. if (!data || !data.log) return;
  40.  
  41. for (const logId in data.log) {
  42. const log = data.log[logId];
  43. let tradeId;
  44.  
  45. if (log.log === 4413) {
  46. tradeId = log.data.trade_id.toString();
  47. } else {
  48. if (typeof log.data.trade_id !== 'string') continue;
  49. const tradeIdMatch = log.data.trade_id.match(/ID=(\d+)/);
  50. if (!tradeIdMatch) continue;
  51. tradeId = tradeIdMatch[1];
  52. }
  53.  
  54. const timestamp = log.timestamp;
  55. if (now - timestamp > 180) continue;
  56.  
  57. if ((log.log === 4482 || log.log === 4413) && !GM_getValue(`notified_${tradeId}_${timestamp}`)) {
  58. const userName = await fetchUserDetails(log.data.user);
  59. if (!userName) continue;
  60.  
  61. let notificationText = log.log === 4482 ? `${userName} has added items to your trade. Click to view` : `${userName} has declined the trade. Click to view`;
  62.  
  63. GM_notification({
  64. text: notificationText,
  65. title: "Trade Update",
  66. onclick: () => window.open(`https://www.torn.com/trade.php#step=view&ID=${tradeId}`, '_blank')
  67. });
  68. GM_setValue(`notified_${tradeId}_${timestamp}`, true);
  69. }
  70. }
  71. };
  72.  
  73. await checkTrades();
  74. setInterval(checkTrades, checkInterval);
  75. })();

QingJ © 2025

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