Torn Trade Notification

Notifies when a user adds items to a trade

当前为 2024-02-14 提交的版本,查看 最新版本

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

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

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Torn Trade Notification
// @namespace    http://tampermonkey.net/
// @version      1.1
// @description  Notifies when a user adds items to a trade
// @author       Weav3r
// @match        https://www.torn.com/*
// @grant        GM_setValue
// @grant        GM_getValue
// @grant        GM_registerMenuCommand
// ==/UserScript==

(function() {
    'use strict';

    GM_registerMenuCommand("Set API Key", function() {
        const apiKey = prompt("Please enter your Torn API Key:", GM_getValue("apiKey", ""));
        if (apiKey) {
            GM_setValue("apiKey", apiKey);
            alert("API Key set successfully.");
        }
    }, "k");

    const apiKey = GM_getValue("apiKey");
    if (!apiKey) {
        console.warn("API Key is not set. Use the Tampermonkey menu to set your API Key.");
        return; // Prevent further execution if the API key is not set
    }

    async function fetchUserData(userId) {
        const storedName = localStorage.getItem(`userName_${userId}`);
        if (storedName) return storedName;

        const response = await fetch(`https://api.torn.com/user/${userId}?selections=basic&key=${apiKey}`);
        const data = await response.json();
        if (data && data.name) {
            localStorage.setItem(`userName_${userId}`, data.name);
            return data.name;
        }
        return "Unknown";
    }

    async function notifyTrade({tradeId, userName}) {
        if (Notification.permission !== "granted") await Notification.requestPermission();
        if (Notification.permission === "granted") {
            const notification = new Notification("New Trade Item Added", {
                body: `Items have been added to your trade with ${userName}. Click to view.`,
                icon: 'https://www.torn.com/favicon.ico'
            });
            notification.onclick = () => window.open(`https://www.torn.com/trade.php#step=view&ID=${tradeId}`, '_blank');
        }
    }

    async function processTrades(data) {
        const completedTrades = new Set();
        const notifiedTimestamps = JSON.parse(localStorage.getItem("notifiedTimestamps") || "[]");

        for (const log of Object.values(data.log)) {
            if (log.title === "Trade completed") {
                const match = log.data?.trade_id?.match(/ID=(\d+)/);
                if (match) completedTrades.add(match[1]);
            }

            if (log.title === "Trade items add other user") {
                if (!log.data || !log.data.trade_id) {
                    console.error("Encountered a log entry without trade_id data. Log entry:", log);
                    continue; // Skip this iteration if trade_id is undefined
                }
                const match = log.data.trade_id.match(/ID=(\d+)/);
                if (!match) continue; // Skip if we cannot extract trade ID
                const tradeId = match[1];
                const timestamp = log.timestamp.toString();

                if (!completedTrades.has(tradeId) && !notifiedTimestamps.includes(timestamp)) {
                    if (notifiedTimestamps.push(timestamp) > 100) notifiedTimestamps.shift();
                    localStorage.setItem("notifiedTimestamps", JSON.stringify(notifiedTimestamps));
                    const userName = await fetchUserData(log.data.user);
                    await notifyTrade({ tradeId, userName });
                }
            }
        }
    }

    async function checkTrades() {
        if (!shouldMakeApiCall()) return;
        localStorage.setItem("lastApiCallTimestamp", Date.now().toString());

        const response = await fetch(`https://api.torn.com/user/?selections=log&key=${apiKey}`);
        const data = await response.json();
        if (!data.error) await processTrades(data);
    }

    function shouldMakeApiCall() {
        const lastCallTimestamp = parseInt(localStorage.getItem("lastApiCallTimestamp") || "0");
        return Date.now() - lastCallTimestamp >= 60000;
    }

    setInterval(checkTrades, 60000);
    checkTrades();
})();