Torn Trade Notification

Notifies when a user adds items to a trade

目前為 2024-02-14 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 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();
})();